// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #include "MatrixRoomBridge.h" #include "MatrixRoomBridge_p.h" #include #include using namespace Quotient; namespace koutnet { // KOutNet - polls and pins: making a poll, voting in one, and the pinned rows the // model reads. void MatrixRoomBridge::sendPoll(const QString &chatId, const QString &question, const QStringList &answers, bool disclosed) { Room *room = roomFor(chatId); if (room == nullptr) { Q_EMIT sendFailed(chatId, i18nc("@info:status", "That Matrix room is not available in this session.")); return; } if (!canSendEncrypted(room)) { Q_EMIT sendFailed(chatId, encryptionUnavailableReason()); return; } QJsonArray answersJson; int index = 0; for (const QString &answer : answers) { if (answer.trimmed().isEmpty()) continue; QJsonObject entry; entry.insert(QStringLiteral("id"), QString::number(index)); entry.insert(QStringLiteral("org.matrix.msc1767.text"), answer); answersJson.append(entry); ++index; } // Keep the unstable shape for compatibility with existing readers. QJsonObject inner; inner.insert(QStringLiteral("kind"), disclosed ? QStringLiteral("org.matrix.msc3381.poll.disclosed") : QStringLiteral("org.matrix.msc3381.poll.undisclosed")); inner.insert(QStringLiteral("max_selections"), 1); inner.insert(QStringLiteral("question"), QJsonObject{{QStringLiteral("org.matrix.msc1767.text"), question}}); inner.insert(QStringLiteral("answers"), answersJson); QString fallback = question; for (int i = 0; i < answers.count(); ++i) fallback += QStringLiteral("\n%1. %2").arg(i + 1).arg(answers.at(i)); QJsonObject content; content.insert(QStringLiteral("org.matrix.msc1767.text"), fallback); content.insert(QStringLiteral("org.matrix.msc3381.poll.start"), inner); room->postJson(QStringLiteral("org.matrix.msc3381.poll.start"), content); } void MatrixRoomBridge::sendPollVote(const QString &chatId, const QString &msgId, const QString &answerId) { Room *room = roomFor(chatId); if (room == nullptr) { Q_EMIT sendFailed(chatId, i18nc("@info:status", "That Matrix room is not available in this session.")); return; } if (!canSendEncrypted(room)) { Q_EMIT sendFailed(chatId, encryptionUnavailableReason()); return; } QJsonArray answers; answers.append(answerId); QJsonObject response; response.insert(QStringLiteral("answers"), answers); QJsonObject content; content.insert(QStringLiteral("body"), i18nc("@info a poll vote with no readable text", "Voted.")); content.insert(QStringLiteral("org.matrix.msc3381.poll.response"), response); content.insert(QStringLiteral("m.relates_to"), QJsonObject{{QStringLiteral("rel_type"), QStringLiteral("m.reference")}, {QStringLiteral("event_id"), msgId}}); room->postJson(QStringLiteral("org.matrix.msc3381.poll.response"), content); } QVariantList MatrixRoomBridge::pinnedRows(Room *room) const { QVariantList out; const QStringList ids = room->pinnedEventIds(); for (const QString &id : ids) { const auto it = room->findInTimeline(id); if (it == room->historyEdge()) continue; out.append(rowForEvent(it->event())); } return out; } } // namespace koutnet