Reaction
Interactions are more fun when you can express yourself! Let users react using emojis, stickers, or thumbs up to messages.
To maximize engagement on the content of your application, you can use reactions on messages, posts, or comments. The reaction is similar to Facebook's Like. While a user can add many reactions to a model, it can add each reaction one time only.

Reaction Model

When you are observing reactions, it will always appear in this format:
1
{
2
"referenceId": "string",
3
"referenceType": "string",
4
"reactors": [
5
{
6
"reactionName": "string",
7
"userId": "string",
8
"reactionId": "string",
9
"createdAt": "2021-08-26T06:07:23.712Z"
10
}
11
]
12
}
Copied!

Reaction Description

Name
Data Type
Description
Attributes
referenceId
String
ID of a document
referenceType
String
Type of document
reactors
Array of Objects
List of mapping between reaction and reactor ID.
reactionName : ( 'like', 'love' , 'wow')
userID: ID of a creator
reactionId: ID of a reaction
createdAt: The date/time when a reaction is created

Reading the reactions

Each model which can be reacted upon will carry a set of properties useful to display its reactions. You will find:
    1.
    reactions: an object containing the name of the reactions as key, and their count as value (ex: { like: 1, love: 1 })
    2.
    reactionCount: the sum of all the counts for all the reactions
    3.
    myReactions: an array containing the current's users reactions
1
const liveObject = messageRepo.findById('exampleMessageId')
2
const messageModel = liveObject.model
3
4
console.log(messageModel.reactions) // { like: 1, love: 4, care: 3 }
5
console.log(messageModel.reactionCount) // 8
6
console.log(messageModel.myReactions) // ['love', 'care']
Copied!

Reacting on a model

First, simply import the ReactorRepository and pass in the message you want to react upon.
1
import { ReactorRepository } from '@amityco/js-sdk';
2
const reactorRepo = new ReactorRepository(messageModel);
Copied!
Then call for addReaction() and pass along the identifier for your reaction.
1
reactorRepo.addReaction('love')
Copied!

Removing a reaction

Just as you did for adding a reaction, you can call removeReaction() to remove a reaction.
1
reactorRepo.removeReaction('love')
Copied!

Server feedback

Both addReaction() and removeReaction() methods return a Promise resolving a boolean acknowledging the server's successful response. If necessary, you can use await to receive the result of those operations.
1
const didReact = await reactorRepo.addReaction('love')
2
console.log(didReact) // true
3
const didUnreact = reactorRepo.removeReaction('love')
4
console.log(didUnreact) // true
Copied!
Last modified 1mo ago