NIMKitUtil.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. //
  2. // NIMUtil.m
  3. // NIMKit
  4. //
  5. // Created by chris on 15/8/10.
  6. // Copyright (c) 2015年 NetEase. All rights reserved.
  7. //
  8. #import "NIMKitUtil.h"
  9. #import "NIMKit.h"
  10. #import "NIMKitInfoFetchOption.h"
  11. @interface NIMKitUtil ()
  12. @end
  13. @implementation NIMKitUtil
  14. + (NSString *)genFilenameWithExt:(NSString *)ext
  15. {
  16. CFUUIDRef uuid = CFUUIDCreate(nil);
  17. NSString *uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(nil, uuid);
  18. CFRelease(uuid);
  19. NSString *uuidStr = [[uuidString stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  20. NSString *name = [NSString stringWithFormat:@"%@",uuidStr];
  21. return [ext length] ? [NSString stringWithFormat:@"%@.%@",name,ext]:name;
  22. }
  23. + (NSString *)showNick:(NSString*)uid inMessage:(NIMMessage*)message
  24. {
  25. if (!uid.length) {
  26. return nil;
  27. }
  28. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  29. option.message = message;
  30. if (message.messageType == NIMMessageTypeRobot)
  31. {
  32. NIMRobotObject *object = (NIMRobotObject *)message.messageObject;
  33. if (object.isFromRobot) {
  34. return [[NIMKit sharedKit] infoByUser:object.robotId option:option].showName;
  35. }
  36. }
  37. return [[NIMKit sharedKit] infoByUser:uid option:option].showName;
  38. }
  39. + (NSString *)showNick:(NSString*)uid inSession:(NIMSession*)session{
  40. if (!uid.length) {
  41. return nil;
  42. }
  43. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  44. option.session = session;
  45. return [[NIMKit sharedKit] infoByUser:uid option:option].showName;
  46. }
  47. + (NSString*)showTime:(NSTimeInterval) msglastTime showDetail:(BOOL)showDetail
  48. {
  49. //今天的时间
  50. NSDate * nowDate = [NSDate date];
  51. NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
  52. NSString *result = nil;
  53. NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
  54. NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
  55. NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
  56. NSInteger hour = msgDateComponents.hour;
  57. double OnedayTimeIntervalValue = 24*60*60; //一天的秒数
  58. result = [NIMKitUtil getPeriodOfTime:hour withMinute:msgDateComponents.minute];
  59. if (hour > 12)
  60. {
  61. hour = hour - 12;
  62. }
  63. BOOL isSameMonth = (nowDateComponents.year == msgDateComponents.year) && (nowDateComponents.month == msgDateComponents.month);
  64. if(isSameMonth && (nowDateComponents.day == msgDateComponents.day)) //同一天,显示时间
  65. {
  66. result = [[NSString alloc] initWithFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute];
  67. }
  68. else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+1)))//昨天
  69. {
  70. result = showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天";
  71. }
  72. else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+2))) //前天
  73. {
  74. result = showDetail? [[NSString alloc] initWithFormat:@"前天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"前天";
  75. }
  76. // else if([nowDate timeIntervalSinceDate:msgDate] < 7 * OnedayTimeIntervalValue)//一周内
  77. // {
  78. // NSString *weekDay = [NIMKitUtil weekdayStr:msgDateComponents.weekday];
  79. // result = showDetail? [weekDay stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : weekDay;
  80. // }
  81. else//显示日期
  82. {
  83. NSString *day = [NSString stringWithFormat:@"%zd-%zd-%zd", msgDateComponents.year, msgDateComponents.month, msgDateComponents.day];
  84. result = showDetail? [day stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute]:day;
  85. }
  86. return result;
  87. }
  88. #pragma mark - Private
  89. + (NSString *)getPeriodOfTime:(NSInteger)time withMinute:(NSInteger)minute
  90. {
  91. NSInteger totalMin = time *60 + minute;
  92. NSString *showPeriodOfTime = @"";
  93. if (totalMin > 0 && totalMin <= 5 * 60)
  94. {
  95. showPeriodOfTime = @"凌晨";
  96. }
  97. else if (totalMin > 5 * 60 && totalMin < 12 * 60)
  98. {
  99. showPeriodOfTime = @"上午";
  100. }
  101. else if (totalMin >= 12 * 60 && totalMin <= 18 * 60)
  102. {
  103. showPeriodOfTime = @"下午";
  104. }
  105. else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0)
  106. {
  107. showPeriodOfTime = @"晚上";
  108. }
  109. return showPeriodOfTime;
  110. }
  111. +(NSString*)weekdayStr:(NSInteger)dayOfWeek
  112. {
  113. static NSDictionary *daysOfWeekDict = nil;
  114. daysOfWeekDict = @{@(1):@"星期日",
  115. @(2):@"星期一",
  116. @(3):@"星期二",
  117. @(4):@"星期三",
  118. @(5):@"星期四",
  119. @(6):@"星期五",
  120. @(7):@"星期六",};
  121. return [daysOfWeekDict objectForKey:@(dayOfWeek)];
  122. }
  123. + (NSString *)messageTipContent:(NIMMessage *)message{
  124. NSString *text = nil;
  125. if (text == nil) {
  126. switch (message.messageType) {
  127. case NIMMessageTypeNotification:
  128. text = [NIMKitUtil notificationMessage:message];
  129. break;
  130. case NIMMessageTypeTip:
  131. text = message.text;
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. return text;
  138. }
  139. + (NSString *)notificationMessage:(NIMMessage *)message{
  140. NIMNotificationObject *object = message.messageObject;
  141. switch (object.notificationType) {
  142. case NIMNotificationTypeTeam:{
  143. return [NIMKitUtil teamNotificationFormatedMessage:message];
  144. }
  145. case NIMNotificationTypeNetCall:{
  146. return [NIMKitUtil netcallNotificationFormatedMessage:message];
  147. }
  148. case NIMNotificationTypeChatroom:{
  149. return [NIMKitUtil chatroomNotificationFormatedMessage:message];
  150. }
  151. default:
  152. return @"";
  153. }
  154. }
  155. + (NSString*)teamNotificationFormatedMessage:(NIMMessage *)message{
  156. NSString *formatedMessage = @"";
  157. NIMNotificationObject *object = message.messageObject;
  158. if (object.notificationType == NIMNotificationTypeTeam)
  159. {
  160. NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;
  161. NSString *source = [NIMKitUtil teamNotificationSourceName:message];
  162. NSArray *targets = [NIMKitUtil teamNotificationTargetNames:message];
  163. NSString *targetText = [targets count] > 1 ? [targets componentsJoinedByString:@","] : [targets firstObject];
  164. NSString *teamName = [NIMKitUtil teamNotificationTeamShowName:message];
  165. switch (content.operationType) {
  166. case NIMTeamOperationTypeInvite:{
  167. NSString *str = [NSString stringWithFormat:@"%@邀请%@",source,targets.firstObject];
  168. if (targets.count>1) {
  169. str = [str stringByAppendingFormat:@"等%zd人",targets.count];
  170. }
  171. str = [str stringByAppendingFormat:@"进入了%@",teamName];
  172. formatedMessage = str;
  173. }
  174. break;
  175. case NIMTeamOperationTypeDismiss:
  176. formatedMessage = [NSString stringWithFormat:@"%@解散了%@",source,teamName];
  177. break;
  178. case NIMTeamOperationTypeKick:{
  179. NSString *str = [NSString stringWithFormat:@"%@将%@",source,targets.firstObject];
  180. if (targets.count>1) {
  181. str = [str stringByAppendingFormat:@"等%zd人",targets.count];
  182. }
  183. str = [str stringByAppendingFormat:@"移出了%@",teamName];
  184. formatedMessage = str;
  185. }
  186. break;
  187. case NIMTeamOperationTypeUpdate:
  188. {
  189. id attachment = [content attachment];
  190. if ([attachment isKindOfClass:[NIMUpdateTeamInfoAttachment class]]) {
  191. NIMUpdateTeamInfoAttachment *teamAttachment = (NIMUpdateTeamInfoAttachment *)attachment;
  192. formatedMessage = [NSString stringWithFormat:@"%@更新了%@信息",source,teamName];
  193. //如果只是单个项目项被修改则显示具体的修改项
  194. if ([teamAttachment.values count] == 1) {
  195. NIMTeamUpdateTag tag = [[[teamAttachment.values allKeys] firstObject] integerValue];
  196. switch (tag) {
  197. case NIMTeamUpdateTagName:
  198. formatedMessage = [NSString stringWithFormat:@"%@更新了%@名称",source,teamName];
  199. break;
  200. case NIMTeamUpdateTagIntro:
  201. formatedMessage = [NSString stringWithFormat:@"%@更新了%@介绍",source,teamName];
  202. break;
  203. case NIMTeamUpdateTagAnouncement:
  204. formatedMessage = [NSString stringWithFormat:@"%@更新了%@公告",source,teamName];
  205. break;
  206. case NIMTeamUpdateTagJoinMode:
  207. formatedMessage = [NSString stringWithFormat:@"%@更新了%@验证方式",source,teamName];
  208. break;
  209. case NIMTeamUpdateTagAvatar:
  210. formatedMessage = [NSString stringWithFormat:@"%@更新了%@头像",source,teamName];
  211. break;
  212. case NIMTeamUpdateTagInviteMode:
  213. formatedMessage = [NSString stringWithFormat:@"%@更新了邀请他人权限",source];
  214. break;
  215. case NIMTeamUpdateTagBeInviteMode:
  216. formatedMessage = [NSString stringWithFormat:@"%@更新了被邀请人身份验证权限",source];
  217. break;
  218. case NIMTeamUpdateTagUpdateInfoMode:
  219. formatedMessage = [NSString stringWithFormat:@"%@更新了群资料修改权限",source];
  220. break;
  221. case NIMTeamUpdateTagMuteMode:{
  222. NSString *muteState = teamAttachment.values.allValues.firstObject;
  223. BOOL muted = [muteState isEqualToString:@"0"] ? NO : YES;
  224. formatedMessage = muted? [NSString stringWithFormat:@"%@设置了群全体禁言",source]: [NSString stringWithFormat:@"%@取消了全体禁言",source];
  225. break;
  226. }
  227. default:
  228. break;
  229. }
  230. }
  231. }
  232. if (formatedMessage == nil){
  233. formatedMessage = [NSString stringWithFormat:@"%@更新了%@信息",source,teamName];
  234. }
  235. }
  236. break;
  237. case NIMTeamOperationTypeLeave:
  238. formatedMessage = [NSString stringWithFormat:@"%@离开了%@",source,teamName];
  239. break;
  240. case NIMTeamOperationTypeApplyPass:{
  241. if ([source isEqualToString:targetText]) {
  242. //说明是以不需要验证的方式进入
  243. formatedMessage = [NSString stringWithFormat:@"%@进入了%@",source,teamName];
  244. }else{
  245. formatedMessage = [NSString stringWithFormat:@"%@通过了%@的申请",source,targetText];
  246. }
  247. }
  248. break;
  249. case NIMTeamOperationTypeTransferOwner:
  250. formatedMessage = [NSString stringWithFormat:@"%@转移了群主身份给%@",source,targetText];
  251. break;
  252. case NIMTeamOperationTypeAddManager:
  253. formatedMessage = [NSString stringWithFormat:@"%@被添加为群管理员",targetText];
  254. break;
  255. case NIMTeamOperationTypeRemoveManager:
  256. formatedMessage = [NSString stringWithFormat:@"%@被撤销了群管理员身份",targetText];
  257. break;
  258. case NIMTeamOperationTypeAcceptInvitation:
  259. formatedMessage = [NSString stringWithFormat:@"%@接受%@的邀请进群",source,targetText];
  260. break;
  261. case NIMTeamOperationTypeMute:{
  262. id attachment = [content attachment];
  263. if ([attachment isKindOfClass:[NIMMuteTeamMemberAttachment class]])
  264. {
  265. BOOL mute = [(NIMMuteTeamMemberAttachment *)attachment flag];
  266. NSString *muteStr = mute? @"禁言" : @"解除禁言";
  267. NSString *str = [targets componentsJoinedByString:@","];
  268. formatedMessage = [NSString stringWithFormat:@"%@被%@%@",str,source,muteStr];
  269. }
  270. }
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. if (!formatedMessage.length) {
  277. formatedMessage = [NSString stringWithFormat:@"未知系统消息"];
  278. }
  279. return formatedMessage;
  280. }
  281. + (NSString *)netcallNotificationFormatedMessage:(NIMMessage *)message{
  282. NIMNotificationObject *object = message.messageObject;
  283. NIMNetCallNotificationContent *content = (NIMNetCallNotificationContent *)object.content;
  284. NSString *text = @"";
  285. NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
  286. switch (content.eventType) {
  287. case NIMNetCallEventTypeMiss:{
  288. text = @"未接听";
  289. break;
  290. }
  291. case NIMNetCallEventTypeBill:{
  292. text = ([object.message.from isEqualToString:currentAccount])? @"通话拨打时长 " : @"通话接听时长 ";
  293. NSTimeInterval duration = content.duration;
  294. NSString *durationDesc = [NSString stringWithFormat:@"%02d:%02d",(int)duration/60,(int)duration%60];
  295. text = [text stringByAppendingString:durationDesc];
  296. break;
  297. }
  298. case NIMNetCallEventTypeReject:{
  299. text = ([object.message.from isEqualToString:currentAccount])? @"对方正忙" : @"已拒绝";
  300. break;
  301. }
  302. case NIMNetCallEventTypeNoResponse:{
  303. text = @"未接通,已取消";
  304. break;
  305. }
  306. default:
  307. break;
  308. }
  309. return text;
  310. }
  311. + (NSString *)chatroomNotificationFormatedMessage:(NIMMessage *)message{
  312. NIMNotificationObject *object = message.messageObject;
  313. NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)object.content;
  314. NSMutableArray *targetNicks = [[NSMutableArray alloc] init];
  315. for (NIMChatroomNotificationMember *memebr in content.targets) {
  316. if ([memebr.userId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]]) {
  317. [targetNicks addObject:@"你"];
  318. }else{
  319. [targetNicks addObject:memebr.nick];
  320. }
  321. }
  322. NSString *opeText = content.source.nick;
  323. NSString *targetText = [targetNicks componentsJoinedByString:@","];
  324. switch (content.eventType) {
  325. case NIMChatroomEventTypeEnter:
  326. {
  327. return [NSString stringWithFormat:@"欢迎%@进入直播间",targetText];
  328. }
  329. case NIMChatroomEventTypeAddBlack:
  330. {
  331. return [NSString stringWithFormat:@"%@被管理员拉入黑名单",targetText];
  332. }
  333. case NIMChatroomEventTypeRemoveBlack:
  334. {
  335. return [NSString stringWithFormat:@"%@被管理员解除拉黑",targetText];
  336. }
  337. case NIMChatroomEventTypeAddMute:
  338. {
  339. if (content.targets.count == 1 && [[content.targets.firstObject userId] isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]])
  340. {
  341. return @"你已被禁言";
  342. }
  343. else
  344. {
  345. return [NSString stringWithFormat:@"%@被管理员禁言",targetText];
  346. }
  347. }
  348. case NIMChatroomEventTypeRemoveMute:
  349. {
  350. return [NSString stringWithFormat:@"%@被管理员解除禁言",targetText];
  351. }
  352. case NIMChatroomEventTypeAddManager:
  353. {
  354. return [NSString stringWithFormat:@"%@被任命管理员身份",targetText];
  355. }
  356. case NIMChatroomEventTypeRemoveManager:
  357. {
  358. return [NSString stringWithFormat:@"%@被解除管理员身份",targetText];
  359. }
  360. case NIMChatroomEventTypeRemoveCommon:
  361. {
  362. return [NSString stringWithFormat:@"%@被解除直播室成员身份",targetText];
  363. }
  364. case NIMChatroomEventTypeAddCommon:
  365. {
  366. return [NSString stringWithFormat:@"%@被添加为直播室成员身份",targetText];
  367. }
  368. case NIMChatroomEventTypeInfoUpdated:
  369. {
  370. return [NSString stringWithFormat:@"直播间公告已更新"];
  371. }
  372. case NIMChatroomEventTypeKicked:
  373. {
  374. return [NSString stringWithFormat:@"%@被管理员移出直播间",targetText];
  375. }
  376. case NIMChatroomEventTypeExit:
  377. {
  378. return [NSString stringWithFormat:@"%@离开了直播间",targetText];
  379. }
  380. case NIMChatroomEventTypeClosed:
  381. {
  382. return [NSString stringWithFormat:@"直播间已关闭"];
  383. }
  384. case NIMChatroomEventTypeAddMuteTemporarily:
  385. {
  386. if (content.targets.count == 1 && [[content.targets.firstObject userId] isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]])
  387. {
  388. return @"你已被临时禁言";
  389. }
  390. else
  391. {
  392. return [NSString stringWithFormat:@"%@被管理员禁言",targetText];
  393. }
  394. }
  395. case NIMChatroomEventTypeRemoveMuteTemporarily:
  396. {
  397. return [NSString stringWithFormat:@"%@被管理员解除临时禁言",targetText];
  398. }
  399. case NIMChatroomEventTypeMemberUpdateInfo:
  400. {
  401. return [NSString stringWithFormat:@"%@更新了自己的个人信息",targetText];
  402. }
  403. case NIMChatroomEventTypeRoomMuted:
  404. {
  405. return @"全体禁言,管理员可发言";
  406. }
  407. case NIMChatroomEventTypeRoomUnMuted:
  408. {
  409. return @"解除全体禁言";
  410. }
  411. case NIMChatroomEventTypeQueueChange:
  412. case NIMChatroomEventTypeQueueBatchChange:
  413. return [NSString stringWithFormat:@"%@改变了聊天室队列",opeText];
  414. default:
  415. break;
  416. }
  417. return @"";
  418. }
  419. #pragma mark - Private
  420. + (NSString *)teamNotificationSourceName:(NIMMessage *)message{
  421. NSString *source;
  422. NIMNotificationObject *object = message.messageObject;
  423. NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;
  424. NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
  425. if ([content.sourceID isEqualToString:currentAccount]) {
  426. source = @"你";
  427. }else{
  428. source = [NIMKitUtil showNick:content.sourceID inSession:message.session];
  429. }
  430. return source;
  431. }
  432. + (NSArray *)teamNotificationTargetNames:(NIMMessage *)message{
  433. NSMutableArray *targets = [[NSMutableArray alloc] init];
  434. NIMNotificationObject *object = message.messageObject;
  435. NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;
  436. NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
  437. for (NSString *item in content.targetIDs) {
  438. if ([item isEqualToString:currentAccount]) {
  439. [targets addObject:@"你"];
  440. }else{
  441. NSString *targetShowName = [NIMKitUtil showNick:item inSession:message.session];
  442. [targets addObject:targetShowName];
  443. }
  444. }
  445. return targets;
  446. }
  447. + (NSString *)teamNotificationTeamShowName:(NIMMessage *)message{
  448. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:message.session.sessionId];
  449. NSString *teamName = team.type == NIMTeamTypeNormal ? @"讨论组" : @"群";
  450. return teamName;
  451. }
  452. + (BOOL)canEditTeamInfo:(NIMTeamMember *)member
  453. {
  454. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:member.teamId];
  455. if (team.updateInfoMode == NIMTeamUpdateInfoModeManager)
  456. {
  457. return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager;
  458. }
  459. else
  460. {
  461. return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager || member.type == NIMTeamMemberTypeNormal;
  462. }
  463. }
  464. + (BOOL)canInviteMember:(NIMTeamMember *)member
  465. {
  466. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:member.teamId];
  467. if (team.inviteMode == NIMTeamInviteModeManager)
  468. {
  469. return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager;
  470. }
  471. else
  472. {
  473. return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager || member.type == NIMTeamMemberTypeNormal;
  474. }
  475. }
  476. @end