rocketchatchannel.cpp 10.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/********************************************************************************************
 *                                                                                          *
 * Copyright (C) 2017 Armin Felder, Dennis Beier                                            *
 * This file is part of RocketChatMobileEngine <https://git.fairkom.net/chat/fairchat>.     *
 *                                                                                          *
 * RocketChatMobileEngine is free software: you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by                     *
 * the Free Software Foundation, either version 3 of the License, or                        *
 * (at your option) any later version.                                                      *
 *                                                                                          *
 * RocketChatMobileEngine 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 General Public License for more details.                                             *
 *                                                                                          *
 * You should have received a copy of the GNU General Public License                        *
 * along with RocketChatMobileEngine. If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                                          *
 ********************************************************************************************/


armin's avatar
armin committed
22
23
24
25
26
#include "rocketchatchannel.h"
#include "segfaulthandler.h"



armin's avatar
as    
armin committed
27
RocketChatChannel::RocketChatChannel( RocketChatServerData *pServer, MessageService *pMessageService, const QString &pRoomId, const QString &pName, const QString &pType ): mDeleted( false )
armin's avatar
armin committed
28
{
armin's avatar
armin committed
29
30
    Q_UNUSED( pServer )
    Q_UNUSED( pMessageService )
armin's avatar
armin committed
31
32
33
34
35
    this->mRoomId = pRoomId;
    this->mName = pName;
    this->mType = pType;
}

armin's avatar
as    
armin committed
36
RocketChatChannel::RocketChatChannel(): mDeleted( false )
armin's avatar
ias    
armin committed
37
38
39
40
{

}

armin's avatar
armin committed
41
42
43
44
45
RocketChatChannel::~RocketChatChannel()
{
    mUsers.clear();
}

armin's avatar
cleanup    
armin committed
46
bool RocketChatChannel::addMessage( const ChatMessage &pMessage, bool nosignal )
armin's avatar
armin committed
47
48
{
    if ( !pMessage.isNull() ) {
armin's avatar
armin committed
49
        if ( mMessages.add( pMessage->getId(), pMessage ) ) {
armin's avatar
armin committed
50
51
            qint64 timestamp = pMessage.data()->getTimestamp();
            qint64 youngest = mMessages.youngest().data()->getTimestamp();
armin's avatar
armin committed
52

armin's avatar
armin committed
53
            if ( timestamp >= youngest && !nosignal ) {
armin's avatar
armin committed
54
                emit( messageAdded( getRoomId(), timestamp ) );
armin's avatar
armin committed
55
56
57
58
59
            }

            return true;
        }
    }
armin's avatar
armin committed
60
61

    return false;
armin's avatar
armin committed
62
63
}

64
const QHash<QString, QSharedPointer< RocketChatMessage >> &RocketChatChannel::getMessages() const
armin's avatar
armin committed
65
66
67
68
{
    return mMessages.getElements();
}

69
const QString &RocketChatChannel::getRoomId() const
armin's avatar
armin committed
70
71
72
73
74
75
76
77
78
{
    return mRoomId;
}

void RocketChatChannel::setRoomId( const QString &pValue )
{
    mRoomId = pValue;
}

79
const QString &RocketChatChannel::getName() const
armin's avatar
armin committed
80
81
82
83
84
85
{
    return mName;
}

void RocketChatChannel::setName( const QString &value )
{
armin's avatar
armin committed
86
    if ( mName != value && !value.isEmpty() ) {
armin's avatar
armin committed
87
88
89
        mName = value;
        emit dataChanged( mName, "name" );
    }
armin's avatar
armin committed
90
91
92
}


armin's avatar
cleanup    
armin committed
93
bool RocketChatChannel::addUser( const QSharedPointer<RocketChatUser> &user )
armin's avatar
armin committed
94
{
armin's avatar
armin committed
95
    QString userId = user->getUserId();
armin's avatar
armin committed
96

armin's avatar
armin committed
97
98
    if ( mUsers.empty() || !mUsers.contains( userId ) ) {
        mUsers[userId] = user;
armin's avatar
armin committed
99
100
101
102
103
104
        return true;
    }

    return false;
}

105
const QMap<QString, QSharedPointer<RocketChatUser> > &RocketChatChannel::getUsers() const
armin's avatar
armin committed
106
107
108
109
110
111
112
113
114
115
116
117
118
{
    return mUsers;
}

void RocketChatChannel::setUsers( const QMap<QString, QSharedPointer<RocketChatUser> > &value )
{
    mUsers = value;
}

