IMAContactManager.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. //
  2. // IMAContactManager.m
  3. // TIMAdapter
  4. //
  5. // Created by AlexiChen on 16/2/18.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMAContactManager.h"
  9. @implementation IMAContactChangedNotifyItem
  10. - (instancetype)initWith:(IMAContactChangedNotifyType)type
  11. {
  12. if (self = [super init])
  13. {
  14. _type = type;
  15. }
  16. return self;
  17. }
  18. - (NSNotification *)changedNotification
  19. {
  20. NSNotification *notify = [NSNotification notificationWithName:[self notificationName] object:self];
  21. return notify;
  22. }
  23. - (NSString *)notificationName
  24. {
  25. return [NSString stringWithFormat:@"IMAContactChangedNotification_%d", (int)_type];
  26. }
  27. @end
  28. @implementation IMAContactManager
  29. - (void)saveToLocal
  30. {
  31. [self saveSubGroupStateInfoToLocal];
  32. }
  33. - (void)asyncConfigContact
  34. {
  35. // 分组是FriendShipProxy下的内容
  36. // 同步分组列表
  37. [self asyncSubGroupList];
  38. }
  39. // 异步配置群相关内容
  40. - (void)asyncConfigGroup
  41. {
  42. // 是GroupAssistant下的内容
  43. // 同步群列表
  44. [self syncGroupList];
  45. // 异步黑名单
  46. [self asyncBlackList];
  47. }
  48. - (CLSafeMutableArray *)clearPickedSubGroupList
  49. {
  50. NSInteger count = [_subGroupList count];
  51. for (NSInteger i = 0; i < count; i++)
  52. {
  53. IMASubGroup *group = [_subGroupList objectAtIndex:i];
  54. NSInteger fcount = group.friends.count;
  55. for (NSInteger j = 0; j < fcount; j++)
  56. {
  57. IMAUser *user = [group.friends objectAtIndex:j];
  58. user.isSelected = NO;
  59. }
  60. }
  61. return _subGroupList;
  62. }
  63. - (IMASubGroup *)getSubGroupOf:(NSString *)sgNAme
  64. {
  65. IMASubGroup *temp = [[IMASubGroup alloc] initWithName:sgNAme];
  66. NSInteger idx = [_subGroupList indexOfObject:temp];
  67. if (idx >= 0 && idx < _subGroupList.count)
  68. {
  69. return [_subGroupList objectAtIndex:idx];
  70. }
  71. else
  72. {
  73. return [self defaultAddToSubGroup];
  74. }
  75. }
  76. - (IMASubGroup *)subgroupOf:(IMAUser *)user
  77. {
  78. if ([user isC2CType])
  79. {
  80. for (NSInteger i = 0; i < _subGroupList.count; i++)
  81. {
  82. IMASubGroup *sg = [_subGroupList objectAtIndex:i];
  83. NSInteger index = [sg.friends indexOfObject:user];
  84. if (index >= 0 && index < sg.friends.count)
  85. {
  86. return sg;
  87. }
  88. }
  89. }
  90. return nil;
  91. }
  92. - (IMAUser *)isContainUser:(IMAUser *)user
  93. {
  94. if ([user isC2CType])
  95. {
  96. for (NSInteger i = 0; i < _subGroupList.count; i++)
  97. {
  98. IMASubGroup *sg = [_subGroupList objectAtIndex:i];
  99. NSInteger index = [sg.friends indexOfObject:user];
  100. if (index >= 0 && index < sg.friends.count)
  101. {
  102. return [sg.friends objectAtIndex:index];
  103. }
  104. }
  105. }
  106. else if ([user isGroupType])
  107. {
  108. NSInteger index = [_groupList indexOfObject:user];
  109. if (index >= 0 && index < _groupList.count)
  110. {
  111. return [_groupList objectAtIndex:index];
  112. }
  113. }
  114. return nil;
  115. }
  116. - (IMAUser *)getUserByGroupId:(NSString *)groupID
  117. {
  118. if ([NSString isEmpty:groupID])
  119. {
  120. return nil;
  121. }
  122. IMAUser *item = [[IMAGroup alloc] initWith:groupID];
  123. return [self isContainUser:item];
  124. }
  125. - (IMAUser *)getUserByUserId:(NSString *)userID
  126. {
  127. if ([NSString isEmpty:userID])
  128. {
  129. return nil;
  130. }
  131. IMAUser *item = [[IMAUser alloc] initWith:userID];
  132. return [self isContainUser:item];
  133. }
  134. - (BOOL)isMyFriend:(IMAUser *)user
  135. {
  136. // 检查通讯录中是否含有些人
  137. IMAUser *u = [self isContainUser:user];
  138. return u != nil;
  139. }
  140. - (BOOL)isInBlackListByID:(NSString *)userID
  141. {
  142. IMAUser *u = [[IMAUser alloc] initWith:userID];
  143. return [self isInBlackList:u];
  144. }
  145. - (BOOL)isInBlackList:(IMAUser *)user
  146. {
  147. return _blackList && [_blackList containsObject:user];
  148. }
  149. - (void)addContactChangedObserver:(id)observer handler:(SEL)selector forEvent:(NSUInteger)eventID
  150. {
  151. NSUInteger op = EIMAContact_AddNewSubGroup;
  152. do
  153. {
  154. if (op & eventID)
  155. {
  156. NSString *notification = [NSString stringWithFormat:@"IMAContactChangedNotification_%d", (int)op];
  157. [[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:notification object:nil];
  158. eventID -= op;
  159. }
  160. op = op << 1;
  161. } while (eventID > 0);
  162. }
  163. - (void)removeContactChangedObser:(id)observer
  164. {
  165. [[NSNotificationCenter defaultCenter] removeObserver:observer];
  166. }
  167. - (void)removeUser:(IMAUser *)user
  168. {
  169. if (user)
  170. {
  171. [self onDeleteFriend:user];
  172. [[IMAPlatform sharedInstance].conversationMgr removeConversationWith:user];
  173. }
  174. }
  175. - (void)addUser:(IMAUser *)user toSubGroup:(IMASubGroup *)sg
  176. {
  177. if (user && [user isC2CType])
  178. {
  179. NSInteger idx = [_subGroupList indexOfObject:sg];
  180. if (idx >= 0 && idx < _subGroupList.count)
  181. {
  182. IMASubGroup *dsg = [_subGroupList objectAtIndex:idx];
  183. NSInteger uidx = [dsg.friends indexOfObject:user];
  184. if (!(uidx >= 0 && uidx < dsg.friends.count))
  185. {
  186. [dsg.friends addObject:user];
  187. [self onAddUser:user toSubGroup:dsg];
  188. }
  189. }
  190. }
  191. else
  192. {
  193. DebugLog(@"参数有误");
  194. }
  195. }
  196. - (void)addUserToDefaultSubGroup:(IMAUser *)user
  197. {
  198. if (user)
  199. {
  200. IMASubGroup *sub = [[IMASubGroup alloc] initDefaultSubGroup];
  201. NSInteger idx = [_subGroupList indexOfObject:sub];
  202. if (idx >= 0 && idx < _subGroupList.count)
  203. {
  204. IMASubGroup *dsg = [_subGroupList objectAtIndex:idx];
  205. NSInteger uidx = [dsg.friends indexOfObject:user];
  206. if (!(uidx >= 0 && uidx < dsg.friends.count))
  207. {
  208. [dsg.friends addObject:user];
  209. [self onAddUser:user toSubGroup:dsg];
  210. }
  211. }
  212. }
  213. }
  214. - (void)removeUserToBlackList:(IMAUser *)user
  215. {
  216. // [self removeUser:user];//这里不需要移除,添加到黑名单时,会收到一个删除好友的回调通知,-(void) OnDelFriends:(NSArray*)identifiers,在这个回调通知里面已经处理了
  217. [self onAddToBlackList:user];
  218. }
  219. - (void)removeUserOutBlackList:(IMAUser *)user
  220. {
  221. [self onRemoveOutBlackList:user];
  222. }
  223. @end
  224. @implementation IMAContactManager (Protected)
  225. - (void)onAddNewSubGroup:(IMASubGroup *)group
  226. {
  227. NSString * log = [NSString stringWithFormat:@"onAddNewSubGroup1 group.count = %ld,fun = %s",(long)group.itemsCount, __func__];
  228. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log];
  229. NSString * log1 = [NSString stringWithFormat:@"onAddNewSubGroup1 subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  230. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
  231. [self saveSubGroupStateInfoToLocal];
  232. [_subGroupList addObject:group];
  233. NSInteger index = [self.subGroupList count] - 1;
  234. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_AddNewSubGroup];
  235. item.subGroup = group;
  236. item.index = index;
  237. if (_contactChangedCompletion)
  238. {
  239. _contactChangedCompletion(item);
  240. }
  241. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  242. NSString * log2 = [NSString stringWithFormat:@"onAddNewSubGroup1 subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  243. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log2];
  244. }
  245. - (void)onAddUser:(IMAUser *)user toSubGroup:(IMASubGroup *)group
  246. {
  247. NSInteger index = [group.friends indexOfObject:user];
  248. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_AddFriend];
  249. item.subGroup = group;
  250. item.user = user;
  251. item.index = index;
  252. if (_contactChangedCompletion)
  253. {
  254. _contactChangedCompletion(item);
  255. }
  256. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  257. }
  258. - (void)onDeleteSubGroup:(IMASubGroup *)group
  259. {
  260. NSString * log1 = [NSString stringWithFormat:@"onDeleteSubGroup subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  261. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
  262. NSInteger index = [_subGroupList indexOfObject:group];
  263. // todo-todo-
  264. [_subGroupList removeObject:group];
  265. [self saveSubGroupStateInfoToLocal];
  266. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_DeleteSubGroup];
  267. item.subGroup = group;
  268. item.index = index;
  269. if (_contactChangedCompletion)
  270. {
  271. _contactChangedCompletion(item);
  272. }
  273. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  274. if (group.friends.count)
  275. {
  276. IMASubGroup *defg = [self defaultAddToSubGroup];
  277. [defg.friends addObjectsFromArray:group.friends.safeArray];
  278. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_SubGroupChanged];
  279. item.subGroup = defg;
  280. item.index = [_subGroupList indexOfObject:defg];
  281. if (_contactChangedCompletion)
  282. {
  283. _contactChangedCompletion(item);
  284. }
  285. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  286. }
  287. NSString * log2 = [NSString stringWithFormat:@"onDeleteSubGroup subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  288. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log2];
  289. }
  290. - (void)onDeleteFriend:(IMAUser *)user
  291. {
  292. if ([user isC2CType])
  293. {
  294. for (NSInteger i = 0; i < _subGroupList.count; i++)
  295. {
  296. IMASubGroup *sg = [_subGroupList objectAtIndex:i];
  297. NSInteger index = [sg.friends indexOfObject:user];
  298. if (index >= 0 && index < sg.friends.count)
  299. {
  300. IMAUser *u = [sg.friends objectAtIndex:index];
  301. [sg.friends removeObject:u];
  302. // 更新
  303. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_DeleteFriend];
  304. item.subGroup = sg;
  305. item.user = u;
  306. item.index = index;
  307. if (_contactChangedCompletion)
  308. {
  309. _contactChangedCompletion(item);
  310. }
  311. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  312. break;
  313. }
  314. }
  315. }
  316. else if([user isGroupType])
  317. {
  318. NSInteger index = [_groupList indexOfObject:user];
  319. if (index >= 0 && index < _groupList.count)
  320. {
  321. IMAGroup *sg = [_groupList objectAtIndex:index];
  322. [_groupList removeObject:sg];
  323. // UI退出后上不存在更新逻辑,所以不发送通知
  324. // // 更新
  325. // IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_DeleteFriend];
  326. // item.subGroup = sg;
  327. // item.user = u;
  328. // item.index = index;
  329. // [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  330. }
  331. }
  332. }
  333. - (void)onFriendInfoChanged:(IMAUser *)user remark:(NSString *)remark
  334. {
  335. for (NSInteger i = 0; i < _subGroupList.count; i++)
  336. {
  337. IMASubGroup *sg = [_subGroupList objectAtIndex:i];
  338. NSInteger index = [sg.friends indexOfObject:user];
  339. if (index >= 0 && index < sg.friends.count)
  340. {
  341. IMAUser *u = [sg.friends objectAtIndex:index];
  342. u.remark = remark;
  343. // 更新
  344. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_FriendInfoChanged];
  345. item.subGroup = sg;
  346. item.user = u;
  347. item.index = index;
  348. if (_contactChangedCompletion)
  349. {
  350. _contactChangedCompletion(item);
  351. }
  352. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  353. break;
  354. }
  355. }
  356. // 有可能不在好友里面
  357. [[IMAPlatform sharedInstance].conversationMgr updateConversationWith:user];
  358. }
  359. - (void)onLoadDefaultSubGroup:(IMASubGroup *)group
  360. {
  361. NSString * log = [NSString stringWithFormat:@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  362. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log];
  363. // todo-todo-
  364. [_subGroupList insertObject:group atIndex:0];
  365. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_LoadDefaultFriends];
  366. item.subGroup = group;
  367. item.index = 0;
  368. if (_contactChangedCompletion)
  369. {
  370. _contactChangedCompletion(item);
  371. }
  372. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  373. NSString * log1 = [NSString stringWithFormat:@"subGroupList.count = %ld,fun = %s",(long)_subGroupList.count, __func__];
  374. [[TIMManager sharedInstance] log:TIM_LOG_ERROR tag:@"ui crash" msg:log1];
  375. }
  376. - (void)onAddToBlackList:(IMAUser *)user
  377. {
  378. if (!_blackList)
  379. {
  380. _blackList = [[CLSafeMutableArray alloc] init];
  381. }
  382. [_blackList addObject:user];
  383. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_BlackListAddIn];
  384. item.index = _blackList.count - 1;
  385. if (_contactChangedCompletion)
  386. {
  387. _contactChangedCompletion(item);
  388. }
  389. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  390. }
  391. - (void)onRemoveOutBlackList:(IMAUser *)user
  392. {
  393. NSInteger idx = [_blackList indexOfObject:user];
  394. if (idx >= 0 && idx < _blackList.count)
  395. {
  396. [_blackList removeObjectAtIndex:idx];
  397. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_BlackListMoveOut];
  398. item.index = idx;
  399. if (_contactChangedCompletion)
  400. {
  401. _contactChangedCompletion(item);
  402. }
  403. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  404. }
  405. }
  406. - (void)onMove:(IMAUser *)user from:(IMASubGroup *)fromG to:(IMASubGroup *)toG
  407. {
  408. IMAUser *u = [self isContainUser:user];
  409. if (u)
  410. {
  411. NSInteger fi = [_subGroupList indexOfObject:fromG];
  412. NSInteger ti = [_subGroupList indexOfObject:toG];
  413. NSInteger count = [_subGroupList count];
  414. if (fi >= 0 && fi < count && ti >= 0 && ti < count)
  415. {
  416. IMASubGroup *sg = [_subGroupList objectAtIndex:fi];
  417. NSInteger index = [sg.friends indexOfObject:user];
  418. [sg.friends removeObject:u];
  419. // 更新
  420. IMAContactChangedNotifyItem *item = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_MoveFriend];
  421. item.subGroup = sg;
  422. item.user = u;
  423. item.index = index;
  424. IMASubGroup *tg = [_subGroupList objectAtIndex:ti];
  425. [tg.friends addObject:u];
  426. NSInteger tindex = [tg.friends indexOfObject:u];
  427. IMAContactChangedNotifyItem *titem = [[IMAContactChangedNotifyItem alloc] initWith:EIMAContact_AddFriend];
  428. titem.subGroup = tg;
  429. titem.user = u;
  430. titem.index = tindex;
  431. item.toItem = titem;
  432. if (_contactChangedCompletion)
  433. {
  434. _contactChangedCompletion(item);
  435. }
  436. [[NSNotificationQueue defaultQueue] enqueueNotification:[item changedNotification] postingStyle:NSPostWhenIdle];
  437. }
  438. else
  439. {
  440. DebugLog(@"不存在的分组");
  441. }
  442. }
  443. else
  444. {
  445. DebugLog(@"不存在的User");
  446. }
  447. }
  448. - (void)onAddGroup:(IMAGroup *)group
  449. {
  450. if (group)
  451. {
  452. [_groupList addObject:group];
  453. }
  454. }
  455. @end