// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - One peer's profile picture, exchanged over the pinned TLS channel #pragma once #include #include #include #include #include #include #include namespace koutnet { // The avatar half of the peer table, stood on its own so NetworkManager can // stop carrying both the picture bytes and the "who has been pushed what" book // keeping. It is deliberately transport-agnostic: the channel, the set of seats // and the peer info map all come in as callbacks, because none of the three is // owned here - TlsChannel lives in NetworkManager, the seats are its // m_peerIdByAddress keys, and recording where a peer's picture landed happens // back in the peer info map for anyone who reads peers(). // // The picture itself is not a presence field: presence is broadcast and // unsigned, and an image is either bytes on the wire or an MXC/URL the peer // tells us to load. This pushes bytes over the authenticated channel in a // frame (protocol::kMsgAvatar), replay-protected by the per-peer sha dedup // rather than by presence. "data" empty is how a peer says its avatar went // away, and an unchanged sha is never pushed twice to the same seat. class AvatarExchange : public QObject { Q_OBJECT public: // Places one frame on the authenticated channel to a seat. Returns false // when there is no channel to that address; a broadcast skips such seats. using ChannelWriter = std::function; // The current seats - every authenticated peer address worth reaching. using Seats = std::function()>; // Records where a peer's picture landed (NetworkManager files it under the // peer info map). Defaults to a no-op. using PeerInfoRecorder = std::function; explicit AvatarExchange(ChannelWriter channelWriter, Seats seats, PeerInfoRecorder peerInfoRecorder = {}, QObject *parent = nullptr); // Re-reads the local picture and pushes to each seat that has not seen this // sha yet. An empty (or unreadable) path means "no avatar" and is pushed to // seats that still think we have one. Q_INVOKABLE void setAvatar(const QString &avatarPath); // A seat's channel just authenticated: it has never seen our picture, so // push it right then or it stays anonymous until the next change. void onPeerAuthenticated(const QString &address); // An avatar frame arrived off a channel. host is the chat id the peer is // filed under; the bytes are parked under this app's data dir and the local // file URL is raised so the UI can repaint without touching the network. void onFrame(const QString &host, const QJsonObject &msg); Q_SIGNALS: // A peer's picture landed (or, with an empty source, went away). fromIp is // the chat id the avatar belongs to - the same key the peer is filed under. void peerAvatar(QString fromIp, QString avatarSource); private: void sendAvatarToPeer(const QString &targetAddress); void broadcastAvatar(); ChannelWriter m_channelWriter; Seats m_seats; PeerInfoRecorder m_peerInfoRecorder; // This station's avatar: the source path, the bytes read from it, and their // sha-256 hex for dedup. Empty path (or unreadable file) means no avatar. QString m_avatarPath; QByteArray m_avatarData; QString m_avatarSha; // seat -> the sha that seat last received from us, so an unchanged picture // is pushed once per seat instead of on every presence or reconnect. QHash m_lastAvatarSent; static constexpr int kMaxAvatarBytes = 4 * 1024 * 1024; }; } // namespace koutnet