void RocketChatChannel::setUsers( const QList<QSharedPointer<RocketChatUser> > &value )
{
    mUsers.clear();

armin's avatar
armin committed
119
    for ( const auto &currentUser : value ) {
armin's avatar
armin committed
120
121
        mUsers[currentUser->getUserName()] = currentUser;
    }
armin's avatar
armin committed
122

armin's avatar
armin committed
123
124
125
126
127
128
129
}

ChatMessage RocketChatChannel::getYoungestMessage()
{
    if ( !mMessages.isEmpty() ) {
        return mMessages.youngest();
    }
armin's avatar
as    
armin committed
130
131
132

    return ChatMessage( nullptr );

armin's avatar
armin committed
133
}
134
const ChatMessage &RocketChatChannel::getYoungestMessage() const
armin's avatar
cleanup    
armin committed
135
{
136
    return mMessages.youngest();
armin's avatar
cleanup    
armin committed
137
}
armin's avatar
armin committed
138
139
140
141
142
143

ChatMessage RocketChatChannel::getOldestMessage()
{
    if ( !mMessages.isEmpty() ) {
        return mMessages.oldest();
    }
armin's avatar
as    
armin committed
144
145
146

    return ChatMessage( nullptr );

armin's avatar
armin committed
147
148
}

149
150
151
152
153
const ChatMessage &RocketChatChannel::getOldestMessage() const
{
    return mMessages.oldest();
}

armin's avatar
armin committed
154
155
156
157
158
MessageRepository *RocketChatChannel::getMessageRepo()
{
    return &mMessages;
}

159
QList<QSharedPointer<RocketChatMessage>> RocketChatChannel::addMessages( const QList<QSharedPointer<RocketChatMessage>> &messagesList )
armin's avatar
armin committed
160
161
{
    QList<QSharedPointer<RocketChatMessage>> newMessages;
armin's avatar
armin committed
162

163
    for ( auto &currentMessage : messagesList ) {
armin's avatar
armin committed
164
        if ( !currentMessage.isNull() ) {
armin's avatar
armin committed
165
            if ( addMessage( currentMessage ) ) {
armin's avatar
as    
armin committed
166
                newMessages.append( currentMessage );
armin's avatar
armin committed
167
            }
armin's avatar
armin committed
168
169
        }
    }
armin's avatar
armin committed
170
171
172

    if ( newMessages.count() ) {
        //   emit messageAdded( getRoomId(), 0 );
173
    }
armin's avatar
armin committed
174

175
    return newMessages;
armin's avatar
armin committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
}

bool RocketChatChannel::getJoined() const
{
    return mJoined;
}

void RocketChatChannel::setJoined( bool value )
{
    mJoined = value;
}

unsigned int RocketChatChannel::getUnreadMessages() const
{
    return mUnreadMessages;
}

Dennis Beier's avatar
Dennis Beier committed
193

armin's avatar
armin committed
194
195
void RocketChatChannel::setUnreadMessages( unsigned int value )
{
armin's avatar
armin committed
196
    qDebug() << "set unread to: " << value;
armin's avatar
armin committed
197
198

    if ( mUnreadMessages != value ) {
Armin Felder's avatar
Armin Felder committed
199
200
201
        mUnreadMessages = value;
        emit unreadMessagesChanged( mRoomId, value );
    }
armin's avatar
armin committed
202
203
}

204
const QString &RocketChatChannel::getType() const
armin's avatar
armin committed
205
206
207
208
{
    return mType;
}

armin's avatar
armin committed
209
210
void RocketChatChannel::setType( const QString &pType )
{
armin's avatar
armin committed
211
    if ( mType != pType  && !pType.isEmpty() ) {
armin's avatar
armin committed
212
213
214
215
216
        mType = pType;
        emit dataChanged( mRoomId, "type" );
    }
}

armin's avatar
armin committed
217
218
219
220
221
222
223
224

bool RocketChatChannel::getReadOnly() const
{
    return mReadOnly;
}

void RocketChatChannel::setReadOnly( bool value )
{
armin's avatar
armin committed
225
226
227
228
    if ( mReadOnly != value ) {
        mReadOnly = value;
        emit dataChanged( mRoomId, "readonly" );
    }
armin's avatar
armin committed
229
230
}

231
const QStringList &RocketChatChannel::getMuted() const
armin's avatar
armin committed
232
233
234
235
236
237
{
    return mMuted;
}

