Skip to content

Commit 519f620

Browse files
committed
Send custom emoji reaction as mxc
1 parent b0741fa commit 519f620

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

src/app/molecules/message/Message.jsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import React, {
55
import PropTypes from 'prop-types';
66
import './Message.scss';
77

8-
import { getShortcodeToCustomEmoji } from '../../organisms/emoji-board/custom-emoji';
98
import { twemojify } from '../../../util/twemojify';
109

1110
import initMatrix from '../../../client/initMatrix';
@@ -322,25 +321,25 @@ function getMyEmojiEvent(emojiKey, eventId, roomTimeline) {
322321
return rEvent;
323322
}
324323

325-
function toggleEmoji(roomId, eventId, emojiKey, roomTimeline) {
324+
function toggleEmoji(roomId, eventId, emojiKey, shortcode, roomTimeline) {
326325
const myAlreadyReactEvent = getMyEmojiEvent(emojiKey, eventId, roomTimeline);
327326
if (myAlreadyReactEvent) {
328327
const rId = myAlreadyReactEvent.getId();
329328
if (rId.startsWith('~')) return;
330329
redactEvent(roomId, rId);
331330
return;
332331
}
333-
sendReaction(roomId, eventId, emojiKey);
332+
sendReaction(roomId, eventId, emojiKey, shortcode);
334333
}
335334

336335
function pickEmoji(e, roomId, eventId, roomTimeline) {
337336
openEmojiBoard(getEventCords(e), (emoji) => {
338-
toggleEmoji(roomId, eventId, emoji.unicode, roomTimeline);
337+
toggleEmoji(roomId, eventId, emoji.mxc ?? emoji.unicode, emoji.shortcodes[0], roomTimeline);
339338
e.target.click();
340339
});
341340
}
342341

343-
function genReactionMsg(userIds, reaction) {
342+
function genReactionMsg(userIds, reaction, shortcode) {
344343
return (
345344
<>
346345
{userIds.map((userId, index) => (
@@ -354,24 +353,22 @@ function genReactionMsg(userIds, reaction) {
354353
</React.Fragment>
355354
))}
356355
<span style={{ opacity: '.6' }}>{' reacted with '}</span>
357-
{twemojify(reaction, { className: 'react-emoji' })}
356+
{twemojify(shortcode ? `:${shortcode}:` : reaction, { className: 'react-emoji' })}
358357
</>
359358
);
360359
}
361360

362361
function MessageReaction({
363-
shortcodeToEmoji, reaction, count, users, isActive, onClick,
362+
reaction, shortcode, count, users, isActive, onClick,
364363
}) {
365-
const customEmojiMatch = reaction.match(/^:(\S+):$/);
366364
let customEmojiUrl = null;
367-
if (customEmojiMatch) {
368-
const customEmoji = shortcodeToEmoji.get(customEmojiMatch[1]);
369-
customEmojiUrl = initMatrix.matrixClient.mxcUrlToHttp(customEmoji?.mxc);
365+
if (reaction.match(/^mxc:\/\/\S+$/)) {
366+
customEmojiUrl = initMatrix.matrixClient.mxcUrlToHttp(reaction);
370367
}
371368
return (
372369
<Tooltip
373370
className="msg__reaction-tooltip"
374-
content={<Text variant="b2">{users.length > 0 ? genReactionMsg(users, reaction) : 'Unable to load who has reacted'}</Text>}
371+
content={<Text variant="b2">{users.length > 0 ? genReactionMsg(users, reaction, shortcode) : 'Unable to load who has reacted'}</Text>}
375372
>
376373
<button
377374
onClick={onClick}
@@ -380,17 +377,20 @@ function MessageReaction({
380377
>
381378
{
382379
customEmojiUrl
383-
? <img className="react-emoji" draggable="false" alt={reaction} src={customEmojiUrl} />
380+
? <img className="react-emoji" draggable="false" alt={shortcode ?? reaction} src={customEmojiUrl} />
384381
: twemojify(reaction, { className: 'react-emoji' })
385382
}
386383
<Text variant="b3" className="msg__reaction-count">{count}</Text>
387384
</button>
388385
</Tooltip>
389386
);
390387
}
388+
MessageReaction.defaultProps = {
389+
shortcode: undefined,
390+
};
391391
MessageReaction.propTypes = {
392-
shortcodeToEmoji: PropTypes.shape({}).isRequired,
393392
reaction: PropTypes.node.isRequired,
393+
shortcode: PropTypes.string,
394394
count: PropTypes.number.isRequired,
395395
users: PropTypes.arrayOf(PropTypes.string).isRequired,
396396
isActive: PropTypes.bool.isRequired,
@@ -401,17 +401,17 @@ function MessageReactionGroup({ roomTimeline, mEvent }) {
401401
const { roomId, room, reactionTimeline } = roomTimeline;
402402
const mx = initMatrix.matrixClient;
403403
const reactions = {};
404-
const shortcodeToEmoji = getShortcodeToCustomEmoji(room);
405404
const canSendReaction = room.currentState.maySendEvent('m.reaction', mx.getUserId());
406405

407406
const eventReactions = reactionTimeline.get(mEvent.getId());
408-
const addReaction = (key, count, senderId, isActive) => {
407+
const addReaction = (key, shortcode, count, senderId, isActive) => {
409408
let reaction = reactions[key];
410409
if (reaction === undefined) {
411410
reaction = {
412411
count: 0,
413412
users: [],
414413
isActive: false,
414+
shortcode,
415415
};
416416
}
417417
if (count) {
@@ -429,17 +429,18 @@ function MessageReactionGroup({ roomTimeline, mEvent }) {
429429
if (rEvent.getRelation() === null) return;
430430
const reaction = rEvent.getRelation();
431431
const senderId = rEvent.getSender();
432+
const { shortcode } = rEvent.getContent();
432433
const isActive = senderId === mx.getUserId();
433434

434-
addReaction(reaction.key, undefined, senderId, isActive);
435+
addReaction(reaction.key, shortcode, undefined, senderId, isActive);
435436
});
436437
} else {
437438
// Use aggregated reactions
438439
const aggregatedReaction = mEvent.getServerAggregatedRelation('m.annotation')?.chunk;
439440
if (!aggregatedReaction) return null;
440441
aggregatedReaction.forEach((reaction) => {
441442
if (reaction.type !== 'm.reaction') return;
442-
addReaction(reaction.key, reaction.count, undefined, false);
443+
addReaction(reaction.key, undefined, reaction.count, undefined, false);
443444
});
444445
}
445446

@@ -449,13 +450,13 @@ function MessageReactionGroup({ roomTimeline, mEvent }) {
449450
Object.keys(reactions).map((key) => (
450451
<MessageReaction
451452
key={key}
452-
shortcodeToEmoji={shortcodeToEmoji}
453453
reaction={key}
454+
shortcode={reactions[key].shortcode}
454455
count={reactions[key].count}
455456
users={reactions[key].users}
456457
isActive={reactions[key].isActive}
457458
onClick={() => {
458-
toggleEmoji(roomId, mEvent.getId(), key, roomTimeline);
459+
toggleEmoji(roomId, mEvent.getId(), key, reactions[key].shortcode, roomTimeline);
459460
}}
460461
/>
461462
))

src/app/organisms/emoji-board/EmojiBoard.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const EmojiGroup = React.memo(({ name, groupEmojis }) => {
7070
unicode={`:${emoji.shortcode}:`}
7171
shortcodes={emoji.shortcode}
7272
src={initMatrix.matrixClient.mxcUrlToHttp(emoji.mxc)}
73-
data-mx-emoticon
73+
data-mx-emoticon={emoji.mxc}
7474
/>
7575
)
7676
}
@@ -141,10 +141,13 @@ function EmojiBoard({ onSelect, searchRef }) {
141141
function getEmojiDataFromTarget(target) {
142142
const unicode = target.getAttribute('unicode');
143143
const hexcode = target.getAttribute('hexcode');
144+
const mxc = target.getAttribute('data-mx-emoticon');
144145
let shortcodes = target.getAttribute('shortcodes');
145146
if (typeof shortcodes === 'undefined') shortcodes = undefined;
146147
else shortcodes = shortcodes.split(',');
147-
return { unicode, hexcode, shortcodes };
148+
return {
149+
unicode, hexcode, shortcodes, mxc,
150+
};
148151
}
149152

150153
function selectEmoji(e) {

src/client/action/roomTimeline.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ async function redactEvent(roomId, eventId, reason) {
1111
}
1212
}
1313

14-
async function sendReaction(roomId, toEventId, reaction) {
14+
async function sendReaction(roomId, toEventId, reaction, shortcode) {
1515
const mx = initMatrix.matrixClient;
16-
16+
const content = {
17+
'm.relates_to': {
18+
event_id: toEventId,
19+
key: reaction,
20+
rel_type: 'm.annotation',
21+
},
22+
};
23+
if (typeof shortcode === 'string') content.shortcode = shortcode;
1724
try {
18-
await mx.sendEvent(roomId, 'm.reaction', {
19-
'm.relates_to': {
20-
event_id: toEventId,
21-
key: reaction,
22-
rel_type: 'm.annotation',
23-
},
24-
});
25+
await mx.sendEvent(roomId, 'm.reaction', content);
2526
} catch (e) {
2627
throw new Error(e);
2728
}

0 commit comments

Comments
 (0)