// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet Matrix rooms, as chats and as rooms. // // the whole seam between Quotient and the rest of the app: room/timeline // signals become the same conversation events the LAN path makes. nothing // below here knows Quotient. Rooms have topic, address and members, pulled // through roomInfo()/roomMembers(); sending goes the other way through ChatBackend. #pragma once #include #include #include #include #include #include #include #include #include #include #include "core/backend/ChatBackend.h" namespace Quotient { class Connection; class Room; class RoomEvent; class RoomMessageEvent; } namespace koutnet { class MatrixManager; class NetworkManager; class VoiceCallManager; class MatrixManager; class MatrixRoomBridge : public ChatBackend { Q_OBJECT public: explicit MatrixRoomBridge(MatrixManager *manager, QObject *parent = nullptr); // these refuse quietly when the chat id is not a Matrix one or the room is // not in the session; reached via chatTransport, under mx:. Q_INVOKABLE bool sendText(const QString &chatId, const QString &text) override; // libQuotient sequences the upload and the event; this only picks the // content type. false means it was refused here, sendFailed already fired. Q_INVOKABLE bool sendFile(const QString &chatId, const QString &localFilePath) override; // mediaUrl and fileName are handed over rather than looked up: a forwarded // attachment has no event of its own here. Q_INVOKABLE void downloadAttachment(const QString &chatId, const QString &eventId, const QString &mediaUrl = {}, const QString &fileName = {}); Q_INVOKABLE void markRead(const QString &chatId) override; Q_INVOKABLE bool leaveChat(const QString &chatId) override; // sends m.room.encryption, which no event can take back. Before this, every // room this program made stayed in the clear. Q_INVOKABLE void enableEncryption(const QString &chatId); // everything the room column shows, in one map - a dozen properties would be // a dozen signals for one sync. empty map when the chat id names no room. Q_INVOKABLE QVariantMap roomInfo(const QString &chatId) const override; // members, most powerful first then by name. invited ones are included and // marked, since the column asks both "who is in" and "who was asked". Q_INVOKABLE QVariantList roomMembers(const QString &chatId) const override; // one member, as the member card wants it. empty map when unknown to room. Q_INVOKABLE QVariantMap memberInfo(const QString &chatId, const QString &userId) const override; // The newest banner a member has announced into this room, as a media URL // the profile card can load. A banner is per-room gossip, not account data, // so a room the sender never spoke in says nothing about it. Q_INVOKABLE QString memberBanner(const QString &chatId, const QString &userId) const; // the rest of the ChatBackend contract: this is the mx: transport, the // homeserver echoes rows back through sync, and rooms have members, info // and leave. The bridge maps row stamps to event ids. chatid::Transport transport() const override; bool canHandle(const QString &chatId) const override; bool serverOwnsTimeline(const QString &chatId) const override; bool hasRooms(const QString &chatId) const override; bool supportsCalls(const QString &chatId) const override; bool supportsTyping(const QString &chatId) const override; bool supportsEdits(const QString &chatId) const override; bool supportsReactions(const QString &chatId) const override; void sendTyping(const QString &chatId) override; void sendReaction(const QString &chatId, const QVariant &identifier, const QString &emoji, bool added) override; bool sendEdit(const QString &chatId, const QVariant &identifier, const QString &newText) override; bool sendDelete(const QString &chatId, const QVariant &identifier) override; // ---- features the interface must offer at parity with other clients ---- // quote-reply to a message on the timeline; ts is the row stamp, not the // event id. identifier takes an event id or the legacy timestamp. Q_INVOKABLE bool sendReply(const QString &chatId, const QVariant &identifier, const QString &plainText); // send formatted (HTML) text; plainText is the fallback body. the composer // derives the HTML from whatever markdown the user typed. Q_INVOKABLE bool sendRichText(const QString &chatId, const QString &plainText, const QString &html); // rewrite the room's name or topic. avatar is set through setRoomAvatar(). Q_INVOKABLE void setRoomName(const QString &chatId, const QString &name); Q_INVOKABLE void setRoomTopic(const QString &chatId, const QString &topic); Q_INVOKABLE void setRoomAvatar(const QString &chatId, const QString &localFilePath); // pin / unpin a message by its row stamp; the room re-emits pinnedEventsChanged. Q_INVOKABLE void pinMessage(const QString &chatId, const QVariant &identifier); Q_INVOKABLE void unpinMessage(const QString &chatId, const QVariant &identifier); // mute / unmute a user across the whole account. Q_INVOKABLE void ignoreUser(const QString &userId); Q_INVOKABLE void unignoreUser(const QString &userId); // search the loaded timeline of a room for a substring; results arrive via // roomSearchResults(). a server /search is not used - the local history is. Q_INVOKABLE void searchMessages(const QString &chatId, const QString &query); // share a geo: point (Matrix m.location). label is the human text; the uri // is built from the coordinates. Q_INVOKABLE void sendLocation(const QString &chatId, double latitude, double longitude, const QString &label); // send a sticker (Matrix m.sticker). mxcUrl is an already-uploaded media id; // the composer uploads the image and passes the id back. Q_INVOKABLE void sendSticker(const QString &chatId, const QString &mxcUrl, const QString &description); // upload a local image and send it as a sticker; the composer only has a path. Q_INVOKABLE void sendStickerFile(const QString &chatId, const QString &localFilePath); // send a recorded voice message (MSC3245): an m.audio carrying the voice flag. Q_INVOKABLE void sendVoice(const QString &chatId, const QString &localFilePath, int durationMs); // send a poll (MSC3386 stable m.poll.start). disclosed shows results as they // come; undisclosed hides them until the writer closes the poll. Q_INVOKABLE void sendPoll(const QString &chatId, const QString &question, const QStringList &answers, bool disclosed); // cast a vote in a poll (m.poll.response). msgId is the poll's event id; the // window resolves a row stamp to it the same way replies do. Q_INVOKABLE void sendPollVote(const QString &chatId, const QString &msgId, const QString &answerId); // send text hidden until revealed (MSC2446 spoiler), as a formatted m.text. Q_INVOKABLE void sendSpoiler(const QString &chatId, const QString &text); // rooms are made and joined, not sent, so these sit outside ChatBackend and // answer via roomListed()/roomLeft() or roomOperationFailed(). Q_INVOKABLE void createRoom(const QString &name, const QString &topic, const QString &alias, const QStringList &invitedUsers, bool isPrivate); // by canonical alias or by full room id, both the same homeserver call. Q_INVOKABLE void joinRoom(const QString &aliasOrId); Q_INVOKABLE void acceptInvite(const QString &chatId); Q_INVOKABLE void declineInvite(const QString &chatId); Q_INVOKABLE void inviteMember(const QString &chatId, const QString &userId); Q_INVOKABLE void kickMember(const QString &chatId, const QString &userId); Q_INVOKABLE void banMember(const QString &chatId, const QString &userId); Q_INVOKABLE void unbanMember(const QString &chatId, const QString &userId); // a one-on-one with a member of this homeserver. Existing direct rooms are // reused; the join chimes directChatOpened() so the window opens it. Q_INVOKABLE void openDirectChat(const QString &userId); // calls between KOutNet sessions inside a Matrix room. Voice media is peer // to peer over the same TCP channel a LAN call uses; the homeserver only // signals m.call.* with each side's LAN address. void setCallStack(NetworkManager *net, VoiceCallManager *voice); // start a room call as the caller; everyone already in the room is invited, // the first to answer becomes the peer. Q_INVOKABLE void callRoom(const QString &chatId); // Answer an invitation the window is showing (roomCallInvited()). Q_INVOKABLE void acceptCall(const QString &chatId, const QString &callId); Q_INVOKABLE void declineCall(const QString &chatId, const QString &callId); Q_INVOKABLE void hangupRoomCall(const QString &chatId); Q_SIGNALS: // a room the list has not heard about, or one whose name changed, idempotent // like ChatListModel::openChat(). // isRoom files the room into the direct-message bookkeeping: a Matrix chat // id does not say which kind it is. void roomListed(QString chatId, QString displayName, QString avatarUrl = QString(), bool isRoom = true); void attachmentReady(QString chatId, QString eventId, QUrl localUrl); void attachmentFailed(QString chatId, QString eventId, QString error); void roomLeft(QString chatId); // one signal for every text row; ChatModel takes them all and de-dupes by // eventId. System rows carry a synthetic id so a restart does not stack. void roomMessage(QString chatId, QString eventId, QString text, QString sender, bool isOwn, double ts, bool isSystem, QString senderAvatar = QString(), QString formattedText = QString(), QString senderId = QString()); // a picture, a recording or a file, as a map (not nine args) shaped like // ChatModel::ingestRemoteAttachment() expects. keys: kind, url, name, // mime, size, width, height, duration. void roomAttachment(QString chatId, QString eventId, QVariantMap media, QString sender, bool isOwn, double ts, QString senderAvatar = QString(), QString senderId = QString()); // a poll (m.poll.start) arrived. answers is a list of {id, body} maps; the // window renders the voter and sends choices back through sendPollVote(). void roomPoll(QString chatId, QString eventId, QString question, QVariantList answers, bool disclosed, QString sender, bool isOwn, double ts, QString senderAvatar = QString(), QString senderId = QString()); // one vote on a poll (m.poll.response). eventId is the poll start it answers, // answerId the chosen option; the window folds it into that poll's tally. void roomPollVote(QString chatId, QString eventId, QString answerId, QString voterId, bool isOwn); // a reaction to a message. ts is the target message stamp, never the // reaction itself - the ReactionStore keys on the target, like the LAN one. void roomReaction(QString chatId, double ts, QString emoji, QString sender, bool added); // an m.replace for an event already shown. never a row of its own, or a // corrected typo would show up as two messages. void roomMessageEdited(QString chatId, QString eventId, QString newText, QString formattedText = QString()); // someone typed or stopped, in an open room. a boolean not a name: the // window shows one indicator per chat, the member hardly matters. void roomTyping(QString chatId, bool typing); // a read receipt passed one of our messages; upToTs of zero says it was // older than the loaded history, so everything held is read. void roomReadReceipt(QString chatId, double upToTs = 0.0); // a message this window already shows was redacted. the row is removed, // like the LAN unsend does, and like every client in the room will. void roomMessageRemoved(QString chatId, QString eventId); // A member announced a header picture into this room. A profile card that // is open re-asks memberBanner() for the newest one. void roomBanner(QString chatId, QString userId); // a message that was "no key for this" has now decrypted; not an edit, and // marking it edited would lie about a message that just became readable. void roomMessageRevealed(QString chatId, QString eventId, QString text, QString formattedText = QString()); // the topic, name, members, address or picture moved. deliberately coarse, // so whoever shows the room just asks again. void roomInfoChanged(QString chatId); // the pinned set moved; each entry is a map like roomMessage()'s row, // resolved from the timeline so the pin sheet needs no round trip. void roomPinnedChanged(QString chatId, QVariantList pinned); // results of searchMessages(): a list of maps (eventId, ts, text, sender), // newest first, capped to a sane number. void roomSearchResults(QString chatId, QVariantList results); // the account ignore list changed (ignoreUser/unignoreUser). the member list // and the room re-read it to grey out muted users. void ignoreListChanged(); void sendFailed(QString chatId, QString reason); // an invitation arrived; the list shows it with accept/decline, not as a chat. // inviterId/inviterName say who asked; either may be empty at first. void roomInvited(QString chatId, QString displayName, QString inviterId, QString inviterName); // the invitation is gone: accepted, declined, or withdrawn. void roomInviteGone(QString chatId); // a room operation the homeserver refused: create, join, invite, kick. // one channel for all, since the window answers each with the same toast. void roomOperationFailed(QString chatId, QString reason); // a room operation the homeserver accepted (e.g. an avatar was set). void roomOperationSucceeded(QString chatId); // a one-on-one requested by openDirectChat() is ready, whether found or // made. The window opens it as a chat. void directChatOpened(QString chatId); // somebody asked us to join a call. The call id travels with it, and so does // the media address the invitation named, so the user's consent covers the // connection dialed. Every byte is room content, so nothing connects first. void roomCallInvited(QString chatId, QString callId, QString sender, QString mediaAddress); // the invite this session sent was answered. the caller opens the call UI // here, the media channel is already up. void roomCallAccepted(QString chatId); // the call in the room is over. the window closes its call UI. void roomCallEnded(QString chatId); private: void attach(Quotient::Connection *connection); void trackRoom(Quotient::Room *room); // The transaction ids of files this bridge is uploading. libQuotient reports // a failed upload on fileTransferFailed and then discards the pending item, // so this set tells an upload apart from a download id and raises sendFailed. QSet m_uploadTxns; // A download from a homeserver, and how it is known to have landed. The file // is watched rather than the library's word for it: see pollMediaDownloads. struct MediaWatch { QString chatId; // "--" in the media cache directory, which is what the // download is asked to write and what is looked for afterwards. QString filePrefix; // what the event says the file measures, when it does. Zero means only a // file that stopped growing says it is whole. qint64 expectedSize = 0; // the file the last poll found, so finishing need not look again; a watch // that has not found one yet pays for the lookup. QString file; qint64 lastSize = -1; int stillSamples = 0; QElapsedTimer since; }; void pollMediaDownloads(); // the file a finished download is in, or empty. Matched by prefix, because // the library may add an extension of its own to the name. QString finishedMediaFile(const MediaWatch &watch) const; void finishMediaDownload(const QString &eventId); void failMediaDownload(const QString &eventId, const QString &reason); // the ordinary case, where the room knows the event and fetches it itself void downloadEventMedia(Quotient::Room *room, const QString &chatId, const QString &eventId, const Quotient::RoomMessageEvent *message); // the forwarded case, where the event is not in this room at all and the // media is fetched by the address the row carries void downloadMediaByUrl(const QString &chatId, const QString &eventId, const QString &mediaUrl, const QString &fileName); bool isCurrentRoom(const Quotient::Room *room, quint64 generation) const; void publishRoom(Quotient::Room *room); void publishRange(Quotient::Room *room, int fromIndex, int toIndex); void publishEvent(Quotient::Room *room, const Quotient::RoomEvent *event); // one member's receipt, when it is about our own message: what // roomReadReceipt() carries. Replayed once per room on first sync too. void emitReceiptUpTo(Quotient::Room *room, const QString &userId); // turns a pending event libQuotient gave up on into something the user sees. // without it a refused send is as quiet as a delivered one. void reportPendingFailure(Quotient::Room *room, int pendingIndex); // a timeline event libQuotient swapped for its decrypted self. void revealEvent(Quotient::Room *room, const Quotient::RoomEvent *event); Quotient::Room *roomFor(const QString &chatId) const; // the pinned event ids, resolved to timeline rows (see rowForEvent). QVariantList pinnedRows(Quotient::Room *room) const; // the invite object for a room we were asked into but have not joined yet Quotient::Room *invitedRoomFor(const QString &chatId) const; // who sent the invite and their display name; either may be empty early void inviterOf(Quotient::Room *room, QString *inviterId, QString *inviterName) const; // a m.call.* event arrived. Voice signalling only: media is peer to peer and // never touches the homeserver. Nothing here dials or installs a session; // every offer is queued for the confirmation dialog first. void handleCallEvent(Quotient::Room *room, const Quotient::RoomEvent *event); // what one side hands the other in an m.call SDP field. The address is where // the media dials; candidates are every address dialed in parallel, first to // connect wins. All of it is attacker-written until callOfferFromSdp() validates. struct CallOffer { QString address; QStringList candidates; }; // Route an offer from a room member to the confirmation dialog. Accepting // (acceptCall()) is the single place a room-call media channel may come up. void queueCallOffer(const QString &chatId, const QString &callId, Quotient::Room *room, const QString &senderName, const CallOffer &offer); static CallOffer callOfferFromSdp(const QString &sdp); CallOffer ownCallOffer() const; QPointer m_manager; // room id to the object whose timeline signals are connected. libQuotient // replaces the object on join-state change, so the pointer is what says the // connections are live; guarded because a room can vanish without leftRoom(). QHash> m_tracked; quint64 m_roomGeneration = 0; // downloads in flight, by event id, and the timer that looks at their files. QHash m_mediaWatches; QTimer m_mediaWatch; // the window addresses rows by stamp, the homeserver by event id. Both // directions are kept per room, so a reaction/edit/unsend resolves to its // event and an echo files under the stamp ReactionStore wants. QHash> m_tsToEventId; QHash> m_eventIdToTs; // when each room last got a typing packet, so a typist costs one round trip // per sentence, not per keystroke. QHash m_lastTypingSent; // reactions this bridge published, keyed by reaction event id: a redaction // wipes the content, so the badge is remembered from the original. struct PublishedReaction { double targetTs = 0.0; QString emoji; QString sender; }; QHash> m_reactions; // voice call state per room. One call id per room; another member who answers // becomes another peer on the caller mixer, like a LAN group call. The table // stores addresses, not member ids, because the media is keyed by address. struct RoomCall { QString callId; QString role; // "caller" or "answerer" QStringList peerAddresses; bool established = false; }; QHash m_calls; // the invite the window is showing, if any: call id, address to ring when // accepted, and the candidates it was announced with. One at a time. struct PendingCall { QString chatId; QString callId; QString peerAddress; QStringList peerCandidates; }; PendingCall m_pending; // set by openDirectChat() while the homeserver makes the room; a matching // join chimes directChatOpened(). QString m_pendingDirectTarget; QPointer m_net; QPointer m_voice; }; } // namespace koutnet