aboutsummaryrefslogtreecommitdiff
path: root/room.cpp
blob: 178ada27fdaf4a3d0a7df009b586fb6b248439b0 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/******************************************************************************
 * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "room.h"

#include <QtCore/QJsonArray>
#include <QtCore/QDebug>

#include "connection.h"
#include "state.h"
#include "user.h"
#include "events/event.h"
#include "events/roommessageevent.h"
#include "events/roomnameevent.h"
#include "events/roomaliasesevent.h"
#include "events/roomcanonicalaliasevent.h"
#include "events/roomtopicevent.h"
#include "events/roommemberevent.h"
#include "events/typingevent.h"
#include "events/receiptevent.h"
#include "jobs/roommessagesjob.h"

using namespace QMatrixClient;

class Room::Private: public QObject
{
    public:
        Private(Room* parent): q(parent) {}

        Room* q;

        //static LogMessage* parseMessage(const QJsonObject& message);
        void gotMessages(KJob* job);

        Connection* connection;
        QList<Event*> messageEvents;
        QString id;
        QStringList aliases;
        QString canonicalAlias;
        QString name;
        QString topic;
        JoinState joinState;
        int highlightCount;
        int notificationCount;
        QList<User*> users;
        QList<User*> usersTyping;
        QHash<User*, QString> lastReadEvent;
        QString prevBatch;
        bool gettingNewContent;
};

Room::Room(Connection* connection, QString id)
    : d(new Private(this))
{
    d->id = id;
    d->connection = connection;
    d->joinState = JoinState::Join;
    d->gettingNewContent = false;
    qDebug() << "New Room: " << id;

    //connection->getMembers(this); // I don't think we need this anymore in r0.0.1
}

Room::~Room()
{
    delete d;
}

QString Room::id() const
{
    return d->id;
}

QList< Event* > Room::messages() const
{
    return d->messageEvents;
}

QString Room::name() const
{
    return d->name;
}

QStringList Room::aliases() const
{
    return d->aliases;
}

QString Room::canonicalAlias() const
{
    return d->canonicalAlias;
}

QString Room::displayName() const
{
    if (name().isEmpty())
    {
        // Without a human name, try to find a substitute
        if (!canonicalAlias().isEmpty())
            return canonicalAlias();
        if (!aliases().empty() && !aliases().at(0).isEmpty())
            return aliases().at(0);
        // Ok, last attempt - one on one chat
        if (users().size() == 2) {
            return users().at(0)->displayname() + " and " +
                    users().at(1)->displayname();
        }
        // Fail miserably
        return id();
    }

    // If we have a non-empty name, try to stack canonical alias to it.
    // The format is unwittingly borrowed from the email address format.
    QString dispName = name();
    if (!canonicalAlias().isEmpty())
        dispName += " <" + canonicalAlias() + ">";

    return dispName;
}

QString Room::topic() const
{
    return d->topic;
}

JoinState Room::joinState() const
{
    return d->joinState;
}

void Room::setJoinState(JoinState state)
{
    JoinState oldState = d->joinState;
    if( state == oldState )
        return;
    d->joinState = state;
    emit joinStateChanged(oldState, state);
}

void Room::markMessageAsRead(Event* event)
{
    d->connection->postReceipt(this, event);
}

QString Room::lastReadEvent(User* user)
{
    return d->lastReadEvent.value(user);
}

int Room::notificationCount() const
{
    return d->notificationCount;
}

void Room::resetNotificationCount()
{
    if( d->notificationCount == 0 )
        return;
    d->notificationCount = 0;
    emit notificationCountChanged(this);
}

int Room::highlightCount() const
{
    return d->highlightCount;
}

void Room::resetHighlightCount()
{
if( d->highlightCount == 0 )
        return;
    d->highlightCount = 0;
    emit highlightCountChanged(this);
}

QList< User* > Room::usersTyping() const
{
    return d->usersTyping;
}

QList< User* > Room::users() const
{
    return d->users;
}

void Room::addMessage(Event* event)
{
    processMessageEvent(event);
    emit newMessage(event);
    //d->addState(event);
}

void Room::addInitialState(State* state)
{
    processStateEvent(state->event());
}

void Room::updateData(const SyncRoomData& data)
{
    if( d->prevBatch.isEmpty() )
        d->prevBatch = data.timelinePrevBatch;
    setJoinState(data.joinState);

    for( Event* stateEvent: data.state )
    {
        processStateEvent(stateEvent);
    }

    for( Event* timelineEvent: data.timeline )
    {

        processMessageEvent(timelineEvent);
        emit newMessage(timelineEvent);
        // State changes can arrive in a timeline event - try to check those.
        processStateEvent(timelineEvent);
    }

    for( Event* ephemeralEvent: data.ephemeral )
    {
        processEphemeralEvent(ephemeralEvent);
    }

    if( data.highlightCount != d->highlightCount )
    {
        d->highlightCount = data.highlightCount;
        emit highlightCountChanged(this);
    }
    if( data.notificationCount != d->notificationCount )
    {
        d->notificationCount = data.notificationCount;
        emit notificationCountChanged(this);
    }
}

void Room::getPreviousContent()
{
    if( !d->gettingNewContent )
    {
        d->gettingNewContent = true;
        RoomMessagesJob* job = d->connection->getMessages(this, d->prevBatch);
        connect( job, &RoomMessagesJob::result, d, &Room::Private::gotMessages );
    }
}

void Room::Private::gotMessages(KJob* job)
{
    RoomMessagesJob* realJob = static_cast<RoomMessagesJob*>(job);
    if( realJob->error() )
    {
        qDebug() << realJob->errorString();
    }
    else
    {
        for( Event* event: realJob->events() )
        {
            q->processMessageEvent(event);
            emit q->newMessage(event);
        }
        prevBatch = realJob->end();
    }
    gettingNewContent = false;
}

Connection* Room::connection()
{
    return d->connection;
}

void Room::processMessageEvent(Event* event)
{
    for( int i=0; i<d->messageEvents.count(); i++ )
    {
        if( event->timestamp() < d->messageEvents.at(i)->timestamp() )
        {
            d->messageEvents.insert(i, event);
            return;
        }
    }
    d->messageEvents.append(event);
}

void Room::processStateEvent(Event* event)
{
    if( event->type() == EventType::RoomName )
    {
        if (RoomNameEvent* nameEvent = static_cast<RoomNameEvent*>(event))
        {
            d->name = nameEvent->name();
            qDebug() << "room name: " << d->name;
            emit namesChanged(this);
        } else
        {
            qDebug() <<
                "!!! event type is RoomName but the event is not RoomNameEvent";
        }
    }
    if( event->type() == EventType::RoomAliases )
    {
        RoomAliasesEvent* aliasesEvent = static_cast<RoomAliasesEvent*>(event);
        d->aliases = aliasesEvent->aliases();
        qDebug() << "room aliases: " << d->aliases;
        emit namesChanged(this);
    }
    if( event->type() == EventType::RoomCanonicalAlias )
    {
        RoomCanonicalAliasEvent* aliasEvent = static_cast<RoomCanonicalAliasEvent*>(event);
        d->canonicalAlias = aliasEvent->alias();
        qDebug() << "room canonical alias: " << d->canonicalAlias;
        emit namesChanged(this);
    }
    if( event->type() == EventType::RoomTopic )
    {
        RoomTopicEvent* topicEvent = static_cast<RoomTopicEvent*>(event);
        d->topic = topicEvent->topic();
        emit topicChanged();
    }
    if( event->type() == EventType::RoomMember )
    {
        RoomMemberEvent* memberEvent = static_cast<RoomMemberEvent*>(event);
        User* u = d->connection->user(memberEvent->userId());
        if( !u ) qDebug() << "addState: invalid user!" << u << memberEvent->userId();
        u->processEvent(event);
        if( memberEvent->membership() == MembershipType::Join and !d->users.contains(u) )
        {
            d->users.append(u);
            emit userAdded(u);
        }
        else if( memberEvent->membership() == MembershipType::Leave
                 and d->users.contains(u) )
        {
            d->users.removeAll(u);
            emit userRemoved(u);
        }
    }
}

void Room::processEphemeralEvent(Event* event)
{
    if( event->type() == EventType::Typing )
    {
        TypingEvent* typingEvent = static_cast<TypingEvent*>(event);
        d->usersTyping.clear();
        for( const QString& user: typingEvent->users() )
        {
            d->usersTyping.append(d->connection->user(user));
        }
        emit typingChanged();
    }
    if( event->type() == EventType::Receipt )
    {
        ReceiptEvent* receiptEvent = static_cast<ReceiptEvent*>(event);
        for( QString eventId: receiptEvent->events() )
        {
            QList<Receipt> receipts = receiptEvent->receiptsForEvent(eventId);
            for( Receipt r: receipts )
            {
                d->lastReadEvent.insert(d->connection->user(r.userId), eventId);
            }
        }
    }
}

// void Room::setAlias(QString alias)
// {
//     d->alias = alias;
//     emit aliasChanged(this);
// }
//
// bool Room::parseEvents(const QJsonObject& json)
// {
//     QList<LogMessage*> newMessages;
//     QJsonValue value = json.value("messages").toObject().value("chunk");
//     if( !value.isArray() )
//     {
//         return false;
//     }
//     QJsonArray messages = value.toArray();
//     for(const QJsonValue& val: messages )
//     {
//         if( !val.isObject() )
//             continue;
//         LogMessage* msg = Private::parseMessage(val.toObject());
//         if( msg )
//         {
//             newMessages.append(msg);
//         }
//
//     }
//     addMessages(newMessages);
//     return true;
// }
//
// bool Room::parseSingleEvent(const QJsonObject& json)
// {
//     qDebug() << "parseSingleEvent";
//     LogMessage* msg = Private::parseMessage(json);
//     if( msg )
//     {
//         addMessage(msg);
//         return true;
//     }
//     return false;
// }
//
// bool Room::parseState(const QJsonObject& json)
// {
//     QJsonValue value = json.value("state");
//     if( !value.isArray() )
//     {
//         return false;
//     }
//     QJsonArray states = value.toArray();
//     for( const QJsonValue& val: states )
//     {
//         QJsonObject state = val.toObject();
//         QString type = state.value("type").toString();
//         if( type == "m.room.aliases" )
//         {
//             QJsonArray aliases = state.value("content").toObject().value("aliases").toArray();
//             if( aliases.count() > 0 )
//             {
//                 setAlias(aliases.at(0).toString());
//             }
//         }
//     }
//     return true;
// }
//
// LogMessage* Room::Private::parseMessage(const QJsonObject& message)
// {
//     if( message.value("type") == "m.room.message" )
//     {
//         QJsonObject content = message.value("content").toObject();
//         if( content.value("msgtype").toString() != "m.text" )
//             return 0;
//         QString user = message.value("user_id").toString();
//         QString body = content.value("body").toString();
//         LogMessage* msg = new LogMessage( LogMessage::UserMessage, body, user );
//         return msg;
//     }
//     return 0;
// }