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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#include "connection.h"
#include "room.h"
#include "user.h"
#include "jobs/sendeventjob.h"
#include <QtCore/QCoreApplication>
#include <QtCore/QStringBuilder>
#include <QtCore/QTimer>
#include <iostream>
using namespace QMatrixClient;
using std::cout;
using std::endl;
using namespace std::placeholders;
class QMCTest : public QObject
{
public:
QMCTest(Connection* conn, const QString& testRoomName, QString source);
private slots:
void onNewRoom(Room* r, const QString& testRoomName);
void doTests();
void addAndRemoveTag();
void sendAndRedact();
void finalize();
private:
QScopedPointer<Connection, QScopedPointerDeleteLater> c;
QString origin;
Room* targetRoom = nullptr;
int semaphor = 0;
};
#define QMC_CHECK(description, condition) \
cout << (description) \
<< (!!(condition) ? " successul" : " FAILED") << endl; \
targetRoom->postMessage(origin % ": " % QStringLiteral(description) % \
(!!(condition) ? QStringLiteral(" successful") : \
QStringLiteral(" FAILED")), MessageEventType::Notice)
QMCTest::QMCTest(Connection* conn, const QString& testRoomName, QString source)
: c(conn), origin(std::move(source))
{
if (!origin.isEmpty())
cout << "Origin for the test message: " << origin.toStdString() << endl;
if (!testRoomName.isEmpty())
cout << "Test room name: " << testRoomName.toStdString() << endl;
connect(c.data(), &Connection::newRoom,
this, [this,testRoomName] (Room* r) { onNewRoom(r, testRoomName); });
connect(c.data(), &Connection::syncDone, c.data(), [this] {
cout << "Sync complete, " << semaphor << " tests in the air" << endl;
if (semaphor)
{
// if (targetRoom)
// targetRoom->postMessage(
// QString("%1: sync done, %2 test(s) in the air")
// .arg(origin).arg(semaphor),
// MessageEventType::Notice);
c->sync(10000);
}
else
{
if (targetRoom)
{
auto j = c->callApi<SendEventJob>(targetRoom->id(),
RoomMessageEvent(origin % ": All tests finished"));
connect(j, &BaseJob::finished, this, &QMCTest::finalize);
}
else
finalize();
}
});
// Big countdown watchdog
QTimer::singleShot(180000, this, &QMCTest::finalize);
}
void QMCTest::onNewRoom(Room* r, const QString& testRoomName)
{
cout << "New room: " << r->id().toStdString() << endl;
connect(r, &Room::namesChanged, this, [=] {
cout << "Room " << r->id().toStdString() << ", name(s) changed:" << endl
<< " Name: " << r->name().toStdString() << endl
<< " Canonical alias: " << r->canonicalAlias().toStdString() << endl
<< endl << endl;
if (!testRoomName.isEmpty() && (r->name() == testRoomName ||
r->canonicalAlias() == testRoomName))
{
cout << "Found the target room, proceeding for tests" << endl;
targetRoom = r;
++semaphor;
auto j = targetRoom->connection()->callApi<SendEventJob>(
targetRoom->id(),
RoomMessageEvent(origin % ": connected to test room",
MessageEventType::Notice));
connect(j, &BaseJob::success,
this, [this] { doTests(); --semaphor; });
}
});
connect(r, &Room::aboutToAddNewMessages, r, [r] (RoomEventsRange timeline) {
cout << timeline.size() << " new event(s) in room "
<< r->id().toStdString() << endl;
// for (const auto& item: timeline)
// {
// cout << "From: "
// << r->roomMembername(item->senderId()).toStdString()
// << endl << "Timestamp:"
// << item->timestamp().toString().toStdString() << endl
// << "JSON:" << endl << item->originalJson().toStdString() << endl;
// }
});
}
void QMCTest::doTests()
{
addAndRemoveTag();
sendAndRedact();
}
void QMCTest::addAndRemoveTag()
{
++semaphor;
static const auto TestTag = QStringLiteral("org.qmatrixclient.test");
QObject::connect(targetRoom, &Room::tagsChanged, targetRoom, [=] {
cout << "Room " << targetRoom->id().toStdString()
<< ", tag(s) changed:" << endl
<< " " << targetRoom->tagNames().join(", ").toStdString() << endl;
if (targetRoom->tags().contains(TestTag))
{
cout << "Test tag set, removing it now" << endl;
targetRoom->removeTag(TestTag);
QMC_CHECK("Tagging test", !targetRoom->tags().contains(TestTag));
--semaphor;
QObject::disconnect(targetRoom, &Room::tagsChanged, nullptr, nullptr);
}
});
// The reverse order because tagsChanged is emitted synchronously.
cout << "Adding a tag" << endl;
targetRoom->addTag(TestTag);
}
void QMCTest::sendAndRedact()
{
++semaphor;
cout << "Sending a message to redact" << endl;
auto* job = targetRoom->connection()->callApi<SendEventJob>(targetRoom->id(),
RoomMessageEvent(origin % ": Message to redact"));
QObject::connect(job, &BaseJob::success, targetRoom, [job,this] {
cout << "Message to redact has been succesfully sent, redacting" << endl;
targetRoom->redactEvent(job->eventId(), "qmc-example");
});
QObject::connect(targetRoom, &Room::replacedEvent, targetRoom,
[=] (const RoomEvent* newEvent) {
QMC_CHECK("Redaction", newEvent->isRedacted() &&
newEvent->redactionReason() == "qmc-example");
--semaphor;
QObject::disconnect(targetRoom, &Room::replacedEvent,
nullptr, nullptr);
});
}
void QMCTest::finalize()
{
if (semaphor)
cout << "One or more tests FAILED" << endl;
cout << "Logging out" << endl;
c->logout();
connect(c.data(), &Connection::loggedOut, QCoreApplication::instance(),
[this] {
c->deleteLater();
QCoreApplication::processEvents();
QCoreApplication::exit(semaphor);
});
}
int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
if (argc < 4)
{
cout << "Usage: qmc-example <user> <passwd> <device_name> [<room_alias> [origin]]" << endl;
return -1;
}
cout << "Connecting to the server as " << argv[1] << endl;
auto conn = new Connection;
conn->connectToServer(argv[1], argv[2], argv[3]);
QObject::connect(conn, &Connection::connected, [=] {
cout << "Connected, server: "
<< conn->homeserver().toDisplayString().toStdString() << endl;
cout << "Access token: " << conn->accessToken().toStdString() << endl;
conn->sync();
});
QMCTest test { conn, argc >= 5 ? argv[4] : nullptr,
argc >= 6 ? argv[5] : nullptr };
return app.exec();
}
|