TUIChatModifyMessageHelper.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // TUIChatModifyMessageHelper.m
  3. // TUIChat
  4. //
  5. // Created by wyl on 2022/6/13.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIChatModifyMessageHelper.h"
  9. #import <TUICore/TUILogin.h>
  10. #import <TUICore/TUITool.h>
  11. #import "TUICloudCustomDataTypeCenter.h"
  12. #define RETRY_MIN_TIME 500
  13. #define RETRY_MAX_TIME 3000
  14. #define TIMES_CONTROL 3
  15. @interface TUIChatModifyMessageObj : NSObject
  16. @property(nonatomic, assign) NSInteger time;
  17. @property(nonatomic, copy) NSString *msgID;
  18. @property(nonatomic, strong) V2TIMMessage *msg;
  19. // reply
  20. @property(nonatomic, strong) NSDictionary *simpleCurrentContent;
  21. // revoke
  22. @property(nonatomic, copy) NSString *revokeMsgID;
  23. @end
  24. @implementation TUIChatModifyMessageObj
  25. - (instancetype)init {
  26. if (self = [super init]) {
  27. self.time = 0;
  28. }
  29. return self;
  30. }
  31. - (V2TIMMessage *)resolveOriginCloudCustomData:(V2TIMMessage *)rootMsg {
  32. if (self.simpleCurrentContent) {
  33. return [self.class resolveOriginCloudCustomData:rootMsg simpleCurrentContent:self.simpleCurrentContent];
  34. }
  35. if (self.revokeMsgID) {
  36. return [self.class resolveOriginCloudCustomData:rootMsg revokeMsgID:self.revokeMsgID];
  37. }
  38. return rootMsg;
  39. }
  40. // Input
  41. + (V2TIMMessage *)resolveOriginCloudCustomData:(V2TIMMessage *)rootMsg simpleCurrentContent:(NSDictionary *)simpleCurrentContent {
  42. NSMutableDictionary *mudic = [[NSMutableDictionary alloc] initWithCapacity:5];
  43. NSMutableArray *replies = [[NSMutableArray alloc] initWithCapacity:5];
  44. NSMutableDictionary *messageReplies = [[NSMutableDictionary alloc] initWithCapacity:5];
  45. if (rootMsg.cloudCustomData) {
  46. NSDictionary *originDic = [TUITool jsonData2Dictionary:rootMsg.cloudCustomData];
  47. if (originDic && [originDic isKindOfClass:[NSDictionary class]]) {
  48. [messageReplies addEntriesFromDictionary:originDic];
  49. }
  50. NSArray *messageRepliesArray = originDic[@"messageReplies"][@"replies"];
  51. if (messageRepliesArray && [messageRepliesArray isKindOfClass:NSArray.class] && messageRepliesArray.count > 0) {
  52. [replies addObjectsFromArray:messageRepliesArray];
  53. }
  54. }
  55. [replies addObject:simpleCurrentContent];
  56. [mudic setValue:replies forKey:@"replies"];
  57. [messageReplies setValue:mudic forKey:@"messageReplies"];
  58. [messageReplies setValue:@"1" forKey:@"version"];
  59. NSData *data = [TUITool dictionary2JsonData:messageReplies];
  60. if (data) {
  61. rootMsg.cloudCustomData = data;
  62. }
  63. return rootMsg;
  64. }
  65. // revoke
  66. + (V2TIMMessage *)resolveOriginCloudCustomData:(V2TIMMessage *)rootMsg revokeMsgID:(NSString *)revokeMsgId {
  67. NSMutableDictionary *mudic = [[NSMutableDictionary alloc] initWithCapacity:5];
  68. NSMutableArray *replies = [[NSMutableArray alloc] initWithCapacity:5];
  69. NSMutableDictionary *messageReplies = [[NSMutableDictionary alloc] initWithCapacity:5];
  70. if (rootMsg.cloudCustomData) {
  71. NSDictionary *originDic = [TUITool jsonData2Dictionary:rootMsg.cloudCustomData];
  72. if (originDic && [originDic isKindOfClass:[NSDictionary class]]) {
  73. [messageReplies addEntriesFromDictionary:originDic];
  74. if (originDic[@"messageReplies"] && [originDic[@"messageReplies"] isKindOfClass:[NSDictionary class]]) {
  75. NSArray *messageRepliesArray = originDic[@"messageReplies"][@"replies"];
  76. if (messageRepliesArray && [messageRepliesArray isKindOfClass:NSArray.class] && messageRepliesArray.count > 0) {
  77. [replies addObjectsFromArray:messageRepliesArray];
  78. }
  79. }
  80. }
  81. }
  82. NSMutableArray *filterReplies = [[NSMutableArray alloc] initWithCapacity:5];
  83. [replies enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
  84. if (obj && [obj isKindOfClass:NSDictionary.class]) {
  85. NSDictionary *dic = (NSDictionary *)obj;
  86. if (IS_NOT_EMPTY_NSSTRING(dic[@"messageID"])) {
  87. NSString *messageID = dic[@"messageID"];
  88. if (![messageID isEqualToString:revokeMsgId]) {
  89. [filterReplies addObject:dic];
  90. }
  91. }
  92. }
  93. }];
  94. [mudic setValue:filterReplies forKey:@"replies"];
  95. [messageReplies setValue:mudic forKey:@"messageReplies"];
  96. [messageReplies setValue:@"1" forKey:@"version"];
  97. NSData *data = [TUITool dictionary2JsonData:messageReplies];
  98. if (data) {
  99. rootMsg.cloudCustomData = data;
  100. }
  101. return rootMsg;
  102. }
  103. @end
  104. @interface ModifyCustomOperation : NSOperation
  105. @property(nonatomic, strong) TUIChatModifyMessageObj *obj;
  106. @property(nonatomic, copy) void (^SuccessBlock)(void);
  107. @property(nonatomic, copy) void (^FailedBlock)(int code, NSString *desc, V2TIMMessage *msg);
  108. @property(nonatomic, assign) BOOL bees_executing;
  109. @property(nonatomic, assign) BOOL bees_finished;
  110. @end
  111. @implementation ModifyCustomOperation
  112. - (void)dealloc {
  113. NSLog(@"operation-------dealloc");
  114. }
  115. - (void)start {
  116. if (self.isCancelled) {
  117. [self completeOperation];
  118. return;
  119. }
  120. __block V2TIMMessage *resolveMsg = nil;
  121. __weak typeof(self) weakSelf = self;
  122. self.bees_executing = YES;
  123. resolveMsg = [self.obj resolveOriginCloudCustomData:self.obj.msg];
  124. [[V2TIMManager sharedInstance] modifyMessage:resolveMsg
  125. completion:^(int code, NSString *desc, V2TIMMessage *msg) {
  126. __strong typeof(weakSelf) strongSelf = weakSelf;
  127. if (code != 0) {
  128. if (strongSelf.FailedBlock) {
  129. strongSelf.FailedBlock(code, desc, msg);
  130. }
  131. } else {
  132. if (strongSelf.SuccessBlock) {
  133. strongSelf.SuccessBlock();
  134. }
  135. }
  136. [strongSelf completeOperation];
  137. }];
  138. }
  139. - (void)completeOperation {
  140. self.bees_executing = NO;
  141. self.bees_finished = YES;
  142. }
  143. - (void)cancel {
  144. [super cancel];
  145. [self completeOperation];
  146. }
  147. #pragma mark - settter and getter
  148. - (void)setBees_executing:(BOOL)bees_executing {
  149. [self willChangeValueForKey:@"isExecuting"];
  150. _bees_executing = bees_executing;
  151. [self didChangeValueForKey:@"isExecuting"];
  152. }
  153. - (void)setBees_finished:(BOOL)bees_finished {
  154. [self willChangeValueForKey:@"isFinished"];
  155. _bees_finished = bees_finished;
  156. [self didChangeValueForKey:@"isFinished"];
  157. }
  158. - (BOOL)isExecuting {
  159. return self.bees_executing;
  160. }
  161. - (BOOL)isFinished {
  162. return self.bees_finished;
  163. }
  164. @end
  165. @interface TUIChatModifyMessageHelper () <V2TIMAdvancedMsgListener>
  166. @property(nonatomic, strong) NSMutableDictionary<NSString *, TUIChatModifyMessageObj *> *modifyMessageHelperMap;
  167. @property(nonatomic, strong) NSOperationQueue *queue;
  168. @end
  169. @implementation TUIChatModifyMessageHelper
  170. + (TUIChatModifyMessageHelper *)defaultHelper {
  171. static dispatch_once_t onceToken;
  172. static TUIChatModifyMessageHelper *config;
  173. dispatch_once(&onceToken, ^{
  174. config = [[TUIChatModifyMessageHelper alloc] init];
  175. });
  176. return config;
  177. }
  178. - (id)init {
  179. self = [super init];
  180. if (self) {
  181. [self registerTUIKitNotification];
  182. self.modifyMessageHelperMap = [NSMutableDictionary dictionaryWithCapacity:10];
  183. [self setupOpQueue];
  184. }
  185. return self;
  186. }
  187. - (void)setupOpQueue {
  188. self.queue = [[NSOperationQueue alloc] init];
  189. self.queue.maxConcurrentOperationCount = 1;
  190. }
  191. - (void)registerTUIKitNotification {
  192. [[V2TIMManager sharedInstance] addAdvancedMsgListener:self];
  193. }
  194. - (void)onRecvMessageModified:(V2TIMMessage *)msg {
  195. NSString *msgID = msg.msgID;
  196. TUIChatModifyMessageObj *obj = self.modifyMessageHelperMap[msgID];
  197. if (obj && [obj isKindOfClass:[TUIChatModifyMessageObj class]]) {
  198. // update;
  199. obj.msg = msg;
  200. }
  201. }
  202. #pragma mark - public
  203. - (void)modifyMessage:(V2TIMMessage *)msg reactEmoji:(NSString *)emojiName {
  204. [self modifyMessage:msg reactEmoji:nil simpleCurrentContent:nil revokeMsgID:nil timeControl:0];
  205. }
  206. - (void)modifyMessage:(V2TIMMessage *)msg simpleCurrentContent:(NSDictionary *)simpleCurrentContent {
  207. [self modifyMessage:msg reactEmoji:nil simpleCurrentContent:simpleCurrentContent revokeMsgID:nil timeControl:0];
  208. }
  209. - (void)modifyMessage:(V2TIMMessage *)msg revokeMsgID:(NSString *)revokeMsgID {
  210. [self modifyMessage:msg reactEmoji:nil simpleCurrentContent:nil revokeMsgID:revokeMsgID timeControl:0];
  211. }
  212. #pragma mark - private
  213. - (void)modifyMessage:(V2TIMMessage *)msg
  214. reactEmoji:(NSString *)emojiName
  215. simpleCurrentContent:(NSDictionary *)simpleCurrentContent
  216. revokeMsgID:(NSString *)revokeMsgID
  217. timeControl:(NSInteger)time {
  218. NSString *msgID = msg.msgID;
  219. if (!IS_NOT_EMPTY_NSSTRING(msgID)) {
  220. return;
  221. }
  222. TUIChatModifyMessageObj *obj = [[TUIChatModifyMessageObj alloc] init];
  223. obj.msgID = msgID;
  224. obj.msg = msg;
  225. obj.time = time;
  226. if (simpleCurrentContent) {
  227. obj.simpleCurrentContent = simpleCurrentContent;
  228. }
  229. if (revokeMsgID) {
  230. obj.revokeMsgID = revokeMsgID;
  231. }
  232. [self.modifyMessageHelperMap setObject:obj forKey:obj.msgID];
  233. __weak typeof(self) weakSelf = self;
  234. ModifyCustomOperation *modifyop = [[ModifyCustomOperation alloc] init];
  235. modifyop.obj = obj;
  236. modifyop.SuccessBlock = ^{
  237. __strong typeof(weakSelf) strongSelf = weakSelf;
  238. [strongSelf.modifyMessageHelperMap removeObjectForKey:msgID];
  239. };
  240. modifyop.FailedBlock = ^(int code, NSString *desc, V2TIMMessage *msg) {
  241. __strong typeof(weakSelf) strongSelf = weakSelf;
  242. if (obj.time <= TIMES_CONTROL) {
  243. int delay;
  244. delay = [self getRandomNumber:RETRY_MIN_TIME to:RETRY_MAX_TIME];
  245. TUIChatModifyMessageObj *obj = strongSelf.modifyMessageHelperMap[msgID];
  246. // update
  247. obj.msg = msg;
  248. obj.time = obj.time + 1;
  249. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
  250. TUIChatModifyMessageObj *obj = strongSelf.modifyMessageHelperMap[msgID];
  251. if (obj && [obj isKindOfClass:[TUIChatModifyMessageObj class]]) {
  252. [strongSelf modifyMessage:obj.msg
  253. reactEmoji:nil
  254. simpleCurrentContent:obj.simpleCurrentContent
  255. revokeMsgID:obj.revokeMsgID
  256. timeControl:obj.time];
  257. }
  258. });
  259. } else {
  260. [strongSelf.modifyMessageHelperMap removeObjectForKey:msgID];
  261. }
  262. };
  263. if (!modifyop.isCancelled) {
  264. [self.queue addOperation:modifyop];
  265. }
  266. }
  267. - (int)getRandomNumber:(int)from to:(int)to {
  268. return (int)(from + (arc4random() % (to - from + 1)));
  269. }
  270. @end