updateMessageWithReactionInfo static method

Future<BaseMessage?> updateMessageWithReactionInfo(
  1. BaseMessage baseMessage,
  2. MessageReaction messageReaction,
  3. String action
)

Updates the baseMessage with the reaction information provided in messageReaction.

The action parameter specifies the type of reaction action, which can be either ReactionAction.reactionAdded or ReactionAction.reactionRemoved. Returns a Future that resolves to the updated baseMessage. Returns null if an error occurs during the process.

Implementation

static Future<BaseMessage?> updateMessageWithReactionInfo(BaseMessage baseMessage, MessageReaction messageReaction, String action) async {
  try {
    if (action == ReactionAction.reactionAdded) {
      if (baseMessage.id == messageReaction.messageId) {
        if (baseMessage.getReactions().length > 0) {
          int foundReactionIndex = -1;
          for (int j = 0; j < baseMessage.getReactions().length; j++) {
            ReactionCount reactionCount = baseMessage.getReactions()[j];
            if (reactionCount.reaction == messageReaction.reaction) {
              foundReactionIndex = j;
              break;
            }
          }
          if (foundReactionIndex != -1) {
            ReactionCount tempReactionCount = baseMessage.getReactions()[foundReactionIndex];
            baseMessage.getReactions()[foundReactionIndex].count = tempReactionCount.count! + 1;
          } else {
            ReactionCount tempReactionCount = ReactionCount();
            tempReactionCount.count = 1;
            tempReactionCount.reaction = messageReaction.reaction;
            baseMessage.getReactions().add(tempReactionCount);
          }
        } else {
          ReactionCount reactionCount = ReactionCount();
          reactionCount.count = 1;
          reactionCount.reaction = messageReaction.reaction;
          baseMessage.getReactions().add(reactionCount);
        }
        return baseMessage;
      }
    } else if (action == ReactionAction.reactionRemoved) {
      if (baseMessage.id == messageReaction.messageId) {
        if (baseMessage.getReactions().length > 0) {
          int foundReactionIndex = -1;
          for (int j = 0; j < baseMessage.getReactions().length; j++) {
            ReactionCount reactionCount = baseMessage.getReactions()[j];
            if (reactionCount.reaction == messageReaction.reaction) {
              foundReactionIndex = j;
              break;
            }
          }
          if (foundReactionIndex != -1) {
            ReactionCount tempReactionCount = baseMessage.getReactions()[foundReactionIndex];
            if (tempReactionCount.count! > 1) {
              baseMessage.getReactions()[foundReactionIndex].count = tempReactionCount.count! - 1;
              final loggedInUserObj = await CometChat.getLoggedInUser();
              if (loggedInUserObj?.uid == messageReaction.uid) {
                baseMessage.getReactions()[foundReactionIndex].reactedByMe = false;
              }
            } else {
              baseMessage.getReactions().removeAt(foundReactionIndex);
            }
          }
        }
        return baseMessage;
      }
    }
  } on PlatformException catch (platformException) {
    debugPrint("Error: $platformException");
  } catch (e) {
    debugPrint("Error: $e");
  }
  return null;
}