From b385baadc8e73ff3c499a0111e2a553d35dd29b6 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 26 Mar 2018 12:30:23 -0700 Subject: Direct chat (un)marking: update internal structure synchronously The asynchronous update first implemented was more verbose and caused more problems than provided solutions. The idea was that the internal directChats map would better reflect the server state if updated asynchronously. However, it also causes a local race condition; e.g., to quickly remove rooms from direct chats one after another becomes very non-trivial (one has to wait until the previous operation succeeds). So after some playing with the code, hitting pitfalls along the way, I decided to align the logic with the one for room tags; synchronously issued signals look uglyish but at least work predictably. And race conditions between several clients generally cannot be cleanly resolved anyway. --- connection.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'connection.h') diff --git a/connection.h b/connection.h index 4497e200..6a5285f9 100644 --- a/connection.h +++ b/connection.h @@ -89,18 +89,30 @@ namespace QMatrixClient /** Get the list of rooms with the specified tag */ QVector roomsWithTag(const QString& tagName) const; - /** Mark the room as a direct chat with the user */ + /** Mark the room as a direct chat with the user + * This function marks \p room as a direct chat with \p user. + * Emits the signal synchronously, without waiting to complete + * synchronisation with the server. + * + * \sa directChatsListChanged + */ void addToDirectChats(const Room* room, const User* user); /** Unmark the room from direct chats - * This function removes the room from direct chats either for + * This function removes the room id from direct chats either for * a specific \p user or for all users if \p user in nullptr. + * The room id is used to allow removal of, e.g., ids of forgotten + * rooms; a Room object need not exist. Emits the signal + * immediately, without waiting to complete synchronisation with + * the server. + * + * \sa directChatsListChanged */ - void removeFromDirectChats(const Room* room, + void removeFromDirectChats(const QString& roomId, const User* user = nullptr); - /** Check whether the room is a direct chat */ - bool isDirectChat(const Room* room) const; + /** Check whether the room id corresponds to a direct chat */ + bool isDirectChat(const QString& roomId) const; QMap users() const; -- cgit v1.2.3 From 381ab25563cce26be8e3983b3fb3b8090385a766 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Mon, 26 Mar 2018 12:35:49 +0900 Subject: Connection::doInDirectChat() and refactored direct chat requesting logic Basically, the whole requestDirectChat() body has been moved and generalised to doInDirectChat(), and requestDirectChat() delegates to doInDirectChat(). The logic has been updated to cope with formerly left/forgotten rooms present in the list of direct chats (cleaning up the list along the way). --- connection.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'connection.h') diff --git a/connection.h b/connection.h index 6a5285f9..7c11c32d 100644 --- a/connection.h +++ b/connection.h @@ -261,6 +261,15 @@ namespace QMatrixClient */ Q_INVOKABLE void requestDirectChat(const QString& userId); + /** Run an operation in a direct chat with the user + * This method may return synchronously or asynchoronously depending + * on whether a direct chat room with the respective person exists + * already. Instead of emitting a signal it executes the passed + * function object with the direct chat room as its parameter. + */ + Q_INVOKABLE void doInDirectChat(const QString& userId, + std::function operation); + /** Create a direct chat with a single user, optional name and topic * A room will always be created, unlike in requestDirectChat. * It is advised to use requestDirectChat as a default way of getting -- cgit v1.2.3