void RocketChatChannel::setMuted( const QStringList &value )
{
armin's avatar
armin committed
238
239
240
241
    if ( mMuted != value ) {
        mMuted = value;
        //emit dataChanged( mRoomId, "readonly" );
    }
armin's avatar
armin committed
242
243
244
245
246
247
248
249
250
}

bool RocketChatChannel::getArchived() const
{
    return mArchived;
}

void RocketChatChannel::setArchived( bool value )
{
armin's avatar
armin committed
251
252
253
254
    if ( mArchived != value ) {
        mArchived = value;
        emit dataChanged( mRoomId, "archived" );
    }
armin's avatar
armin committed
255
256
257
258
259
260
261
262
263
264
265
266
}

bool RocketChatChannel::getSelfMuted() const
{
    return mSelfMuted;
}

void RocketChatChannel::setSelfMuted( bool value )
{
    mSelfMuted = value;
}

armin's avatar
cleanup    
armin committed
267
int RocketChatChannel::operator >( const RocketChatChannel &channel ) const
armin's avatar
armin committed
268
{
armin's avatar
armin committed
269

Armin Felder's avatar
Armin Felder committed
270
    return getUpdatedAt() > channel.getUpdatedAt();
armin's avatar
armin committed
271
272
273
274
}

int RocketChatChannel::operator <( const RocketChatChannel &channel ) const
{
Armin Felder's avatar
Armin Felder committed
275
    return getUpdatedAt() < channel.getUpdatedAt();
armin's avatar
armin committed
276
}
armin's avatar
armin committed
277
278
279
280
281
282
283
284
285

bool RocketChatChannel::getDeleted() const
{
    return mDeleted;
}

void RocketChatChannel::setDeleted( bool deleted )
{
    mDeleted = deleted;
armin's avatar
armin committed
286
    emit channelDeleted( this->getRoomId(), deleted );
287
}
288

289
const QString &RocketChatChannel::getOwnerName() const
290
291
292
293
294
295
{
    return mOwnerName;
}

void RocketChatChannel::setOwnerName( const QString &pOwner )
{
armin's avatar
armin committed
296
    if ( mOwnerName != pOwner && !pOwner.isEmpty() ) {
armin's avatar
armin committed
297
298
299
        mOwnerName = pOwner;
        emit dataChanged( mRoomId, "ownerName" );
    }
300
301
}

302
const QString &RocketChatChannel::getOwnerId() const
303
304
305
306
307
308
{
    return mOwnerId;
}

void RocketChatChannel::setOwnerId( const QString &ownerId )
{
armin's avatar
armin committed
309
    if ( mOwnerId != ownerId && !ownerId.isEmpty() ) {
armin's avatar
armin committed
310
311
312
        mOwnerId = ownerId;
        emit dataChanged( mRoomId, "ownerId" );
    }
313
}
armin's avatar
ias    
armin committed
314

315
const QString &RocketChatChannel::getUsername() const
armin's avatar
ias    
armin committed
316
317
318
319
{
    return mUsername;
}

armin's avatar
armin committed
320
void RocketChatChannel::setUsername( const QString &username )
armin's avatar
ias    
armin committed
321
{
armin's avatar
armin committed
322
    if ( mUsername != username && !username.isEmpty() ) {
armin's avatar
armin committed
323
324
325
        mUsername = username;
        emit dataChanged( mRoomId, "username" );
    }
armin's avatar
ias    
armin committed
326
}
armin's avatar
armin committed
327

328
const QSharedPointer<RocketChatUser> &RocketChatChannel::getChatPartner() const
armin's avatar
armin committed
329
330
331
332
333
334
{
    return mChatPartner;
}

void RocketChatChannel::setChatPartner( const QSharedPointer<RocketChatUser> &chatPartner )
{
armin's avatar
armin committed
335
    if ( mChatPartner != chatPartner && !chatPartner.isNull() ) {
Armin Felder's avatar
Armin Felder committed
336
337
338
339
340
        mChatPartner = chatPartner;
        connect( chatPartner.data(), &RocketChatUser::statusChanged, this, [ = ]() {
            emit dataChanged( mRoomId, "userStatus" );
        } );
    }
armin's avatar
armin committed
341
342
}

343
const QString &RocketChatChannel::getChatPartnerId() const
armin's avatar
armin committed
344
345
346
347
348
349
{
    return mChatPartnerId;
}

