2018-09-17 13:01:02 +00:00
|
|
|
#include "spectralroom.h"
|
2018-08-18 08:02:47 +00:00
|
|
|
|
|
|
|
#include "connection.h"
|
2018-08-20 14:40:36 +00:00
|
|
|
#include "user.h"
|
|
|
|
|
2018-08-18 08:02:47 +00:00
|
|
|
#include "csapi/leaving.h"
|
2018-08-20 14:40:36 +00:00
|
|
|
#include "csapi/typing.h"
|
|
|
|
#include "events/typingevent.h"
|
2018-08-18 08:02:47 +00:00
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
SpectralRoom::SpectralRoom(Connection* connection, QString roomId,
|
2018-08-18 08:02:47 +00:00
|
|
|
JoinState joinState)
|
2018-09-06 04:34:15 +00:00
|
|
|
: Room(connection, std::move(roomId), joinState) {
|
2018-09-17 13:01:02 +00:00
|
|
|
connect(this, &SpectralRoom::notificationCountChanged, this,
|
|
|
|
&SpectralRoom::countChanged);
|
|
|
|
connect(this, &SpectralRoom::highlightCountChanged, this,
|
|
|
|
&SpectralRoom::countChanged);
|
2018-09-06 04:34:15 +00:00
|
|
|
}
|
2018-08-18 08:02:47 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::chooseAndUploadFile() {
|
2018-08-18 08:02:47 +00:00
|
|
|
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
|
|
|
|
if (!localFile.isEmpty()) {
|
|
|
|
uploadFile(localFile.toString(), localFile, getMIME(localFile));
|
|
|
|
QMetaObject::Connection* const connection = new QMetaObject::Connection;
|
|
|
|
*connection = connect(this, &Room::fileTransferCompleted,
|
|
|
|
[=](QString id, QUrl localFile, QUrl mxcUrl) {
|
|
|
|
disconnect(*connection);
|
|
|
|
postFile(localFile, mxcUrl);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::postFile(const QUrl& localFile, const QUrl& mxcUrl) {
|
2018-08-18 08:02:47 +00:00
|
|
|
const QString mime = getMIME(localFile);
|
|
|
|
const QString fileName = localFile.fileName();
|
|
|
|
QString msgType = "m.file";
|
|
|
|
if (mime.startsWith("image")) msgType = "m.image";
|
|
|
|
if (mime.startsWith("video")) msgType = "m.video";
|
|
|
|
if (mime.startsWith("audio")) msgType = "m.audio";
|
|
|
|
QJsonObject json{QJsonObject{{"msgtype", msgType},
|
|
|
|
{"body", fileName},
|
|
|
|
{"filename", fileName},
|
|
|
|
{"url", mxcUrl.url()}}};
|
|
|
|
postJson("m.room.message", json);
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
QString SpectralRoom::getMIME(const QUrl& fileUrl) const {
|
2018-08-18 08:02:47 +00:00
|
|
|
return QMimeDatabase().mimeTypeForFile(fileUrl.toLocalFile()).name();
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::saveFileAs(QString eventId) {
|
2018-08-18 08:02:47 +00:00
|
|
|
auto fileName = QFileDialog::getSaveFileName(Q_NULLPTR, tr("Save File as"),
|
|
|
|
fileNameToDownload(eventId));
|
|
|
|
if (!fileName.isEmpty()) downloadFile(eventId, QUrl::fromLocalFile(fileName));
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::acceptInvitation() { connection()->joinRoom(id()); }
|
2018-08-18 08:02:47 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::forget() { connection()->forgetRoom(id()); }
|
2018-08-20 14:40:36 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
bool SpectralRoom::hasUsersTyping() {
|
2018-08-20 14:40:36 +00:00
|
|
|
QList<User*> users = usersTyping();
|
|
|
|
if (users.isEmpty()) return false;
|
|
|
|
int count = users.length();
|
|
|
|
if (users.contains(localUser())) count--;
|
|
|
|
return count != 0;
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
QString SpectralRoom::getUsersTyping() {
|
2018-08-20 14:40:36 +00:00
|
|
|
QString usersTypingStr;
|
|
|
|
QList<User*> users = usersTyping();
|
|
|
|
users.removeOne(localUser());
|
|
|
|
for (User* user : users) {
|
|
|
|
usersTypingStr += user->displayname() + " ";
|
|
|
|
}
|
2018-08-24 05:25:41 +00:00
|
|
|
usersTypingStr += users.count() < 2 ? "is" : "are";
|
2018-08-20 14:40:36 +00:00
|
|
|
usersTypingStr += " typing.";
|
|
|
|
return usersTypingStr;
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::sendTypingNotification(bool isTyping) {
|
2018-08-20 14:40:36 +00:00
|
|
|
connection()->callApi<SetTypingJob>(BackgroundRequest, localUser()->id(),
|
|
|
|
id(), isTyping, 10000);
|
|
|
|
}
|
2018-08-22 15:21:39 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
QString SpectralRoom::lastEvent() {
|
2018-08-22 15:21:39 +00:00
|
|
|
if (timelineSize() == 0) return "";
|
|
|
|
const RoomEvent* lastEvent = messageEvents().rbegin()->get();
|
2018-09-03 01:56:21 +00:00
|
|
|
if (lastEvent->contentJson().value("body").toString() == "") return "";
|
2018-08-22 15:21:39 +00:00
|
|
|
return user(lastEvent->senderId())->displayname() + ": " +
|
|
|
|
lastEvent->contentJson().value("body").toString();
|
|
|
|
}
|
2018-09-06 04:34:15 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
bool SpectralRoom::isEventHighlighted(const RoomEvent* e) const {
|
2018-09-06 04:34:15 +00:00
|
|
|
return highlights.contains(e);
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::checkForHighlights(const QMatrixClient::TimelineItem& ti) {
|
2018-09-06 04:34:15 +00:00
|
|
|
auto localUserId = localUser()->id();
|
|
|
|
if (ti->senderId() == localUserId) return;
|
|
|
|
if (auto* e = ti.viewAs<RoomMessageEvent>()) {
|
|
|
|
const auto& text = e->plainBody();
|
|
|
|
if (text.contains(localUserId) ||
|
|
|
|
text.contains(roomMembername(localUserId)))
|
|
|
|
highlights.insert(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::onAddNewTimelineEvents(timeline_iter_t from) {
|
2018-09-06 04:34:15 +00:00
|
|
|
std::for_each(from, messageEvents().cend(),
|
|
|
|
[this](const TimelineItem& ti) { checkForHighlights(ti); });
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::onAddHistoricalTimelineEvents(rev_iter_t from) {
|
2018-09-06 04:34:15 +00:00
|
|
|
std::for_each(from, messageEvents().crend(),
|
|
|
|
[this](const TimelineItem& ti) { checkForHighlights(ti); });
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::countChanged() {
|
2018-09-06 04:34:15 +00:00
|
|
|
if (displayed() && !hasUnreadMessages()) {
|
|
|
|
resetNotificationCount();
|
|
|
|
resetHighlightCount();
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 03:58:02 +00:00
|
|
|
|
2018-09-17 13:01:02 +00:00
|
|
|
void SpectralRoom::sendReply(QString userId, QString eventId,
|
2018-09-17 03:58:02 +00:00
|
|
|
QString replyContent, QString sendContent) {
|
|
|
|
QJsonObject json{
|
|
|
|
{"msgtype", "m.text"},
|
|
|
|
{"body", "> <" + userId + "> " + replyContent + "\n\n" + sendContent},
|
|
|
|
{"format", "org.matrix.custom.html"},
|
2018-09-17 13:01:02 +00:00
|
|
|
{"m.relates_to",
|
|
|
|
QJsonObject{{"m.in_reply_to", QJsonObject{{"event_id", eventId}}}}},
|
2018-09-17 03:58:02 +00:00
|
|
|
{"formatted_body",
|
|
|
|
"<mx-reply><blockquote><a href=\"https://matrix.to/#/" + id() + "/" +
|
|
|
|
eventId + "\">In reply to</a> <a href=\"https://matrix.to/#/" +
|
|
|
|
userId + "\">" + userId + "</a><br>" + replyContent +
|
|
|
|
"</blockquote></mx-reply>" + sendContent}};
|
|
|
|
postJson("m.room.message", json);
|
|
|
|
}
|
2018-09-18 12:57:38 +00:00
|
|
|
|
|
|
|
QDateTime SpectralRoom::lastActiveTime() {
|
2018-09-19 23:01:55 +00:00
|
|
|
if (timelineSize() == 0) return QDateTime();
|
|
|
|
return messageEvents().rbegin()->get()->timestamp();
|
2018-09-18 12:57:38 +00:00
|
|
|
}
|
2018-09-19 23:01:55 +00:00
|
|
|
|
|
|
|
float SpectralRoom::orderForTag(QString name) { return tag(name).order; }
|