TUIChatBaseDataProvider.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Created by Tencent on 2023/06/09.
  2. // Copyright © 2023 Tencent. All rights reserved.
  3. @import ImSDK_Plus;
  4. #import <objc/runtime.h>
  5. #import <TUICore/NSDictionary+TUISafe.h>
  6. #import <TUICore/TUICore.h>
  7. #import <TUICore/TUIThemeManager.h>
  8. #import "TUIChatBaseDataProvider.h"
  9. #import "TUIMessageBaseDataProvider.h"
  10. #import <TUICore/TUILogin.h>
  11. #import "TUIChatDefine.h"
  12. #define Input_SendBtn_Key @"Input_SendBtn_Key"
  13. #define Input_SendBtn_Title @"Input_SendBtn_Title"
  14. #define Input_SendBtn_ImageName @"Input_SendBtn_ImageName"
  15. static NSArray *gCustomInputBtnInfo = nil;
  16. @implementation TUIChatBaseDataProvider
  17. + (void)initialize {
  18. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(onChangeLanguage) name:TUIChangeLanguageNotification object:nil];
  19. }
  20. + (void)onChangeLanguage {
  21. gCustomInputBtnInfo = nil;
  22. }
  23. + (NSArray *)customInputBtnInfo {
  24. if (gCustomInputBtnInfo == nil) {
  25. gCustomInputBtnInfo = @[ @{
  26. Input_SendBtn_Key : TUIInputMoreCellKey_Link,
  27. Input_SendBtn_Title : TIMCommonLocalizableString(TUIKitMoreLink),
  28. Input_SendBtn_ImageName : @"chat_more_link_img"
  29. } ];
  30. }
  31. return gCustomInputBtnInfo;
  32. }
  33. - (void)getForwardMessageWithCellDatas:(NSArray<TUIMessageCellData *> *)uiMsgs
  34. toTargets:(NSArray<TUIChatConversationModel *> *)targets
  35. Merge:(BOOL)merge
  36. ResultBlock:(void (^)(TUIChatConversationModel *targetConversation, NSArray<V2TIMMessage *> *msgs))resultBlock
  37. fail:(nullable V2TIMFail)fail {
  38. if (uiMsgs.count == 0) {
  39. if (fail) {
  40. fail(ERR_SVR_PROFILE_INVALID_PARAMETERS, @"uiMsgs is empty");
  41. }
  42. return;
  43. }
  44. dispatch_apply(targets.count, dispatch_get_global_queue(0, 0), ^(size_t index) {
  45. TUIChatConversationModel *convCellData = targets[index];
  46. NSMutableArray *tmpMsgs = [NSMutableArray array];
  47. for (TUIMessageCellData *uiMsg in uiMsgs) {
  48. V2TIMMessage *msg = uiMsg.innerMessage;
  49. if (msg) {
  50. [tmpMsgs addObject:msg];
  51. }
  52. }
  53. NSArray *msgs = [NSArray arrayWithArray:tmpMsgs];
  54. msgs = [msgs sortedArrayUsingComparator:^NSComparisonResult(V2TIMMessage *obj1, V2TIMMessage *obj2) {
  55. if ([obj1.timestamp timeIntervalSince1970] == [obj2.timestamp timeIntervalSince1970]) {
  56. return obj1.seq > obj2.seq;
  57. } else {
  58. return [obj1.timestamp compare:obj2.timestamp];
  59. }
  60. }];
  61. if (!merge) {
  62. NSMutableArray *forwardMsgs = [NSMutableArray array];
  63. for (V2TIMMessage *msg in msgs) {
  64. V2TIMMessage *forwardMessage = [V2TIMManager.sharedInstance createForwardMessage:msg];
  65. if (forwardMessage) {
  66. forwardMessage.isExcludedFromUnreadCount = [TUIConfig defaultConfig].isExcludedFromUnreadCount;
  67. forwardMessage.isExcludedFromLastMessage = [TUIConfig defaultConfig].isExcludedFromLastMessage;
  68. [forwardMsgs addObject:forwardMessage];
  69. }
  70. }
  71. if (resultBlock) {
  72. resultBlock(convCellData, forwardMsgs);
  73. }
  74. return;
  75. }
  76. @weakify(self);
  77. NSString *loginUserId = [V2TIMManager.sharedInstance getLoginUser];
  78. [V2TIMManager.sharedInstance getUsersInfo:@[ loginUserId ]
  79. succ:^(NSArray<V2TIMUserFullInfo *> *infoList) {
  80. @strongify(self);
  81. NSString *myName = loginUserId;
  82. if (infoList.firstObject.nickName.length > 0) {
  83. myName = infoList.firstObject.nickName;
  84. }
  85. NSString *title = [self.delegate dataProvider:self mergeForwardTitleWithMyName:myName];
  86. NSMutableArray *abstactList = [NSMutableArray array];
  87. if (uiMsgs.count > 0) {
  88. [abstactList addObject:[self abstractDisplayWithMessage:msgs[0]]];
  89. }
  90. if (uiMsgs.count > 1) {
  91. [abstactList addObject:[self abstractDisplayWithMessage:msgs[1]]];
  92. }
  93. if (uiMsgs.count > 2) {
  94. [abstactList addObject:[self abstractDisplayWithMessage:msgs[2]]];
  95. }
  96. NSString *compatibleText = TIMCommonLocalizableString(TUIKitRelayCompatibleText);
  97. V2TIMMessage *mergeMessage = [V2TIMManager.sharedInstance createMergerMessage:msgs
  98. title:title
  99. abstractList:abstactList
  100. compatibleText:compatibleText];
  101. if (mergeMessage == nil) {
  102. if (fail) {
  103. fail(ERR_NO_SUCC_RESULT, @"failed to merge-forward");
  104. }
  105. return;
  106. }
  107. mergeMessage.isExcludedFromUnreadCount = [TUIConfig defaultConfig].isExcludedFromUnreadCount;
  108. mergeMessage.isExcludedFromLastMessage = [TUIConfig defaultConfig].isExcludedFromLastMessage;
  109. if (resultBlock) {
  110. resultBlock(convCellData, @[ mergeMessage ]);
  111. }
  112. }
  113. fail:fail];
  114. });
  115. }
  116. - (NSString *)abstractDisplayWithMessage:(V2TIMMessage *)msg {
  117. return nil;
  118. }
  119. @end
  120. #pragma mark - TUIChatBaseDataProvider (IMSDK)
  121. @implementation TUIChatBaseDataProvider (IMSDK)
  122. + (void)getTotalUnreadMessageCountWithSuccBlock:(void (^)(UInt64 totalCount))succ fail:(nullable V2TIMFail)fail {
  123. [V2TIMManager.sharedInstance getTotalUnreadMessageCount:succ fail:fail];
  124. }
  125. + (void)saveDraftWithConversationID:(NSString *)conversationId Text:(NSString *)text {
  126. NSString *draft = text;
  127. draft = [draft stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
  128. [[V2TIMManager sharedInstance] setConversationDraft:conversationId draftText:draft succ:nil fail:nil];
  129. }
  130. #pragma mark - C2C
  131. + (void)getFriendInfoWithUserId:(nullable NSString *)userID
  132. SuccBlock:(void (^)(V2TIMFriendInfoResult *friendInfoResult))succ
  133. failBlock:(nullable V2TIMFail)fail {
  134. NSParameterAssert(userID);
  135. if (fail && !userID) {
  136. fail(ERR_INVALID_PARAMETERS, @"userID is nil");
  137. return;
  138. }
  139. [[V2TIMManager sharedInstance] getFriendsInfo:@[ userID ]
  140. succ:^(NSArray<V2TIMFriendInfoResult *> *resultList) {
  141. V2TIMFriendInfoResult *result = resultList.firstObject;
  142. succ(result);
  143. }
  144. fail:fail];
  145. }
  146. + (void)getUserInfoWithUserId:(NSString *)userID SuccBlock:(void (^)(V2TIMUserFullInfo *userInfo))succ failBlock:(nullable V2TIMFail)fail {
  147. NSParameterAssert(userID);
  148. if (!userID) {
  149. if (fail) {
  150. fail(ERR_INVALID_PARAMETERS, @"userID is nil");
  151. }
  152. return;
  153. }
  154. [[V2TIMManager sharedInstance] getUsersInfo:@[ userID ]
  155. succ:^(NSArray<V2TIMUserFullInfo *> *infoList) {
  156. V2TIMUserFullInfo *info = infoList.firstObject;
  157. if (succ) {
  158. succ(info);
  159. }
  160. }
  161. fail:fail];
  162. }
  163. #pragma mark - Group
  164. + (void)getGroupInfoWithGroupID:(NSString *)groupID SuccBlock:(void (^)(V2TIMGroupInfoResult *groupResult))succ failBlock:(nullable V2TIMFail)fail {
  165. NSParameterAssert(groupID);
  166. if (fail && !groupID) {
  167. fail(ERR_INVALID_PARAMETERS, @"groupID is nil");
  168. return;
  169. }
  170. [[V2TIMManager sharedInstance] getGroupsInfo:@[ groupID ]
  171. succ:^(NSArray<V2TIMGroupInfoResult *> *groupResultList) {
  172. V2TIMGroupInfoResult *result = groupResultList.firstObject;
  173. if (result && result.resultCode == 0) {
  174. if (succ) {
  175. succ(result);
  176. }
  177. } else {
  178. if (fail) {
  179. fail(result.resultCode, result.resultMsg);
  180. }
  181. }
  182. }
  183. fail:fail];
  184. }
  185. + (void)findMessages:(NSArray *)msgIDs callback:(void (^)(BOOL succ, NSString *error_message, NSArray *msgs))callback {
  186. [V2TIMManager.sharedInstance findMessages:msgIDs
  187. succ:^(NSArray<V2TIMMessage *> *msgs) {
  188. if (callback) {
  189. callback(YES, nil, msgs);
  190. }
  191. }
  192. fail:^(int code, NSString *desc) {
  193. if (callback) {
  194. callback(NO, desc, @[]);
  195. }
  196. }];
  197. }
  198. + (void)insertLocalTipsMessage:(NSString *)content chatID:(NSString *)chatID isGroup:(BOOL)isGroup {
  199. NSDictionary *dic = @{
  200. @"version" : @(1),
  201. BussinessID : @"local_tips",
  202. @"content" : content.length>0?content:@""
  203. };
  204. NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
  205. V2TIMMessage *msg = [[V2TIMManager sharedInstance] createCustomMessage:data];
  206. if (msg == nil) {
  207. return;
  208. }
  209. NSString *messageID = nil;
  210. NSString *senderID = [TUILogin getUserID];
  211. if (isGroup) {
  212. NSString *groupID = chatID.length>0?chatID:@"";
  213. messageID = [V2TIMManager.sharedInstance insertGroupMessageToLocalStorage:msg to:groupID sender:senderID succ:^{
  214. NSDictionary *userInfo = @{@"message" : msg,@"needScrollToBottom":@"1"};
  215. [[NSNotificationCenter defaultCenter] postNotificationName:TUIChatInsertMessageWithoutUpdateUINotification object:nil userInfo:userInfo];
  216. } fail:^(int code, NSString *desc) {
  217. }];
  218. }
  219. else {
  220. NSString *userID = chatID.length>0?chatID:@"";
  221. messageID = [V2TIMManager.sharedInstance insertC2CMessageToLocalStorage:msg to:userID sender:senderID succ:^{
  222. NSDictionary *userInfo = @{@"message" : msg,@"needScrollToBottom":@"1"};
  223. [[NSNotificationCenter defaultCenter] postNotificationName:TUIChatInsertMessageWithoutUpdateUINotification object:nil userInfo:userInfo];
  224. } fail:^(int code, NSString *desc) {
  225. }];
  226. }
  227. }
  228. @end