Spectral/src/spectralroom.cpp

241 lines
7.4 KiB
C++
Raw Normal View History

#include "spectralroom.h"
#include "connection.h"
2018-08-20 14:40:36 +00:00
#include "user.h"
#include "csapi/content-repo.h"
#include "csapi/leaving.h"
2018-08-20 14:40:36 +00:00
#include "csapi/typing.h"
#include "events/accountdataevents.h"
2018-08-20 14:40:36 +00:00
#include "events/typingevent.h"
#include "jobs/downloadfilejob.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QImageReader>
#include <QMetaObject>
2018-10-07 12:38:30 +00:00
#include <QMimeDatabase>
2019-02-13 08:14:13 +00:00
#include "html.h"
#include "utils.h"
2019-04-21 05:06:48 +00:00
SpectralRoom::SpectralRoom(Connection* connection,
QString roomId,
JoinState joinState)
: Room(connection, std::move(roomId), joinState) {
connect(this, &SpectralRoom::notificationCountChanged, this,
&SpectralRoom::countChanged);
connect(this, &SpectralRoom::highlightCountChanged, this,
&SpectralRoom::countChanged);
2018-10-07 12:38:30 +00:00
connect(this, &Room::addedMessages, this, [=] { setBusy(false); });
connect(this, &Room::fileTransferCompleted, this, [=] {
setFileUploadingProgress(0);
setHasFileUploading(false);
});
2018-09-06 04:34:15 +00:00
}
inline QString getMIME(const QUrl& fileUrl) {
return QMimeDatabase().mimeTypeForFile(fileUrl.toLocalFile()).name();
}
inline QSize getImageSize(const QUrl& imageUrl) {
QImageReader reader(imageUrl.toLocalFile());
return reader.size();
}
void SpectralRoom::chooseAndUploadFile() {
auto localFile = QFileDialog::getOpenFileUrl(Q_NULLPTR, tr("Save File as"));
if (!localFile.isEmpty()) {
QString txnID = postFile(localFile.fileName(), localFile, false);
setHasFileUploading(true);
connect(this, &Room::fileTransferCompleted,
[=](QString id, QUrl localFile, QUrl mxcUrl) {
if (id == txnID) {
setFileUploadingProgress(0);
setHasFileUploading(false);
}
});
connect(this, &Room::fileTransferFailed, [=](QString id, QString error) {
if (id == txnID) {
setFileUploadingProgress(0);
setHasFileUploading(false);
}
});
connect(
this, &Room::fileTransferProgress,
[=](QString id, qint64 progress, qint64 total) {
if (id == txnID) {
qDebug() << "Progress:" << progress << total;
setFileUploadingProgress(int(float(progress) / float(total) * 100));
}
});
}
}
2019-04-21 05:06:48 +00:00
void SpectralRoom::acceptInvitation() {
connection()->joinRoom(id());
}
2019-04-21 05:06:48 +00:00
void SpectralRoom::forget() {
connection()->forgetRoom(id());
}
2018-08-20 14:40:36 +00:00
bool SpectralRoom::hasUsersTyping() {
2018-08-20 14:40:36 +00:00
QList<User*> users = usersTyping();
2019-04-21 05:06:48 +00:00
if (users.isEmpty())
return false;
2018-08-20 14:40:36 +00:00
int count = users.length();
2019-04-21 05:06:48 +00:00
if (users.contains(localUser()))
count--;
2018-08-20 14:40:36 +00:00
return count != 0;
}
2018-12-23 03:24:01 +00:00
QVariantList SpectralRoom::getUsersTyping() {
2018-08-20 14:40:36 +00:00
QList<User*> users = usersTyping();
users.removeOne(localUser());
2018-12-23 03:24:01 +00:00
QVariantList out;
2018-08-20 14:40:36 +00:00
for (User* user : users) {
2018-12-23 03:24:01 +00:00
out.append(QVariant::fromValue(user));
2018-08-20 14:40:36 +00:00
}
2018-12-23 03:24:01 +00:00
return out;
2018-08-20 14:40:36 +00:00
}
void SpectralRoom::sendTypingNotification(bool isTyping) {
2018-08-20 14:40:36 +00:00
connection()->callApi<SetTypingJob>(BackgroundRequest, localUser()->id(),
id(), isTyping, 10000);
}
QString SpectralRoom::lastEvent() {
2019-04-21 05:06:48 +00:00
if (timelineSize() == 0)
return "";
const RoomEvent* lastEvent = messageEvents().rbegin()->get();
return user(lastEvent->senderId())->displayname() + ": " +
utils::removeReply(eventToString(*lastEvent));
}
2018-09-06 04:34:15 +00:00
bool SpectralRoom::isEventHighlighted(const RoomEvent* e) const {
2018-09-06 04:34:15 +00:00
return highlights.contains(e);
}
void SpectralRoom::checkForHighlights(const QMatrixClient::TimelineItem& ti) {
2018-09-06 04:34:15 +00:00
auto localUserId = localUser()->id();
2019-04-21 05:06:48 +00:00
if (ti->senderId() == localUserId)
return;
2018-09-06 04:34:15 +00:00
if (auto* e = ti.viewAs<RoomMessageEvent>()) {
const auto& text = e->plainBody();
if (text.contains(localUserId) ||
text.contains(roomMembername(localUserId)))
highlights.insert(e);
}
}
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); });
}
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); });
}
void SpectralRoom::countChanged() {
2018-09-06 04:34:15 +00:00
if (displayed() && !hasUnreadMessages()) {
resetNotificationCount();
resetHighlightCount();
}
}
2018-09-17 03:58:02 +00:00
2019-04-21 05:06:48 +00:00
void SpectralRoom::sendReply(QString userId,
QString eventId,
QString replyContent,
QString sendContent) {
2018-09-17 03:58:02 +00:00
QJsonObject json{
{"msgtype", "m.text"},
{"body", "> <" + userId + "> " + replyContent + "\n\n" + sendContent},
{"format", "org.matrix.custom.html"},
{"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);
}
QDateTime SpectralRoom::lastActiveTime() {
2019-04-21 05:06:48 +00:00
if (timelineSize() == 0)
return QDateTime();
2018-09-19 23:01:55 +00:00
return messageEvents().rbegin()->get()->timestamp();
}
2018-09-19 23:01:55 +00:00
int SpectralRoom::savedTopVisibleIndex() const {
return firstDisplayedMarker() == timelineEdge()
? 0
: firstDisplayedMarker() - messageEvents().rbegin();
}
int SpectralRoom::savedBottomVisibleIndex() const {
return lastDisplayedMarker() == timelineEdge()
? 0
: lastDisplayedMarker() - messageEvents().rbegin();
}
void SpectralRoom::saveViewport(int topIndex, int bottomIndex) {
if (topIndex == -1 || bottomIndex == -1 ||
(bottomIndex == savedBottomVisibleIndex() &&
(bottomIndex == 0 || topIndex == savedTopVisibleIndex())))
return;
if (bottomIndex == 0) {
setFirstDisplayedEventId({});
setLastDisplayedEventId({});
return;
}
setFirstDisplayedEvent(maxTimelineIndex() - topIndex);
setLastDisplayedEvent(maxTimelineIndex() - bottomIndex);
}
2018-10-07 12:38:30 +00:00
void SpectralRoom::getPreviousContent(int limit) {
setBusy(true);
Room::getPreviousContent(limit);
}
QVariantList SpectralRoom::getUsers(const QString& prefix) {
auto userList = users();
QVariantList matchedList;
for (auto u : userList)
if (u->displayname(this).toLower().startsWith(prefix.toLower()))
matchedList.append(QVariant::fromValue(u));
return matchedList;
}
QString SpectralRoom::postMarkdownText(const QString& markdown) {
2019-04-21 05:06:48 +00:00
unsigned char* sequence =
(unsigned char*)qstrdup(markdown.toUtf8().constData());
qint64 length = strlen((char*)sequence);
hoedown_renderer* renderer =
hoedown_html_renderer_new(HOEDOWN_HTML_USE_XHTML, 32);
hoedown_extensions extensions = (hoedown_extensions)(
(HOEDOWN_EXT_BLOCK | HOEDOWN_EXT_SPAN | HOEDOWN_EXT_MATH_EXPLICIT) &
~HOEDOWN_EXT_QUOTE);
2019-02-13 08:14:13 +00:00
hoedown_document* document = hoedown_document_new(renderer, extensions, 32);
hoedown_buffer* html = hoedown_buffer_new(length);
hoedown_document_render(document, html, sequence, length);
2019-04-21 05:06:48 +00:00
QString result = QString::fromUtf8((char*)html->data, html->size);
2019-02-13 08:14:13 +00:00
free(sequence);
hoedown_buffer_free(html);
hoedown_document_free(document);
hoedown_html_renderer_free(renderer);
return postHtmlText(markdown, result);
}
QUrl SpectralRoom::urlToMxcUrl(QUrl mxcUrl) {
return DownloadFileJob::makeRequestUrl(connection()->homeserver(), mxcUrl);
}