NIMKitUtil.m 21 KB

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