From 3cd16b357ffd43f0be8968339656f88dfa720bcc Mon Sep 17 00:00:00 2001 From: wicked-tc130 Date: Fri, 22 Mar 2019 13:09:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ios-=E6=96=B0=E5=A2=9E=E5=B7=B2=E8=AF=BB?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=9B=9E=E6=89=A7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/app/routes/Chat/index.js | 2 +- ios/RCTJMessageModule/JMessageHelper.h | 1 + ios/RCTJMessageModule/JMessageHelper.m | 923 ++--- ios/RCTJMessageModule/RCTJMessageModule.h | 1 + ios/RCTJMessageModule/RCTJMessageModule.m | 4330 +++++++++++---------- 5 files changed, 2659 insertions(+), 2598 deletions(-) diff --git a/example/app/routes/Chat/index.js b/example/app/routes/Chat/index.js index f84cd60..5968000 100755 --- a/example/app/routes/Chat/index.js +++ b/example/app/routes/Chat/index.js @@ -208,7 +208,7 @@ export default class Chat extends Component { id: message.id, } - JMessage.setMsgHaveRead(readParams,(successCallback) => {},(failCallback) => {}) + JMessage.setMsgHaveRead(readParams,(result) => {},(error) => {}) if (this.conversation.type === 'single') { if (message.target.type === 'user') { diff --git a/ios/RCTJMessageModule/JMessageHelper.h b/ios/RCTJMessageModule/JMessageHelper.h index 0f138bc..3960f62 100644 --- a/ios/RCTJMessageModule/JMessageHelper.h +++ b/ios/RCTJMessageModule/JMessageHelper.h @@ -10,6 +10,7 @@ #import #define kJJMessageReceiveMessage @"kJJMessageReceiveMessage" +#define kJJMessageReceiptMessage @"kJJMessageReceiptMessage" #define kJJMessageReceiveChatRoomMessage @"kJJMessageReceiveChatRoomMessage" #define kJJMessageSendMessageRespone @"kJJMessageSendMessageRespone" diff --git a/ios/RCTJMessageModule/JMessageHelper.m b/ios/RCTJMessageModule/JMessageHelper.m index 6c3027c..a247f6e 100644 --- a/ios/RCTJMessageModule/JMessageHelper.m +++ b/ios/RCTJMessageModule/JMessageHelper.m @@ -17,158 +17,160 @@ @interface JMessageHelper () @implementation JMessageHelper + (JMessageHelper *)shareInstance { - static JMessageHelper *instance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - instance = [[JMessageHelper alloc] init]; - }); - return instance; + static JMessageHelper *instance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + instance = [[JMessageHelper alloc] init]; + }); + return instance; } -(void)initJMessage:(NSDictionary*)options{ - //TODO: add init jmessage - // init third-party SDK - - NSString *appkey = @""; - NSString *channel = @""; - BOOL isProduction = true; - BOOL isOpenMessageRoaming = false; - - if (options[@"appkey"]) { - appkey = options[@"appkey"]; - self.JMessageAppKey = appkey; - } - - if (options[@"channel"]) { - channel = options[@"channel"]; - } - - if (options[@"isOpenMessageRoaming"]) { - NSNumber *isOpenMessageRoamingNum = options[@"isOpenMessageRoaming"]; - isOpenMessageRoaming = [isOpenMessageRoamingNum boolValue]; - } - - if (options[@"isProduction"]) { - NSNumber *isProductionNum = options[@"isProduction"]; - isProduction = [isProductionNum boolValue]; - } - - [JMessage addDelegate:self withConversation:nil]; - - [JMessage setupJMessage:_launchOptions - appKey:appkey - channel:@"" - apsForProduction:isProduction - category:nil - messageRoaming:isOpenMessageRoaming]; + //TODO: add init jmessage + // init third-party SDK + + NSString *appkey = @""; + NSString *channel = @""; + BOOL isProduction = true; + BOOL isOpenMessageRoaming = false; + + if (options[@"appkey"]) { + appkey = options[@"appkey"]; + self.JMessageAppKey = appkey; + } + + if (options[@"channel"]) { + channel = options[@"channel"]; + } + + if (options[@"isOpenMessageRoaming"]) { + NSNumber *isOpenMessageRoamingNum = options[@"isOpenMessageRoaming"]; + isOpenMessageRoaming = [isOpenMessageRoamingNum boolValue]; + } + + if (options[@"isProduction"]) { + NSNumber *isProductionNum = options[@"isProduction"]; + isProduction = [isProductionNum boolValue]; + } + + [JMessage addDelegate:self withConversation:nil]; + + [JMessage setupJMessage:_launchOptions + appKey:appkey + channel:@"" + apsForProduction:isProduction + category:nil + messageRoaming:isOpenMessageRoaming]; } - (void)onReceiveMessageRetractEvent:(JMSGMessageRetractEvent *)retractEvent { - NSDictionary *conversation = [retractEvent.conversation conversationToDictionary]; - NSDictionary *messageDic = [retractEvent.retractMessage messageToDictionary]; - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageRetractMessage - object:@{@"conversation":conversation, - @"retractedMessage":messageDic}]; + NSDictionary *conversation = [retractEvent.conversation conversationToDictionary]; + NSDictionary *messageDic = [retractEvent.retractMessage messageToDictionary]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageRetractMessage + object:@{@"conversation":conversation, + @"retractedMessage":messageDic}]; } - (void)onReceiveMessage:(JMSGMessage *)message error:(NSError *)error{ - NSMutableDictionary *dict = [NSMutableDictionary new]; - dict = [message messageToDictionary]; - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageReceiveMessage object:dict]; + NSMutableDictionary *dict = [NSMutableDictionary new]; + dict = [message messageToDictionary]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageReceiveMessage object:dict]; } -- (void)onReceiveChatRoomConversation:(JMSGConversation *)conversation messages:(NSArray<__kindof JMSGMessage *> *)messages { - NSArray *msgDicArr = [messages mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGMessage *msg = obj; - return [msg messageToDictionary]; - }]; - - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageReceiveChatRoomMessage object:msgDicArr]; + +- (void)onReceiveChatRoomConversation:(JMSGConversation *)conversation messages:(NSArray<__kindof JMSGMessage *> *)messages { + + NSArray *msgDicArr = [messages mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGMessage *msg = obj; + return [msg messageToDictionary]; + }]; + + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageReceiveChatRoomMessage object:msgDicArr]; } - (void)onReceiveNotificationEvent:(JMSGNotificationEvent *)event { - switch (event.eventType) { -// case kJMSGEventNotificationLoginKicked: -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageLoginStateChanged -// object:@{@"type":@"user_kicked"}]; -// break; -// case kJMSGEventNotificationServerAlterPassword: -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageLoginStateChanged -// object:@{@"type":@"user_password_change"}]; -// break; -// case kJMSGEventNotificationUserLoginStatusUnexpected: -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageLoginStateChanged -// object:@{@"type":@"user_login_state_unexpected"}]; -// break; - case kJMSGEventNotificationCurrentUserInfoChange: - break; -// case kJMSGEventNotificationReceiveFriendInvitation:{ -// JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; -// JMSGUser *user = [friendEvent getFromUser]; -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify -// object:@{ -// @"type":@"invite_received", -// @"reason":[friendEvent eventDescription], -// @"fromUsername":[friendEvent getFromUser].username, -// @"fromUserAppKey":user.appKey}]; -// } -// break; -// case kJMSGEventNotificationAcceptedFriendInvitation:{ -// JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; -// JMSGUser *user = [friendEvent getFromUser]; -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify -// object:@{ -// @"type":@"invite_accepted", -// @"reason":[friendEvent eventDescription], -// @"fromUsername":[friendEvent getFromUser].username, -// @"fromUserAppKey":user.appKey}]; -// } -// break; -// case kJMSGEventNotificationDeclinedFriendInvitation:{ -// JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; -// JMSGUser *user = [friendEvent getFromUser]; -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify -// object:@{ -// @"type":@"invite_declined", -// @"reason":[friendEvent eventDescription], -// @"fromUsername":[friendEvent getFromUser].username, -// @"fromUserAppKey":user.appKey}]; -// } -// break; -// case kJMSGEventNotificationDeletedFriend:{ -// JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; -// JMSGUser *user = [friendEvent getFromUser]; -// [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify -// object:@{ -// @"type":@"contact_deleted", -// @"reason":[friendEvent eventDescription], -// @"fromUsername":[friendEvent getFromUser].username, -// @"fromUserAppKey":user.appKey}]; -// } -// break; - case kJMSGEventNotificationReceiveServerFriendUpdate: - - break; - case kJMSGEventNotificationCreateGroup: - - break; - case kJMSGEventNotificationExitGroup: - - break; - case kJMSGEventNotificationAddGroupMembers: - - break; - case kJMSGEventNotificationRemoveGroupMembers: - - break; - case kJMSGEventNotificationUpdateGroupInfo: - - break; - default: - break; - } + switch (event.eventType) { + // case kJMSGEventNotificationLoginKicked: + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageLoginStateChanged + // object:@{@"type":@"user_kicked"}]; + // break; + // case kJMSGEventNotificationServerAlterPassword: + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageLoginStateChanged + // object:@{@"type":@"user_password_change"}]; + // break; + // case kJMSGEventNotificationUserLoginStatusUnexpected: + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageLoginStateChanged + // object:@{@"type":@"user_login_state_unexpected"}]; + // break; + case kJMSGEventNotificationCurrentUserInfoChange: + break; + // case kJMSGEventNotificationReceiveFriendInvitation:{ + // JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; + // JMSGUser *user = [friendEvent getFromUser]; + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify + // object:@{ + // @"type":@"invite_received", + // @"reason":[friendEvent eventDescription], + // @"fromUsername":[friendEvent getFromUser].username, + // @"fromUserAppKey":user.appKey}]; + // } + // break; + // case kJMSGEventNotificationAcceptedFriendInvitation:{ + // JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; + // JMSGUser *user = [friendEvent getFromUser]; + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify + // object:@{ + // @"type":@"invite_accepted", + // @"reason":[friendEvent eventDescription], + // @"fromUsername":[friendEvent getFromUser].username, + // @"fromUserAppKey":user.appKey}]; + // } + // break; + // case kJMSGEventNotificationDeclinedFriendInvitation:{ + // JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; + // JMSGUser *user = [friendEvent getFromUser]; + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify + // object:@{ + // @"type":@"invite_declined", + // @"reason":[friendEvent eventDescription], + // @"fromUsername":[friendEvent getFromUser].username, + // @"fromUserAppKey":user.appKey}]; + // } + // break; + // case kJMSGEventNotificationDeletedFriend:{ + // JMSGFriendNotificationEvent *friendEvent = (JMSGFriendNotificationEvent *) event; + // JMSGUser *user = [friendEvent getFromUser]; + // [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageContactNotify + // object:@{ + // @"type":@"contact_deleted", + // @"reason":[friendEvent eventDescription], + // @"fromUsername":[friendEvent getFromUser].username, + // @"fromUserAppKey":user.appKey}]; + // } + // break; + case kJMSGEventNotificationReceiveServerFriendUpdate: + + break; + case kJMSGEventNotificationCreateGroup: + + break; + case kJMSGEventNotificationExitGroup: + + break; + case kJMSGEventNotificationAddGroupMembers: + + break; + case kJMSGEventNotificationRemoveGroupMembers: + + break; + case kJMSGEventNotificationUpdateGroupInfo: + + break; + default: + break; + } } // 登录状态变更事件 @@ -187,7 +189,7 @@ - (void)onReceiveUserLoginStatusChangeEvent:(JMSGUserLoginStatusChangeEvent *)ev object:@{@"type":@"user_login_state_unexpected"}]; break; default: - break; + break; } } @@ -253,7 +255,22 @@ - (void)onReceiveFriendNotificationEvent:(JMSGFriendNotificationEvent *)event { * @discussion 上层可以通过 receiptEvent 获取相应信息 */ - (void)onReceiveMessageReceiptStatusChangeEvent:(JMSGMessageReceiptStatusChangeEvent *)receiptEvent { - + + NSArray <__kindof JMSGMessage *>*messages =receiptEvent.messages; + NSMutableArray *array = [[NSMutableArray alloc] init]; + NSUInteger index; + NSUInteger rangeCount = messages.count; + for (index = 1; index < rangeCount - 1; index++) { + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + long unreadCount = messages[index].getMessageUnreadCount; + [dict setObject:@(unreadCount) forKey:@"unReceiptCount"]; + NSString *serverMessageId = messages[index].serverMessageId; + [dict setObject:serverMessageId?:@"" forKey:@"serverMessageId"]; + NSNumber *time = messages[index].timestamp; + [dict setObject:time?:@(0) forKey:@"unReceiptMTime"]; + [array addObject:dict]; + } + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageReceiptMessage object:nil userInfo:@{@"receiptResult":array}]; } /*! @@ -309,51 +326,51 @@ - (void)onReceiveGroupAdminApprovalEvent:(JMSGGroupAdminApprovalEvent *)event { - (NSString *)getFullPathWith:(NSString *) path { - NSString * homeDir = NSHomeDirectory(); - return [NSString stringWithFormat:@"%@/Documents/%@", homeDir,path]; + NSString * homeDir = NSHomeDirectory(); + return [NSString stringWithFormat:@"%@/Documents/%@", homeDir,path]; } - (void)onSendMessageResponse:(JMSGMessage *)message error:(NSError *)error { - NSMutableDictionary *response = @{}.mutableCopy; - if (error) { - response[@"error"] = error; - } - response[@"message"] = [message messageToDictionary]; - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageSendMessageRespone object:response]; + NSMutableDictionary *response = @{}.mutableCopy; + if (error) { + response[@"error"] = error; + } + response[@"message"] = [message messageToDictionary]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageSendMessageRespone object:response]; } - (void)onReceiveMessageDownloadFailed:(JMSGMessage *)message{ - NSLog(@"onReceiveMessageDownloadFailed"); + NSLog(@"onReceiveMessageDownloadFailed"); } #pragma mark - Conversation 回调 - (void)onConversationChanged:(JMSGConversation *)conversation{ - NSMutableDictionary * dict = [NSMutableDictionary new]; - dict = [conversation conversationToDictionary]; - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageConversationChanged object:dict]; + NSMutableDictionary * dict = [NSMutableDictionary new]; + dict = [conversation conversationToDictionary]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageConversationChanged object:dict]; } - (void)onUnreadChanged:(NSUInteger)newCount{ - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageUnreadChanged - object:[NSNumber numberWithUnsignedInteger:newCount]]; + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageUnreadChanged + object:[NSNumber numberWithUnsignedInteger:newCount]]; } - (void)onSyncRoamingMessageConversation:(JMSGConversation *)conversation { - [[NSNotificationCenter defaultCenter] postNotificationName: kJJMessageSyncRoamingMessage - object: [conversation conversationToDictionary]]; + [[NSNotificationCenter defaultCenter] postNotificationName: kJJMessageSyncRoamingMessage + object: [conversation conversationToDictionary]]; } - (void)onSyncOfflineMessageConversation:(JMSGConversation *)conversation offlineMessages:(NSArray JMSG_GENERIC ( __kindof JMSGMessage *) *)offlineMessages { - NSMutableDictionary *callBackDic = @{}.mutableCopy; - callBackDic[@"conversation"] = [conversation conversationToDictionary]; - NSMutableArray *messageArr = @[].mutableCopy; - for (JMSGMessage *message in offlineMessages) { - [messageArr addObject: [message messageToDictionary]]; - } - callBackDic[@"messageArray"] = messageArr; - [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageSyncOfflineMessage object:callBackDic]; + NSMutableDictionary *callBackDic = @{}.mutableCopy; + callBackDic[@"conversation"] = [conversation conversationToDictionary]; + NSMutableArray *messageArr = @[].mutableCopy; + for (JMSGMessage *message in offlineMessages) { + [messageArr addObject: [message messageToDictionary]]; + } + callBackDic[@"messageArray"] = messageArr; + [[NSNotificationCenter defaultCenter] postNotificationName:kJJMessageSyncOfflineMessage object:callBackDic]; } @end @@ -365,52 +382,52 @@ - (void)onSyncOfflineMessageConversation:(JMSGConversation *)conversation @implementation NSDictionary (JPush) -(NSString*)toJsonString{ - NSError *error; - NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error]; - NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; - return jsonString; + NSError *error; + NSData *data = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error]; + NSString *jsonString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; + return jsonString; } @end @implementation NSString (JPush) -(NSMutableDictionary*)toDictionary{ - NSError *error; - NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding]; - NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; - return dict; + NSError *error; + NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; + return dict; } @end @implementation JMSGConversation (JMessage) -(NSMutableDictionary*)conversationToDictionary{ - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - - switch (self.conversationType) { - case kJMSGConversationTypeSingle:{ - JMSGUser *user = self.target; - dict[@"target"] = [user userToDictionary]; - dict[@"conversationType"] = @"single"; - break; - } - case kJMSGConversationTypeGroup:{ - JMSGGroup *group = self.target; - dict[@"target"] = [group groupToDictionary]; - dict[@"conversationType"] = @"group"; - break; - } - case kJMSGConversationTypeChatRoom:{ - JMSGChatRoom *chatRoom = self.target; - dict[@"target"] = [chatRoom chatRoomToDictionary]; - dict[@"conversationType"] = @"chatRoom"; - break; + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + + switch (self.conversationType) { + case kJMSGConversationTypeSingle:{ + JMSGUser *user = self.target; + dict[@"target"] = [user userToDictionary]; + dict[@"conversationType"] = @"single"; + break; + } + case kJMSGConversationTypeGroup:{ + JMSGGroup *group = self.target; + dict[@"target"] = [group groupToDictionary]; + dict[@"conversationType"] = @"group"; + break; + } + case kJMSGConversationTypeChatRoom:{ + JMSGChatRoom *chatRoom = self.target; + dict[@"target"] = [chatRoom chatRoomToDictionary]; + dict[@"conversationType"] = @"chatRoom"; + break; + } } - } - - dict[@"latestMessage"] = [self.latestMessage messageToDictionary]; - dict[@"unreadCount"] = self.unreadCount; - dict[@"title"] = [self title]; - dict[@"extras"] = [self getConversationExtras]; - return dict; + + dict[@"latestMessage"] = [self.latestMessage messageToDictionary]; + dict[@"unreadCount"] = self.unreadCount; + dict[@"title"] = [self title]; + dict[@"extras"] = [self getConversationExtras]; + return dict; } @@ -418,76 +435,76 @@ -(NSMutableDictionary*)conversationToDictionary{ @implementation JMSGUser (JMessage) -(NSMutableDictionary*)userToDictionary{ - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - dict[@"type"] = @"user"; - dict[@"username"] = self.username; - dict[@"nickname"] = self.nickname; - dict[@"birthday"] = self.birthday; - dict[@"region"] = self.region; - dict[@"signature"] = self.signature; - dict[@"address"] = [self address]; - dict[@"noteName"] = self.noteName; - dict[@"noteText"] = self.noteText; - dict[@"appKey"] = self.appKey; - dict[@"isNoDisturb"] = @(self.isNoDisturb); - dict[@"isInBlackList"] = @(self.isInBlacklist); - dict[@"isFriend"] = @(self.isFriend); - dict[@"extras"] = self.extras; - - if([[NSFileManager defaultManager] fileExistsAtPath: [self thumbAvatarLocalPath] ?: @""]){ - dict[@"avatarThumbPath"] = [self thumbAvatarLocalPath]; - } else { - dict[@"avatarThumbPath"] = @""; - } - - switch (self.gender) { - case kJMSGUserGenderUnknown: - dict[@"gender"] = @"unknown"; - break; - case kJMSGUserGenderFemale: - dict[@"gender"] = @"female"; - break; - case kJMSGUserGenderMale: - dict[@"gender"] = @"male"; - break; - default: - break; - } - return dict; + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + dict[@"type"] = @"user"; + dict[@"username"] = self.username; + dict[@"nickname"] = self.nickname; + dict[@"birthday"] = self.birthday; + dict[@"region"] = self.region; + dict[@"signature"] = self.signature; + dict[@"address"] = [self address]; + dict[@"noteName"] = self.noteName; + dict[@"noteText"] = self.noteText; + dict[@"appKey"] = self.appKey; + dict[@"isNoDisturb"] = @(self.isNoDisturb); + dict[@"isInBlackList"] = @(self.isInBlacklist); + dict[@"isFriend"] = @(self.isFriend); + dict[@"extras"] = self.extras; + + if([[NSFileManager defaultManager] fileExistsAtPath: [self thumbAvatarLocalPath] ?: @""]){ + dict[@"avatarThumbPath"] = [self thumbAvatarLocalPath]; + } else { + dict[@"avatarThumbPath"] = @""; + } + + switch (self.gender) { + case kJMSGUserGenderUnknown: + dict[@"gender"] = @"unknown"; + break; + case kJMSGUserGenderFemale: + dict[@"gender"] = @"female"; + break; + case kJMSGUserGenderMale: + dict[@"gender"] = @"male"; + break; + default: + break; + } + return dict; } - (NSString *)getLargeAvatarFilePath { - NSString *avatarPath = [self largeAvatarLocalPath]; - if([[NSFileManager defaultManager] fileExistsAtPath: avatarPath]){ - return avatarPath; - } else { - return @""; - } + NSString *avatarPath = [self largeAvatarLocalPath]; + if([[NSFileManager defaultManager] fileExistsAtPath: avatarPath]){ + return avatarPath; + } else { + return @""; + } } @end @implementation JMSGGroup (JMessage) -(NSMutableDictionary*)groupToDictionary{ - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - dict[@"type"] = @"group"; - dict[@"id"] = self.gid; - dict[@"name"] = self.name; - dict[@"desc"] = self.desc; - dict[@"level"] = self.level; - dict[@"flag"] = self.flag; - dict[@"owner"] = self.owner; - dict[@"ownerAppKey"] = self.ownerAppKey; - dict[@"maxMemberCount"] = self.maxMemberCount; - dict[@"isNoDisturb"] = @(self.isNoDisturb); - dict[@"isShieldMessage"] = @(self.isShieldMessage); - dict[@"displayName"] = self.displayName; - - if([[NSFileManager defaultManager] fileExistsAtPath: [self thumbAvatarLocalPath] ?: @""]){ - dict[@"avatarThumbPath"] = [self thumbAvatarLocalPath]; - } else { - dict[@"avatarThumbPath"] = @""; - } - + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + dict[@"type"] = @"group"; + dict[@"id"] = self.gid; + dict[@"name"] = self.name; + dict[@"desc"] = self.desc; + dict[@"level"] = self.level; + dict[@"flag"] = self.flag; + dict[@"owner"] = self.owner; + dict[@"ownerAppKey"] = self.ownerAppKey; + dict[@"maxMemberCount"] = self.maxMemberCount; + dict[@"isNoDisturb"] = @(self.isNoDisturb); + dict[@"isShieldMessage"] = @(self.isShieldMessage); + dict[@"displayName"] = self.displayName; + + if([[NSFileManager defaultManager] fileExistsAtPath: [self thumbAvatarLocalPath] ?: @""]){ + dict[@"avatarThumbPath"] = [self thumbAvatarLocalPath]; + } else { + dict[@"avatarThumbPath"] = @""; + } + switch (self.groupType) { case kJMSGGroupTypePublic: dict[@"groupType"] = @"public"; @@ -497,29 +514,29 @@ -(NSMutableDictionary*)groupToDictionary{ dict[@"groupType"] = @"private"; break; } - return dict; + return dict; } @end @implementation JMSGGroupMemberInfo (JMessage) - (NSMutableDictionary *)memberToDictionary { - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - dict[@"user"] = [self.user userToDictionary]; - dict[@"groupNickname"] = [self displayName] ? : @""; - - switch (self.memberType) { - case kJMSGGroupMemberTypeOrdinary: - dict[@"memberType"] = @"ordinary"; - break; - case kJMSGGroupMemberTypeOwner: - - break; - case kJMSGGroupMemberTypeAdmin: - dict[@"memberType"] = @"admin"; - break; - } - dict[@"joinGroupTime"] = @(self.ctime); - return dict; + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + dict[@"user"] = [self.user userToDictionary]; + dict[@"groupNickname"] = [self displayName] ? : @""; + + switch (self.memberType) { + case kJMSGGroupMemberTypeOrdinary: + dict[@"memberType"] = @"ordinary"; + break; + case kJMSGGroupMemberTypeOwner: + + break; + case kJMSGGroupMemberTypeAdmin: + dict[@"memberType"] = @"admin"; + break; + } + dict[@"joinGroupTime"] = @(self.ctime); + return dict; } @end @@ -546,210 +563,210 @@ -(NSMutableDictionary*)groupToDictionary { @implementation JMSGMessage (JMessage) - (NSMutableDictionary *)messageToDictionary { - NSMutableDictionary *dict = [NSMutableDictionary new]; - - dict[@"id"] = self.msgId; - dict[@"serverMessageId"] = self.serverMessageId; - dict[@"from"] = [self.fromUser userToDictionary]; - - if (self.content.extras != nil) { - dict[@"extras"] = self.content.extras; - } - - switch (self.targetType) { - case kJMSGConversationTypeSingle: { - JMSGUser *user = self.target; - dict[@"target"] = [user userToDictionary]; - break; - } - - case kJMSGConversationTypeGroup:{ - JMSGGroup *group = self.target; - dict[@"target"] = [group groupToDictionary]; - break; - } - case kJMSGConversationTypeChatRoom:{ - JMSGChatRoom *chatRoom= self.target; - dict[@"target"] = [chatRoom chatRoomToDictionary]; - break; - } - } - - dict[@"createTime"] = self.timestamp; - - switch (self.contentType) { - case kJMSGContentTypeUnknown: { - dict[@"type"] = @"unknown"; - break; - } - case kJMSGContentTypeText: { - dict[@"type"] = @"text"; - JMSGTextContent *textContent = (JMSGTextContent *) self.content; - dict[@"text"] = textContent.text; - break; - } - case kJMSGContentTypeImage: { - dict[@"type"] = @"image"; - JMSGImageContent *imageContent = (JMSGImageContent *) self.content; - dict[@"thumbPath"] = [imageContent thumbImageLocalPath]; - break; - } - case kJMSGContentTypeVoice: { - dict[@"type"] = @"voice"; - dict[@"path"] = [self getOriginMediaFilePath]; - JMSGVoiceContent *voiceContent = (JMSGVoiceContent *) self.content; - dict[@"duration"] = [voiceContent duration]; - break; - } - case kJMSGContentTypeCustom: { - dict[@"type"] = @"custom"; - JMSGCustomContent *customContent = (JMSGCustomContent *) self.content; - dict[@"customObject"] = customContent.customDictionary; - break; + NSMutableDictionary *dict = [NSMutableDictionary new]; + + dict[@"id"] = self.msgId; + dict[@"serverMessageId"] = self.serverMessageId; + dict[@"from"] = [self.fromUser userToDictionary]; + + if (self.content.extras != nil) { + dict[@"extras"] = self.content.extras; } - case kJMSGContentTypeEventNotification: { - dict[@"type"] = @"event"; - JMSGEventContent *eventContent = (JMSGEventContent *) self.content; - - switch (eventContent.eventType) { - case kJMSGEventNotificationAcceptedFriendInvitation: { - dict[@"eventType"] = @"acceptedFriendInvitation"; - break; - } - case kJMSGEventNotificationAddGroupMembers: { - dict[@"eventType"] = @"group_member_added"; - break; - } - case kJMSGEventNotificationCreateGroup: { - dict[@"eventType"] = @"createGroup"; - break; - } - case kJMSGEventNotificationCurrentUserInfoChange: { - dict[@"eventType"] = @"currentUserInfoChange"; - break; - } - case kJMSGEventNotificationDeclinedFriendInvitation: { - dict[@"eventType"] = @"declinedFriendInvitation"; - break; - } - case kJMSGEventNotificationDeletedFriend: { - dict[@"eventType"] = @"deletedFriend"; - break; + + switch (self.targetType) { + case kJMSGConversationTypeSingle: { + JMSGUser *user = self.target; + dict[@"target"] = [user userToDictionary]; + break; } - case kJMSGEventNotificationExitGroup: { - dict[@"eventType"] = @"group_member_exit"; - break; + + case kJMSGConversationTypeGroup:{ + JMSGGroup *group = self.target; + dict[@"target"] = [group groupToDictionary]; + break; } - case kJMSGEventNotificationLoginKicked: { - dict[@"eventType"] = @"loginKicked"; - break; + case kJMSGConversationTypeChatRoom:{ + JMSGChatRoom *chatRoom= self.target; + dict[@"target"] = [chatRoom chatRoomToDictionary]; + break; } - case kJMSGEventNotificationMessageRetract: { - dict[@"eventType"] = @"messageRetract"; - break; + } + + dict[@"createTime"] = self.timestamp; + + switch (self.contentType) { + case kJMSGContentTypeUnknown: { + dict[@"type"] = @"unknown"; + break; } - case kJMSGEventNotificationReceiveFriendInvitation: { - dict[@"eventType"] = @"receiveFriendInvitation"; - break; + case kJMSGContentTypeText: { + dict[@"type"] = @"text"; + JMSGTextContent *textContent = (JMSGTextContent *) self.content; + dict[@"text"] = textContent.text; + break; } - case kJMSGEventNotificationReceiveServerFriendUpdate: { - dict[@"eventType"] = @"receiveServerFriendUpdate"; - break; + case kJMSGContentTypeImage: { + dict[@"type"] = @"image"; + JMSGImageContent *imageContent = (JMSGImageContent *) self.content; + dict[@"thumbPath"] = [imageContent thumbImageLocalPath]; + break; } - case kJMSGEventNotificationRemoveGroupMembers: { - dict[@"eventType"] = @"group_member_removed"; - break; + case kJMSGContentTypeVoice: { + dict[@"type"] = @"voice"; + dict[@"path"] = [self getOriginMediaFilePath]; + JMSGVoiceContent *voiceContent = (JMSGVoiceContent *) self.content; + dict[@"duration"] = [voiceContent duration]; + break; } - case kJMSGEventNotificationServerAlterPassword: { - dict[@"eventType"] = @"serverAlterPassword"; - break; + case kJMSGContentTypeCustom: { + dict[@"type"] = @"custom"; + JMSGCustomContent *customContent = (JMSGCustomContent *) self.content; + dict[@"customObject"] = customContent.customDictionary; + break; } - case kJMSGEventNotificationUpdateGroupInfo: { - dict[@"eventType"] = @"group_info_updated"; - break; + case kJMSGContentTypeEventNotification: { + dict[@"type"] = @"event"; + JMSGEventContent *eventContent = (JMSGEventContent *) self.content; + + switch (eventContent.eventType) { + case kJMSGEventNotificationAcceptedFriendInvitation: { + dict[@"eventType"] = @"acceptedFriendInvitation"; + break; + } + case kJMSGEventNotificationAddGroupMembers: { + dict[@"eventType"] = @"group_member_added"; + break; + } + case kJMSGEventNotificationCreateGroup: { + dict[@"eventType"] = @"createGroup"; + break; + } + case kJMSGEventNotificationCurrentUserInfoChange: { + dict[@"eventType"] = @"currentUserInfoChange"; + break; + } + case kJMSGEventNotificationDeclinedFriendInvitation: { + dict[@"eventType"] = @"declinedFriendInvitation"; + break; + } + case kJMSGEventNotificationDeletedFriend: { + dict[@"eventType"] = @"deletedFriend"; + break; + } + case kJMSGEventNotificationExitGroup: { + dict[@"eventType"] = @"group_member_exit"; + break; + } + case kJMSGEventNotificationLoginKicked: { + dict[@"eventType"] = @"loginKicked"; + break; + } + case kJMSGEventNotificationMessageRetract: { + dict[@"eventType"] = @"messageRetract"; + break; + } + case kJMSGEventNotificationReceiveFriendInvitation: { + dict[@"eventType"] = @"receiveFriendInvitation"; + break; + } + case kJMSGEventNotificationReceiveServerFriendUpdate: { + dict[@"eventType"] = @"receiveServerFriendUpdate"; + break; + } + case kJMSGEventNotificationRemoveGroupMembers: { + dict[@"eventType"] = @"group_member_removed"; + break; + } + case kJMSGEventNotificationServerAlterPassword: { + dict[@"eventType"] = @"serverAlterPassword"; + break; + } + case kJMSGEventNotificationUpdateGroupInfo: { + dict[@"eventType"] = @"group_info_updated"; + break; + } + case kJMSGEventNotificationUserLoginStatusUnexpected: { + dict[@"eventType"] = @"userLoginStatusUnexpected"; + break; + } + case kJMSGEventNotificationGroupTypeChange: { + dict[@"eventType"] = @"group_type_changed"; + break; + } + case kJMSGEventNotificationDissolveGroup: { + dict[@"eventType"] = @"group_dissolved"; + break; + } + default: + break; + } + break; } - case kJMSGEventNotificationUserLoginStatusUnexpected: { - dict[@"eventType"] = @"userLoginStatusUnexpected"; - break; + case kJMSGContentTypeFile: { + dict[@"type"] = @"file"; + JMSGFileContent *fileContent = (JMSGFileContent *) self.content; + dict[@"fileName"] = [fileContent fileName]; + dict[@"path"] = [self getOriginMediaFilePath]; + break; } - case kJMSGEventNotificationGroupTypeChange: { - dict[@"eventType"] = @"group_type_changed"; - break; + case kJMSGContentTypeLocation: { + dict[@"type"] = @"location"; + JMSGLocationContent *locationContent = (JMSGLocationContent *) self.content; + dict[@"latitude"] = locationContent.latitude; + dict[@"longitude"] = locationContent.longitude; + dict[@"scale"] = locationContent.scale; + dict[@"address"] = locationContent.address; + break; } - case kJMSGEventNotificationDissolveGroup: { - dict[@"eventType"] = @"group_dissolved"; - break; + case kJMSGContentTypePrompt: { + dict[@"type"] = @"prompt"; + JMSGPromptContent *promptContent = (JMSGPromptContent *) self.content; + dict[@"promptText"] = promptContent.promptText; + break; } default: - break; - } - break; - } - case kJMSGContentTypeFile: { - dict[@"type"] = @"file"; - JMSGFileContent *fileContent = (JMSGFileContent *) self.content; - dict[@"fileName"] = [fileContent fileName]; - dict[@"path"] = [self getOriginMediaFilePath]; - break; - } - case kJMSGContentTypeLocation: { - dict[@"type"] = @"location"; - JMSGLocationContent *locationContent = (JMSGLocationContent *) self.content; - dict[@"latitude"] = locationContent.latitude; - dict[@"longitude"] = locationContent.longitude; - dict[@"scale"] = locationContent.scale; - dict[@"address"] = locationContent.address; - break; - } - case kJMSGContentTypePrompt: { - dict[@"type"] = @"prompt"; - JMSGPromptContent *promptContent = (JMSGPromptContent *) self.content; - dict[@"promptText"] = promptContent.promptText; - break; + break; } - default: - break; - } - return dict; + return dict; } - (NSString *)getOriginMediaFilePath { - JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *) self.content; - NSString *mediaPath = [content originMediaLocalPath]; - if([[NSFileManager defaultManager] fileExistsAtPath:mediaPath]){ - return mediaPath; - } else { - return @""; - } + JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *) self.content; + NSString *mediaPath = [content originMediaLocalPath]; + if([[NSFileManager defaultManager] fileExistsAtPath:mediaPath]){ + return mediaPath; + } else { + return @""; + } } - (NSString *)getFullPathWith:(NSString *) path { - NSString * homeDir = NSHomeDirectory(); - return [NSString stringWithFormat:@"%@/Documents/%@", homeDir,path]; + NSString * homeDir = NSHomeDirectory(); + return [NSString stringWithFormat:@"%@/Documents/%@", homeDir,path]; } @end @implementation NSError (JMessage) - (NSDictionary *)errorToDictionary { - return @{@"code": @(self.code), @"description": [self description]}; + return @{@"code": @(self.code), @"description": [self description]}; } @end @implementation JMSGChatRoom (JMessage) - (NSMutableDictionary *)chatRoomToDictionary { - NSMutableDictionary *dict = @{}.mutableCopy; - dict[@"type"] = @"chatRoom"; - dict[@"roomId"] = self.roomID; - dict[@"roomName"] = self.name; - dict[@"appKey"] = self.appkey; - dict[@"description"] = self.description; - dict[@"createTime"] = self.ctime; - dict[@"maxMemberCount"] = @([self.maxMemberCount integerValue]); - dict[@"memberCount"] = @(self.totalMemberCount); - - return dict; + NSMutableDictionary *dict = @{}.mutableCopy; + dict[@"type"] = @"chatRoom"; + dict[@"roomId"] = self.roomID; + dict[@"roomName"] = self.name; + dict[@"appKey"] = self.appkey; + dict[@"description"] = self.description; + dict[@"createTime"] = self.ctime; + dict[@"maxMemberCount"] = @([self.maxMemberCount integerValue]); + dict[@"memberCount"] = @(self.totalMemberCount); + + return dict; } @end @@ -757,11 +774,11 @@ - (NSMutableDictionary *)chatRoomToDictionary { @implementation NSArray (JMessage) - (NSArray *)mapObjectsUsingBlock:(id (^)(id obj, NSUInteger idx))block { - NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]]; - [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [result addObject:block(obj, idx)]; - }]; - return result; + NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]]; + [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + [result addObject:block(obj, idx)]; + }]; + return result; } @end diff --git a/ios/RCTJMessageModule/RCTJMessageModule.h b/ios/RCTJMessageModule/RCTJMessageModule.h index 38aebb4..39db6d9 100644 --- a/ios/RCTJMessageModule/RCTJMessageModule.h +++ b/ios/RCTJMessageModule/RCTJMessageModule.h @@ -18,6 +18,7 @@ #define receiveMsgEvent @"JMessage.ReceiveMsgEvent" // 接收到消息事件 +#define receiptMsgEvent @"JMessage.ReceiptMsgEvent" // 接收到已读消息回执事件 #define receiveChatRoomMsgEvent @"JMessage.ReceiveChatRoomMsgEvent" // 接收到消息事件 #define conversationChangeEvent @"JMessage.conversationChange" // 会话变更事件 #define loginStateChangedEvent @"JMessage.LoginStateChanged" // 登录状态变更通知 diff --git a/ios/RCTJMessageModule/RCTJMessageModule.m b/ios/RCTJMessageModule/RCTJMessageModule.m index 8055ed9..d5774a7 100644 --- a/ios/RCTJMessageModule/RCTJMessageModule.m +++ b/ios/RCTJMessageModule/RCTJMessageModule.m @@ -38,84 +38,89 @@ @implementation RCTJMessageModule @synthesize bridge = _bridge; + (id)allocWithZone:(NSZone *)zone { - static RCTJMessageModule *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [super allocWithZone:zone]; - }); - return sharedInstance; + static RCTJMessageModule *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [super allocWithZone:zone]; + }); + return sharedInstance; } - (id)init { - self = [super init]; - [self initNotifications]; - self.SendMsgCallbackDic = @{}.mutableCopy; - return self; + self = [super init]; + [self initNotifications]; + self.SendMsgCallbackDic = @{}.mutableCopy; + return self; } - (void)setBridge:(RCTBridge *)bridge { - _bridge = bridge; - JMessageHelper.shareInstance.launchOptions = _bridge.launchOptions; + _bridge = bridge; + JMessageHelper.shareInstance.launchOptions = _bridge.launchOptions; } -(void)initNotifications { - [[NSNotificationCenter defaultCenter] removeObserver:self]; - - NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; - [defaultCenter addObserver:self - selector:@selector(didReceiveJMessageMessage:) - name:kJJMessageReceiveMessage - object:nil]; - - [defaultCenter addObserver:self - selector:@selector(didReceiveJMessageChatRoomMessage:) - name:kJJMessageReceiveChatRoomMessage - object:nil]; - - [defaultCenter addObserver:self - selector:@selector(conversationChanged:) - name:kJJMessageConversationChanged - object:nil]; - - [defaultCenter addObserver:self - selector:@selector(didSendMessage:) - name:kJJMessageSendMessageRespone - object:nil]; - -// [defaultCenter addObserver:self -// selector:@selector(unreadChanged:) -// name:kJJMessageUnreadChanged -// object:nil]; - - [defaultCenter addObserver:self - selector:@selector(loginStateChanged:) - name:kJJMessageLoginStateChanged - object:nil]; - // have - [defaultCenter addObserver:self - selector:@selector(onContactNotify:) - name:kJJMessageContactNotify - object:nil]; - // have - [defaultCenter addObserver:self - selector:@selector(didReceiveRetractMessage:) - name:kJJMessageRetractMessage - object:nil]; - - // have - [defaultCenter addObserver:self - selector:@selector(onSyncOfflineMessage:) - name:kJJMessageSyncOfflineMessage - object:nil]; - // have - [defaultCenter addObserver:self - selector:@selector(onSyncRoamingMessage:) - name:kJJMessageSyncRoamingMessage - object:nil]; - - -// group event - + [[NSNotificationCenter defaultCenter] removeObserver:self]; + + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; + [defaultCenter addObserver:self + selector:@selector(didReceiveJMessageMessage:) + name:kJJMessageReceiveMessage + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(didReceiptJMessageMessage:) + name:kJJMessageReceiptMessage + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(didReceiveJMessageChatRoomMessage:) + name:kJJMessageReceiveChatRoomMessage + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(conversationChanged:) + name:kJJMessageConversationChanged + object:nil]; + + [defaultCenter addObserver:self + selector:@selector(didSendMessage:) + name:kJJMessageSendMessageRespone + object:nil]; + + // [defaultCenter addObserver:self + // selector:@selector(unreadChanged:) + // name:kJJMessageUnreadChanged + // object:nil]; + + [defaultCenter addObserver:self + selector:@selector(loginStateChanged:) + name:kJJMessageLoginStateChanged + object:nil]; + // have + [defaultCenter addObserver:self + selector:@selector(onContactNotify:) + name:kJJMessageContactNotify + object:nil]; + // have + [defaultCenter addObserver:self + selector:@selector(didReceiveRetractMessage:) + name:kJJMessageRetractMessage + object:nil]; + + // have + [defaultCenter addObserver:self + selector:@selector(onSyncOfflineMessage:) + name:kJJMessageSyncOfflineMessage + object:nil]; + // have + [defaultCenter addObserver:self + selector:@selector(onSyncRoamingMessage:) + name:kJJMessageSyncRoamingMessage + object:nil]; + + + // group event + [defaultCenter addObserver:self selector:@selector(groupInfoChanged:) name:kJJMessageGroupInfoChanged @@ -138,220 +143,220 @@ -(void)initNotifications { } - (void)getConversationWithDictionary:(NSDictionary *)param callback:(JMSGConversationCallback)callback { - if (param[@"type"] == nil) { - NSError *error = [NSError errorWithDomain:@"param error!" code: 1 userInfo: nil]; - callback(nil,error); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - JMSGConversationType conversationType = [self convertStringToConvsersationType:param[@"type"]]; - switch (conversationType) { - case kJMSGConversationTypeSingle:{ - [JMSGConversation createSingleConversationWithUsername:param[@"username"] - appKey:appKey - completionHandler:^(id resultObject, NSError *error) { - if (error) { - callback(nil, error); - return; - } - - JMSGConversation *conversation = resultObject; - callback(conversation,nil); - }]; - break; - } - case kJMSGConversationTypeGroup:{ - [JMSGConversation createGroupConversationWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - callback(nil, error); - return; - } - - JMSGConversation *conversation = resultObject; - callback(conversation,nil); - }]; - break; + if (param[@"type"] == nil) { + NSError *error = [NSError errorWithDomain:@"param error!" code: 1 userInfo: nil]; + callback(nil,error); + return; } - case kJMSGConversationTypeChatRoom:{ - [JMSGConversation createChatRoomConversationWithRoomId:param[@"roomId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - callback(nil, error); - return; + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + JMSGConversationType conversationType = [self convertStringToConvsersationType:param[@"type"]]; + switch (conversationType) { + case kJMSGConversationTypeSingle:{ + [JMSGConversation createSingleConversationWithUsername:param[@"username"] + appKey:appKey + completionHandler:^(id resultObject, NSError *error) { + if (error) { + callback(nil, error); + return; + } + + JMSGConversation *conversation = resultObject; + callback(conversation,nil); + }]; + break; + } + case kJMSGConversationTypeGroup:{ + [JMSGConversation createGroupConversationWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + callback(nil, error); + return; + } + + JMSGConversation *conversation = resultObject; + callback(conversation,nil); + }]; + break; + } + case kJMSGConversationTypeChatRoom:{ + [JMSGConversation createChatRoomConversationWithRoomId:param[@"roomId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + callback(nil, error); + return; + } + + JMSGConversation *conversation = resultObject; + callback(conversation,nil); + }]; + break; } - - JMSGConversation *conversation = resultObject; - callback(conversation,nil); - }]; - break; } - } } - (JMSGMessage *)createMessageWithDictionary:(NSDictionary *)param type:(JMSGContentType)type { - - if (param[@"type"] == nil) { - return nil; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = nil; - JMSGAbstractContent *content = nil; - switch (type) { - case kJMSGContentTypeText:{ - content = [[JMSGTextContent alloc] initWithText:param[@"text"]]; - break; - } - case kJMSGContentTypeImage:{ - NSString *mediaPath = param[@"path"]; - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ - mediaPath = mediaPath; - } else { + + if (param[@"type"] == nil) { return nil; - } - content = [[JMSGImageContent alloc] initWithImageData: [NSData dataWithContentsOfFile: mediaPath]]; - JMSGImageContent *imgContent = content; - imgContent.format = [mediaPath pathExtension]; - break; - } - case kJMSGContentTypeVoice:{ - NSString *mediaPath = param[@"path"]; - double duration = 0; - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ - mediaPath = mediaPath; - - NSError *error = nil; - AVAudioPlayer *avAudioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:mediaPath] error: &error]; - if (error) { - return nil; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + JMSGMessage *message = nil; + JMSGAbstractContent *content = nil; + switch (type) { + case kJMSGContentTypeText:{ + content = [[JMSGTextContent alloc] initWithText:param[@"text"]]; + break; } - - duration = avAudioPlayer.duration; - avAudioPlayer = nil; - - } else { - - return nil; - } - content = [[JMSGVoiceContent alloc] initWithVoiceData:[NSData dataWithContentsOfFile: mediaPath] voiceDuration:@(duration)]; - break; + case kJMSGContentTypeImage:{ + NSString *mediaPath = param[@"path"]; + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ + mediaPath = mediaPath; + } else { + return nil; + } + content = [[JMSGImageContent alloc] initWithImageData: [NSData dataWithContentsOfFile: mediaPath]]; + JMSGImageContent *imgContent = content; + imgContent.format = [mediaPath pathExtension]; + break; + } + case kJMSGContentTypeVoice:{ + NSString *mediaPath = param[@"path"]; + double duration = 0; + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ + mediaPath = mediaPath; + + NSError *error = nil; + AVAudioPlayer *avAudioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:mediaPath] error: &error]; + if (error) { + return nil; + } + + duration = avAudioPlayer.duration; + avAudioPlayer = nil; + + } else { + + return nil; + } + content = [[JMSGVoiceContent alloc] initWithVoiceData:[NSData dataWithContentsOfFile: mediaPath] voiceDuration:@(duration)]; + break; + } + case kJMSGContentTypeLocation:{ + content = [[JMSGLocationContent alloc] initWithLatitude:param[@"latitude"] longitude:param[@"longitude"] scale:param[@"scale"] address: param[@"address"]]; + break; + } + case kJMSGContentTypeFile:{ + NSString *mediaPath = param[@"path"]; + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ + mediaPath = mediaPath; + } else { + return nil; + } + + NSString *fileName = @""; + if (param[@"fileName"]) { + fileName = param[@"fileName"]; + } + + content = [[JMSGFileContent alloc] initWithFileData:[NSData dataWithContentsOfFile: mediaPath] fileName: fileName]; + JMSGFileContent *fileContent = content; + fileContent.format =[mediaPath pathExtension]; + break; + } + case kJMSGContentTypeCustom:{ + content = [[JMSGCustomContent alloc] initWithCustomDictionary: param[@"customObject"]]; + break; + } + + default: + return nil; } - case kJMSGContentTypeLocation:{ - content = [[JMSGLocationContent alloc] initWithLatitude:param[@"latitude"] longitude:param[@"longitude"] scale:param[@"scale"] address: param[@"address"]]; - break; + + JMSGConversationType targetType = [self convertStringToConvsersationType:param[@"type"]]; + + switch (targetType) { + case kJMSGConversationTypeSingle:{ + message = [JMSGMessage createSingleMessageWithContent:content username:param[@"username"]]; + break; + } + case kJMSGConversationTypeGroup:{ + message = [JMSGMessage createGroupMessageWithContent:content groupId:param[@"groupId"]]; + break; + } + + case kJMSGConversationTypeChatRoom:{ + message = [JMSGMessage createChatRoomMessageWithContent:content chatRoomId:param[@"roomId"]]; + break; + } } - case kJMSGContentTypeFile:{ - NSString *mediaPath = param[@"path"]; - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ - mediaPath = mediaPath; - } else { + + if (message) { + if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) { + NSDictionary *extras = param[@"extras"]; + for (NSString *key in extras.allKeys) { + [message.content addStringExtra:extras[key] forKey:key]; + } + } + return message; + } else { return nil; - } - - NSString *fileName = @""; - if (param[@"fileName"]) { - fileName = param[@"fileName"]; - } - - content = [[JMSGFileContent alloc] initWithFileData:[NSData dataWithContentsOfFile: mediaPath] fileName: fileName]; - JMSGFileContent *fileContent = content; - fileContent.format =[mediaPath pathExtension]; - break; - } - case kJMSGContentTypeCustom:{ - content = [[JMSGCustomContent alloc] initWithCustomDictionary: param[@"customObject"]]; - break; - } - - default: - return nil; - } - - JMSGConversationType targetType = [self convertStringToConvsersationType:param[@"type"]]; - - switch (targetType) { - case kJMSGConversationTypeSingle:{ - message = [JMSGMessage createSingleMessageWithContent:content username:param[@"username"]]; - break; - } - case kJMSGConversationTypeGroup:{ - message = [JMSGMessage createGroupMessageWithContent:content groupId:param[@"groupId"]]; - break; - } - - case kJMSGConversationTypeChatRoom:{ - message = [JMSGMessage createChatRoomMessageWithContent:content chatRoomId:param[@"roomId"]]; - break; - } - } - - if (message) { - if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) { - NSDictionary *extras = param[@"extras"]; - for (NSString *key in extras.allKeys) { - [message.content addStringExtra:extras[key] forKey:key]; - } - } - return message; - } else { - return nil; - } + } } - (JMSGContentType)convertStringToContentType:(NSString *)str { - if ([str isEqualToString:@"text"]) { - return kJMSGContentTypeText; - } - - if ([str isEqualToString:@"image"]) { - return kJMSGContentTypeImage; - } - - if ([str isEqualToString:@"voice"]) { - return kJMSGContentTypeVoice; - } - - if ([str isEqualToString:@"location"]) { - return kJMSGContentTypeLocation; - } - - if ([str isEqualToString:@"file"]) { - return kJMSGContentTypeFile; - } - - if ([str isEqualToString:@"custom"]) { - return kJMSGContentTypeCustom; - } - - return kJMSGContentTypeUnknown; + if ([str isEqualToString:@"text"]) { + return kJMSGContentTypeText; + } + + if ([str isEqualToString:@"image"]) { + return kJMSGContentTypeImage; + } + + if ([str isEqualToString:@"voice"]) { + return kJMSGContentTypeVoice; + } + + if ([str isEqualToString:@"location"]) { + return kJMSGContentTypeLocation; + } + + if ([str isEqualToString:@"file"]) { + return kJMSGContentTypeFile; + } + + if ([str isEqualToString:@"custom"]) { + return kJMSGContentTypeCustom; + } + + return kJMSGContentTypeUnknown; } - (JMSGConversationType)convertStringToConvsersationType:(NSString *)str { - if ([str isEqualToString:@"group"]) { - return kJMSGConversationTypeGroup; - } - - if ([str isEqualToString:@"chatRoom"]) { - return kJMSGConversationTypeChatRoom; - } - - return kJMSGConversationTypeSingle; + if ([str isEqualToString:@"group"]) { + return kJMSGConversationTypeGroup; + } + + if ([str isEqualToString:@"chatRoom"]) { + return kJMSGConversationTypeChatRoom; + } + + return kJMSGConversationTypeSingle; } - (JMSGGroupType)convertStringToGroupType:(NSString *)str { @@ -367,82 +372,86 @@ - (JMSGGroupType)convertStringToGroupType:(NSString *)str { } - (NSDictionary *)getParamError { - return @{@"code": @(1), @"description": @"param error!"}; + return @{@"code": @(1), @"description": @"param error!"}; } - (NSDictionary *)getErrorWithLog:(NSString *)log { - return @{@"code": @(1), @"description": log}; + return @{@"code": @(1), @"description": log}; } - (NSDictionary *)getMediafileError { - return @{@"code": @(2), @"description": @"media file not exit!"}; + return @{@"code": @(2), @"description": @"media file not exit!"}; } RCT_EXPORT_METHOD(setup:(NSDictionary *)param) { - [[JMessageHelper shareInstance] initJMessage:param]; + [[JMessageHelper shareInstance] initJMessage:param]; } RCT_EXPORT_METHOD(setDebugMode:(NSDictionary *)param) { - if (param[@"enable"]) { - [JMessage setDebugMode]; - } else { - [JMessage setLogOFF]; - } + if (param[@"enable"]) { + [JMessage setDebugMode]; + } else { + [JMessage setLogOFF]; + } } #pragma mark IM - Notifications - begin - (void)onSyncOfflineMessage: (NSNotification *) notification { - [self.bridge.eventDispatcher sendAppEventWithName:syncOfflineMessageEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:syncOfflineMessageEvent body:notification.object]; } - (void)onSyncRoamingMessage: (NSNotification *) notification { - [self.bridge.eventDispatcher sendAppEventWithName:syncRoamingMessageEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:syncRoamingMessageEvent body:notification.object]; } -(void)didSendMessage:(NSNotification *)notification { - NSDictionary *response = notification.object; - - if (!response[@"message"]) { - return; - } - + NSDictionary *response = notification.object; + + if (!response[@"message"]) { + return; + } + NSDictionary *msgDic = response[@"message"]; NSArray *callBacks = self.SendMsgCallbackDic[msgDic[@"id"]]; - if (response[@"error"] == nil) { - RCTResponseSenderBlock successCallback = callBacks[0]; - successCallback(@[msgDic]); - } else { - NSError *error = response[@"error"]; - RCTResponseSenderBlock failCallback = callBacks[1]; - failCallback(@[@{@"code": @(error.code), @"description": [error description]}]); - } - [self.SendMsgCallbackDic removeObjectForKey:msgDic[@"id"]]; + if (response[@"error"] == nil) { + RCTResponseSenderBlock successCallback = callBacks[0]; + successCallback(@[msgDic]); + } else { + NSError *error = response[@"error"]; + RCTResponseSenderBlock failCallback = callBacks[1]; + failCallback(@[@{@"code": @(error.code), @"description": [error description]}]); + } + [self.SendMsgCallbackDic removeObjectForKey:msgDic[@"id"]]; } - (void)loginStateChanged:(NSNotification *)notification{ - [self.bridge.eventDispatcher sendAppEventWithName:loginStateChangedEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:loginStateChangedEvent body:notification.object]; } - (void)onContactNotify:(NSNotification *)notification{ - [self.bridge.eventDispatcher sendAppEventWithName:contactNotifyEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:contactNotifyEvent body:notification.object]; } - (void)didReceiveRetractMessage:(NSNotification *)notification{ - [self.bridge.eventDispatcher sendAppEventWithName:messageRetractEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:messageRetractEvent body:notification.object]; } //didReceiveJMessageMessage change name - (void)didReceiveJMessageMessage:(NSNotification *)notification { - [self.bridge.eventDispatcher sendAppEventWithName:receiveMsgEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:receiveMsgEvent body:notification.object]; +} + +- (void)didReceiptJMessageMessage:(NSNotification *)notification { + [self.bridge.eventDispatcher sendAppEventWithName:receiptMsgEvent body:notification.object]; } - (void)didReceiveJMessageChatRoomMessage:(NSNotification *)notification { - [self.bridge.eventDispatcher sendAppEventWithName:receiveChatRoomMsgEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:receiveChatRoomMsgEvent body:notification.object]; } - (void)conversationChanged:(NSNotification *)notification { - [self.bridge.eventDispatcher sendAppEventWithName:conversationChangeEvent body:notification.object]; + [self.bridge.eventDispatcher sendAppEventWithName:conversationChangeEvent body:notification.object]; } @@ -474,1203 +483,592 @@ - (void)didReceiveGroupAdminApproval:(NSNotification *)notification { RCT_EXPORT_METHOD(userRegister:(NSDictionary *)user successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { -// NSDictionary * user = [command argumentAtIndex:0]; - NSLog(@"username %@",user); - - [JMSGUser registerWithUsername: user[@"username"] password: user[@"password"] completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[@{}]); - } else { - failCallback(@[[error errorToDictionary]]); - } - }]; -} - -RCT_EXPORT_METHOD(login:(NSDictionary *)user - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - [JMSGUser loginWithUsername:user[@"username"] password:user[@"password"] completionHandler:^(id resultObject, NSError *error) { - if (!error) { - JMSGUser *myInfo = [JMSGUser myInfo]; - [myInfo thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) { - successCallback(@[@{}]); - }]; - } else { - failCallback(@[[error errorToDictionary]]); - } - }]; -} - -RCT_EXPORT_METHOD(logout) { - [JMSGUser logout:^(id resultObject, NSError *error) {}]; -} - -RCT_EXPORT_METHOD(getMyInfo:(RCTResponseSenderBlock)successCallback) { - JMSGUser *myInfo = [JMSGUser myInfo]; - if (myInfo.username == nil) { - successCallback(@[@{}]); - } else { - successCallback(@[[myInfo userToDictionary]]); - } -} - -RCT_EXPORT_METHOD(getUserInfo:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - if (param[@"username"]) { - [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (!error) { - NSArray *users = resultObject; - JMSGUser *user = users[0]; - successCallback(@[[user userToDictionary]]); - } else { - failCallback(@[[error errorToDictionary]]); - } - }]; - } else { - failCallback(@[[self getParamError]]); + // NSDictionary * user = [command argumentAtIndex:0]; + NSLog(@"username %@",user); - } -} - -RCT_EXPORT_METHOD(updateMyPassword:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"oldPwd"] && param[@"newPwd"]) { - [JMSGUser updateMyPasswordWithNewPassword:param[@"newPwd"] oldPassword:param[@"oldPwd"] completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[@{}]); - } else { - failCallback(@[[error errorToDictionary]]); - } + [JMSGUser registerWithUsername: user[@"username"] password: user[@"password"] completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[@{}]); + } else { + failCallback(@[[error errorToDictionary]]); + } }]; - } else { - failCallback(@[[self getParamError]]); - } } - -RCT_EXPORT_METHOD(updateMyAvatar:(NSDictionary *)param +RCT_EXPORT_METHOD(login:(NSDictionary *)user successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - if (!param[@"imgPath"]) { - failCallback(@[[self getParamError]]); - } - - NSString *mediaPath = param[@"imgPath"]; - - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ - mediaPath = mediaPath; - NSData *img = [NSData dataWithContentsOfFile: mediaPath]; - - [JMSGUser updateMyAvatarWithData:img avatarFormat:[mediaPath pathExtension] completionHandler:^(id resultObject, NSError *error) { + [JMSGUser loginWithUsername:user[@"username"] password:user[@"password"] completionHandler:^(id resultObject, NSError *error) { if (!error) { - successCallback(@[]); + JMSGUser *myInfo = [JMSGUser myInfo]; + [myInfo thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) { + successCallback(@[@{}]); + }]; } else { - failCallback(@[[error errorToDictionary]]); + failCallback(@[[error errorToDictionary]]); } }]; - } else { - failCallback(@[[self getParamError]]); - } } -RCT_EXPORT_METHOD(updateMyInfo:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - JMSGUserInfo *info = [[JMSGUserInfo alloc] init]; - - if (param[@"nickname"]) { - info.nickname = param[@"nickname"]; - } - - if (param[@"birthday"]) { - NSNumber *birthday = param[@"birthday"]; - info.birthday = birthday; - } - - if (param[@"signature"]) { - info.signature = param[@"signature"]; - } - - if (param[@"extras"]) { - info.extras = param[@"extras"]; - } - - if (param[@"gender"]) { - if ([param[@"gender"] isEqualToString:@"male"]) { - info.gender = kJMSGUserGenderMale; - } - - if ([param[@"gender"] isEqualToString:@"female"]) { - info.gender = kJMSGUserGenderFemale; - } - - if ([param[@"gender"] isEqualToString:@"unknow"]) { - info.gender = kJMSGUserGenderUnknown; - } - } - - if (param[@"region"]) { - info.region = param[@"region"]; - } - - if (param[@"address"]) { - info.address = param[@"address"]; - } - - [JMSGUser updateMyInfoWithUserInfo:info completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[]); - } else { - failCallback(@[[error errorToDictionary]]); - } - }]; -} - -- (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { - JMSGCustomNotification *customNotification = [[JMSGCustomNotification alloc] init]; - JMSGOptionalContent *optionlContent = [[JMSGOptionalContent alloc] init]; - - if(dic[@"isShowNotification"]) { - NSNumber *isShowNotification = dic[@"isShowNotification"]; - optionlContent.noSaveNotification = ![isShowNotification boolValue]; - } - - if(dic[@"isRetainOffline"]) { - NSNumber *isRetainOffline = dic[@"isRetainOffline"]; - optionlContent.noSaveOffline = ![isRetainOffline boolValue]; - } - - if(dic[@"isCustomNotificationEnabled"]) { - NSNumber *isCustomNotificationEnabled = dic[@"isCustomNotificationEnabled"]; - customNotification.enabled= [isCustomNotificationEnabled boolValue]; - } - - if(dic[@"notificationTitle"]) { - customNotification.title = dic[@"notificationTitle"]; - } - - if(dic[@"notificationText"]) { - customNotification.alert = dic[@"notificationText"]; - } - - optionlContent.customNotification = customNotification; - - return optionlContent; +RCT_EXPORT_METHOD(logout) { + [JMSGUser logout:^(id resultObject, NSError *error) {}]; } - -RCT_EXPORT_METHOD(sendTextMessage:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeText]; - - if (!message) { - NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; - failCallback(@[[error errorToDictionary]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; +RCT_EXPORT_METHOD(getMyInfo:(RCTResponseSenderBlock)successCallback) { + JMSGUser *myInfo = [JMSGUser myInfo]; + if (myInfo.username == nil) { + successCallback(@[@{}]); } else { - [conversation sendMessage:message]; + successCallback(@[[myInfo userToDictionary]]); } - }]; - } -RCT_EXPORT_METHOD(sendImageMessage:(NSDictionary *)param +RCT_EXPORT_METHOD(getUserInfo:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeImage]; - - if (!message) { - NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; - failCallback(@[[error errorToDictionary]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - [conversation sendMessage:message]; + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - }]; -} - -RCT_EXPORT_METHOD(sendVoiceMessage:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeVoice]; - - if (!message) { - NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; - failCallback(@[[error errorToDictionary]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; + + if (param[@"username"]) { + [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (!error) { + NSArray *users = resultObject; + JMSGUser *user = users[0]; + successCallback(@[[user userToDictionary]]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; } else { - [conversation sendMessage:message]; + failCallback(@[[self getParamError]]); + } - }]; } -RCT_EXPORT_METHOD(sendCustomMessage:(NSDictionary *)param +RCT_EXPORT_METHOD(updateMyPassword:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeCustom]; - - if (!message) { - NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; - failCallback(@[[error errorToDictionary]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; + + if (param[@"oldPwd"] && param[@"newPwd"]) { + [JMSGUser updateMyPasswordWithNewPassword:param[@"newPwd"] oldPassword:param[@"oldPwd"] completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[@{}]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; } else { - [conversation sendMessage:message]; + failCallback(@[[self getParamError]]); } - }]; } -RCT_EXPORT_METHOD(sendLocationMessage:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeLocation]; - - if (!message) { - NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; - failCallback(@[[error errorToDictionary]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; - } else { - [conversation sendMessage:message]; - } - }]; -} -RCT_EXPORT_METHOD(sendFileMessage:(NSDictionary *)param +RCT_EXPORT_METHOD(updateMyAvatar:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } - - JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeFile]; - - if (!message) { - NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; - failCallback(@[[error errorToDictionary]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; + + if (!param[@"imgPath"]) { + failCallback(@[[self getParamError]]); + } + + NSString *mediaPath = param[@"imgPath"]; + + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ + mediaPath = mediaPath; + NSData *img = [NSData dataWithContentsOfFile: mediaPath]; + + [JMSGUser updateMyAvatarWithData:img avatarFormat:[mediaPath pathExtension] completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; } else { - [conversation sendMessage:message]; + failCallback(@[[self getParamError]]); } - }]; } -RCT_EXPORT_METHOD(getHistoryMessages:(NSDictionary *)param +RCT_EXPORT_METHOD(updateMyInfo:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"type"] == nil || - param[@"from"] == nil || - param[@"limit"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - BOOL isDescend = false; - if (param[@"isDescend"]) { - NSNumber *number = param[@"isDescend"]; - isDescend = [number boolValue]; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + + JMSGUserInfo *info = [[JMSGUserInfo alloc] init]; + + if (param[@"nickname"]) { + info.nickname = param[@"nickname"]; } - NSArray *messageArr = [conversation messageArrayFromNewestWithOffset:param[@"from"] limit:param[@"limit"]]; - NSArray *messageDicArr = [messageArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGMessage *message = obj; - return [message messageToDictionary]; - }]; + if (param[@"birthday"]) { + NSNumber *birthday = param[@"birthday"]; + info.birthday = birthday; + } - if (!messageArr) { - successCallback(@[@[]]); - return; + if (param[@"signature"]) { + info.signature = param[@"signature"]; } - if (isDescend) { - messageDicArr = [[messageDicArr reverseObjectEnumerator] allObjects]; + if (param[@"extras"]) { + info.extras = param[@"extras"]; } - successCallback(@[messageDicArr]); - }]; -} - -RCT_EXPORT_METHOD(sendInvitationRequest:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"username"] == nil || - param[@"reason"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGFriendManager sendInvitationRequestWithUsername:param[@"username"] - appKey:appKey - reason:param[@"reason"] - completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - successCallback(@[]); - }]; -} - -RCT_EXPORT_METHOD(acceptInvitation:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { -// NSDictionary * param = [command argumentAtIndex:0]; - if (param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGFriendManager acceptInvitationWithUsername:param[@"username"] - appKey:appKey - completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - successCallback(@[]); - }]; -} - -RCT_EXPORT_METHOD(declineInvitation:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"username"] == nil || - param[@"reason"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - [JMSGFriendManager rejectInvitationWithUsername:param[@"username"] - appKey:appKey - reason:param[@"reason"] - completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - successCallback(@[]); - }]; -} - -RCT_EXPORT_METHOD(removeFromFriendList:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGFriendManager removeFriendWithUsername:param[@"username"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - successCallback(@[]); - }]; -} - -RCT_EXPORT_METHOD(updateFriendNoteName:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"username"] == nil || - param[@"noteName"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - NSArray *userArr = resultObject; - if (userArr.count < 1) { - failCallback(@[[self getErrorWithLog:@"cann't find user by usernaem"]]); - } else { - JMSGUser *user = resultObject[0]; - [user updateNoteName:param[@"noteName"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + if (param[@"gender"]) { + if ([param[@"gender"] isEqualToString:@"male"]) { + info.gender = kJMSGUserGenderMale; } - successCallback(@[]); - }]; - } - }]; -} - -RCT_EXPORT_METHOD(updateFriendNoteText:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"username"] == nil || - param[@"noteText"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - NSArray *userArr = resultObject; - if (userArr.count < 1) { - failCallback(@[[self getErrorWithLog:@"cann't find user by usernaem"]]); - } else { - JMSGUser *user = resultObject[0]; - - [user updateNoteName:param[@"noteText"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + + if ([param[@"gender"] isEqualToString:@"female"]) { + info.gender = kJMSGUserGenderFemale; } - successCallback(@[]); - }]; + + if ([param[@"gender"] isEqualToString:@"unknow"]) { + info.gender = kJMSGUserGenderUnknown; + } + } + + if (param[@"region"]) { + info.region = param[@"region"]; + } + + if (param[@"address"]) { + info.address = param[@"address"]; } - }]; + + [JMSGUser updateMyInfoWithUserInfo:info completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; } -RCT_EXPORT_METHOD(getFriends:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - [JMSGFriendManager getFriendList:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; +- (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { + JMSGCustomNotification *customNotification = [[JMSGCustomNotification alloc] init]; + JMSGOptionalContent *optionlContent = [[JMSGOptionalContent alloc] init]; + + if(dic[@"isShowNotification"]) { + NSNumber *isShowNotification = dic[@"isShowNotification"]; + optionlContent.noSaveNotification = ![isShowNotification boolValue]; } - NSArray *userList = resultObject; - NSMutableArray *userDicList = @[].mutableCopy; - for (JMSGUser *user in userList) { - [userDicList addObject: [user userToDictionary]]; + if(dic[@"isRetainOffline"]) { + NSNumber *isRetainOffline = dic[@"isRetainOffline"]; + optionlContent.noSaveOffline = ![isRetainOffline boolValue]; } - successCallback(@[userDicList]); - }]; + + if(dic[@"isCustomNotificationEnabled"]) { + NSNumber *isCustomNotificationEnabled = dic[@"isCustomNotificationEnabled"]; + customNotification.enabled= [isCustomNotificationEnabled boolValue]; + } + + if(dic[@"notificationTitle"]) { + customNotification.title = dic[@"notificationTitle"]; + } + + if(dic[@"notificationText"]) { + customNotification.alert = dic[@"notificationText"]; + } + + if(dic[@"needReadReceipt"]) { + NSNumber *needRead = dic[@"needReadReceipt"]; + optionlContent.needReadReceipt = needRead.boolValue; + } + + optionlContent.customNotification = customNotification; + + return optionlContent; } -RCT_EXPORT_METHOD(createGroup:(NSDictionary *)param + +RCT_EXPORT_METHOD(sendTextMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - NSString *groupName = @""; - NSString *descript = @""; - - if (param[@"name"] != nil) { - groupName = param[@"name"]; - } - - if (param[@"desc"] != nil) { - descript = param[@"desc"]; - } - JMSGGroupType type = [self convertStringToGroupType:param[@"groupType"]] ; - JMSGGroupInfo *groupInfo = [[JMSGGroupInfo alloc] init]; - groupInfo.name = groupName; - groupInfo.groupType = type; - groupInfo.desc = descript; + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } - [JMSGGroup createGroupWithGroupInfo:groupInfo memberArray:nil completionHandler:^(id resultObject, NSError *error) { + JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeText]; + + if (!message) { + NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; + failCallback(@[[error errorToDictionary]]); + return; + } + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { if (error) { failCallback(@[[error errorToDictionary]]); - return ; + return; } - JMSGGroup *group = resultObject; - successCallback(@[group.gid]); + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } }]; -} - -RCT_EXPORT_METHOD(getGroupIds:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - [JMSGGroup myGroupArray:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - NSArray *groudIdList = resultObject; - successCallback(@[groudIdList]); - }]; } -RCT_EXPORT_METHOD(getGroupInfo:(NSDictionary *)param +RCT_EXPORT_METHOD(sendImageMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"id"] == nil) { - failCallback(@[[self getParamError]]); - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeImage]; + + if (!message) { + NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; + failCallback(@[[error errorToDictionary]]); + return; } - JMSGGroup *group = resultObject; - successCallback(@[[group groupToDictionary]]); - }]; + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } + }]; } -RCT_EXPORT_METHOD(updateGroupInfo:(NSDictionary *)param +RCT_EXPORT_METHOD(sendVoiceMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"id"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - if (param[@"newName"] == nil && param[@"newDesc"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - NSString *newName = group.displayName; - NSString *newDesc = group.desc; - if (param[@"newName"]) { - newName = param[@"newName"]; + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; } - if (param[@"newDesc"]) { - newDesc = param[@"newDesc"]; - } + JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeVoice]; - [JMSGGroup updateGroupInfoWithGroupId:group.gid name:newName desc:newDesc completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[]); - } else { + if (!message) { + NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; failCallback(@[[error errorToDictionary]]); - } + return; + } + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } }]; - }]; } -RCT_EXPORT_METHOD(addGroupMembers:(NSDictionary *)param +RCT_EXPORT_METHOD(sendCustomMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"id"] == nil || - param[@"usernameArray"] == nil) { - failCallback(@[[self getParamError]]); - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - [group addMembersWithUsernameArray:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { + + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeCustom]; + + if (!message) { + NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; failCallback(@[[error errorToDictionary]]); - return ; - } - - successCallback(@[]); + return; + } + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } }]; - }]; } -RCT_EXPORT_METHOD(removeGroupMembers:(NSDictionary *)param +RCT_EXPORT_METHOD(sendLocationMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - -// NSDictionary * param = [command argumentAtIndex:0]; - if (param[@"id"] == nil || - param[@"usernameArray"] == nil) { - failCallback(@[[self getParamError]]); - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - [group removeMembersWithUsernameArray:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeLocation]; + + if (!message) { + NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; failCallback(@[[error errorToDictionary]]); - return ; - } - successCallback(@[]); + return; + } + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } }]; - }]; } -RCT_EXPORT_METHOD(exitGroup:(NSDictionary *)param +RCT_EXPORT_METHOD(sendFileMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"id"] == nil) { - failCallback(@[[self getParamError]]); - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - [group exit:^(id resultObject, NSError *error) { - if (error) { + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + JMSGMessage *message = [self createMessageWithDictionary:param type:kJMSGContentTypeFile]; + + if (!message) { + NSError *error = [NSError errorWithDomain:@"cannot create message, check your params!" code:1 userInfo:nil]; failCallback(@[[error errorToDictionary]]); - return ; - } - successCallback(@[]); + return; + } + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } }]; - }]; } -RCT_EXPORT_METHOD(getGroupMembers:(NSDictionary *)param +RCT_EXPORT_METHOD(getHistoryMessages:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"id"] == nil) { - failCallback(@[[self getParamError]]); - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + + if (param[@"type"] == nil || + param[@"from"] == nil || + param[@"limit"] == nil) { + failCallback(@[[self getParamError]]); + return; } - JMSGGroup *group = resultObject; + BOOL isDescend = false; + if (param[@"isDescend"]) { + NSNumber *number = param[@"isDescend"]; + isDescend = [number boolValue]; + } - [group memberInfoList:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - NSArray *memberList = resultObject; - NSMutableArray *memberInfoList = @[].mutableCopy; - for (JMSGGroupMemberInfo *member in memberList) { - [memberInfoList addObject:[member memberToDictionary]]; - } - - successCallback(@[memberInfoList]); + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + NSArray *messageArr = [conversation messageArrayFromNewestWithOffset:param[@"from"] limit:param[@"limit"]]; + NSArray *messageDicArr = [messageArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGMessage *message = obj; + return [message messageToDictionary]; + }]; + + if (!messageArr) { + successCallback(@[@[]]); + return; + } + + if (isDescend) { + messageDicArr = [[messageDicArr reverseObjectEnumerator] allObjects]; + } + + successCallback(@[messageDicArr]); }]; - }]; } -RCT_EXPORT_METHOD(addUsersToBlacklist:(NSDictionary *)param +RCT_EXPORT_METHOD(sendInvitationRequest:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"usernameArray"] == nil) { - failCallback(@[[self getParamError]]); - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGUser addUsersToBlacklist:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[]); + if (param[@"username"] == nil || + param[@"reason"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - failCallback(@[[error errorToDictionary]]); + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - }]; + + [JMSGFriendManager sendInvitationRequestWithUsername:param[@"username"] + appKey:appKey + reason:param[@"reason"] + completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + successCallback(@[]); + }]; } -RCT_EXPORT_METHOD(removeUsersFromBlacklist:(NSDictionary *)param +RCT_EXPORT_METHOD(acceptInvitation:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"usernameArray"] == nil) { - failCallback(@[[self getParamError]]); - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - - [JMSGUser delUsersFromBlacklist:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[]); + // NSDictionary * param = [command argumentAtIndex:0]; + if (param[@"username"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - failCallback(@[[error errorToDictionary]]); + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - }]; + + [JMSGFriendManager acceptInvitationWithUsername:param[@"username"] + appKey:appKey + completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + successCallback(@[]); + }]; } -RCT_EXPORT_METHOD(getBlacklist:(RCTResponseSenderBlock)successCallback +RCT_EXPORT_METHOD(declineInvitation:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - [JMessage blackList:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + + if (param[@"username"] == nil || + param[@"reason"] == nil) { + failCallback(@[[self getParamError]]); + return; } - NSArray *userList = resultObject; - NSMutableArray *userDicList = @[].mutableCopy; - for (JMSGUser *user in userList) { - [userDicList addObject:[user userToDictionary]]; + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - successCallback(@[userDicList]); - }]; + [JMSGFriendManager rejectInvitationWithUsername:param[@"username"] + appKey:appKey + reason:param[@"reason"] + completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + successCallback(@[]); + }]; } -RCT_EXPORT_METHOD(setNoDisturb:(NSDictionary *)param +RCT_EXPORT_METHOD(removeFromFriendList:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - NSNumber *isNoDisturb; - if (param[@"type"] == nil || - param[@"isNoDisturb"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - isNoDisturb = param[@"isNoDisturb"]; - - if ([param[@"type"] isEqualToString:@"single"]) { if (param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - } else { - if ([param[@"type"] isEqualToString:@"group"]) { - if (param[@"groupId"] == nil) { failCallback(@[[self getParamError]]); return; - } - } else { - failCallback(@[[self getParamError]]); - return; } - } - - if ([param[@"type"] isEqualToString:@"single"]) { - NSString *appKey = nil; if (param[@"appKey"]) { - appKey = param[@"appKey"]; + appKey = param[@"appKey"]; } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - NSArray *userList = resultObject; - if (userList.count < 1) { - successCallback(@[]); - return; - } - - JMSGUser *user = userList[0]; - [user setIsNoDisturb:[isNoDisturb boolValue] handler:^(id resultObject, NSError *error) { + [JMSGFriendManager removeFriendWithUsername:param[@"username"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + failCallback(@[[error errorToDictionary]]); + return ; } successCallback(@[]); - }]; }]; - } - - if ([param[@"type"] isEqualToString:@"group"]) { - [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - [group setIsNoDisturb:[isNoDisturb boolValue] handler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - } - - successCallback(@[]); - }]; - }]; - } -} - -RCT_EXPORT_METHOD(getNoDisturbList:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - [JMessage noDisturbList:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - NSArray *disturberList = resultObject; - NSMutableArray *userDicList = @[].mutableCopy; - NSMutableArray *groupDicList = @[].mutableCopy; - for (id disturber in disturberList) { - if ([disturber isKindOfClass:[JMSGUser class]]) { - - [userDicList addObject:[disturber userToDictionary]]; - } - - if ([disturber isKindOfClass:[JMSGGroup class]]) { - [groupDicList addObject:[disturber groupToDictionary]]; - } - } - successCallback(@[@{@"userInfos": userDicList, @"groupInfos": groupDicList}]); - }]; -} - -RCT_EXPORT_METHOD(setNoDisturbGlobal:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"isNoDisturb"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - [JMessage setIsGlobalNoDisturb:[param[@"isNoDisturb"] boolValue] handler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - successCallback(@[]); - }]; -} - -RCT_EXPORT_METHOD(isNoDisturbGlobal:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - BOOL isNodisturb = [JMessage isSetGlobalNoDisturb]; - successCallback(@[@{@"isNoDisturb": @(isNodisturb)}]); -} - -RCT_EXPORT_METHOD(downloadThumbUserAvatar:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - NSArray *userList = resultObject; - if (userList.count < 1) { - successCallback(@[]); - return; - } - - JMSGUser *user = userList[0]; - [user thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - successCallback(@[@{@"username": user.username, - @"appKey": user.appKey, - @"filePath": [user thumbAvatarLocalPath] ?: @""}]); - }]; - }]; -} - -RCT_EXPORT_METHOD(downloadOriginalUserAvatar:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - NSArray *userList = resultObject; - if (userList.count < 1) { - successCallback(@[]); - return; - } - - JMSGUser *user = userList[0]; - [user largeAvatarData:^(NSData *data, NSString *objectId, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - successCallback(@[@{@"username": user.username, - @"appKey": user.appKey ?: @"", - @"filePath": [user largeAvatarLocalPath] ?: @""}]); - }]; - }]; - } -RCT_EXPORT_METHOD(downloadOriginalImage:(NSDictionary *)param +RCT_EXPORT_METHOD(updateFriendNoteName:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"messageId"] == nil || - param[@"type"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { - - } else { - if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { - } else { - failCallback(@[[self getParamError]]); - return; - } - } - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + if (param[@"username"] == nil || + param[@"noteName"] == nil) { + failCallback(@[[self getParamError]]); + return; } - JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; - if (message == nil) { - failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); - return; + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - if (message.contentType != kJMSGContentTypeImage) { - failCallback(@[[self getErrorWithLog:@"It is not voice message"]]); - return; - } else { - JMSGImageContent *content = (JMSGImageContent *) message.content; - [content largeImageDataWithProgress:^(float percent, NSString *msgId) { - - } completionHandler:^(NSData *data, NSString *objectId, NSError *error) { + [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { if (error) { - failCallback(@[[error errorToDictionary]]); - return; + failCallback(@[[error errorToDictionary]]); + return ; } - JMSGMediaAbstractContent *mediaContent = (JMSGMediaAbstractContent *) message.content; - successCallback(@[@{@"messageId": message.msgId, - @"filePath": [mediaContent originMediaLocalPath] ?: @""}]); - }]; - } - }]; + NSArray *userArr = resultObject; + if (userArr.count < 1) { + failCallback(@[[self getErrorWithLog:@"cann't find user by usernaem"]]); + } else { + JMSGUser *user = resultObject[0]; + [user updateNoteName:param[@"noteName"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + successCallback(@[]); + }]; + } + }]; } -RCT_EXPORT_METHOD(downloadThumbImage:(NSDictionary *)param +RCT_EXPORT_METHOD(updateFriendNoteText:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"messageId"] == nil || - param[@"type"] == nil) { + if (param[@"username"] == nil || + param[@"noteText"] == nil) { failCallback(@[[self getParamError]]); return; } @@ -1681,704 +1079,1320 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { } else { appKey = [JMessageHelper shareInstance].JMessageAppKey; } - - if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { - - } else { - if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { - } else { - failCallback(@[[self getParamError]]); - return; - } - } - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { if (error) { failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; - if (message == nil) { - failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); - return; + return ; } - if (message.contentType != kJMSGContentTypeImage) { - failCallback(@[[self getErrorWithLog:@"It is not voice message"]]); - return; + NSArray *userArr = resultObject; + if (userArr.count < 1) { + failCallback(@[[self getErrorWithLog:@"cann't find user by usernaem"]]); } else { - JMSGImageContent *content = (JMSGImageContent *) message.content; - [content thumbImageData:^(NSData *data, NSString *objectId, NSError *error) { + JMSGUser *user = resultObject[0]; + + [user updateNoteName:param[@"noteText"] completionHandler:^(id resultObject, NSError *error) { if (error) { failCallback(@[[error errorToDictionary]]); - return; + return ; } - - JMSGMediaAbstractContent *mediaContent = (JMSGMediaAbstractContent *) message.content; - successCallback(@[@{@"messageId": message.msgId, - @"filePath": [content thumbImageLocalPath] ?: @""}]); + successCallback(@[]); }]; } }]; } -RCT_EXPORT_METHOD(downloadVoiceFile:(NSDictionary *)param +RCT_EXPORT_METHOD(getFriends:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + [JMSGFriendManager getFriendList:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *userList = resultObject; + NSMutableArray *userDicList = @[].mutableCopy; + for (JMSGUser *user in userList) { + [userDicList addObject: [user userToDictionary]]; + } + successCallback(@[userDicList]); + }]; +} + +RCT_EXPORT_METHOD(createGroup:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"messageId"] == nil || - param[@"type"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { - - } else { - if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { - - } else { - failCallback(@[[self getParamError]]); - return; - } - } - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } + NSString *groupName = @""; + NSString *descript = @""; - JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; + if (param[@"name"] != nil) { + groupName = param[@"name"]; + } - if (message == nil) { - failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); - return; + if (param[@"desc"] != nil) { + descript = param[@"desc"]; } - if (message.contentType != kJMSGContentTypeVoice) { - failCallback(@[[self getErrorWithLog:@"It is not image message"]]); - return; - } else { - JMSGVoiceContent *content = (JMSGVoiceContent *) message.content; - [content voiceData:^(NSData *data, NSString *objectId, NSError *error) { + JMSGGroupType type = [self convertStringToGroupType:param[@"groupType"]] ; + JMSGGroupInfo *groupInfo = [[JMSGGroupInfo alloc] init]; + groupInfo.name = groupName; + groupInfo.groupType = type; + groupInfo.desc = descript; + + [JMSGGroup createGroupWithGroupInfo:groupInfo memberArray:nil completionHandler:^(id resultObject, NSError *error) { if (error) { - failCallback(@[[error errorToDictionary]]); - return; + failCallback(@[[error errorToDictionary]]); + return ; } - JMSGMediaAbstractContent *mediaContent = (JMSGMediaAbstractContent *) message.content; - successCallback(@[@{@"messageId": message.msgId, - @"filePath": [mediaContent originMediaLocalPath] ?: @""}]); - }]; - } - }]; + JMSGGroup *group = resultObject; + successCallback(@[group.gid]); + }]; } -RCT_EXPORT_METHOD(downloadFile:(NSDictionary *)param +RCT_EXPORT_METHOD(getGroupIds:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + [JMSGGroup myGroupArray:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *groudIdList = resultObject; + successCallback(@[groudIdList]); + }]; +} + +RCT_EXPORT_METHOD(getGroupInfo:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - if (param[@"messageId"] == nil || - param[@"type"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { - - } else { - if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { - - } else { - failCallback(@[[self getParamError]]); - return; - } - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; - - if (message == nil) { - failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); - return; + if (param[@"id"] == nil) { + failCallback(@[[self getParamError]]); } - if (message.contentType != kJMSGContentTypeFile) { - failCallback(@[[self getErrorWithLog:@"It is not file message"]]); - return; - } else { - JMSGFileContent *content = (JMSGFileContent *) message.content; - [content fileData:^(NSData *data, NSString *objectId, NSError *error) { + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { if (error) { - failCallback(@[[error errorToDictionary]]); - return; + failCallback(@[[error errorToDictionary]]); + return ; } - JMSGFileContent *fileContent = (JMSGFileContent *) message.content; - successCallback(@[@{@"messageId": message.msgId, - @"filePath":[fileContent originMediaLocalPath] ?: @"" }]); - }]; - } - }]; + + JMSGGroup *group = resultObject; + successCallback(@[[group groupToDictionary]]); + }]; } -RCT_EXPORT_METHOD(createConversation:(NSDictionary *)param +RCT_EXPORT_METHOD(updateGroupInfo:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + if (param[@"id"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + if (param[@"newName"] == nil && param[@"newDesc"] == nil) { + failCallback(@[[self getParamError]]); + return; } - successCallback(@[[conversation conversationToDictionary]]); - }]; + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + NSString *newName = group.displayName; + NSString *newDesc = group.desc; + + if (param[@"newName"]) { + newName = param[@"newName"]; + } + + if (param[@"newDesc"]) { + newDesc = param[@"newDesc"]; + } + + [JMSGGroup updateGroupInfoWithGroupId:group.gid name:newName desc:newDesc completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; + }]; } -RCT_EXPORT_METHOD(deleteConversation:(NSDictionary *)param +RCT_EXPORT_METHOD(addGroupMembers:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - if (param[@"type"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - if (([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) || - ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) || - ([param[@"type"] isEqual: @"chatRoom"] && param[@"roomId"] != nil)) { - - } else { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - JMSGConversationType type = [self convertStringToConvsersationType:param[@"type"]]; - switch (type) { - case kJMSGConversationTypeSingle: { - [JMSGConversation deleteSingleConversationWithUsername:param[@"username"] appKey:appKey]; - break; - } - case kJMSGConversationTypeGroup: { - [JMSGConversation deleteGroupConversationWithGroupId:param[@"groupId"]]; - break; - } - case kJMSGConversationTypeChatRoom: { - [JMSGConversation deleteChatRoomConversationWithRoomId:param[@"roomId"]]; - break; - } - } - - successCallback(@[]); + if (param[@"id"] == nil || + param[@"usernameArray"] == nil) { + failCallback(@[[self getParamError]]); + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + [group addMembersWithUsernameArray:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + successCallback(@[]); + }]; + }]; } -RCT_EXPORT_METHOD(getConversation:(NSDictionary *)param +RCT_EXPORT_METHOD(removeGroupMembers:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - successCallback(@[[conversation conversationToDictionary]]); - }]; -} - -RCT_EXPORT_METHOD(getConversations:(RCTResponseSenderBlock)successCallback - failCallback:(RCTResponseSenderBlock)failCallback) { - [JMSGConversation allConversations:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + + // NSDictionary * param = [command argumentAtIndex:0]; + if (param[@"id"] == nil || + param[@"usernameArray"] == nil) { + failCallback(@[[self getParamError]]); } - NSArray *conversationList = resultObject; - NSMutableArray *conversationDicList = @[].mutableCopy; - if (conversationList.count < 1) { - successCallback(@[@[]]); + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - for (JMSGConversation *conversation in conversationList) { - [conversationDicList addObject:[conversation conversationToDictionary]]; - } - successCallback(@[conversationDicList]); + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - }]; + + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + [group removeMembersWithUsernameArray:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + successCallback(@[]); + }]; + }]; } -RCT_EXPORT_METHOD(resetUnreadMessageCount:(NSDictionary *)param +RCT_EXPORT_METHOD(exitGroup:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + if (param[@"id"] == nil) { + failCallback(@[[self getParamError]]); } - [conversation clearUnreadCount]; - successCallback(@[]); - }]; + + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + [group exit:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + successCallback(@[]); + }]; + }]; } -RCT_EXPORT_METHOD(retractMessage:(NSDictionary *)param +RCT_EXPORT_METHOD(getGroupMembers:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallback:(RCTResponseSenderBlock)failCallback) { - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; - if (message == nil) { - failCallback(@[[self getErrorWithLog:@"cann't found this message"]]); - return; + if (param[@"id"] == nil) { + failCallback(@[[self getParamError]]); } - [conversation retractMessage:message completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - successCallback(@[]); - }]; - }]; -} - -RCT_EXPORT_METHOD(createSendMessage:(NSDictionary *)param - callback:(RCTResponseSenderBlock)callback) { + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + + [group memberInfoList:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *memberList = resultObject; + NSMutableArray *memberInfoList = @[].mutableCopy; + for (JMSGGroupMemberInfo *member in memberList) { + [memberInfoList addObject:[member memberToDictionary]]; + } + + successCallback(@[memberInfoList]); + }]; + }]; +} - if (!param[@"type"]) { - callback(@[]); - return; - } - - NSString *mediaPath = @""; - if ([param[@"messageType"] isEqualToString:@"image"] || - [param[@"messageType"] isEqualToString:@"voice"] || - [param[@"messageType"] isEqualToString:@"file"]) { - mediaPath = param[@"path"]; - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ - mediaPath = mediaPath; +RCT_EXPORT_METHOD(addUsersToBlacklist:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"usernameArray"] == nil) { + failCallback(@[[self getParamError]]); + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - callback(@[[self getMediafileError]]);//TODO: fix - } - } - - JMSGAbstractContent *content = nil; - if ([param[@"messageType"] isEqualToString:@"text"]) { - content = [[JMSGTextContent alloc] initWithText:param[@"text"]]; - } - - if ([param[@"messageType"] isEqualToString:@"image"]) { - JMSGImageContent *imgContent = [[JMSGImageContent alloc] initWithImageData: [NSData dataWithContentsOfFile: mediaPath]]; - imgContent.format = [mediaPath pathExtension]; - content = imgContent; - } - - if ([param[@"messageType"] isEqualToString:@"voice"]) { - double duration = 0; - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]) { - mediaPath = mediaPath; - - NSError *error = nil; - AVAudioPlayer *avAudioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:mediaPath] error: &error]; - if (error) { - callback(@[[self getMediafileError]]); + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGUser addUsersToBlacklist:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; +} + +RCT_EXPORT_METHOD(removeUsersFromBlacklist:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"usernameArray"] == nil) { + failCallback(@[[self getParamError]]); + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + + [JMSGUser delUsersFromBlacklist:param[@"usernameArray"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; +} + +RCT_EXPORT_METHOD(getBlacklist:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + [JMessage blackList:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *userList = resultObject; + NSMutableArray *userDicList = @[].mutableCopy; + for (JMSGUser *user in userList) { + [userDicList addObject:[user userToDictionary]]; + } + successCallback(@[userDicList]); + }]; +} + +RCT_EXPORT_METHOD(setNoDisturb:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + NSNumber *isNoDisturb; + if (param[@"type"] == nil || + param[@"isNoDisturb"] == nil) { + failCallback(@[[self getParamError]]); return; - } - - duration = avAudioPlayer.duration; - avAudioPlayer = nil; - + } + isNoDisturb = param[@"isNoDisturb"]; + + if ([param[@"type"] isEqualToString:@"single"]) { + if (param[@"username"] == nil) { + failCallback(@[[self getParamError]]); + return; + } } else { - callback(@[[self getMediafileError]]); - return; + if ([param[@"type"] isEqualToString:@"group"]) { + if (param[@"groupId"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + } else { + failCallback(@[[self getParamError]]); + return; + } + } - content = [[JMSGVoiceContent alloc] initWithVoiceData:[NSData dataWithContentsOfFile: mediaPath] voiceDuration:@(duration)]; + if ([param[@"type"] isEqualToString:@"single"]) { + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *userList = resultObject; + if (userList.count < 1) { + successCallback(@[]); + return; + } + + JMSGUser *user = userList[0]; + [user setIsNoDisturb:[isNoDisturb boolValue] handler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + successCallback(@[]); + }]; + }]; + } + + if ([param[@"type"] isEqualToString:@"group"]) { + [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + [group setIsNoDisturb:[isNoDisturb boolValue] handler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + } + + successCallback(@[]); + }]; + }]; + } +} + +RCT_EXPORT_METHOD(getNoDisturbList:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + [JMessage noDisturbList:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + NSArray *disturberList = resultObject; + NSMutableArray *userDicList = @[].mutableCopy; + NSMutableArray *groupDicList = @[].mutableCopy; + for (id disturber in disturberList) { + if ([disturber isKindOfClass:[JMSGUser class]]) { + + [userDicList addObject:[disturber userToDictionary]]; + } + + if ([disturber isKindOfClass:[JMSGGroup class]]) { + [groupDicList addObject:[disturber groupToDictionary]]; + } + } + successCallback(@[@{@"userInfos": userDicList, @"groupInfos": groupDicList}]); + }]; +} + +RCT_EXPORT_METHOD(setNoDisturbGlobal:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + + if (param[@"isNoDisturb"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + [JMessage setIsGlobalNoDisturb:[param[@"isNoDisturb"] boolValue] handler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + successCallback(@[]); + }]; +} + +RCT_EXPORT_METHOD(isNoDisturbGlobal:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + BOOL isNodisturb = [JMessage isSetGlobalNoDisturb]; + successCallback(@[@{@"isNoDisturb": @(isNodisturb)}]); +} + +RCT_EXPORT_METHOD(downloadThumbUserAvatar:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"username"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *userList = resultObject; + if (userList.count < 1) { + successCallback(@[]); + return; + } + + JMSGUser *user = userList[0]; + [user thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + successCallback(@[@{@"username": user.username, + @"appKey": user.appKey, + @"filePath": [user thumbAvatarLocalPath] ?: @""}]); + }]; + }]; +} + +RCT_EXPORT_METHOD(downloadOriginalUserAvatar:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"username"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGUser userInfoArrayWithUsernameArray:@[param[@"username"]] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *userList = resultObject; + if (userList.count < 1) { + successCallback(@[]); + return; + } + + JMSGUser *user = userList[0]; + [user largeAvatarData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + successCallback(@[@{@"username": user.username, + @"appKey": user.appKey ?: @"", + @"filePath": [user largeAvatarLocalPath] ?: @""}]); + }]; + }]; + +} + +RCT_EXPORT_METHOD(downloadOriginalImage:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"messageId"] == nil || + param[@"type"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { + + } else { + if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { + } else { + failCallback(@[[self getParamError]]); + return; + } + } + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; + if (message == nil) { + failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); + return; + } + + if (message.contentType != kJMSGContentTypeImage) { + failCallback(@[[self getErrorWithLog:@"It is not voice message"]]); + return; + } else { + JMSGImageContent *content = (JMSGImageContent *) message.content; + [content largeImageDataWithProgress:^(float percent, NSString *msgId) { + + } completionHandler:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMediaAbstractContent *mediaContent = (JMSGMediaAbstractContent *) message.content; + successCallback(@[@{@"messageId": message.msgId, + @"filePath": [mediaContent originMediaLocalPath] ?: @""}]); + }]; + } + }]; +} + +RCT_EXPORT_METHOD(downloadThumbImage:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"messageId"] == nil || + param[@"type"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { + + } else { + if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { + } else { + failCallback(@[[self getParamError]]); + return; + } + } + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; + if (message == nil) { + failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); + return; + } + + if (message.contentType != kJMSGContentTypeImage) { + failCallback(@[[self getErrorWithLog:@"It is not voice message"]]); + return; + } else { + JMSGImageContent *content = (JMSGImageContent *) message.content; + [content thumbImageData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMediaAbstractContent *mediaContent = (JMSGMediaAbstractContent *) message.content; + successCallback(@[@{@"messageId": message.msgId, + @"filePath": [content thumbImageLocalPath] ?: @""}]); + }]; + } + }]; +} + +RCT_EXPORT_METHOD(downloadVoiceFile:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"messageId"] == nil || + param[@"type"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { + + } else { + if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { + + } else { + failCallback(@[[self getParamError]]); + return; + } + } + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; + + if (message == nil) { + failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); + return; + } + + if (message.contentType != kJMSGContentTypeVoice) { + failCallback(@[[self getErrorWithLog:@"It is not image message"]]); + return; + } else { + JMSGVoiceContent *content = (JMSGVoiceContent *) message.content; + [content voiceData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMediaAbstractContent *mediaContent = (JMSGMediaAbstractContent *) message.content; + successCallback(@[@{@"messageId": message.msgId, + @"filePath": [mediaContent originMediaLocalPath] ?: @""}]); + }]; + } + }]; +} + +RCT_EXPORT_METHOD(downloadFile:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + if (param[@"messageId"] == nil || + param[@"type"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + if ([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) { + + } else { + if ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) { + + } else { + failCallback(@[[self getParamError]]); + return; + } + } + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; + + if (message == nil) { + failCallback(@[[self getErrorWithLog:@"cann't find this message"]]); + return; + } + + if (message.contentType != kJMSGContentTypeFile) { + failCallback(@[[self getErrorWithLog:@"It is not file message"]]); + return; + } else { + JMSGFileContent *content = (JMSGFileContent *) message.content; + [content fileData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + JMSGFileContent *fileContent = (JMSGFileContent *) message.content; + successCallback(@[@{@"messageId": message.msgId, + @"filePath":[fileContent originMediaLocalPath] ?: @"" }]); + }]; + } + }]; +} + +RCT_EXPORT_METHOD(createConversation:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + successCallback(@[[conversation conversationToDictionary]]); + }]; +} + +RCT_EXPORT_METHOD(deleteConversation:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + + if (param[@"type"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + if (([param[@"type"] isEqual: @"single"] && param[@"username"] != nil) || + ([param[@"type"] isEqual: @"group"] && param[@"groupId"] != nil) || + ([param[@"type"] isEqual: @"chatRoom"] && param[@"roomId"] != nil)) { + + } else { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + JMSGConversationType type = [self convertStringToConvsersationType:param[@"type"]]; + switch (type) { + case kJMSGConversationTypeSingle: { + [JMSGConversation deleteSingleConversationWithUsername:param[@"username"] appKey:appKey]; + break; + } + case kJMSGConversationTypeGroup: { + [JMSGConversation deleteGroupConversationWithGroupId:param[@"groupId"]]; + break; + } + case kJMSGConversationTypeChatRoom: { + [JMSGConversation deleteChatRoomConversationWithRoomId:param[@"roomId"]]; + break; + } + } + + successCallback(@[]); +} + +RCT_EXPORT_METHOD(getConversation:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { - - } - - if ([param[@"messageType"] isEqualToString:@"location"]) { + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + successCallback(@[[conversation conversationToDictionary]]); + }]; +} +RCT_EXPORT_METHOD(getConversations:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + [JMSGConversation allConversations:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + NSArray *conversationList = resultObject; + NSMutableArray *conversationDicList = @[].mutableCopy; + + if (conversationList.count < 1) { + successCallback(@[@[]]); + } else { + for (JMSGConversation *conversation in conversationList) { + [conversationDicList addObject:[conversation conversationToDictionary]]; + } + successCallback(@[conversationDicList]); + } + }]; +} + +RCT_EXPORT_METHOD(resetUnreadMessageCount:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + [conversation clearUnreadCount]; + successCallback(@[]); + }]; +} + +RCT_EXPORT_METHOD(retractMessage:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallback:(RCTResponseSenderBlock)failCallback) { - content = [[JMSGLocationContent alloc] initWithLatitude:param[@"latitude"] - longitude:param[@"longitude"] - scale:param[@"scale"] - address:param[@"address"]]; - - - } - - if ([param[@"messageType"] isEqualToString:@"file"]) { + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId:param[@"messageId"]]; + if (message == nil) { + failCallback(@[[self getErrorWithLog:@"cann't found this message"]]); + return; + } + + [conversation retractMessage:message completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + successCallback(@[]); + }]; + }]; +} - content = [[JMSGFileContent alloc] initWithFileData:[NSData dataWithContentsOfFile: mediaPath] - fileName: param[@"fileName"]]; - ((JMSGFileContent *)content).format = [mediaPath pathExtension]; +RCT_EXPORT_METHOD(createSendMessage:(NSDictionary *)param + callback:(RCTResponseSenderBlock)callback) { - } - - if ([param[@"messageType"] isEqualToString:@"custom"]) { - content = [[JMSGCustomContent alloc] initWithCustomDictionary: param[@"customObject"]]; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - callback(@[[error errorToDictionary]]); - return; + if (!param[@"type"]) { + callback(@[]); + return; } - JMSGMessage *message = [conversation createMessageWithContent:content]; - if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) { - NSDictionary *extras = param[@"extras"]; - for (NSString *key in extras.allKeys) { - [message.content addStringExtra:extras[key] forKey:key]; - } - } - callback(@[[message messageToDictionary]]); - }]; -} - -RCT_EXPORT_METHOD(sendMessage:(NSDictionary *)param - successCallback:(RCTResponseSenderBlock)successCallback - failCallBack:(RCTResponseSenderBlock)failCallback) { - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + NSString *mediaPath = @""; + if ([param[@"messageType"] isEqualToString:@"image"] || + [param[@"messageType"] isEqualToString:@"voice"] || + [param[@"messageType"] isEqualToString:@"file"]) { + mediaPath = param[@"path"]; + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ + mediaPath = mediaPath; + } else { + callback(@[[self getMediafileError]]);//TODO: fix + } } - JMSGMessage *message = [conversation messageWithMessageId: param[@"id"]]; + JMSGAbstractContent *content = nil; + if ([param[@"messageType"] isEqualToString:@"text"]) { + content = [[JMSGTextContent alloc] initWithText:param[@"text"]]; + } - if (!message) { - failCallback(@[[self getErrorWithLog:@"cann't find the message from this id"]]); - return; + if ([param[@"messageType"] isEqualToString:@"image"]) { + JMSGImageContent *imgContent = [[JMSGImageContent alloc] initWithImageData: [NSData dataWithContentsOfFile: mediaPath]]; + imgContent.format = [mediaPath pathExtension]; + content = imgContent; } - if ([message.content isKindOfClass:[JMSGMediaAbstractContent class]]) { - JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *)message.content; - content.uploadHandler = ^(float percent, NSString *msgID) { + if ([param[@"messageType"] isEqualToString:@"voice"]) { + double duration = 0; + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]) { + mediaPath = mediaPath; + + NSError *error = nil; + AVAudioPlayer *avAudioPlayer = [[AVAudioPlayer alloc] initWithData:[NSData dataWithContentsOfFile:mediaPath] error: &error]; + if (error) { + callback(@[[self getMediafileError]]); + return; + } + + duration = avAudioPlayer.duration; + avAudioPlayer = nil; + + } else { + callback(@[[self getMediafileError]]); + return; + } + + content = [[JMSGVoiceContent alloc] initWithVoiceData:[NSData dataWithContentsOfFile: mediaPath] voiceDuration:@(duration)]; + - [self.bridge.eventDispatcher sendAppEventWithName:uploadProgressEvent body:@{@"messageId": msgID, - @"progress": @(percent)}]; - }; } - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + if ([param[@"messageType"] isEqualToString:@"location"]) { + + + content = [[JMSGLocationContent alloc] initWithLatitude:param[@"latitude"] + longitude:param[@"longitude"] + scale:param[@"scale"] + address:param[@"address"]]; + + } - self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + if ([param[@"messageType"] isEqualToString:@"file"]) { + + content = [[JMSGFileContent alloc] initWithFileData:[NSData dataWithContentsOfFile: mediaPath] + fileName: param[@"fileName"]]; + ((JMSGFileContent *)content).format = [mediaPath pathExtension]; + + } - if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) { - NSDictionary *extras = param[@"extras"]; - for (NSString *key in extras.allKeys) { - [message.content addStringExtra:extras[key] forKey:key]; - } + if ([param[@"messageType"] isEqualToString:@"custom"]) { + content = [[JMSGCustomContent alloc] initWithCustomDictionary: param[@"customObject"]]; } - if (messageSendingOptions) { - [conversation sendMessage:message optionalContent:messageSendingOptions]; + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - [conversation sendMessage:message]; + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - }]; + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + callback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation createMessageWithContent:content]; + if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) { + NSDictionary *extras = param[@"extras"]; + for (NSString *key in extras.allKeys) { + [message.content addStringExtra:extras[key] forKey:key]; + } + } + callback(@[[message messageToDictionary]]); + }]; } -RCT_EXPORT_METHOD(forwardMessage:(NSDictionary *)param +RCT_EXPORT_METHOD(sendMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGMessage *message = [conversation messageWithMessageId: param[@"id"]]; - if ([message.content isKindOfClass:[JMSGMediaAbstractContent class]]) { - JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *)message.content; - content.uploadHandler = ^(float percent, NSString *msgID) { - - [self.bridge.eventDispatcher sendAppEventWithName:uploadProgressEvent body:@{@"messageId": msgID, - @"progress": @(percent)}]; - }; + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - JMSGOptionalContent *messageSendingOptions = nil; - if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { - messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; - } + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId: param[@"id"]]; + + if (!message) { + failCallback(@[[self getErrorWithLog:@"cann't find the message from this id"]]); + return; + } + + if ([message.content isKindOfClass:[JMSGMediaAbstractContent class]]) { + JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *)message.content; + content.uploadHandler = ^(float percent, NSString *msgID) { + + [self.bridge.eventDispatcher sendAppEventWithName:uploadProgressEvent body:@{@"messageId": msgID, + @"progress": @(percent)}]; + }; + } + + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + self.SendMsgCallbackDic[message.msgId] = @[successCallback,failCallback]; + + if (param[@"extras"] && [param[@"extras"] isKindOfClass: [NSDictionary class]]) { + NSDictionary *extras = param[@"extras"]; + for (NSString *key in extras.allKeys) { + [message.content addStringExtra:extras[key] forKey:key]; + } + } + + if (messageSendingOptions) { + [conversation sendMessage:message optionalContent:messageSendingOptions]; + } else { + [conversation sendMessage:message]; + } + }]; +} + +RCT_EXPORT_METHOD(forwardMessage:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallBack:(RCTResponseSenderBlock)failCallback) { - NSDictionary *target = nil; - if (param[@"target"]) { - target = param[@""]; + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; } else { - failCallback(@[[self getParamError]]); - return; - } - - if ([target[@"type"] isEqualToString:@"chatRoom"]) { - failCallback(@[[self getErrorWithLog:@"cann't forward message to chat room"]]); - return; + appKey = [JMessageHelper shareInstance].JMessageAppKey; } - if ([target[@"type"] isEqualToString:@"group"]) { - [JMSGGroup groupInfoWithGroupId:target[@"id"] completionHandler:^(id resultObject, NSError *error) { + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + failCallback(@[[error errorToDictionary]]); + return; } - JMSGGroup *group = resultObject; - [JMSGMessage forwardMessage:message target:group optionalContent:messageSendingOptions]; - }]; - } else { - NSString *targetAppkey = nil; - if (target[@"appKey"]) { - targetAppkey = target[@"appKey"]; - } - [JMSGUser userInfoArrayWithUsernameArray:@[target[@"user"]] appKey:targetAppkey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; + + JMSGMessage *message = [conversation messageWithMessageId: param[@"id"]]; + + if ([message.content isKindOfClass:[JMSGMediaAbstractContent class]]) { + JMSGMediaAbstractContent *content = (JMSGMediaAbstractContent *)message.content; + content.uploadHandler = ^(float percent, NSString *msgID) { + + [self.bridge.eventDispatcher sendAppEventWithName:uploadProgressEvent body:@{@"messageId": msgID, + @"progress": @(percent)}]; + }; } - - NSArray *userArr = resultObject; - if (userArr.count < 1) { - failCallback(@[[self getErrorWithLog:@"cann't find user by usernaem"]]); + + JMSGOptionalContent *messageSendingOptions = nil; + if (param[@"messageSendingOptions"] && [param[@"messageSendingOptions"] isKindOfClass: [NSDictionary class]]) { + messageSendingOptions = [self convertDicToJMSGOptionalContent:param[@"messageSendingOptions"]]; + } + + NSDictionary *target = nil; + if (param[@"target"]) { + target = param[@""]; + } else { + failCallback(@[[self getParamError]]); + return; + } + + if ([target[@"type"] isEqualToString:@"chatRoom"]) { + failCallback(@[[self getErrorWithLog:@"cann't forward message to chat room"]]); + return; + } + + if ([target[@"type"] isEqualToString:@"group"]) { + [JMSGGroup groupInfoWithGroupId:target[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + JMSGGroup *group = resultObject; + [JMSGMessage forwardMessage:message target:group optionalContent:messageSendingOptions]; + }]; } else { - JMSGUser *user = resultObject[0]; - [JMSGMessage forwardMessage:message target:user optionalContent:messageSendingOptions]; + NSString *targetAppkey = nil; + if (target[@"appKey"]) { + targetAppkey = target[@"appKey"]; + } + [JMSGUser userInfoArrayWithUsernameArray:@[target[@"user"]] appKey:targetAppkey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + NSArray *userArr = resultObject; + if (userArr.count < 1) { + failCallback(@[[self getErrorWithLog:@"cann't find user by usernaem"]]); + } else { + JMSGUser *user = resultObject[0]; + [JMSGMessage forwardMessage:message target:user optionalContent:messageSendingOptions]; + } + }]; } - }]; - } - }]; - + }]; + } RCT_EXPORT_METHOD(blockGroupMessage:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (!param[@"id"] || !param[@"isBlock"]) { - failCallback(@[[self getParamError]]); - return; - } - - NSNumber *isBlock = param[@"isBlock"]; - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGGroup *group = resultObject; - [group setIsShield:[isBlock boolValue] handler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - } else { - successCallback(@[]); - } + + if (!param[@"id"] || !param[@"isBlock"]) { + failCallback(@[[self getParamError]]); + return; + } + + NSNumber *isBlock = param[@"isBlock"]; + + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGGroup *group = resultObject; + [group setIsShield:[isBlock boolValue] handler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + } else { + successCallback(@[]); + } + }]; }]; - }]; } RCT_EXPORT_METHOD(isGroupBlocked:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (!param[@"id"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + + if (!param[@"id"]) { + failCallback(@[[self getParamError]]); + return; } - JMSGGroup *group = resultObject; - successCallback(@[@{@"isBlocked": @(group.isShieldMessage)}]); - }]; + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGGroup *group = resultObject; + successCallback(@[@{@"isBlocked": @(group.isShieldMessage)}]); + }]; } RCT_EXPORT_METHOD(getBlockedGroupList:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - [JMSGGroup shieldList:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - } - - NSArray *groupArr = resultObject; - NSMutableArray *groupList = @[].mutableCopy; - - for (JMSGGroup *group in groupArr) { - [groupList addObject:[group groupToDictionary]]; - } - - successCallback(@[groupList]); - }]; + [JMSGGroup shieldList:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + } + + NSArray *groupArr = resultObject; + NSMutableArray *groupList = @[].mutableCopy; + + for (JMSGGroup *group in groupArr) { + [groupList addObject:[group groupToDictionary]]; + } + + successCallback(@[groupList]); + }]; } RCT_EXPORT_METHOD(updateGroupAvatar:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (!param[@"id"]) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *mediaPath = param[@"imgPath"]; - - if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ - mediaPath = mediaPath; - NSData *img = [NSData dataWithContentsOfFile: mediaPath]; - - [JMSGGroup updateGroupAvatarWithGroupId:param[@"id"] avatarData:img avatarFormat:[mediaPath pathExtension] completionHandler:^(id resultObject, NSError *error) { - if (!error) { - successCallback(@[]); - } else { - failCallback(@[[error errorToDictionary]]); - } - }]; - } else { - failCallback(@[[self getParamError]]); - } + + if (!param[@"id"]) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *mediaPath = param[@"imgPath"]; + + if([[NSFileManager defaultManager] fileExistsAtPath: mediaPath]){ + mediaPath = mediaPath; + NSData *img = [NSData dataWithContentsOfFile: mediaPath]; + + [JMSGGroup updateGroupAvatarWithGroupId:param[@"id"] avatarData:img avatarFormat:[mediaPath pathExtension] completionHandler:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; + } else { + failCallback(@[[self getParamError]]); + } } RCT_EXPORT_METHOD(downloadThumbGroupAvatar:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (!param[@"id"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - [group thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - successCallback(@[@{@"id": objectId, @"filePath": group.thumbAvatarLocalPath ?: @""}]); + + if (!param[@"id"]) { + failCallback(@[[self getParamError]]); + return; + } + + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + [group thumbAvatarData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + successCallback(@[@{@"id": objectId, @"filePath": group.thumbAvatarLocalPath ?: @""}]); + }]; }]; - }]; } RCT_EXPORT_METHOD(downloadOriginalGroupAvatar:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - if (!param[@"id"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - JMSGGroup *group = resultObject; - [group largeAvatarData:^(NSData *data, NSString *objectId, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return ; - } - - successCallback(@[@{@"id": objectId, @"filePath": group.largeAvatarLocalPath ?: @""}]); + if (!param[@"id"]) { + failCallback(@[[self getParamError]]); + return; + } + + [JMSGGroup groupInfoWithGroupId:param[@"id"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + JMSGGroup *group = resultObject; + [group largeAvatarData:^(NSData *data, NSString *objectId, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return ; + } + + successCallback(@[@{@"id": objectId, @"filePath": group.largeAvatarLocalPath ?: @""}]); + }]; }]; - }]; } RCT_EXPORT_METHOD(setConversationExtras:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (!param[@"type"]) { - failCallback(@[[self getParamError]]); - return; - } - - if (!param[@"extras"]) { - failCallback(@[[self getParamError]]); - return; - } - - [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + + if (!param[@"type"]) { + failCallback(@[[self getParamError]]); + return; } - NSDictionary *extras = param[@"extras"]; - for (NSString *key in extras) { - [conversation setExtraValue:extras[key] forKey:key]; + if (!param[@"extras"]) { + failCallback(@[[self getParamError]]); + return; } - successCallback(@[[conversation conversationToDictionary]]); - }]; + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + NSDictionary *extras = param[@"extras"]; + for (NSString *key in extras) { + [conversation setExtraValue:extras[key] forKey:key]; + } + successCallback(@[[conversation conversationToDictionary]]); + }]; } @@ -2406,41 +2420,41 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { RCT_EXPORT_METHOD(getChatRoomListByApp:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - NSNumber *start = nil; - NSNumber *count = nil; - if (!param[@"start"]) { - failCallback(@[[self getParamError]]); - return; - } - - if (!param[@"count"]) { - failCallback(@[[self getParamError]]); - return; - } - - start = param[@"start"]; - count = param[@"count"]; - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGChatRoom getChatRoomListWithAppKey:appKey start:[start integerValue] count:[count integerValue] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - NSArray *chatRoomArr = resultObject; - NSArray *chatRoomDicArr = [chatRoomArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGChatRoom *chatRoom = obj; - return [chatRoom chatRoomToDictionary]; - }]; - - successCallback(@[chatRoomDicArr]); - }]; + NSNumber *start = nil; + NSNumber *count = nil; + if (!param[@"start"]) { + failCallback(@[[self getParamError]]); + return; + } + + if (!param[@"count"]) { + failCallback(@[[self getParamError]]); + return; + } + + start = param[@"start"]; + count = param[@"count"]; + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGChatRoom getChatRoomListWithAppKey:appKey start:[start integerValue] count:[count integerValue] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + NSArray *chatRoomArr = resultObject; + NSArray *chatRoomDicArr = [chatRoomArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGChatRoom *chatRoom = obj; + return [chatRoom chatRoomToDictionary]; + }]; + + successCallback(@[chatRoomDicArr]); + }]; } /** @@ -2451,20 +2465,20 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { //static getChatRoomListByUser(success, error) { RCT_EXPORT_METHOD(getChatRoomListByUser:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - [JMSGChatRoom getMyChatRoomListCompletionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - NSArray *chatRoomArr = resultObject; - NSArray *chatRoomDicArr = [chatRoomArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGChatRoom *chatRoom = obj; - return [chatRoom chatRoomToDictionary]; + [JMSGChatRoom getMyChatRoomListCompletionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + NSArray *chatRoomArr = resultObject; + NSArray *chatRoomDicArr = [chatRoomArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGChatRoom *chatRoom = obj; + return [chatRoom chatRoomToDictionary]; + }]; + successCallback(@[chatRoomDicArr]); }]; - successCallback(@[chatRoomDicArr]); - }]; - + } /** @@ -2477,25 +2491,25 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { RCT_EXPORT_METHOD(getChatRoomInfos:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - if (!param[@"roomIds"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGChatRoom getChatRoomInfosWithRoomIds:param[@"roomIds"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + if (!param[@"roomIds"]) { + failCallback(@[[self getParamError]]); + return; } - NSArray *chatRoomArr = resultObject; - NSArray *chatRoomDicArr = [chatRoomArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGChatRoom *chatRoom = obj; - return [chatRoom chatRoomToDictionary]; + [JMSGChatRoom getChatRoomInfosWithRoomIds:param[@"roomIds"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + NSArray *chatRoomArr = resultObject; + NSArray *chatRoomDicArr = [chatRoomArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGChatRoom *chatRoom = obj; + return [chatRoom chatRoomToDictionary]; + }]; + + successCallback(@[chatRoomDicArr]); }]; - - successCallback(@[chatRoomDicArr]); - }]; } /** @@ -2508,20 +2522,20 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { RCT_EXPORT_METHOD(enterChatRoom:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - if (!param[@"roomId"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGChatRoom enterChatRoomWithRoomId:param[@"roomId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + if (!param[@"roomId"]) { + failCallback(@[[self getParamError]]); + return; } - JMSGConversation *conversation = resultObject; - successCallback(@[[conversation conversationToDictionary]]); - }]; + [JMSGChatRoom enterChatRoomWithRoomId:param[@"roomId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGConversation *conversation = resultObject; + successCallback(@[[conversation conversationToDictionary]]); + }]; } /** @@ -2534,18 +2548,18 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { RCT_EXPORT_METHOD(leaveChatRoom:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - if (!param[@"roomId"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGChatRoom leaveChatRoomWithRoomId:param[@"roomId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; + if (!param[@"roomId"]) { + failCallback(@[[self getParamError]]); + return; } - successCallback(@[]); - }]; + + [JMSGChatRoom leaveChatRoomWithRoomId:param[@"roomId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + successCallback(@[]); + }]; } /** @@ -2555,55 +2569,55 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { */ //static getChatRoomConversationList(callback) { RCT_EXPORT_METHOD(getChatRoomConversationList:(RCTResponseSenderBlock)successCallback) { - [JMSGConversation allChatRoomConversation:^(id resultObject, NSError *error) { - if (error) { - successCallback(@[@[]]); - return; - } - - NSArray *conversationArr = resultObject; - NSArray *conversationDicArr = [conversationArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGConversation *conversation = obj; - return [conversation conversationToDictionary]; + [JMSGConversation allChatRoomConversation:^(id resultObject, NSError *error) { + if (error) { + successCallback(@[@[]]); + return; + } + + NSArray *conversationArr = resultObject; + NSArray *conversationDicArr = [conversationArr mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGConversation *conversation = obj; + return [conversation conversationToDictionary]; + }]; + successCallback(@[conversationDicArr]); }]; - successCallback(@[conversationDicArr]); - }]; } RCT_EXPORT_METHOD(getChatRoomOwner:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - if (!param[@"roomId"]) { - failCallback(@[[self getParamError]]); - return; - } - - [JMSGChatRoom getChatRoomInfosWithRoomIds:@[param[@"roomId"]] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - NSArray *chatRoomArr = resultObject; - if (chatRoomArr == nil || chatRoomArr.count == 0) { - failCallback(@[[self getErrorWithLog:@"cann't found chat room from this roomId!"]]); - return; - } - JMSGChatRoom *chatRoom = chatRoomArr[0]; - [chatRoom getChatRoomOwnerInfo:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); + if (!param[@"roomId"]) { + failCallback(@[[self getParamError]]); return; - } - JMSGUser *user = resultObject; - successCallback(@[[user userToDictionary]]); + } + + [JMSGChatRoom getChatRoomInfosWithRoomIds:@[param[@"roomId"]] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + NSArray *chatRoomArr = resultObject; + if (chatRoomArr == nil || chatRoomArr.count == 0) { + failCallback(@[[self getErrorWithLog:@"cann't found chat room from this roomId!"]]); + return; + } + JMSGChatRoom *chatRoom = chatRoomArr[0]; + [chatRoom getChatRoomOwnerInfo:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + JMSGUser *user = resultObject; + successCallback(@[[user userToDictionary]]); + }]; }]; - }]; } RCT_EXPORT_METHOD(setBadge:(NSInteger)value callback:(RCTResponseSenderBlock)callback) {// ->Bool - [[UIApplication sharedApplication] setApplicationIconBadgeNumber:value]; - NSNumber *badgeNumber = [NSNumber numberWithBool:[JMessage setBadge: value]]; - callback(@[badgeNumber]); + [[UIApplication sharedApplication] setApplicationIconBadgeNumber:value]; + NSNumber *badgeNumber = [NSNumber numberWithBool:[JMessage setBadge: value]]; + callback(@[badgeNumber]); } RCT_EXPORT_METHOD(getAllUnreadCount:(RCTResponseSenderBlock)callback) { @@ -2798,169 +2812,197 @@ - (JMSGOptionalContent *)convertDicToJMSGOptionalContent:(NSDictionary *)dic { RCT_EXPORT_METHOD(transferGroupOwner:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (param[@"groupId"] == nil || - param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGGroup *group = resultObject; - [group transferGroupOwnerWithUsername:param[@"username"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); + + if (param[@"groupId"] == nil || + param[@"username"] == nil) { + failCallback(@[[self getParamError]]); return; - } - successCallback(@[]); + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGGroup *group = resultObject; + [group transferGroupOwnerWithUsername:param[@"username"] appKey:appKey completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + successCallback(@[]); + }]; }]; - }]; } RCT_EXPORT_METHOD(setGroupMemberSilence:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (param[@"groupId"] == nil || - param[@"username"] == nil || - param[@"isSilence"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGGroup *group = resultObject; - [group setGroupMemberSilence:[param[@"isSilence"] boolValue] username:param[@"username"] appKey:appKey handler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); + + if (param[@"groupId"] == nil || + param[@"username"] == nil || + param[@"isSilence"] == nil) { + failCallback(@[[self getParamError]]); return; - } - successCallback(@[]); + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGGroup *group = resultObject; + [group setGroupMemberSilence:[param[@"isSilence"] boolValue] username:param[@"username"] appKey:appKey handler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + successCallback(@[]); + }]; }]; - }]; } RCT_EXPORT_METHOD(isSilenceMember:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (param[@"groupId"] == nil || - param[@"username"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGGroup *group = resultObject; - - BOOL isSilence = [group isSilenceMemberWithUsername:param[@"username"] appKey:appKey]; - successCallback(@[@{@"isSilence": @(isSilence)}]); - }]; + + if (param[@"groupId"] == nil || + param[@"username"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGGroup *group = resultObject; + + BOOL isSilence = [group isSilenceMemberWithUsername:param[@"username"] appKey:appKey]; + successCallback(@[@{@"isSilence": @(isSilence)}]); + }]; } RCT_EXPORT_METHOD(groupSilenceMembers:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - if (param[@"groupId"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[error errorToDictionary]]); - return; - } - - JMSGGroup *group = resultObject; - NSArray *silenceMembers = [group groupSilenceMembers]; - NSArray *silenceUserDicArr = [silenceMembers mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { - JMSGUser *user = obj; - return [user userToDictionary]; - }]; - successCallback(@[silenceUserDicArr]); - }]; + if (param[@"groupId"] == nil) { + failCallback(@[[self getParamError]]); + return; + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGGroup *group = resultObject; + NSArray *silenceMembers = [group groupSilenceMembers]; + NSArray *silenceUserDicArr = [silenceMembers mapObjectsUsingBlock:^id(id obj, NSUInteger idx) { + JMSGUser *user = obj; + return [user userToDictionary]; + }]; + successCallback(@[silenceUserDicArr]); + }]; } RCT_EXPORT_METHOD(setGroupNickname:(NSDictionary *)param successCallback:(RCTResponseSenderBlock)successCallback failCallBack:(RCTResponseSenderBlock)failCallback) { - - if (param[@"groupId"] == nil || - param[@"username"] == nil || - param[@"nickName"] == nil) { - failCallback(@[[self getParamError]]); - return; - } - - NSString *appKey = nil; - if (param[@"appKey"]) { - appKey = param[@"appKey"]; - } else { - appKey = [JMessageHelper shareInstance].JMessageAppKey; - } - - [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { - if (error) { - failCallback(@[[self getParamError]]); - return; - } - - JMSGGroup *group = resultObject; - - [group setGroupNickname:param[@"nickName"] username:param[@"username"] appKey:appKey handler:^(id resultObject, NSError *error) { - if (error) { + + if (param[@"groupId"] == nil || + param[@"username"] == nil || + param[@"nickName"] == nil) { failCallback(@[[self getParamError]]); return; - } - - successCallback(@[]); + } + + NSString *appKey = nil; + if (param[@"appKey"]) { + appKey = param[@"appKey"]; + } else { + appKey = [JMessageHelper shareInstance].JMessageAppKey; + } + + [JMSGGroup groupInfoWithGroupId:param[@"groupId"] completionHandler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[self getParamError]]); + return; + } + + JMSGGroup *group = resultObject; + + [group setGroupNickname:param[@"nickName"] username:param[@"username"] appKey:appKey handler:^(id resultObject, NSError *error) { + if (error) { + failCallback(@[[self getParamError]]); + return; + } + + successCallback(@[]); + }]; + }]; +} + + +RCT_EXPORT_METHOD(setMsgHaveRead:(NSDictionary *)param + successCallback:(RCTResponseSenderBlock)successCallback + failCallBack:(RCTResponseSenderBlock)failCallback) { + + + [self getConversationWithDictionary:param callback:^(JMSGConversation *conversation, NSError *error) { + if (error) { + failCallback(@[[error errorToDictionary]]); + return; + } + + JMSGMessage *message = [conversation messageWithMessageId:param[@"id"]]; + + [message setMessageHaveRead:^(id resultObject, NSError *error) { + if (!error) { + successCallback(@[@{}]); + } else { + failCallback(@[[error errorToDictionary]]); + } + }]; + }]; - }]; } + + + @end From 10cdbd13f061ef828ad9ea503d127bdbb2fba0c1 Mon Sep 17 00:00:00 2001 From: wicked-tc130 Date: Fri, 22 Mar 2019 13:11:08 +0800 Subject: [PATCH 2/2] update version to 3.1.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dd4d0f9..fe19808 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jmessage-react-plugin", - "version": "3.1.2", + "version": "3.1.3", "description": "a jmessage plugin for react native application", "main": "index.js", "repository": {