123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- //
- // NIMUtil.m
- // NIMKit
- //
- // Created by chris on 15/8/10.
- // Copyright (c) 2015年 NetEase. All rights reserved.
- //
- #import "NIMKitUtil.h"
- #import "NIMKit.h"
- #import "NIMKitInfoFetchOption.h"
- @interface NIMKitUtil ()
- @end
- @implementation NIMKitUtil
- + (NSString *)genFilenameWithExt:(NSString *)ext
- {
- CFUUIDRef uuid = CFUUIDCreate(nil);
- NSString *uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(nil, uuid);
- CFRelease(uuid);
- NSString *uuidStr = [[uuidString stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
- NSString *name = [NSString stringWithFormat:@"%@",uuidStr];
- return [ext length] ? [NSString stringWithFormat:@"%@.%@",name,ext]:name;
- }
- + (NSString *)showNick:(NSString*)uid inMessage:(NIMMessage*)message
- {
- if (!uid.length) {
- return nil;
- }
- NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
- option.message = message;
- if (message.messageType == NIMMessageTypeRobot)
- {
- NIMRobotObject *object = (NIMRobotObject *)message.messageObject;
- if (object.isFromRobot) {
- return [[NIMKit sharedKit] infoByUser:object.robotId option:option].showName;
- }
- }
- return [[NIMKit sharedKit] infoByUser:uid option:option].showName;
- }
- + (NSString *)showNick:(NSString*)uid inSession:(NIMSession*)session{
- if (!uid.length) {
- return nil;
- }
- NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
- option.session = session;
- return [[NIMKit sharedKit] infoByUser:uid option:option].showName;
- }
- + (NSString*)showTime:(NSTimeInterval) msglastTime showDetail:(BOOL)showDetail
- {
- //今天的时间
- NSDate * nowDate = [NSDate date];
- NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
- NSString *result = nil;
- NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
- NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
- NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
-
- NSInteger hour = msgDateComponents.hour;
- double OnedayTimeIntervalValue = 24*60*60; //一天的秒数
- result = [NIMKitUtil getPeriodOfTime:hour withMinute:msgDateComponents.minute];
- if (hour > 12)
- {
- hour = hour - 12;
- }
-
- BOOL isSameMonth = (nowDateComponents.year == msgDateComponents.year) && (nowDateComponents.month == msgDateComponents.month);
-
- if(isSameMonth && (nowDateComponents.day == msgDateComponents.day)) //同一天,显示时间
- {
- result = [[NSString alloc] initWithFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute];
- }
- else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+1)))//昨天
- {
- result = showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天";
- }
- else if(isSameMonth && (nowDateComponents.day == (msgDateComponents.day+2))) //前天
- {
- result = showDetail? [[NSString alloc] initWithFormat:@"前天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"前天";
- }
- // else if([nowDate timeIntervalSinceDate:msgDate] < 7 * OnedayTimeIntervalValue)//一周内
- // {
- // NSString *weekDay = [NIMKitUtil weekdayStr:msgDateComponents.weekday];
- // result = showDetail? [weekDay stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : weekDay;
- // }
- else//显示日期
- {
- NSString *day = [NSString stringWithFormat:@"%zd-%zd-%zd", msgDateComponents.year, msgDateComponents.month, msgDateComponents.day];
- result = showDetail? [day stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute]:day;
- }
- return result;
- }
- #pragma mark - Private
- + (NSString *)getPeriodOfTime:(NSInteger)time withMinute:(NSInteger)minute
- {
- NSInteger totalMin = time *60 + minute;
- NSString *showPeriodOfTime = @"";
- if (totalMin > 0 && totalMin <= 5 * 60)
- {
- showPeriodOfTime = @"凌晨";
- }
- else if (totalMin > 5 * 60 && totalMin < 12 * 60)
- {
- showPeriodOfTime = @"上午";
- }
- else if (totalMin >= 12 * 60 && totalMin <= 18 * 60)
- {
- showPeriodOfTime = @"下午";
- }
- else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0)
- {
- showPeriodOfTime = @"晚上";
- }
- return showPeriodOfTime;
- }
- +(NSString*)weekdayStr:(NSInteger)dayOfWeek
- {
- static NSDictionary *daysOfWeekDict = nil;
- daysOfWeekDict = @{@(1):@"星期日",
- @(2):@"星期一",
- @(3):@"星期二",
- @(4):@"星期三",
- @(5):@"星期四",
- @(6):@"星期五",
- @(7):@"星期六",};
- return [daysOfWeekDict objectForKey:@(dayOfWeek)];
- }
- + (NSString *)messageTipContent:(NIMMessage *)message{
-
- NSString *text = nil;
-
- if (text == nil) {
- switch (message.messageType) {
- case NIMMessageTypeNotification:
- text = [NIMKitUtil notificationMessage:message];
- break;
- case NIMMessageTypeTip:
-
- text = message.text;
- break;
- default:
- break;
- }
- }
- return text;
- }
- + (NSString *)notificationMessage:(NIMMessage *)message{
- NIMNotificationObject *object = message.messageObject;
- switch (object.notificationType) {
- case NIMNotificationTypeTeam:{
- return [NIMKitUtil teamNotificationFormatedMessage:message];
- }
- case NIMNotificationTypeNetCall:{
- return [NIMKitUtil netcallNotificationFormatedMessage:message];
- }
- case NIMNotificationTypeChatroom:{
- return [NIMKitUtil chatroomNotificationFormatedMessage:message];
- }
- default:
- return @"";
- }
- }
- + (NSString*)teamNotificationFormatedMessage:(NIMMessage *)message{
- NSString *formatedMessage = @"";
- NIMNotificationObject *object = message.messageObject;
- if (object.notificationType == NIMNotificationTypeTeam)
- {
- NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;
- NSString *source = [NIMKitUtil teamNotificationSourceName:message];
- NSArray *targets = [NIMKitUtil teamNotificationTargetNames:message];
- NSString *targetText = [targets count] > 1 ? [targets componentsJoinedByString:@","] : [targets firstObject];
- NSString *teamName = [NIMKitUtil teamNotificationTeamShowName:message];
-
- switch (content.operationType) {
- case NIMTeamOperationTypeInvite:{
- NSString *str = [NSString stringWithFormat:@"%@邀请%@",source,targets.firstObject];
- if (targets.count>1) {
- str = [str stringByAppendingFormat:@"等%zd人",targets.count];
- }
- str = [str stringByAppendingFormat:@"进入了%@",teamName];
- formatedMessage = str;
- }
- break;
- case NIMTeamOperationTypeDismiss:
- formatedMessage = [NSString stringWithFormat:@"%@解散了%@",source,teamName];
- break;
- case NIMTeamOperationTypeKick:{
- NSString *str = [NSString stringWithFormat:@"%@将%@",source,targets.firstObject];
- if (targets.count>1) {
- str = [str stringByAppendingFormat:@"等%zd人",targets.count];
- }
- str = [str stringByAppendingFormat:@"移出了%@",teamName];
- formatedMessage = str;
- }
- break;
- case NIMTeamOperationTypeUpdate:
- {
- id attachment = [content attachment];
- if ([attachment isKindOfClass:[NIMUpdateTeamInfoAttachment class]]) {
- NIMUpdateTeamInfoAttachment *teamAttachment = (NIMUpdateTeamInfoAttachment *)attachment;
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@信息",source,teamName];
- //如果只是单个项目项被修改则显示具体的修改项
- if ([teamAttachment.values count] == 1) {
- NIMTeamUpdateTag tag = [[[teamAttachment.values allKeys] firstObject] integerValue];
- switch (tag) {
- case NIMTeamUpdateTagName:
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@名称",source,teamName];
- break;
- case NIMTeamUpdateTagIntro:
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@介绍",source,teamName];
- break;
- case NIMTeamUpdateTagAnouncement:
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@公告",source,teamName];
- break;
- case NIMTeamUpdateTagJoinMode:
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@验证方式",source,teamName];
- break;
- case NIMTeamUpdateTagAvatar:
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@头像",source,teamName];
- break;
- case NIMTeamUpdateTagInviteMode:
- formatedMessage = [NSString stringWithFormat:@"%@更新了邀请他人权限",source];
- break;
- case NIMTeamUpdateTagBeInviteMode:
- formatedMessage = [NSString stringWithFormat:@"%@更新了被邀请人身份验证权限",source];
- break;
- case NIMTeamUpdateTagUpdateInfoMode:
- formatedMessage = [NSString stringWithFormat:@"%@更新了群资料修改权限",source];
- break;
- case NIMTeamUpdateTagMuteMode:{
- NSString *muteState = teamAttachment.values.allValues.firstObject;
- BOOL muted = [muteState isEqualToString:@"0"] ? NO : YES;
- formatedMessage = muted? [NSString stringWithFormat:@"%@设置了群全体禁言",source]: [NSString stringWithFormat:@"%@取消了全体禁言",source];
- break;
- }
- default:
- break;
-
- }
- }
- }
- if (formatedMessage == nil){
- formatedMessage = [NSString stringWithFormat:@"%@更新了%@信息",source,teamName];
- }
- }
- break;
- case NIMTeamOperationTypeLeave:
- formatedMessage = [NSString stringWithFormat:@"%@离开了%@",source,teamName];
- break;
- case NIMTeamOperationTypeApplyPass:{
- if ([source isEqualToString:targetText]) {
- //说明是以不需要验证的方式进入
- formatedMessage = [NSString stringWithFormat:@"%@进入了%@",source,teamName];
- }else{
- formatedMessage = [NSString stringWithFormat:@"%@通过了%@的申请",source,targetText];
- }
- }
- break;
- case NIMTeamOperationTypeTransferOwner:
- formatedMessage = [NSString stringWithFormat:@"%@转移了群主身份给%@",source,targetText];
- break;
- case NIMTeamOperationTypeAddManager:
- formatedMessage = [NSString stringWithFormat:@"%@被添加为群管理员",targetText];
- break;
- case NIMTeamOperationTypeRemoveManager:
- formatedMessage = [NSString stringWithFormat:@"%@被撤销了群管理员身份",targetText];
- break;
- case NIMTeamOperationTypeAcceptInvitation:
- formatedMessage = [NSString stringWithFormat:@"%@接受%@的邀请进群",source,targetText];
- break;
- case NIMTeamOperationTypeMute:{
- id attachment = [content attachment];
- if ([attachment isKindOfClass:[NIMMuteTeamMemberAttachment class]])
- {
- BOOL mute = [(NIMMuteTeamMemberAttachment *)attachment flag];
- NSString *muteStr = mute? @"禁言" : @"解除禁言";
- NSString *str = [targets componentsJoinedByString:@","];
- formatedMessage = [NSString stringWithFormat:@"%@被%@%@",str,source,muteStr];
- }
- }
- break;
- default:
- break;
- }
-
- }
- if (!formatedMessage.length) {
- formatedMessage = [NSString stringWithFormat:@"未知系统消息"];
- }
- return formatedMessage;
- }
- + (NSString *)netcallNotificationFormatedMessage:(NIMMessage *)message{
- NIMNotificationObject *object = message.messageObject;
- NIMNetCallNotificationContent *content = (NIMNetCallNotificationContent *)object.content;
- NSString *text = @"";
- NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
- switch (content.eventType) {
- case NIMNetCallEventTypeMiss:{
- text = @"未接听";
- break;
- }
- case NIMNetCallEventTypeBill:{
- text = ([object.message.from isEqualToString:currentAccount])? @"通话拨打时长 " : @"通话接听时长 ";
- NSTimeInterval duration = content.duration;
- NSString *durationDesc = [NSString stringWithFormat:@"%02d:%02d",(int)duration/60,(int)duration%60];
- text = [text stringByAppendingString:durationDesc];
- break;
- }
- case NIMNetCallEventTypeReject:{
- text = ([object.message.from isEqualToString:currentAccount])? @"对方正忙" : @"已拒绝";
- break;
- }
- case NIMNetCallEventTypeNoResponse:{
- text = @"未接通,已取消";
- break;
- }
- default:
- break;
- }
- return text;
- }
- + (NSString *)chatroomNotificationFormatedMessage:(NIMMessage *)message{
- NIMNotificationObject *object = message.messageObject;
- NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)object.content;
- NSMutableArray *targetNicks = [[NSMutableArray alloc] init];
- for (NIMChatroomNotificationMember *memebr in content.targets) {
- if ([memebr.userId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]]) {
- [targetNicks addObject:@"你"];
- }else{
- [targetNicks addObject:memebr.nick];
- }
- }
- NSString *opeText = content.source.nick;
- NSString *targetText = [targetNicks componentsJoinedByString:@","];
- switch (content.eventType) {
- case NIMChatroomEventTypeEnter:
- {
- return [NSString stringWithFormat:@"欢迎%@进入直播间",targetText];
- }
- case NIMChatroomEventTypeAddBlack:
- {
- return [NSString stringWithFormat:@"%@被管理员拉入黑名单",targetText];
- }
- case NIMChatroomEventTypeRemoveBlack:
- {
- return [NSString stringWithFormat:@"%@被管理员解除拉黑",targetText];
- }
- case NIMChatroomEventTypeAddMute:
- {
- if (content.targets.count == 1 && [[content.targets.firstObject userId] isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]])
- {
- return @"你已被禁言";
- }
- else
- {
- return [NSString stringWithFormat:@"%@被管理员禁言",targetText];
- }
- }
- case NIMChatroomEventTypeRemoveMute:
- {
- return [NSString stringWithFormat:@"%@被管理员解除禁言",targetText];
- }
- case NIMChatroomEventTypeAddManager:
- {
- return [NSString stringWithFormat:@"%@被任命管理员身份",targetText];
- }
- case NIMChatroomEventTypeRemoveManager:
- {
- return [NSString stringWithFormat:@"%@被解除管理员身份",targetText];
- }
- case NIMChatroomEventTypeRemoveCommon:
- {
- return [NSString stringWithFormat:@"%@被解除直播室成员身份",targetText];
- }
- case NIMChatroomEventTypeAddCommon:
- {
- return [NSString stringWithFormat:@"%@被添加为直播室成员身份",targetText];
- }
- case NIMChatroomEventTypeInfoUpdated:
- {
- return [NSString stringWithFormat:@"直播间公告已更新"];
- }
- case NIMChatroomEventTypeKicked:
- {
- return [NSString stringWithFormat:@"%@被管理员移出直播间",targetText];
- }
- case NIMChatroomEventTypeExit:
- {
- return [NSString stringWithFormat:@"%@离开了直播间",targetText];
- }
- case NIMChatroomEventTypeClosed:
- {
- return [NSString stringWithFormat:@"直播间已关闭"];
- }
- case NIMChatroomEventTypeAddMuteTemporarily:
- {
- if (content.targets.count == 1 && [[content.targets.firstObject userId] isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]])
- {
- return @"你已被临时禁言";
- }
- else
- {
- return [NSString stringWithFormat:@"%@被管理员禁言",targetText];
- }
- }
- case NIMChatroomEventTypeRemoveMuteTemporarily:
- {
- return [NSString stringWithFormat:@"%@被管理员解除临时禁言",targetText];
- }
- case NIMChatroomEventTypeMemberUpdateInfo:
- {
- return [NSString stringWithFormat:@"%@更新了自己的个人信息",targetText];
- }
- case NIMChatroomEventTypeRoomMuted:
- {
- return @"全体禁言,管理员可发言";
- }
- case NIMChatroomEventTypeRoomUnMuted:
- {
- return @"解除全体禁言";
- }
- case NIMChatroomEventTypeQueueChange:
- case NIMChatroomEventTypeQueueBatchChange:
- return [NSString stringWithFormat:@"%@改变了聊天室队列",opeText];
- default:
- break;
- }
- return @"";
- }
- #pragma mark - Private
- + (NSString *)teamNotificationSourceName:(NIMMessage *)message{
- NSString *source;
- NIMNotificationObject *object = message.messageObject;
- NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;
- NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
- if ([content.sourceID isEqualToString:currentAccount]) {
- source = @"你";
- }else{
- source = [NIMKitUtil showNick:content.sourceID inSession:message.session];
- }
- return source;
- }
- + (NSArray *)teamNotificationTargetNames:(NIMMessage *)message{
- NSMutableArray *targets = [[NSMutableArray alloc] init];
- NIMNotificationObject *object = message.messageObject;
- NIMTeamNotificationContent *content = (NIMTeamNotificationContent*)object.content;
- NSString *currentAccount = [[NIMSDK sharedSDK].loginManager currentAccount];
- for (NSString *item in content.targetIDs) {
- if ([item isEqualToString:currentAccount]) {
- [targets addObject:@"你"];
- }else{
- NSString *targetShowName = [NIMKitUtil showNick:item inSession:message.session];
- [targets addObject:targetShowName];
- }
- }
- return targets;
- }
- + (NSString *)teamNotificationTeamShowName:(NIMMessage *)message{
- NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:message.session.sessionId];
- NSString *teamName = team.type == NIMTeamTypeNormal ? @"讨论组" : @"群";
- return teamName;
- }
- + (BOOL)canEditTeamInfo:(NIMTeamMember *)member
- {
- NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:member.teamId];
- if (team.updateInfoMode == NIMTeamUpdateInfoModeManager)
- {
- return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager;
- }
- else
- {
- return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager || member.type == NIMTeamMemberTypeNormal;
- }
- }
- + (BOOL)canInviteMember:(NIMTeamMember *)member
- {
- NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:member.teamId];
- if (team.inviteMode == NIMTeamInviteModeManager)
- {
- return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager;
- }
- else
- {
- return member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager || member.type == NIMTeamMemberTypeNormal;
- }
- }
- @end
|