Page MenuHomePhorge

No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/src/matrix-room.cpp b/src/matrix-room.cpp
index ef04cee..179814c 100644
--- a/src/matrix-room.cpp
+++ b/src/matrix-room.cpp
@@ -1,83 +1,95 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2020-2023 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <immer/config.hpp> // https://github.com/arximboldi/immer/issues/168
#include <lager/setter.hpp>
#include <nlohmann/json.hpp>
#include <event.hpp>
#include <QFile>
#include <QMimeDatabase>
#include "matrix-room.hpp"
#include "matrix-room-timeline.hpp"
#include "matrix-room-member.hpp"
#include "helper.hpp"
MatrixRoom::MatrixRoom(Kazv::Room room, QObject *parent)
: QObject(parent)
, m_room(room)
, m_memberNames(m_room.members())
, LAGER_QT(roomId)(m_room.roomId().xform(strToQt))
, LAGER_QT(name)(m_room.nameOpt()[lager::lenses::or_default].xform(strToQt))
, LAGER_QT(heroNames)(m_room.heroDisplayNames().xform(strListToQt))
, LAGER_QT(avatarMxcUri)(m_room.avatarMxcUri().xform(strToQt))
, LAGER_QT(localDraft)(lager::with_setter(
m_room.localDraft().xform(strToQt).make(),
[this](QString localDraft) {
m_room.setLocalDraft(localDraft.toStdString());
},
lager::automatic_tag{}))
, LAGER_QT(memberNames)(m_memberNames.xform(strListToQt))
{
}
MatrixRoom::~MatrixRoom() = default;
MatrixRoomTimeline *MatrixRoom::timeline() const
{
return new MatrixRoomTimeline(m_room);
}
void MatrixRoom::sendTextMessage(QString text) const
{
m_room.sendTextMessage(text.toStdString());
}
void MatrixRoom::sendMediaFileMessage(QUrl localFileUrl, QString mxcUri) const
+{
+ auto j = makeMediaFileMessageJson(localFileUrl, mxcUri);
+ Kazv::Event e{j};
+ m_room.sendMessage(e);
+}
+
+nlohmann::json MatrixRoom::makeMediaFileMessageJson(QUrl localFileUrl, QString mxcUri) const
{
auto mimeTypeStr = QMimeDatabase().mimeTypeForFile(localFileUrl.toLocalFile()).name().toStdString();
- nlohmann::json j{
+ static auto available_msgtype = std::array<std::string, 3>{"m.audio", "m.video", "m.image"};
+ auto try_msgtype = std::find(available_msgtype.begin(), available_msgtype.end(),
+ std::string("m.") + mimeTypeStr.substr(0, mimeTypeStr.find('/')));
+ std::string msgtype;
+ if (try_msgtype == available_msgtype.end()) {
+ msgtype = "m.file";
+ } else {
+ msgtype = *try_msgtype;
+ }
+ return nlohmann::json {
{"type", "m.room.message"},
{"content", {
- // TODO: According to the user's choice
- {"msgtype", std::string("m.") + mimeTypeStr.substr(0, mimeTypeStr.find('/'))},
+ {"msgtype", msgtype},
{"body", localFileUrl.fileName().toStdString()},
{"url", mxcUri.toStdString()},
{"info", {
{"size", QFile(localFileUrl.toLocalFile()).size()},
{"mimetype", mimeTypeStr}
}}
}}
};
-
- Kazv::Event e{j};
- m_room.sendMessage(e);
}
MatrixRoomMember *MatrixRoom::memberAt(int index) const
{
return new MatrixRoomMember(m_room.memberEventByCursor(
m_memberNames[index][lager::lenses::or_default]));
}
MatrixRoomMember *MatrixRoom::member(QString userId) const
{
return new MatrixRoomMember(m_room.memberEventFor(userId.toStdString()));
}
diff --git a/src/matrix-room.hpp b/src/matrix-room.hpp
index 174e8de..093837a 100644
--- a/src/matrix-room.hpp
+++ b/src/matrix-room.hpp
@@ -1,53 +1,56 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2020-2023 Tusooa Zhu <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <libkazv-config.hpp>
#include <immer/config.hpp> // https://github.com/arximboldi/immer/issues/168
#include <QObject>
#include <QQmlEngine>
#include <QUrl>
#include <lager/extra/qt.hpp>
#include <client/room/room.hpp>
class MatrixRoomTimeline;
class MatrixRoomMember;
class MatrixRoom : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("")
Kazv::Room m_room;
lager::reader<immer::flex_vector<std::string>> m_memberNames;
public:
explicit MatrixRoom(Kazv::Room room, QObject *parent = 0);
~MatrixRoom() override;
LAGER_QT_READER(QString, roomId);
LAGER_QT_READER(QString, name);
LAGER_QT_READER(QStringList, heroNames);
LAGER_QT_READER(QString, avatarMxcUri);
LAGER_QT_CURSOR(QString, localDraft);
LAGER_QT_READER(QStringList, memberNames);
Q_INVOKABLE MatrixRoomMember *memberAt(int index) const;
Q_INVOKABLE MatrixRoomMember *member(QString userId) const;
Q_INVOKABLE MatrixRoomTimeline *timeline() const;
Q_INVOKABLE void sendTextMessage(QString text) const;
Q_INVOKABLE void sendMediaFileMessage(QUrl localFileUrl, QString mxcUri) const;
+
+protected:
+ nlohmann::json makeMediaFileMessageJson(QUrl localFileUrl, QString mxcUri) const;
};

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 8:57 AM (1 h, 43 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55051
Default Alt Text
(5 KB)

Event Timeline