2018-03-05 11:12:13 +00:00
|
|
|
#include "messageeventmodel.h"
|
|
|
|
|
2018-08-03 12:58:12 +00:00
|
|
|
#include <connection.h>
|
2018-08-18 08:02:47 +00:00
|
|
|
#include <settings.h>
|
|
|
|
#include <user.h>
|
|
|
|
|
2018-08-03 12:58:12 +00:00
|
|
|
#include <events/redactionevent.h>
|
|
|
|
#include <events/roomavatarevent.h>
|
|
|
|
#include <events/roommemberevent.h>
|
|
|
|
#include <events/simplestateevents.h>
|
2018-08-18 08:02:47 +00:00
|
|
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
#include <QtQml> // for qmlRegisterType()
|
2018-03-05 11:12:13 +00:00
|
|
|
|
2018-08-03 12:58:12 +00:00
|
|
|
QHash<int, QByteArray> MessageEventModel::roleNames() const {
|
|
|
|
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
|
|
|
|
roles[EventTypeRole] = "eventType";
|
|
|
|
roles[EventIdRole] = "eventId";
|
|
|
|
roles[TimeRole] = "time";
|
|
|
|
roles[AboveTimeRole] = "aboveTime";
|
|
|
|
roles[SectionRole] = "section";
|
|
|
|
roles[AboveSectionRole] = "aboveSection";
|
|
|
|
roles[AuthorRole] = "author";
|
|
|
|
roles[AboveAuthorRole] = "aboveAuthor";
|
|
|
|
roles[ContentRole] = "content";
|
|
|
|
roles[ContentTypeRole] = "contentType";
|
2018-08-05 16:53:22 +00:00
|
|
|
roles[HighlightRole] = "highlight";
|
2018-08-03 12:58:12 +00:00
|
|
|
roles[ReadMarkerRole] = "readMarker";
|
|
|
|
roles[SpecialMarksRole] = "marks";
|
|
|
|
roles[LongOperationRole] = "progressInfo";
|
2018-08-05 16:53:22 +00:00
|
|
|
roles[AnnotationRole] = "annotation";
|
2018-08-03 12:58:12 +00:00
|
|
|
roles[EventResolvedTypeRole] = "eventResolvedType";
|
2018-08-04 20:35:31 +00:00
|
|
|
roles[PlainTextRole] = "plainText";
|
2018-08-03 12:58:12 +00:00
|
|
|
return roles;
|
|
|
|
}
|
2018-03-15 09:10:27 +00:00
|
|
|
|
2018-03-05 11:12:13 +00:00
|
|
|
MessageEventModel::MessageEventModel(QObject* parent)
|
2018-08-03 12:58:12 +00:00
|
|
|
: QAbstractListModel(parent), m_currentRoom(nullptr) {
|
2018-08-05 16:53:22 +00:00
|
|
|
using namespace QMatrixClient;
|
|
|
|
qmlRegisterType<FileTransferInfo>();
|
|
|
|
qRegisterMetaType<FileTransferInfo>();
|
|
|
|
qmlRegisterUncreatableType<EventStatus>(
|
|
|
|
"Matrique", 0, 1, "EventStatus", "EventStatus is not an creatable type");
|
2018-03-05 11:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-17 04:55:57 +00:00
|
|
|
MessageEventModel::~MessageEventModel() {}
|
|
|
|
|
2018-09-06 04:34:15 +00:00
|
|
|
void MessageEventModel::setRoom(MatriqueRoom* room) {
|
2018-07-09 02:45:26 +00:00
|
|
|
if (room == m_currentRoom) return;
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
if (m_currentRoom) {
|
|
|
|
m_currentRoom->disconnect(this);
|
|
|
|
qDebug() << "Disconnected from" << m_currentRoom->id();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_currentRoom = room;
|
|
|
|
if (room) {
|
|
|
|
lastReadEventId = room->readMarkerEventId();
|
2018-08-05 16:53:22 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
using namespace QMatrixClient;
|
|
|
|
connect(m_currentRoom, &Room::aboutToAddNewMessages, this,
|
|
|
|
[=](RoomEventsRange events) {
|
2018-08-06 15:16:35 +00:00
|
|
|
beginInsertRows({}, timelineBaseIndex(),
|
|
|
|
timelineBaseIndex() + int(events.size()) - 1);
|
2018-07-09 02:45:26 +00:00
|
|
|
});
|
|
|
|
connect(m_currentRoom, &Room::aboutToAddHistoricalMessages, this,
|
|
|
|
[=](RoomEventsRange events) {
|
2018-08-05 16:53:22 +00:00
|
|
|
if (rowCount() > 0)
|
|
|
|
rowBelowInserted = rowCount() - 1; // See #312
|
2018-08-06 15:16:35 +00:00
|
|
|
beginInsertRows({}, rowCount(),
|
2018-07-09 02:45:26 +00:00
|
|
|
rowCount() + int(events.size()) - 1);
|
|
|
|
});
|
2018-08-06 15:16:35 +00:00
|
|
|
connect(m_currentRoom, &Room::addedMessages, this,
|
|
|
|
[=](int lowest, int biggest) {
|
|
|
|
endInsertRows();
|
|
|
|
if (biggest < m_currentRoom->maxTimelineIndex()) {
|
|
|
|
auto rowBelowInserted = m_currentRoom->maxTimelineIndex() -
|
|
|
|
biggest + timelineBaseIndex() - 1;
|
|
|
|
refreshEventRoles(rowBelowInserted,
|
|
|
|
{AboveAuthorRole, AboveSectionRole});
|
|
|
|
}
|
|
|
|
for (auto i = m_currentRoom->maxTimelineIndex() - biggest;
|
|
|
|
i <= m_currentRoom->maxTimelineIndex() - lowest; ++i)
|
|
|
|
refreshLastUserEvents(i);
|
|
|
|
});
|
2018-07-29 16:00:41 +00:00
|
|
|
connect(m_currentRoom, &Room::pendingEventAboutToAdd, this,
|
|
|
|
[this] { beginInsertRows({}, 0, 0); });
|
|
|
|
connect(m_currentRoom, &Room::pendingEventAdded, this,
|
|
|
|
&MessageEventModel::endInsertRows);
|
|
|
|
connect(m_currentRoom, &Room::pendingEventAboutToMerge, this,
|
|
|
|
[this](RoomEvent*, int i) {
|
2018-08-05 16:53:22 +00:00
|
|
|
if (i == 0) return; // No need to move anything, just refresh
|
|
|
|
|
|
|
|
movingEvent = true;
|
|
|
|
// Reverse i because row 0 is bottommost in the model
|
|
|
|
const auto row = timelineBaseIndex() - i - 1;
|
|
|
|
Q_ASSERT(beginMoveRows({}, row, row, {}, timelineBaseIndex()));
|
2018-07-29 16:00:41 +00:00
|
|
|
});
|
|
|
|
connect(m_currentRoom, &Room::pendingEventMerged, this, [this] {
|
2018-08-05 16:53:22 +00:00
|
|
|
if (movingEvent) {
|
2018-07-29 16:00:41 +00:00
|
|
|
endMoveRows();
|
2018-08-05 16:53:22 +00:00
|
|
|
movingEvent = false;
|
2018-07-29 16:00:41 +00:00
|
|
|
}
|
2018-08-06 15:16:35 +00:00
|
|
|
refreshRow(timelineBaseIndex()); // Refresh the looks
|
|
|
|
refreshLastUserEvents(0);
|
2018-08-05 16:53:22 +00:00
|
|
|
if (m_currentRoom->timelineSize() > 1) // Refresh above
|
2018-08-06 12:13:51 +00:00
|
|
|
refreshEventRoles(timelineBaseIndex() + 1, {ReadMarkerRole});
|
2018-08-05 16:53:22 +00:00
|
|
|
if (timelineBaseIndex() > 0) // Refresh below, see #312
|
|
|
|
refreshEventRoles(timelineBaseIndex() - 1,
|
|
|
|
{AboveAuthorRole, AboveSectionRole});
|
2018-07-29 16:00:41 +00:00
|
|
|
});
|
|
|
|
connect(m_currentRoom, &Room::pendingEventChanged, this,
|
2018-08-05 16:53:22 +00:00
|
|
|
&MessageEventModel::refreshRow);
|
|
|
|
connect(m_currentRoom, &Room::pendingEventAboutToDiscard, this,
|
|
|
|
[this](int i) { beginRemoveRows({}, i, i); });
|
|
|
|
connect(m_currentRoom, &Room::pendingEventDiscarded, this,
|
|
|
|
&MessageEventModel::endRemoveRows);
|
2018-07-09 02:45:26 +00:00
|
|
|
connect(m_currentRoom, &Room::readMarkerMoved, this, [this] {
|
|
|
|
refreshEventRoles(
|
2018-08-06 12:13:51 +00:00
|
|
|
std::exchange(lastReadEventId, m_currentRoom->readMarkerEventId()),
|
|
|
|
{ReadMarkerRole});
|
|
|
|
refreshEventRoles(lastReadEventId, {ReadMarkerRole});
|
2018-07-09 02:45:26 +00:00
|
|
|
});
|
2018-08-06 15:16:35 +00:00
|
|
|
connect(m_currentRoom, &Room::replacedEvent, this,
|
|
|
|
[this](const RoomEvent* newEvent) {
|
|
|
|
refreshLastUserEvents(refreshEvent(newEvent->id()) -
|
|
|
|
timelineBaseIndex());
|
|
|
|
});
|
2018-07-09 02:45:26 +00:00
|
|
|
connect(m_currentRoom, &Room::fileTransferProgress, this,
|
|
|
|
&MessageEventModel::refreshEvent);
|
|
|
|
connect(m_currentRoom, &Room::fileTransferCompleted, this,
|
|
|
|
&MessageEventModel::refreshEvent);
|
|
|
|
connect(m_currentRoom, &Room::fileTransferFailed, this,
|
|
|
|
&MessageEventModel::refreshEvent);
|
|
|
|
connect(m_currentRoom, &Room::fileTransferCancelled, this,
|
|
|
|
&MessageEventModel::refreshEvent);
|
|
|
|
qDebug() << "Connected to room" << room->id() << "as"
|
|
|
|
<< room->localUser()->id();
|
|
|
|
} else
|
|
|
|
lastReadEventId.clear();
|
|
|
|
endResetModel();
|
2018-07-08 05:25:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 15:16:35 +00:00
|
|
|
int MessageEventModel::refreshEvent(const QString& eventId) {
|
|
|
|
return refreshEventRoles(eventId);
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-03-05 11:12:13 +00:00
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
void MessageEventModel::refreshRow(int row) { refreshEventRoles(row); }
|
|
|
|
|
|
|
|
int MessageEventModel::timelineBaseIndex() const {
|
|
|
|
return m_currentRoom ? int(m_currentRoom->pendingEvents().size()) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageEventModel::refreshEventRoles(int row, const QVector<int>& roles) {
|
2018-07-29 16:00:41 +00:00
|
|
|
const auto idx = index(row);
|
|
|
|
emit dataChanged(idx, idx, roles);
|
|
|
|
}
|
|
|
|
|
2018-08-06 15:16:35 +00:00
|
|
|
int MessageEventModel::refreshEventRoles(const QString& eventId,
|
|
|
|
const QVector<int>& roles) {
|
2018-07-09 02:45:26 +00:00
|
|
|
const auto it = m_currentRoom->findInTimeline(eventId);
|
2018-08-05 16:53:22 +00:00
|
|
|
if (it == m_currentRoom->timelineEdge()) {
|
|
|
|
qWarning() << "Trying to refresh inexistent event:" << eventId;
|
2018-08-06 15:16:35 +00:00
|
|
|
return -1;
|
2018-08-05 16:53:22 +00:00
|
|
|
}
|
2018-08-06 15:16:35 +00:00
|
|
|
const auto row =
|
|
|
|
it - m_currentRoom->messageEvents().rbegin() + timelineBaseIndex();
|
|
|
|
refreshEventRoles(row, roles);
|
|
|
|
return row;
|
2018-03-05 11:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
inline bool hasValidTimestamp(const QMatrixClient::TimelineItem& ti) {
|
|
|
|
return ti->timestamp().isValid();
|
2018-03-05 11:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
QDateTime MessageEventModel::makeMessageTimestamp(
|
2018-08-03 12:58:12 +00:00
|
|
|
const QMatrixClient::Room::rev_iter_t& baseIt) const {
|
2018-07-09 02:45:26 +00:00
|
|
|
const auto& timeline = m_currentRoom->messageEvents();
|
|
|
|
auto ts = baseIt->event()->timestamp();
|
|
|
|
if (ts.isValid()) return ts;
|
|
|
|
|
|
|
|
// The event is most likely redacted or just invalid.
|
|
|
|
// Look for the nearest date around and slap zero time to it.
|
|
|
|
using QMatrixClient::TimelineItem;
|
|
|
|
auto rit = std::find_if(baseIt, timeline.rend(), hasValidTimestamp);
|
|
|
|
if (rit != timeline.rend())
|
|
|
|
return {rit->event()->timestamp().date(), {0, 0}, Qt::LocalTime};
|
|
|
|
auto it = std::find_if(baseIt.base(), timeline.end(), hasValidTimestamp);
|
|
|
|
if (it != timeline.end())
|
|
|
|
return {it->event()->timestamp().date(), {0, 0}, Qt::LocalTime};
|
|
|
|
|
|
|
|
// What kind of room is that?..
|
|
|
|
qCritical() << "No valid timestamps in the room timeline!";
|
|
|
|
return {};
|
2018-03-05 11:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
QString MessageEventModel::renderDate(QDateTime timestamp) const {
|
|
|
|
auto date = timestamp.toLocalTime().date();
|
2018-07-09 02:45:26 +00:00
|
|
|
if (QMatrixClient::SettingsGroup("UI")
|
|
|
|
.value("banner_human_friendly_date", true)
|
|
|
|
.toBool()) {
|
|
|
|
if (date == QDate::currentDate()) return tr("Today");
|
|
|
|
if (date == QDate::currentDate().addDays(-1)) return tr("Yesterday");
|
|
|
|
if (date == QDate::currentDate().addDays(-2))
|
|
|
|
return tr("The day before yesterday");
|
|
|
|
if (date > QDate::currentDate().addDays(-7)) return date.toString("dddd");
|
|
|
|
}
|
|
|
|
return date.toString(Qt::DefaultLocaleShortDate);
|
2018-03-05 11:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 12:13:51 +00:00
|
|
|
bool MessageEventModel::isUserActivityNotable(
|
|
|
|
const QMatrixClient::Room::rev_iter_t& baseIt) const {
|
|
|
|
const auto& senderId = (*baseIt)->senderId();
|
|
|
|
// TODO: Go up and down the timeline (limit to 100 events for
|
|
|
|
// the sake of performance) and collect all messages of
|
|
|
|
// this author; find out if there's anything besides joins, leaves
|
|
|
|
// and redactions; if not, double-check whether the current event is
|
|
|
|
// a part of a re-join without following redactions.
|
|
|
|
using namespace QMatrixClient;
|
|
|
|
bool joinFound = false, redactionsFound = false;
|
|
|
|
// Find the nearest join of this user above, or a no-nonsense event.
|
|
|
|
for (auto it = baseIt,
|
2018-08-06 15:16:35 +00:00
|
|
|
limit = baseIt +
|
|
|
|
std::min(int(m_currentRoom->timelineEdge() - baseIt), 100);
|
2018-08-06 12:13:51 +00:00
|
|
|
it != limit; ++it) {
|
|
|
|
const auto& e = **it;
|
|
|
|
if (e.senderId() != senderId) continue;
|
|
|
|
if (e.isRedacted()) {
|
|
|
|
redactionsFound = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto* me = it->viewAs<QMatrixClient::RoomMemberEvent>()) {
|
|
|
|
if (me->isJoin()) {
|
|
|
|
joinFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return true; // Consider all other events notable
|
|
|
|
}
|
|
|
|
// Find the nearest leave of this user below, or a no-nonsense event
|
|
|
|
bool leaveFound = false;
|
|
|
|
for (auto it = baseIt.base() - 1,
|
2018-08-06 15:16:35 +00:00
|
|
|
limit = baseIt.base() +
|
|
|
|
std::min(int(m_currentRoom->messageEvents().end() -
|
|
|
|
baseIt.base()),
|
|
|
|
100);
|
2018-08-06 12:13:51 +00:00
|
|
|
it != limit; ++it) {
|
|
|
|
const auto& e = **it;
|
|
|
|
if (e.senderId() != senderId) continue;
|
|
|
|
if (e.isRedacted()) {
|
|
|
|
redactionsFound = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (auto* me = it->viewAs<RoomMemberEvent>()) {
|
|
|
|
if (me->isLeave() || me->membership() == MembershipType::Ban) {
|
|
|
|
leaveFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// If we are here, it means that no notable events have been found in
|
|
|
|
// the timeline vicinity, and probably redactions are there. Doesn't look
|
|
|
|
// notable but let's give some benefit of doubt.
|
|
|
|
if (redactionsFound) return false; // Join + redactions or redactions + leave
|
|
|
|
return !(joinFound && leaveFound); // Join + (maybe profile changes) + leave
|
|
|
|
}
|
|
|
|
|
2018-08-06 15:16:35 +00:00
|
|
|
void MessageEventModel::refreshLastUserEvents(int baseTimelineRow) {
|
|
|
|
if (!m_currentRoom || m_currentRoom->timelineSize() <= baseTimelineRow)
|
|
|
|
return;
|
|
|
|
const auto& timelineBottom = m_currentRoom->messageEvents().rbegin();
|
|
|
|
const auto& lastSender = (*(timelineBottom + baseTimelineRow))->senderId();
|
|
|
|
const auto limit = timelineBottom + std::min(baseTimelineRow + 100,
|
|
|
|
m_currentRoom->timelineSize());
|
|
|
|
for (auto it = timelineBottom + std::max(baseTimelineRow - 100, 0);
|
|
|
|
it != limit; ++it) {
|
|
|
|
if ((*it)->senderId() == lastSender) {
|
|
|
|
auto idx = index(it - timelineBottom);
|
|
|
|
emit dataChanged(idx, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
int MessageEventModel::rowCount(const QModelIndex& parent) const {
|
|
|
|
if (!m_currentRoom || parent.isValid()) return 0;
|
|
|
|
return m_currentRoom->timelineSize();
|
2018-03-05 11:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
QVariant MessageEventModel::data(const QModelIndex& idx, int role) const {
|
|
|
|
const auto row = idx.row();
|
2018-07-29 16:00:41 +00:00
|
|
|
|
|
|
|
if (!m_currentRoom || row < 0 ||
|
|
|
|
row >= int(m_currentRoom->pendingEvents().size()) +
|
|
|
|
m_currentRoom->timelineSize())
|
|
|
|
return {};
|
2018-07-09 02:45:26 +00:00
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
bool isPending = row < timelineBaseIndex();
|
2018-07-29 16:00:41 +00:00
|
|
|
const auto timelineIt = m_currentRoom->messageEvents().crbegin() +
|
2018-08-05 16:53:22 +00:00
|
|
|
std::max(0, row - timelineBaseIndex());
|
|
|
|
const auto pendingIt = m_currentRoom->pendingEvents().crbegin() +
|
|
|
|
std::min(row, timelineBaseIndex());
|
2018-08-06 12:13:51 +00:00
|
|
|
const auto& evt = isPending ? **pendingIt : **timelineIt;
|
2018-07-09 02:45:26 +00:00
|
|
|
|
|
|
|
using namespace QMatrixClient;
|
|
|
|
if (role == Qt::DisplayRole) {
|
2018-07-29 16:00:41 +00:00
|
|
|
if (evt.isRedacted()) {
|
|
|
|
auto reason = evt.redactedBecause()->reason();
|
|
|
|
if (reason.isEmpty()) return tr("Redacted");
|
2018-03-05 11:12:13 +00:00
|
|
|
|
2018-07-29 16:00:41 +00:00
|
|
|
return tr("Redacted: %1").arg(evt.redactedBecause()->reason());
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-07-29 16:00:41 +00:00
|
|
|
|
|
|
|
return visit(
|
|
|
|
evt,
|
|
|
|
[this](const RoomMessageEvent& e) {
|
|
|
|
using namespace MessageEventContent;
|
|
|
|
|
|
|
|
if (e.hasTextContent() && e.mimeType().name() != "text/plain")
|
|
|
|
return static_cast<const TextContent*>(e.content())->body;
|
|
|
|
if (e.hasFileContent()) {
|
|
|
|
auto fileCaption = e.content()->fileInfo()->originalName;
|
|
|
|
if (fileCaption.isEmpty())
|
|
|
|
fileCaption = m_currentRoom->prettyPrint(e.plainBody());
|
|
|
|
if (fileCaption.isEmpty()) return tr("a file");
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-07-29 16:00:41 +00:00
|
|
|
return m_currentRoom->prettyPrint(e.plainBody());
|
|
|
|
},
|
|
|
|
[this](const RoomMemberEvent& e) {
|
|
|
|
// FIXME: Rewind to the name that was at the time of this event
|
|
|
|
QString subjectName = m_currentRoom->roomMembername(e.userId());
|
|
|
|
// The below code assumes senderName output in AuthorRole
|
|
|
|
switch (e.membership()) {
|
|
|
|
case MembershipType::Invite:
|
|
|
|
if (e.repeatsState())
|
|
|
|
return tr("reinvited %1 to the room").arg(subjectName);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case MembershipType::Join: {
|
|
|
|
if (e.repeatsState()) return tr("joined the room (repeated)");
|
|
|
|
if (!e.prevContent() ||
|
|
|
|
e.membership() != e.prevContent()->membership) {
|
|
|
|
return e.membership() == MembershipType::Invite
|
|
|
|
? tr("invited %1 to the room").arg(subjectName)
|
|
|
|
: tr("joined the room");
|
|
|
|
}
|
2018-08-04 20:35:31 +00:00
|
|
|
QString text{};
|
2018-08-06 12:13:51 +00:00
|
|
|
if (e.isRename()) {
|
2018-08-04 20:35:31 +00:00
|
|
|
if (e.displayName().isEmpty())
|
|
|
|
text = tr("cleared the display name");
|
|
|
|
else
|
|
|
|
text =
|
|
|
|
tr("changed the display name to %1").arg(e.displayName());
|
|
|
|
}
|
2018-08-06 12:13:51 +00:00
|
|
|
if (e.isAvatarUpdate()) {
|
2018-08-04 20:35:31 +00:00
|
|
|
if (!text.isEmpty()) text += " and ";
|
|
|
|
if (e.avatarUrl().isEmpty())
|
|
|
|
text += tr("cleared the avatar");
|
|
|
|
else
|
|
|
|
text += tr("updated the avatar");
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
case MembershipType::Leave:
|
|
|
|
if (e.prevContent() &&
|
|
|
|
e.prevContent()->membership == MembershipType::Ban) {
|
|
|
|
return (e.senderId() != e.userId())
|
|
|
|
? tr("unbanned %1").arg(subjectName)
|
|
|
|
: tr("self-unbanned");
|
|
|
|
}
|
|
|
|
return (e.senderId() != e.userId())
|
|
|
|
? tr("has put %1 out of the room").arg(subjectName)
|
|
|
|
: tr("left the room");
|
|
|
|
case MembershipType::Ban:
|
|
|
|
return (e.senderId() != e.userId())
|
|
|
|
? tr("banned %1 from the room").arg(subjectName)
|
|
|
|
: tr("self-banned from the room");
|
|
|
|
case MembershipType::Knock:
|
|
|
|
return tr("knocked");
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
return tr("made something unknown");
|
|
|
|
},
|
|
|
|
[](const RoomAliasesEvent& e) {
|
|
|
|
return tr("set aliases to: %1").arg(e.aliases().join(", "));
|
|
|
|
},
|
|
|
|
[](const RoomCanonicalAliasEvent& e) {
|
|
|
|
return (e.alias().isEmpty())
|
|
|
|
? tr("cleared the room main alias")
|
|
|
|
: tr("set the room main alias to: %1").arg(e.alias());
|
|
|
|
},
|
|
|
|
[](const RoomNameEvent& e) {
|
|
|
|
return (e.name().isEmpty())
|
|
|
|
? tr("cleared the room name")
|
|
|
|
: tr("set the room name to: %1").arg(e.name());
|
|
|
|
},
|
|
|
|
[](const RoomTopicEvent& e) {
|
|
|
|
return (e.topic().isEmpty())
|
|
|
|
? tr("cleared the topic")
|
|
|
|
: tr("set the topic to: %1").arg(e.topic());
|
|
|
|
},
|
|
|
|
[](const RoomAvatarEvent&) { return tr("changed the room avatar"); },
|
|
|
|
[](const EncryptionEvent&) {
|
|
|
|
return tr("activated End-to-End Encryption");
|
|
|
|
},
|
|
|
|
tr("Unknown Event"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role == PlainTextRole) {
|
|
|
|
if (evt.isRedacted()) {
|
|
|
|
auto reason = evt.redactedBecause()->reason();
|
|
|
|
if (reason.isEmpty()) return tr("Redacted");
|
|
|
|
|
|
|
|
return tr("Redacted: %1").arg(evt.redactedBecause()->reason());
|
|
|
|
}
|
|
|
|
|
|
|
|
return visit(
|
|
|
|
evt,
|
|
|
|
[this](const RoomMessageEvent& e) {
|
|
|
|
using namespace MessageEventContent;
|
|
|
|
|
|
|
|
if (e.hasFileContent()) {
|
|
|
|
auto fileCaption = e.content()->fileInfo()->originalName;
|
2018-08-05 21:00:06 +00:00
|
|
|
if (fileCaption.isEmpty()) fileCaption = e.plainBody();
|
2018-08-04 20:35:31 +00:00
|
|
|
if (fileCaption.isEmpty()) return tr("a file");
|
|
|
|
}
|
|
|
|
return e.plainBody();
|
|
|
|
},
|
|
|
|
[this](const RoomMemberEvent& e) {
|
|
|
|
// FIXME: Rewind to the name that was at the time of this event
|
|
|
|
QString subjectName = m_currentRoom->roomMembername(e.userId());
|
|
|
|
// The below code assumes senderName output in AuthorRole
|
|
|
|
switch (e.membership()) {
|
|
|
|
case MembershipType::Invite:
|
|
|
|
if (e.repeatsState())
|
|
|
|
return tr("reinvited %1 to the room").arg(subjectName);
|
|
|
|
FALLTHROUGH;
|
|
|
|
case MembershipType::Join: {
|
|
|
|
if (e.repeatsState()) return tr("joined the room (repeated)");
|
|
|
|
if (!e.prevContent() ||
|
|
|
|
e.membership() != e.prevContent()->membership) {
|
|
|
|
return e.membership() == MembershipType::Invite
|
|
|
|
? tr("invited %1 to the room").arg(subjectName)
|
|
|
|
: tr("joined the room");
|
|
|
|
}
|
2018-07-29 16:00:41 +00:00
|
|
|
QString text{};
|
|
|
|
if (e.displayName() != e.prevContent()->displayName) {
|
|
|
|
if (e.displayName().isEmpty())
|
|
|
|
text = tr("cleared the display name");
|
|
|
|
else
|
|
|
|
text =
|
|
|
|
tr("changed the display name to %1").arg(e.displayName());
|
|
|
|
}
|
|
|
|
if (e.avatarUrl() != e.prevContent()->avatarUrl) {
|
|
|
|
if (!text.isEmpty()) text += " and ";
|
|
|
|
if (e.avatarUrl().isEmpty())
|
|
|
|
text += tr("cleared the avatar");
|
|
|
|
else
|
|
|
|
text += tr("updated the avatar");
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
case MembershipType::Leave:
|
|
|
|
if (e.prevContent() &&
|
|
|
|
e.prevContent()->membership == MembershipType::Ban) {
|
|
|
|
return (e.senderId() != e.userId())
|
|
|
|
? tr("unbanned %1").arg(subjectName)
|
|
|
|
: tr("self-unbanned");
|
|
|
|
}
|
|
|
|
return (e.senderId() != e.userId())
|
|
|
|
? tr("has put %1 out of the room").arg(subjectName)
|
|
|
|
: tr("left the room");
|
|
|
|
case MembershipType::Ban:
|
|
|
|
return (e.senderId() != e.userId())
|
|
|
|
? tr("banned %1 from the room").arg(subjectName)
|
|
|
|
: tr("self-banned from the room");
|
|
|
|
case MembershipType::Knock:
|
|
|
|
return tr("knocked");
|
|
|
|
default:;
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
return tr("made something unknown");
|
2018-07-29 16:00:41 +00:00
|
|
|
},
|
|
|
|
[](const RoomAliasesEvent& e) {
|
|
|
|
return tr("set aliases to: %1").arg(e.aliases().join(", "));
|
|
|
|
},
|
|
|
|
[](const RoomCanonicalAliasEvent& e) {
|
|
|
|
return (e.alias().isEmpty())
|
|
|
|
? tr("cleared the room main alias")
|
|
|
|
: tr("set the room main alias to: %1").arg(e.alias());
|
|
|
|
},
|
|
|
|
[](const RoomNameEvent& e) {
|
|
|
|
return (e.name().isEmpty())
|
|
|
|
? tr("cleared the room name")
|
|
|
|
: tr("set the room name to: %1").arg(e.name());
|
|
|
|
},
|
|
|
|
[](const RoomTopicEvent& e) {
|
|
|
|
return (e.topic().isEmpty())
|
|
|
|
? tr("cleared the topic")
|
|
|
|
: tr("set the topic to: %1").arg(e.topic());
|
|
|
|
},
|
|
|
|
[](const RoomAvatarEvent&) { return tr("changed the room avatar"); },
|
|
|
|
[](const EncryptionEvent&) {
|
|
|
|
return tr("activated End-to-End Encryption");
|
|
|
|
},
|
|
|
|
tr("Unknown Event"));
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (role == Qt::ToolTipRole) {
|
2018-07-29 16:00:41 +00:00
|
|
|
return evt.originalJson();
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (role == EventTypeRole) {
|
2018-07-29 16:00:41 +00:00
|
|
|
if (auto e = eventCast<const RoomMessageEvent>(&evt)) {
|
|
|
|
switch (e->msgtype()) {
|
2018-07-09 02:45:26 +00:00
|
|
|
case MessageEventType::Emote:
|
|
|
|
return "emote";
|
|
|
|
case MessageEventType::Notice:
|
|
|
|
return "notice";
|
|
|
|
case MessageEventType::Image:
|
|
|
|
return "image";
|
2018-08-17 07:58:08 +00:00
|
|
|
case MessageEventType::Audio:
|
|
|
|
return "audio";
|
2018-07-09 02:45:26 +00:00
|
|
|
default:
|
2018-08-05 16:53:22 +00:00
|
|
|
return e->hasFileContent() ? "file" : "message";
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-29 16:00:41 +00:00
|
|
|
if (evt.isStateEvent()) return "state";
|
2018-03-05 11:12:13 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
return "other";
|
|
|
|
}
|
2018-07-08 05:25:46 +00:00
|
|
|
|
2018-07-29 16:00:41 +00:00
|
|
|
if (role == EventResolvedTypeRole)
|
|
|
|
return EventTypeRegistry::getMatrixType(evt.type());
|
2018-07-09 02:45:26 +00:00
|
|
|
|
|
|
|
if (role == AuthorRole) {
|
|
|
|
// FIXME: It shouldn't be User, it should be its state "as of event"
|
2018-08-05 16:53:22 +00:00
|
|
|
return QVariant::fromValue(isPending ? m_currentRoom->localUser()
|
|
|
|
: m_currentRoom->user(evt.senderId()));
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (role == ContentTypeRole) {
|
2018-07-29 16:00:41 +00:00
|
|
|
if (auto e = eventCast<const RoomMessageEvent>(&evt)) {
|
|
|
|
const auto& contentType = e->mimeType().name();
|
|
|
|
return contentType == "text/plain" ? QStringLiteral("text/html")
|
|
|
|
: contentType;
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
2018-07-29 16:00:41 +00:00
|
|
|
return QStringLiteral("text/plain");
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (role == ContentRole) {
|
2018-07-29 16:00:41 +00:00
|
|
|
if (evt.isRedacted()) {
|
|
|
|
auto reason = evt.redactedBecause()->reason();
|
|
|
|
return (reason.isEmpty())
|
|
|
|
? tr("Redacted")
|
|
|
|
: tr("Redacted: %1").arg(evt.redactedBecause()->reason());
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 16:00:41 +00:00
|
|
|
if (auto e = eventCast<const RoomMessageEvent>(&evt)) {
|
|
|
|
// Cannot use e.contentJson() here because some
|
|
|
|
// EventContent classes inject values into the copy of the
|
|
|
|
// content JSON stored in EventContent::Base
|
|
|
|
return e->hasFileContent()
|
|
|
|
? QVariant::fromValue(e->content()->originalJson)
|
|
|
|
: QVariant();
|
|
|
|
};
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 04:34:15 +00:00
|
|
|
if (role == HighlightRole) return m_currentRoom->isEventHighlighted(&evt);
|
|
|
|
|
2018-07-29 16:00:41 +00:00
|
|
|
if (role == ReadMarkerRole) return evt.id() == lastReadEventId;
|
2018-07-09 02:45:26 +00:00
|
|
|
|
|
|
|
if (role == SpecialMarksRole) {
|
2018-08-05 16:53:22 +00:00
|
|
|
if (isPending) return pendingIt->deliveryStatus();
|
2018-07-29 16:00:41 +00:00
|
|
|
|
2018-08-06 12:13:51 +00:00
|
|
|
if (is<RedactionEvent>(evt)) return EventStatus::Hidden;
|
|
|
|
auto* memberEvent = timelineIt->viewAs<RoomMemberEvent>();
|
|
|
|
if (memberEvent) {
|
|
|
|
if ((memberEvent->isJoin() || memberEvent->isLeave()) &&
|
|
|
|
!Settings().value("UI/show_joinleave", true).toBool())
|
|
|
|
return EventStatus::Hidden;
|
|
|
|
}
|
2018-08-06 15:16:35 +00:00
|
|
|
if (memberEvent || evt.isRedacted()) {
|
2018-08-06 12:13:51 +00:00
|
|
|
if (evt.senderId() == m_currentRoom->localUser()->id() ||
|
|
|
|
Settings().value("UI/show_spammy").toBool()) {
|
2018-08-06 15:16:35 +00:00
|
|
|
// QElapsedTimer et; et.start();
|
|
|
|
auto hide = !isUserActivityNotable(timelineIt);
|
|
|
|
// qDebug() << "Checked user activity for" << evt.id() <<
|
|
|
|
// "in" << et;
|
|
|
|
if (hide) return EventStatus::Hidden;
|
2018-08-06 12:13:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-06 15:16:35 +00:00
|
|
|
if (evt.isRedacted())
|
2018-08-17 04:55:57 +00:00
|
|
|
return Settings().value("UI/show_redacted").toBool()
|
|
|
|
? EventStatus::Redacted
|
|
|
|
: EventStatus::Hidden;
|
2018-08-06 12:13:51 +00:00
|
|
|
|
2018-07-29 16:00:41 +00:00
|
|
|
if (evt.isStateEvent() &&
|
2018-08-05 16:53:22 +00:00
|
|
|
static_cast<const StateEventBase&>(evt).repeatsState() &&
|
2018-08-06 12:13:51 +00:00
|
|
|
!Settings().value("UI/show_noop_events").toBool())
|
2018-08-05 16:53:22 +00:00
|
|
|
return EventStatus::Hidden;
|
|
|
|
|
2018-08-06 12:13:51 +00:00
|
|
|
return EventStatus::Normal;
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
if (role == EventIdRole)
|
|
|
|
return !evt.id().isEmpty() ? evt.id() : evt.transactionId();
|
2018-07-08 05:25:46 +00:00
|
|
|
|
2018-07-09 02:45:26 +00:00
|
|
|
if (role == LongOperationRole) {
|
2018-07-29 16:00:41 +00:00
|
|
|
if (auto e = eventCast<const RoomMessageEvent>(&evt))
|
|
|
|
if (e->hasFileContent())
|
|
|
|
return QVariant::fromValue(m_currentRoom->fileTransferInfo(e->id()));
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
if (role == AnnotationRole)
|
|
|
|
if (isPending) return pendingIt->annotation();
|
2018-07-29 16:00:41 +00:00
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
if (role == TimeRole || role == SectionRole) {
|
|
|
|
auto ts =
|
|
|
|
isPending ? pendingIt->lastUpdated() : makeMessageTimestamp(timelineIt);
|
|
|
|
return role == TimeRole ? QVariant(ts) : renderDate(ts);
|
|
|
|
}
|
2018-07-29 16:00:41 +00:00
|
|
|
|
2018-08-05 21:00:06 +00:00
|
|
|
if (role == AboveSectionRole || role == AboveAuthorRole ||
|
|
|
|
role == AboveTimeRole)
|
2018-08-05 16:53:22 +00:00
|
|
|
for (auto r = row + 1; r < rowCount(); ++r) {
|
|
|
|
auto i = index(r);
|
|
|
|
if (data(i, SpecialMarksRole) != EventStatus::Hidden)
|
2018-08-05 21:00:06 +00:00
|
|
|
return data(i, role == AboveSectionRole
|
|
|
|
? SectionRole
|
|
|
|
: role == AboveAuthorRole ? AuthorRole : TimeRole);
|
2018-07-29 16:00:41 +00:00
|
|
|
}
|
2018-07-09 02:45:26 +00:00
|
|
|
|
2018-08-05 16:53:22 +00:00
|
|
|
return {};
|
2018-07-09 02:45:26 +00:00
|
|
|
}
|