void RocketChatChannel::setChatPartnerId( const QString &chatPartnerId )
{
armin's avatar
armin committed
350
    if ( mChatPartnerId != chatPartnerId && !chatPartnerId.isEmpty() ) {
armin's avatar
armin committed
351
352
353
        mChatPartnerId = chatPartnerId;
        //  emit dataChanged( mRoomId, "username" );
    }
armin's avatar
armin committed
354
}
armin's avatar
armin committed
355
356
357
358
359
360
361
362

QSharedPointer<TempFile> RocketChatChannel::getAvatarImg() const
{
    return mAvatarImg;
}

void RocketChatChannel::setAvatarImg( const QSharedPointer<TempFile> &pAvatar )
{
armin's avatar
armin committed
363
    if ( mAvatarImg != pAvatar && !pAvatar.isNull() ) {
armin's avatar
armin committed
364
365
366
367
368
        mAvatarImg = pAvatar;
        emit dataChanged( mRoomId, "avatarImg" );
    }
}

armin's avatar
armin committed
369
370
371
372
373
374
375
qint64 RocketChatChannel::getUpdatedAt() const
{
    return mUpdatedAt;
}

void RocketChatChannel::setUpdatedAt( const qint64 &updatedAt )
{
Armin Felder's avatar
fix    
Armin Felder committed
376
    //skip signal the first time
armin's avatar
armin committed
377
378
379
    if ( mUpdatedAt != updatedAt && updatedAt != -1 ) {
        if ( mUpdatedAt != -1 ) {
            emit updatedChanged( mRoomId, updatedAt );
Armin Felder's avatar
Armin Felder committed
380
        }
armin's avatar
armin committed
381

Armin Felder's avatar
Armin Felder committed
382
383
        mUpdatedAt = updatedAt;
    }
armin's avatar
armin committed
384
385
386
387
388
389
390
391
392
}

qint64 RocketChatChannel::getCreatedAt() const
{
    return mCreatedAt;
}

void RocketChatChannel::setCreatedAt( const qint64 &createdAt )
{
armin's avatar
armin committed
393
    mCreatedAt = createdAt;
armin's avatar
armin committed
394
395
}

armin's avatar
armin committed
396
397
398
399
400
401
402
bool RocketChatChannel::getBlocked() const
{
    return mBlocked;
}

void RocketChatChannel::setBlocked( bool blocked )
{
armin's avatar
armin committed
403
404
405
406
    if ( mBlocked != blocked ) {
        mBlocked = blocked;
        emit dataChanged( mRoomId, "blocked" );
    }
armin's avatar
armin committed
407
}
Dennis Beier's avatar
Dennis Beier committed
408

armin's avatar
armin committed
409
QStringList RocketChatChannel::whiteList( const QList<QString> ids, const qint64 newest, const qint64 oldest )
Dennis Beier's avatar
Dennis Beier committed
410
411
412
413
414
{
    auto idSet = ids.toSet();
    auto currentList = mMessages.getElements().keys().toSet();
    auto blacklist = currentList - idSet;
    QList<QString> buffer = {};
armin's avatar
armin committed
415
416
    std::copy_if( blacklist.begin(), blacklist.end(), std::back_inserter( buffer ),
    [ = ]( const QString id ) {
Dennis Beier's avatar
Dennis Beier committed
417
        bool keep = true;
armin's avatar
armin committed
418
419
420
421

        if ( mMessages.contains( id ) && mMessages.get( id )->getRoomId() == getRoomId() ) {
            auto message = mMessages.get( id );
            keep = message->inTimeIntervall( newest, oldest );
Dennis Beier's avatar
Dennis Beier committed
422
        }
armin's avatar
armin committed
423

Dennis Beier's avatar
Dennis Beier committed
424
425
        return keep;
    }
armin's avatar
armin committed
426
                );
Dennis Beier's avatar
Dennis Beier committed
427
    blacklist = buffer.toSet();
armin's avatar
armin committed
428
429
430

    for ( QString id : blacklist ) {
        mMessages.remove( id );
Dennis Beier's avatar
Dennis Beier committed
431
    }
armin's avatar
armin committed
432

Dennis Beier's avatar
Dennis Beier committed
433
434
435
    return blacklist.toList();
}

armin's avatar
armin committed
436
void RocketChatChannel::deleteMessage( const QString id )
Dennis Beier's avatar
Dennis Beier committed
437
{
armin's avatar
armin committed
438
439
440
441
    emit messageDeleted( getRoomId(), id );

    if ( mMessages.contains( id ) ) {
        mMessages.remove( id );
442

Dennis Beier's avatar
Dennis Beier committed
443
444
445
    }

}