aboutsummaryrefslogtreecommitdiff
path: root/room.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-09-19 18:08:57 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-09-19 18:08:57 +0900
commit35ef29ead1da906e4978dcd908e7c1513ac9bfd3 (patch)
tree0e784f13ec837b9dd6f40e07423e0a3eb8012256 /room.cpp
parentc5c26ff4a09eecaa6d8e1507087566ccf0fd96b4 (diff)
parent0b11b06379fe668063ea5658a261f53f1dcf117a (diff)
downloadlibquotient-35ef29ead1da906e4978dcd908e7c1513ac9bfd3.tar.gz
libquotient-35ef29ead1da906e4978dcd908e7c1513ac9bfd3.zip
Merge branch 'master' into cache-state-to-json
Diffstat (limited to 'room.cpp')
-rw-r--r--room.cpp47
1 files changed, 29 insertions, 18 deletions
diff --git a/room.cpp b/room.cpp
index 241a885d..1a6b055d 100644
--- a/room.cpp
+++ b/room.cpp
@@ -130,6 +130,10 @@ class Room::Private
void insertEvent(RoomEvent* e, Timeline::iterator where,
TimelineItem::index_t index);
+ bool isLocalUser(const User* u) const
+ {
+ return u == connection->user();
+ }
};
Room::Room(Connection* connection, QString id)
@@ -199,7 +203,7 @@ void Room::Private::setLastReadEvent(User* u, const QString& eventId)
{
lastReadEventIds.insert(u, eventId);
emit q->lastReadEventChanged(u);
- if (u == connection->user())
+ if (isLocalUser(u))
emit q->readMarkerMoved();
}
@@ -221,7 +225,7 @@ Room::Private::promoteReadMarker(User* u, Room::rev_iter_t newMarker)
[=](const TimelineItem& ti) { return ti->senderId() != u->id(); });
setLastReadEvent(u, (*(eagerMarker - 1))->id());
- if (u == connection->user() && unreadMessages)
+ if (isLocalUser(u) && unreadMessages)
{
auto stillUnreadMessagesCount =
count_if(eagerMarker, timeline.cend(),
@@ -244,8 +248,7 @@ Room::Private::promoteReadMarker(User* u, Room::rev_iter_t newMarker)
void Room::markMessagesAsRead(Room::rev_iter_t upToMarker)
{
- auto localUser = connection()->user();
- Private::rev_iter_pair_t markers = d->promoteReadMarker(localUser, upToMarker);
+ Private::rev_iter_pair_t markers = d->promoteReadMarker(localUser(), upToMarker);
if (markers.first != markers.second)
qCDebug(MAIN) << "Marked messages as read until" << *readMarker();
@@ -254,7 +257,7 @@ void Room::markMessagesAsRead(Room::rev_iter_t upToMarker)
// until the previous last-read message, whichever comes first.
for (; markers.second < markers.first; ++markers.second)
{
- if ((*markers.second)->senderId() != localUser->id())
+ if ((*markers.second)->senderId() != localUser()->id())
{
connection()->callApi<PostReceiptJob>(id(), (*markers.second)->id());
break;
@@ -321,12 +324,12 @@ Room::rev_iter_t Room::readMarker(const User* user) const
Room::rev_iter_t Room::readMarker() const
{
- return readMarker(connection()->user());
+ return readMarker(localUser());
}
QString Room::readMarkerEventId() const
{
- return d->lastReadEventIds.value(d->connection->user());
+ return d->lastReadEventIds.value(localUser());
}
int Room::notificationCount() const
@@ -370,16 +373,20 @@ QList< User* > Room::users() const
return d->membersMap.values();
}
-QStringList Room::memberNames() const {
+QStringList Room::memberNames() const
+{
QStringList res;
-
- for (auto u : d->membersMap.values()) {
+ for (auto u : d->membersMap)
res.append( this->roomMembername(u) );
- }
return res;
}
+int Room::memberCount() const
+{
+ return d->membersMap.size();
+}
+
void Room::Private::insertMemberIntoMap(User *u)
{
auto namesakes = membersMap.values(u->name());
@@ -401,10 +408,9 @@ void Room::Private::removeMemberFromMap(const QString& username, User* u)
emit q->memberRenamed(formerNamesakes[0]);
}
-inline QByteArray makeErrorStr(const Event* e, const char* msg)
+inline QByteArray makeErrorStr(const Event* e, QByteArray msg)
{
- return QString("%1; event dump follows:\n%2")
- .arg(msg, QString(e->originalJson())).toUtf8();
+ return msg.append("; event dump follows:\n").append(e->originalJson());
}
void Room::Private::insertEvent(RoomEvent* e, Timeline::iterator where,
@@ -614,9 +620,15 @@ void Room::Private::dropDuplicateEvents(RoomEvents* events) const
Connection* Room::connection() const
{
+ Q_ASSERT(d->connection);
return d->connection;
}
+User* Room::localUser() const
+{
+ return connection()->user();
+}
+
void Room::addNewMessageEvents(RoomEvents events)
{
d->dropDuplicateEvents(&events);
@@ -720,7 +732,7 @@ void Room::processStateEvents(const RoomEvents& events)
case EventType::RoomMember: {
auto memberEvent = static_cast<RoomMemberEvent*>(event);
// Can't use d->member() below because the user may be not a member (yet)
- User* u = d->connection->user(memberEvent->userId());
+ auto u = d->connection->user(memberEvent->userId());
u->processEvent(event);
if( memberEvent->membership() == MembershipType::Join )
{
@@ -814,12 +826,11 @@ QString Room::Private::roomNameFromMemberNames(const QList<User *> &userlist) co
first_two.begin(), first_two.end(),
[this](const User* u1, const User* u2) {
// Filter out the "me" user so that it never hits the room name
- return u2 == connection->user() ||
- (u1 != connection->user() && u1->id() < u2->id());
+ return isLocalUser(u2) || (!isLocalUser(u1) && u1->id() < u2->id());
}
);
- // i. One-on-one chat. first_two[1] == connection->user() in this case.
+ // i. One-on-one chat. first_two[1] == localUser() in this case.
if (userlist.size() == 2)
return q->roomMembername(first_two[0]);