Show custom emoji reactions as per MSC4027

This commit is contained in:
Tobias Fella
2024-02-12 14:11:54 +01:00
parent cd9e98f24b
commit ca57732871
3 changed files with 30 additions and 11 deletions

View File

@@ -80,7 +80,7 @@ QVariant ReactionModel::data(const QModelIndex &index, int role) const
"%2 reacted with %3", "%2 reacted with %3",
reaction.authors.count(), reaction.authors.count(),
text, text,
reactionText(reaction.reaction)); m_shortcodes.contains(reaction.reaction) ? m_shortcodes[reaction.reaction] : reactionText(reaction.reaction));
return text; return text;
} }
@@ -111,6 +111,7 @@ void ReactionModel::updateReactions()
beginResetModel(); beginResetModel();
m_reactions.clear(); m_reactions.clear();
m_shortcodes.clear();
const auto &annotations = m_room->relatedEvents(*m_event, Quotient::EventRelation::AnnotationType); const auto &annotations = m_room->relatedEvents(*m_event, Quotient::EventRelation::AnnotationType);
if (annotations.isEmpty()) { if (annotations.isEmpty()) {
@@ -125,6 +126,9 @@ void ReactionModel::updateReactions()
} }
if (const auto &e = eventCast<const Quotient::ReactionEvent>(a)) { if (const auto &e = eventCast<const Quotient::ReactionEvent>(a)) {
reactions[e->key()].append(m_room->user(e->senderId())); reactions[e->key()].append(m_room->user(e->senderId()));
if (e->contentJson()[QStringLiteral("shortcode")].toString().length()) {
m_shortcodes[e->key()] = e->contentJson()[QStringLiteral("shortcode")].toString().toHtmlEscaped();
}
} }
} }
@@ -158,8 +162,18 @@ QHash<int, QByteArray> ReactionModel::roleNames() const
}; };
} }
QString ReactionModel::reactionText(const QString &text) QString ReactionModel::reactionText(QString text) const
{ {
text = text.toHtmlEscaped();
if (text.startsWith(QStringLiteral("mxc://"))) {
static QFont font;
static int size = font.pixelSize();
if (size == -1) {
size = font.pointSizeF() * 1.333;
}
return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%2\">")
.arg(m_room->connection()->makeMediaUrl(QUrl(text)).toString(), QString::number(size));
}
const auto isEmoji = [](const QString &text) { const auto isEmoji = [](const QString &text) {
#ifdef HAVE_ICU #ifdef HAVE_ICU
QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, text); QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, text);

View File

@@ -71,8 +71,9 @@ private:
const NeoChatRoom *m_room; const NeoChatRoom *m_room;
const Quotient::RoomMessageEvent *m_event; const Quotient::RoomMessageEvent *m_event;
QList<Reaction> m_reactions; QList<Reaction> m_reactions;
QMap<QString, QString> m_shortcodes;
void updateReactions(); void updateReactions();
static QString reactionText(const QString &text); QString reactionText(QString text) const;
}; };
Q_DECLARE_METATYPE(ReactionModel *) Q_DECLARE_METATYPE(ReactionModel *)

View File

@@ -39,14 +39,17 @@ Flow {
width: Math.max(contentItem.implicitWidth + leftPadding + rightPadding, height) width: Math.max(contentItem.implicitWidth + leftPadding + rightPadding, height)
height: Math.round(Kirigami.Units.gridUnit * 1.5) height: Math.round(Kirigami.Units.gridUnit * 1.5)
contentItem: QQC2.Label { contentItem: Item {
id: reactionLabel QQC2.Label {
horizontalAlignment: Text.AlignHCenter id: reactionLabel
verticalAlignment: Text.AlignVCenter anchors.centerIn: parent
text: reactionDelegate.textContent horizontalAlignment: Text.AlignHCenter
background: null verticalAlignment: Text.AlignVCenter
wrapMode: TextEdit.NoWrap text: reactionDelegate.textContent
textFormat: Text.RichText background: null
wrapMode: TextEdit.NoWrap
textFormat: Text.RichText
}
} }
padding: Kirigami.Units.smallSpacing padding: Kirigami.Units.smallSpacing
@@ -66,6 +69,7 @@ Flow {
hoverEnabled: true hoverEnabled: true
QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
QQC2.ToolTip.visible: hovered QQC2.ToolTip.visible: hovered
QQC2.ToolTip.text: reactionDelegate.toolTip QQC2.ToolTip.text: reactionDelegate.toolTip
} }