Skip to content

Commit

Permalink
feat(chat) add support for sending and receiving reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
he-patrick authored Aug 12, 2024
1 parent ae000f1 commit 03f7d88
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions JitsiConference.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,18 @@ JitsiConference.prototype.sendTextMessage = function(message, elementName = 'bod
}
};

/**
* Sends a reaction to the other participants in the conference
* @param reaction the reaction.
* @param messageId the ID of the message to attach the reaction to.
* @param receiverId the intended recipient, if the message is private.
*/
JitsiConference.prototype.sendReaction = function(reaction, messageId, receiverId) {
if (this.room) {
this.room.sendReaction(reaction, messageId, receiverId);
}
};

/**
* Send private text message to another participant of the conference
* @param id the id of the participant to send a private message.
Expand Down
10 changes: 10 additions & 0 deletions JitsiConferenceEventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ JitsiConferenceEventManager.prototype.setupChatRoomListeners = function() {
participantId, txt, ts, nick, isGuest, messageId);
});

chatRoom.addListener(
XMPPEvents.REACTION_RECEIVED,

(jid, reactionList, messageId) => {

conference.eventEmitter.emit(
JitsiConferenceEvents.REACTION_RECEIVED,
jid, reactionList, messageId);
});

chatRoom.addListener(
XMPPEvents.PRIVATE_MESSAGE_RECEIVED,

Expand Down
5 changes: 5 additions & 0 deletions JitsiConferenceEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ export enum JitsiConferenceEvents {
*/
MESSAGE_RECEIVED = 'conference.messageReceived',

/**
* New reaction was received.
*/
REACTION_RECEIVED = 'conference.reactionReceived',

/**
* Event fired when the conference metadata is updated.
*/
Expand Down
38 changes: 38 additions & 0 deletions modules/xmpp/ChatRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,26 @@ export default class ChatRoom extends Listenable {
this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, message);
}

/**
* Sends a reaction message to the other participants in the conference.
* @param {string} reaction - The reaction being sent.
* @param {string} messageId - The id of the message being sent.
* @param {string} receiverId - The receiver of the message if it is private.
*/
sendReaction(reaction, messageId, receiverId) {
// Adds the 'to' attribute depending on if the message is private or not.
const msg = receiverId ? $msg({ to: `${this.roomjid}/${receiverId}`,
type: 'chat' }) : $msg({ to: this.roomjid,
type: 'groupchat' });

msg.c('reactions', { id: messageId,
xmlns: 'urn:xmpp:reactions:0' })
.c('reaction', {}, reaction)
.up().c('store', { xmlns: 'urn:xmpp:hints' });

this.connection.send(msg);
}

/* eslint-disable max-params */
/**
* Send private text message to another participant of the conference
Expand Down Expand Up @@ -1144,6 +1164,24 @@ export default class ChatRoom extends Listenable {
return true;
}

const reactions = $(msg).find('>[xmlns="urn:xmpp:reactions:0"]>reaction');

if (reactions.length > 0) {
const messageId = $(msg).find('>[xmlns="urn:xmpp:reactions:0"]').attr('id');
const reactionList = [];

reactions.each((_, reactionElem) => {
const reaction = $(reactionElem).text();

reactionList.push(reaction);
});

this.eventEmitter.emit(XMPPEvents.REACTION_RECEIVED, from, reactionList, messageId);

return true;
}


const txt = $(msg).find('>body').text();
const subject = $(msg).find('>subject');

Expand Down
4 changes: 4 additions & 0 deletions service/xmpp/XMPPEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export enum XMPPEvents {
// received.
MESSAGE_RECEIVED = 'xmpp.message_received',

// Designates an event indicating that a reaction XMPP message in the MUC
// was received.
REACTION_RECEIVED = "xmpp.reaction_received",

// Designates an event indicating that an invite XMPP message in the MUC was
// received.
INVITE_MESSAGE_RECEIVED = 'xmpp.invite_message_received',
Expand Down

0 comments on commit 03f7d88

Please sign in to comment.