1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
#include "uriresolver.h"
#include "connection.h"
#include "user.h"
using namespace Quotient;
UriResolveResult UriResolverBase::visitResource(Connection* account,
const Uri& uri)
{
switch (uri.type()) {
case Uri::NonMatrix:
return visitNonMatrix(uri.toUrl()) ? UriResolved : CouldNotResolve;
case Uri::Invalid:
case Uri::Empty:
return InvalidUri;
default:;
}
if (!account)
return NoAccount;
switch (uri.type()) {
case Uri::UserId: {
if (uri.action() == "join")
return IncorrectAction;
auto* user = account->user(uri.primaryId());
Q_ASSERT(user != nullptr);
visitUser(user, uri.action());
return UriResolved;
}
case Uri::RoomId:
case Uri::RoomAlias: {
auto* room = uri.type() == Uri::RoomId
? account->room(uri.primaryId())
: account->roomByAlias(uri.primaryId());
if (room != nullptr) {
visitRoom(room, uri.secondaryId());
return UriResolved;
}
if (uri.action() == "join") {
joinRoom(account, uri.primaryId(), uri.viaServers());
return UriResolved;
}
[[fallthrough]];
}
default:
return CouldNotResolve;
}
}
template <typename... FnTs>
class StaticUriDispatcher : public UriResolverBase {
public:
StaticUriDispatcher(const FnTs&... fns) : fns_(fns...) {}
private:
void visitUser(User* user, const QString& action) override
{
std::get<0>(fns_)(user, action);
}
void visitRoom(Room* room, const QString& eventId) override
{
std::get<1>(fns_)(room, eventId);
}
void joinRoom(Connection* account, const QString& roomAliasOrId,
const QStringList& viaServers = {}) override
{
std::get<2>(fns_)(account, roomAliasOrId, viaServers);
}
bool visitNonMatrix(const QUrl& url) override
{
return std::get<3>(fns_)(url);
}
std::tuple<FnTs...> fns_;
};
UriResolveResult Quotient::visitResource(
Connection* account, const Uri& uri,
std::function<void(User*, QString)> userHandler,
std::function<void(Room*, QString)> roomEventHandler,
std::function<void(Connection*, QString, QStringList)> joinHandler,
std::function<bool(const QUrl&)> nonMatrixHandler)
{
return StaticUriDispatcher(userHandler, roomEventHandler, joinHandler,
nonMatrixHandler)
.visitResource(account, uri);
}
void UriDispatcher::visitUser(User *user, const QString &action)
{
emit userAction(user, action);
}
void UriDispatcher::visitRoom(Room *room, const QString &eventId)
{
emit roomAction(room, eventId);
}
void UriDispatcher::joinRoom(Connection *account, const QString &roomAliasOrId, const QStringList &viaServers)
{
emit joinAction(account, roomAliasOrId, viaServers);
}
bool UriDispatcher::visitNonMatrix(const QUrl &url)
{
emit nonMatrixAction(url);
return true;
}
|