Page MenuHomePhorge

matrix-sdk.cpp
No OneTemporary

Size
3 KB
Referenced Files
None
Subscribers
None

matrix-sdk.cpp

/*
* Copyright (C) 2020 Tusooa Zhu <tusooa@vista.aero>
*
* This file is part of kazv.
*
* kazv is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* kazv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with kazv. If not, see <https://www.gnu.org/licenses/>.
*/
#include <libkazv-config.hpp>
#include <immer/config.hpp> // https://github.com/arximboldi/immer/issues/168
#include <eventemitter/lagerstoreeventemitter.hpp>
#include <job/cprjobhandler.hpp>
#include <client/sdk.hpp>
#include <base/descendent.hpp>
#include <lager/event_loop/boost_asio.hpp>
#include <zug/util.hpp>
#include <boost/asio.hpp>
#include "matrix-sdk.hpp"
#include "matrix-room-list.hpp"
#include "helper.hpp"
using namespace Kazv;
// Sdk with Boost event loop, identity transform and no enhancers
using ExecT = decltype(std::declval<boost::asio::io_context>().get_executor());
using SdkT =
decltype(makeSdk(
SdkModel{},
detail::declref<JobInterface>(),
detail::declref<EventInterface>(),
lager::with_boost_asio_event_loop{std::declval<boost::asio::io_context>().get_executor()},
zug::identity));
struct MatrixSdkPrivate
{
MatrixSdkPrivate();
boost::asio::io_context io;
CprJobHandler jobHandler;
LagerStoreEventEmitter ee;
LagerStoreEventEmitter::Watchable watchable;
SdkT sdk;
};
MatrixSdkPrivate::MatrixSdkPrivate()
: io{}
, jobHandler{io.get_executor()}
, ee{lager::with_boost_asio_event_loop{io.get_executor()}}
, watchable(ee.watchable())
, sdk(makeDefaultEncryptedSdk(
static_cast<JobInterface &>(jobHandler),
static_cast<EventInterface &>(ee),
lager::with_boost_asio_event_loop{io.get_executor()},
zug::identity))
{
}
MatrixSdk::MatrixSdk(QObject *parent)
: QObject(parent)
, m_d(new MatrixSdkPrivate)
, LAGER_QT(userId)(m_d->sdk.client().userId().xform(strToQt))
, LAGER_QT(token)(m_d->sdk.client().token().xform(strToQt))
{
m_d->watchable.afterAll(
[this](KazvEvent e) {
Q_EMIT this->trigger(e);
});
m_d->watchable.after<LoginSuccessful>(
[this](LoginSuccessful e) {
Q_EMIT this->loginSuccessful(e);
});
m_d->watchable.after<LoginFailed>(
[this](LoginFailed e) {
Q_EMIT this->loginFailed(e);
});
connect(this, &MatrixSdk::trigger,
this, [](KazvEvent e) {
qDebug() << "receiving trigger:";
if (std::holds_alternative<LoginSuccessful>(e)) {
qDebug() << "Login successful";
}
});
}
MatrixSdk::~MatrixSdk() = default;
QString MatrixSdk::mxcUriToHttp(QString mxcUri) const
{
return QString::fromStdString(m_d->sdk.client().mxcUriToHttp(mxcUri.toStdString()));
}
void MatrixSdk::login(const QString &userId, const QString &password)
{
auto l = userId.split(':');
if (l.size() != 2) {
qDebug() << "User id format error";
return;
}
auto serverUrl = QString("https://") + l[1];
m_d->sdk.client()
.passwordLogin(
serverUrl.toStdString(),
userId.toStdString(),
password.toStdString(),
"kazv 0.0.0"
);
std::thread([&] {
qDebug() << "running io context...";
m_d->io.run();
qDebug() << "should never reach here--";
}).detach();
}
MatrixRoomList *MatrixSdk::roomList() const
{
return new MatrixRoomList(m_d->sdk.client());
};

File Metadata

Mime Type
text/x-c
Expires
Fri, Jul 18, 8:34 AM (6 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
261496
Default Alt Text
matrix-sdk.cpp (3 KB)

Event Timeline