IMAContactManager+User.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // IMAContactManager+User.m
  3. // TIMChat
  4. //
  5. // Created by AlexiChen on 16/3/22.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMAContactManager+User.h"
  9. @implementation IMAContactManager (User)
  10. - (void)asyncModify:(IMAUser *)user remark:(NSString *)remark succ:(TIMSucc)succ fail:(TIMFail)fail
  11. {
  12. if (!user)
  13. {
  14. DebugLog(@"参数有误");
  15. return;
  16. }
  17. if ([remark utf8Length] > 96)
  18. {
  19. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"备注名过长")];
  20. return;
  21. }
  22. __weak IMAContactManager *ws = self;
  23. [[TIMFriendshipManager sharedInstance] setFriendRemark:user.userId remark:remark succ:^{
  24. // 更新用户的名称
  25. user.remark = remark;
  26. [ws onFriendInfoChanged:user remark:remark];
  27. if (succ)
  28. {
  29. succ();
  30. }
  31. } fail:^(int code, NSString *err) {
  32. DebugLog(@"Fail:-->code=%d,msg=%@,fun=%s", code, err,__func__);
  33. [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, err)];
  34. if (fail)
  35. {
  36. fail(code, err);
  37. }
  38. }];
  39. }
  40. - (void)asyncModify:(IMAUser *)user subgroup:(IMASubGroup *)sg succ:(TIMFriendSucc)succ fail:(TIMFail)fail
  41. {
  42. if (!user || !sg)
  43. {
  44. DebugLog(@"参数有误");
  45. return;
  46. }
  47. // TODO:等Tom新增默认分组接口
  48. IMASubGroup *sub = [self subGroupOf:user];
  49. if (!sub)
  50. {
  51. // 好友不存在分组中中
  52. DebugLog(@"参数有误");
  53. return;
  54. }
  55. if ([sub isEqual:sg])
  56. {
  57. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"用户已在该分组")];
  58. return;
  59. }
  60. if (sub.isDefaultSubGroup && !sg.isDefaultSubGroup)
  61. {
  62. // 从我的好友移动到分组
  63. [[TIMFriendshipManager sharedInstance] addFriendsToFriendGroup:sg.name users:@[user.userId] succ:^(NSArray *friends) {
  64. [[IMAPlatform sharedInstance].contactMgr onMove:user from:sub to:sg];
  65. if (succ)
  66. {
  67. succ(friends);
  68. }
  69. } fail:^(int code, NSString *err) {
  70. DebugLog(@"Fail:-->code=%d,msg=%@,fun=%s", code, err,__func__);
  71. [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, err)];
  72. if (fail)
  73. {
  74. fail(code, err);
  75. }
  76. }];
  77. }
  78. else if (!sub.isDefaultSubGroup && !sg.isDefaultSubGroup)
  79. {
  80. //非默认分组之间的移动有两个步骤
  81. //1、将用户从当前分组移动到默认分组 2、将用户从默认分组移动到指定分组
  82. __weak IMASubGroup *wFromSubGroup = sub;
  83. __weak IMASubGroup *wToSubGroup = sg;
  84. __weak IMAUser *wu = user;
  85. [[TIMFriendshipManager sharedInstance] delFriendsFromFriendGroup:wFromSubGroup.name users:@[wu.userId] succ:^(NSArray *friends) {
  86. //从当前分组移动到默认分组
  87. [[TIMFriendshipManager sharedInstance] addFriendsToFriendGroup:wToSubGroup.name users:@[wu.userId] succ:^(NSArray *friends) {
  88. //从默认分组移动到指定分组
  89. [[IMAPlatform sharedInstance].contactMgr onMove:wu from:wFromSubGroup to:wToSubGroup];
  90. if (succ)
  91. {
  92. succ(friends);
  93. }
  94. } fail:^(int code, NSString *msg) {
  95. DebugLog(@"Fail: %d->%@", code, msg);
  96. //
  97. IMASubGroup *defaultGroup = [[IMAPlatform sharedInstance].contactMgr defaultAddToSubGroup];
  98. [[IMAPlatform sharedInstance].contactMgr onMove:wu from:wFromSubGroup to:defaultGroup];
  99. NSString *failInfo =ASLocalizedString( @"移动失败,已将好友移动到默认分组");
  100. NSString *errInfo = [NSString stringWithFormat:@"%@(%@)", IMALocalizedError(code, msg) ,failInfo];
  101. [[BGHUDHelper sharedInstance] tipMessage:errInfo];
  102. if (fail)
  103. {
  104. fail(code, msg);
  105. }
  106. }];
  107. } fail:^(int code, NSString *msg) {
  108. DebugLog(@"Fail: %d->%@", code, msg);
  109. [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, msg)];
  110. if (fail)
  111. {
  112. fail(code, msg);
  113. }
  114. }];
  115. }
  116. else if (!sub.isDefaultSubGroup && sg.isDefaultSubGroup)
  117. {
  118. [[TIMFriendshipManager sharedInstance] delFriendsFromFriendGroup:sub.name users:@[user.userId] succ:^(NSArray *friends) {
  119. [[IMAPlatform sharedInstance].contactMgr onMove:user from:sub to:sg];
  120. if (succ)
  121. {
  122. succ(friends);
  123. }
  124. } fail:^(int code, NSString *err) {
  125. DebugLog(@"Fail:-->code=%d,msg=%@,fun=%s", code, err,__func__);
  126. [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, err)];
  127. if (fail)
  128. {
  129. fail(code, err);
  130. }
  131. }];
  132. }
  133. else
  134. {
  135. // 都是我的好友分组中
  136. // 不作处理
  137. }
  138. }
  139. - (void)asyncMoveToBlackList:(IMAUser *)user succ:(TIMFriendSucc)succ fail:(TIMFail)fail
  140. {
  141. if (!user)
  142. {
  143. DebugLog(@"参数有误");
  144. if (fail)
  145. {
  146. fail(-1,ASLocalizedString( @"参数有误"));
  147. }
  148. return;
  149. }
  150. __weak IMAContactManager *ws = self;
  151. [[TIMFriendshipManager sharedInstance] addBlackList:@[user.userId] succ:^(NSArray *friends) {
  152. // 从好友分组中移除
  153. if (friends.count > 0)
  154. {
  155. TIMFriendResult *res = friends[0];
  156. IMAUser *user = [[IMAUser alloc] initWith:res.identifier];
  157. [ws removeUserToBlackList:user];
  158. if (succ)
  159. {
  160. succ(friends);
  161. }
  162. }
  163. } fail:^(int code, NSString *err) {
  164. DebugLog(@"Fail: %d->%@", code, err);
  165. if (fail)
  166. {
  167. fail(code, err);
  168. }
  169. }];
  170. }
  171. - (void)asyncMoveOutBlackList:(IMAUser *)user succ:(TIMFriendSucc)succ fail:(TIMFail)fail
  172. {
  173. if (!user)
  174. {
  175. DebugLog(@"参数有误");
  176. return;
  177. }
  178. __weak IMAContactManager *ws = self;
  179. [[TIMFriendshipManager sharedInstance] delBlackList:@[user.userId] succ:^(NSArray *friends) {
  180. // 从好友分组中移除
  181. [ws removeUserOutBlackList:user];
  182. if (succ)
  183. {
  184. succ(friends);
  185. }
  186. } fail:^(int code, NSString *err) {
  187. DebugLog(@"Fail:-->code=%d,msg=%@,fun=%s", code, err,__func__);
  188. [[BGHUDHelper sharedInstance] tipMessage:IMALocalizedError(code, err)];
  189. if (fail)
  190. {
  191. fail(code, err);
  192. }
  193. }];
  194. }
  195. @end