Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS bindings for chat reactions #892

Merged
merged 10 commits into from
Sep 14, 2020
1 change: 1 addition & 0 deletions bindings/Objective-C/MEGAChatMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef NS_ENUM(NSInteger, MEGAChatMessageEndCallReason) {
@property (readonly, nonatomic) NSInteger messageIndex;
@property (readonly, nonatomic) uint64_t userHandle;
@property (readonly, nonatomic) MEGAChatMessageType type;
@property (readonly, nonatomic) BOOL hasConfirmedReactions;
@property (readonly, nonatomic) NSDate *timestamp;
@property (readonly, nonatomic) NSString *content;
@property (readonly, nonatomic, getter=isEdited) BOOL edited;
Expand Down
4 changes: 4 additions & 0 deletions bindings/Objective-C/MEGAChatMessage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ - (MEGAChatMessageType)type {
return (MEGAChatMessageType) (self.megaChatMessage ? self.megaChatMessage->getType() : 0);
}

- (BOOL)hasConfirmedReactions {
return self.megaChatMessage ? self.megaChatMessage->hasConfirmedReactions() : NO;
}

- (NSDate *)timestamp {
return self.megaChatMessage ? [[NSDate alloc] initWithTimeIntervalSince1970:self.megaChatMessage->getTimestamp()] : nil;
}
Expand Down
1 change: 1 addition & 0 deletions bindings/Objective-C/MEGAChatRoomDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
- (void)onMessageReceived:(MEGAChatSdk *)api message:(MEGAChatMessage *)message;
- (void)onMessageUpdate:(MEGAChatSdk *)api message:(MEGAChatMessage *)message;
- (void)onHistoryReloaded:(MEGAChatSdk *)api chat:(MEGAChatRoom *)chat;
- (void)onReactionUpdate:(MEGAChatSdk *)api messageId:(uint64_t)messageId reaction:(NSString *)reaction count:(NSInteger)count;

@end
6 changes: 6 additions & 0 deletions bindings/Objective-C/MEGAChatSdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ typedef NS_ENUM (NSInteger, MEGAChatConnection) {
- (MEGAChatMessage *)lastChatMessageSeenForChat:(uint64_t)chatId;
- (void)removeUnsentMessageForChat:(uint64_t)chatId rowId:(uint64_t)rowId;

- (void)addReactionForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction;
- (void)deleteReactionForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction;
- (NSInteger)messageReactionCountForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction;
- (MEGAStringList *)messageReactionsForChat:(uint64_t)chatId messageId:(uint64_t)messageId;
- (MEGAHandleList *)reactionUsersForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction;

- (void)sendTypingNotificationForChat:(uint64_t)chatId;
- (void)sendStopTypingNotificationForChat:(uint64_t)chatId;
- (void)saveCurrentState;
Expand Down
20 changes: 20 additions & 0 deletions bindings/Objective-C/MEGAChatSdk.mm
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,26 @@ - (void)removeUnsentMessageForChat:(uint64_t)chatId rowId:(uint64_t)rowId {
self.megaChatApi->removeUnsentMessage(chatId, rowId);
}

- (void)addReactionForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction {
self.megaChatApi->addReaction(chatId, messageId, reaction ? [reaction UTF8String] : NULL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to replace
reaction ? [reaction UTF8String] : NULL
With
reaction.UTF8String
as performing any operation on nil would return nil. I think we don't have to check for the condition. Let me know your thoughts.

If it possible I think we can replace it everywhere on your PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is possible if reaction is nil it will pass NULL as parameter, so we can replace reaction ? [reaction UTF8String] : NULL with reaction.UTF8String

}

- (void)deleteReactionForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction {
self.megaChatApi->delReaction(chatId, messageId, reaction ? [reaction UTF8String] : NULL);
}

- (NSInteger)messageReactionCountForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction {
return self.megaChatApi->getMessageReactionCount(chatId, messageId, reaction.UTF8String);
}

- (MEGAStringList *)messageReactionsForChat:(uint64_t)chatId messageId:(uint64_t)messageId {
return self.megaChatApi ? [MEGAStringList.alloc initWithMegaStringList:self.megaChatApi->getMessageReactions(chatId, messageId) cMemoryOwn:YES] : nil;
}

- (MEGAHandleList *)reactionUsersForChat:(uint64_t)chatId messageId:(uint64_t)messageId reaction:(NSString *)reaction {
return self.megaChatApi ? [MEGAHandleList.alloc initWithMegaHandleList:self.megaChatApi->getReactionUsers(chatId, messageId, reaction.UTF8String) cMemoryOwn:YES] : nil;
}

- (void)sendTypingNotificationForChat:(uint64_t)chatId {
self.megaChatApi->sendTypingNotification(chatId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class DelegateMEGAChatRoomListener : public megachat::MegaChatRoomListener {
void onMessageReceived(megachat::MegaChatApi *api, megachat::MegaChatMessage *message);
void onMessageUpdate(megachat::MegaChatApi *api, megachat::MegaChatMessage *message);
void onHistoryReloaded(megachat::MegaChatApi *api, megachat::MegaChatRoom *chat);
void onReactionUpdate(megachat::MegaChatApi *api, megachat::MegaChatHandle msgid, const char *reaction, int count);

private:
__weak MEGAChatSdk *megaChatSDK;
Expand Down
11 changes: 11 additions & 0 deletions bindings/Objective-C/Private/DelegateMEGAChatRoomListener.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,14 @@
});
}
}

void DelegateMEGAChatRoomListener::onReactionUpdate(MegaChatApi *api, MegaChatHandle msgid, const char *reaction, int count) {
if (listener != nil && [listener respondsToSelector:@selector(onReactionUpdate:messageId:reaction:count:)]) {
MEGAChatSdk *tempMegaChatSDK = this->megaChatSDK;
id<MEGAChatRoomDelegate> tempListener = this->listener;
NSString *str = [NSString stringWithUTF8String:reaction];
dispatch_async(dispatch_get_main_queue(), ^{
[tempListener onReactionUpdate:tempMegaChatSDK messageId:msgid reaction:str count:count];
});
}
}