NTESSessionUtil.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //
  2. // NTESSessionUtil.m
  3. // NIMDemo
  4. //
  5. // Created by ght on 15-1-27.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NTESSessionUtil.h"
  9. //#import "NTESLoginManager.h"
  10. //#import "NTESSnapchatAttachment.h"
  11. //#import "NTESJanKenPonAttachment.h"
  12. //#import "NTESChartletAttachment.h"
  13. #import "UIImage+NTES.h"
  14. #import "NIMKit.h"
  15. //#import "NTESSnapchatAttachment.h"
  16. //#import "NTESWhiteboardAttachment.h"
  17. //#import "NIMKitInfoFetchOption.h"
  18. //#import "NTESSubscribeManager.h"
  19. //#import "NIMExtensionHelper.h"
  20. //#import "NTESSubscribeDefine.h"
  21. #import "NSDictionary+NTESJson.h"
  22. #import "NTESDevice.h"
  23. double OnedayTimeIntervalValue = 24*60*60; //一天的秒数
  24. static NSString *const NTESRecentSessionAtMark = @"NTESRecentSessionAtMark";
  25. static NSString *const NTESRecentSessionTopMark = @"NTESRecentSessionTopMark";
  26. @implementation NTESSessionUtil
  27. + (CGSize)getImageSizeWithImageOriginSize:(CGSize)originSize
  28. minSize:(CGSize)imageMinSize
  29. maxSize:(CGSize)imageMaxSiz
  30. {
  31. CGSize size;
  32. NSInteger imageWidth = originSize.width ,imageHeight = originSize.height;
  33. NSInteger imageMinWidth = imageMinSize.width, imageMinHeight = imageMinSize.height;
  34. NSInteger imageMaxWidth = imageMaxSiz.width, imageMaxHeight = imageMaxSiz.height;
  35. if (imageWidth > imageHeight) //宽图
  36. {
  37. size.height = imageMinHeight; //高度取最小高度
  38. size.width = imageWidth * imageMinHeight / imageHeight;
  39. if (size.width > imageMaxWidth)
  40. {
  41. size.width = imageMaxWidth;
  42. }
  43. }
  44. else if(imageWidth < imageHeight)//高图
  45. {
  46. size.width = imageMinWidth;
  47. size.height = imageHeight *imageMinWidth / imageWidth;
  48. if (size.height > imageMaxHeight)
  49. {
  50. size.height = imageMaxHeight;
  51. }
  52. }
  53. else//方图
  54. {
  55. if (imageWidth > imageMaxWidth)
  56. {
  57. size.width = imageMaxWidth;
  58. size.height = imageMaxHeight;
  59. }
  60. else if(imageWidth > imageMinWidth)
  61. {
  62. size.width = imageWidth;
  63. size.height = imageHeight;
  64. }
  65. else
  66. {
  67. size.width = imageMinWidth;
  68. size.height = imageMinHeight;
  69. }
  70. }
  71. return size;
  72. }
  73. +(BOOL)isTheSameDay:(NSTimeInterval)currentTime compareTime:(NSDateComponents*)older
  74. {
  75. NSCalendarUnit currentComponents = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour | NSCalendarUnitMinute);
  76. NSDateComponents *current = [[NSCalendar currentCalendar] components:currentComponents fromDate:[NSDate dateWithTimeIntervalSinceNow:currentTime]];
  77. return current.year == older.year && current.month == older.month && current.day == older.day;
  78. }
  79. +(NSString*)weekdayStr:(NSInteger)dayOfWeek
  80. {
  81. static NSDictionary *daysOfWeekDict = nil;
  82. daysOfWeekDict = @{@(1):@"星期日",
  83. @(2):@"星期一",
  84. @(3):@"星期二",
  85. @(4):@"星期三",
  86. @(5):@"星期四",
  87. @(6):@"星期五",
  88. @(7):@"星期六",};
  89. return [daysOfWeekDict objectForKey:@(dayOfWeek)];
  90. }
  91. +(NSDateComponents*)stringFromTimeInterval:(NSTimeInterval)messageTime components:(NSCalendarUnit)components
  92. {
  93. NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:components fromDate:[NSDate dateWithTimeIntervalSince1970:messageTime]];
  94. return dateComponents;
  95. }
  96. + (NSString *)showNick:(NSString*)uid inSession:(NIMSession*)session{
  97. NSString *nickname = nil;
  98. if (session.sessionType == NIMSessionTypeTeam)
  99. {
  100. NIMTeamMember *member = [[NIMSDK sharedSDK].teamManager teamMember:uid inTeam:session.sessionId];
  101. nickname = member.nickname;
  102. }
  103. if (!nickname.length) {
  104. NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:uid option:nil];
  105. nickname = info.showName;
  106. }
  107. return nickname;
  108. }
  109. +(NSString*)showTime:(NSTimeInterval) msglastTime showDetail:(BOOL)showDetail
  110. {
  111. //今天的时间
  112. NSDate * nowDate = [NSDate date];
  113. NSDate * msgDate = [NSDate dateWithTimeIntervalSince1970:msglastTime];
  114. NSString *result = nil;
  115. NSCalendarUnit components = (NSCalendarUnit)(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitHour | NSCalendarUnitMinute);
  116. NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar] components:components fromDate:nowDate];
  117. NSDateComponents *msgDateComponents = [[NSCalendar currentCalendar] components:components fromDate:msgDate];
  118. NSInteger hour = msgDateComponents.hour;
  119. result = [NTESSessionUtil getPeriodOfTime:hour withMinute:msgDateComponents.minute];
  120. if (hour > 12)
  121. {
  122. hour = hour - 12;
  123. }
  124. if(nowDateComponents.day == msgDateComponents.day) //同一天,显示时间
  125. {
  126. result = [[NSString alloc] initWithFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute];
  127. }
  128. else if(nowDateComponents.day == (msgDateComponents.day+1))//昨天
  129. {
  130. result = showDetail? [[NSString alloc] initWithFormat:@"昨天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"昨天";
  131. }
  132. else if(nowDateComponents.day == (msgDateComponents.day+2)) //前天
  133. {
  134. result = showDetail? [[NSString alloc] initWithFormat:@"前天%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : @"前天";
  135. }
  136. else if([nowDate timeIntervalSinceDate:msgDate] < 7 * OnedayTimeIntervalValue)//一周内
  137. {
  138. NSString *weekDay = [NTESSessionUtil weekdayStr:msgDateComponents.weekday];
  139. result = showDetail? [weekDay stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute] : weekDay;
  140. }
  141. else//显示日期
  142. {
  143. NSString *day = [NSString stringWithFormat:@"%zd-%zd-%zd", msgDateComponents.year, msgDateComponents.month, msgDateComponents.day];
  144. result = showDetail? [day stringByAppendingFormat:@"%@ %zd:%02d",result,hour,(int)msgDateComponents.minute]:day;
  145. }
  146. return result;
  147. }
  148. + (NSString *)getPeriodOfTime:(NSInteger)time withMinute:(NSInteger)minute
  149. {
  150. NSInteger totalMin = time *60 + minute;
  151. NSString *showPeriodOfTime = @"";
  152. if (totalMin > 0 && totalMin <= 5 * 60)
  153. {
  154. showPeriodOfTime = @"凌晨";
  155. }
  156. else if (totalMin > 5 * 60 && totalMin < 12 * 60)
  157. {
  158. showPeriodOfTime = @"上午";
  159. }
  160. else if (totalMin >= 12 * 60 && totalMin <= 18 * 60)
  161. {
  162. showPeriodOfTime = @"下午";
  163. }
  164. else if ((totalMin > 18 * 60 && totalMin <= (23 * 60 + 59)) || totalMin == 0)
  165. {
  166. showPeriodOfTime = @"晚上";
  167. }
  168. return showPeriodOfTime;
  169. }
  170. + (void)sessionWithInputURL:(NSURL*)inputURL
  171. outputURL:(NSURL*)outputURL
  172. blockHandler:(void (^)(AVAssetExportSession*))handler
  173. {
  174. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
  175. AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset
  176. presetName:AVAssetExportPresetMediumQuality];
  177. session.outputURL = outputURL;
  178. session.outputFileType = AVFileTypeMPEG4; // 支持安卓某些机器的视频播放
  179. session.shouldOptimizeForNetworkUse = YES;
  180. [session exportAsynchronouslyWithCompletionHandler:^(void)
  181. {
  182. handler(session);
  183. }];
  184. }
  185. + (NSDictionary *)dictByJsonData:(NSData *)data
  186. {
  187. NSDictionary *dict = nil;
  188. if ([data isKindOfClass:[NSData class]])
  189. {
  190. NSError *error = nil;
  191. dict = [NSJSONSerialization JSONObjectWithData:data
  192. options:0
  193. error:&error];
  194. if (error) {
  195. NSLog(@"dictByJsonData failed %@ error %@",data,error);
  196. }
  197. }
  198. return [dict isKindOfClass:[NSDictionary class]] ? dict : nil;
  199. }
  200. + (NSDictionary *)dictByJsonString:(NSString *)jsonString
  201. {
  202. if (!jsonString.length) {
  203. return nil;
  204. }
  205. NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  206. return [NTESSessionUtil dictByJsonData:data];
  207. }
  208. + (NSString *)tipOnMessageRevoked:(NIMRevokeMessageNotification *)notificaton
  209. {
  210. NSString *fromUid = nil;
  211. NIMSession *session = nil;
  212. NSString *tip = @"";
  213. BOOL isFromMe = NO;
  214. if([notificaton isKindOfClass:[NIMRevokeMessageNotification class]])
  215. {
  216. fromUid = [notificaton messageFromUserId];
  217. session = [notificaton session];
  218. isFromMe = [fromUid isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]];
  219. }
  220. else if(!notificaton)
  221. {
  222. isFromMe = YES;
  223. }
  224. else
  225. {
  226. assert(0);
  227. }
  228. if (isFromMe)
  229. {
  230. tip = @"你";
  231. }
  232. else{
  233. switch (session.sessionType) {
  234. case NIMSessionTypeP2P:
  235. tip = @"对方";
  236. break;
  237. case NIMSessionTypeTeam:{
  238. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:session.sessionId];
  239. NIMTeamMember *member = [[NIMSDK sharedSDK].teamManager teamMember:fromUid inTeam:session.sessionId];
  240. if ([fromUid isEqualToString:team.owner])
  241. {
  242. tip = @"群主";
  243. }
  244. else if(member.type == NIMTeamMemberTypeManager)
  245. {
  246. tip = @"管理员";
  247. }
  248. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  249. option.session = session;
  250. NIMKitInfo *info = [[NIMKit sharedKit] infoByUser:fromUid option:option];
  251. tip = [tip stringByAppendingString:info.showName];
  252. }
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. return [NSString stringWithFormat:@"%@撤回了一条消息",tip];
  259. }
  260. //+ (BOOL)canMessageBeForwarded:(NIMMessage *)message
  261. //{
  262. // if (!message.isReceivedMsg && message.deliveryState == NIMMessageDeliveryStateFailed) {
  263. // return NO;
  264. // }
  265. // id<NIMMessageObject> messageObject = message.messageObject;
  266. // if ([messageObject isKindOfClass:[NIMCustomObject class]])
  267. // {
  268. // id<NTESCustomAttachmentInfo> attach = (id<NTESCustomAttachmentInfo>)[(NIMCustomObject *)message.messageObject attachment];
  269. // return [attach canBeForwarded];
  270. // }
  271. // if ([messageObject isKindOfClass:[NIMNotificationObject class]]) {
  272. // return NO;
  273. // }
  274. // if ([messageObject isKindOfClass:[NIMTipObject class]]) {
  275. // return NO;
  276. // }
  277. // if ([messageObject isKindOfClass:[NIMRobotObject class]]) {
  278. // NIMRobotObject *robotObject = (NIMRobotObject *)messageObject;
  279. // return !robotObject.isFromRobot;
  280. // }
  281. // return YES;
  282. //}
  283. //
  284. //+ (BOOL)canMessageBeRevoked:(NIMMessage *)message
  285. //{
  286. // BOOL canRevokeMessageByRole = [self canRevokeMessageByRole:message];
  287. // BOOL isDeliverFailed = !message.isReceivedMsg && message.deliveryState == NIMMessageDeliveryStateFailed;
  288. // if (!canRevokeMessageByRole || isDeliverFailed) {
  289. // return NO;
  290. // }
  291. // id<NIMMessageObject> messageObject = message.messageObject;
  292. // if ([messageObject isKindOfClass:[NIMTipObject class]]
  293. // || [messageObject isKindOfClass:[NIMNotificationObject class]]) {
  294. // return NO;
  295. // }
  296. // if ([messageObject isKindOfClass:[NIMCustomObject class]])
  297. // {
  298. // id<NTESCustomAttachmentInfo> attach = (id<NTESCustomAttachmentInfo>)[(NIMCustomObject *)message.messageObject attachment];
  299. // return [attach canBeRevoked];
  300. // }
  301. // return YES;
  302. //}
  303. + (BOOL)canRevokeMessageByRole:(NIMMessage *)message
  304. {
  305. BOOL isFromMe = [message.from isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]];
  306. BOOL isToMe = [message.session.sessionId isEqualToString:[[NIMSDK sharedSDK].loginManager currentAccount]];
  307. BOOL isTeamManager = NO;
  308. if (message.session.sessionType == NIMSessionTypeTeam)
  309. {
  310. NIMTeamMember *member = [[NIMSDK sharedSDK].teamManager teamMember:[NIMSDK sharedSDK].loginManager.currentAccount inTeam:message.session.sessionId];
  311. isTeamManager = member.type == NIMTeamMemberTypeOwner || member.type == NIMTeamMemberTypeManager;
  312. }
  313. BOOL isRobotMessage = NO;
  314. id<NIMMessageObject> messageObject = message.messageObject;
  315. if ([messageObject isKindOfClass:[NIMRobotObject class]]) {
  316. NIMRobotObject *robotObject = (NIMRobotObject *)messageObject;
  317. isRobotMessage = robotObject.isFromRobot;
  318. }
  319. //我发出去的消息并且不是发给我的电脑的消息并且不是机器人的消息,可以撤回
  320. //群消息里如果我是管理员可以撤回以上所有消息
  321. return (isFromMe && !isToMe && !isRobotMessage) || isTeamManager;
  322. }
  323. + (void)addRecentSessionMark:(NIMSession *)session type:(NTESRecentSessionMarkType)type
  324. {
  325. NIMRecentSession *recent = [[NIMSDK sharedSDK].conversationManager recentSessionBySession:session];
  326. if (recent)
  327. {
  328. NSDictionary *localExt = recent.localExt?:@{};
  329. NSMutableDictionary *dict = [localExt mutableCopy];
  330. NSString *key = [NTESSessionUtil keyForMarkType:type];
  331. [dict setObject:@(YES) forKey:key];
  332. [[NIMSDK sharedSDK].conversationManager updateRecentLocalExt:dict recentSession:recent];
  333. }
  334. }
  335. + (void)removeRecentSessionMark:(NIMSession *)session type:(NTESRecentSessionMarkType)type
  336. {
  337. NIMRecentSession *recent = [[NIMSDK sharedSDK].conversationManager recentSessionBySession:session];
  338. if (recent) {
  339. NSMutableDictionary *localExt = [recent.localExt mutableCopy];
  340. NSString *key = [NTESSessionUtil keyForMarkType:type];
  341. [localExt removeObjectForKey:key];
  342. [[NIMSDK sharedSDK].conversationManager updateRecentLocalExt:localExt recentSession:recent];
  343. }
  344. }
  345. + (BOOL)recentSessionIsMark:(NIMRecentSession *)recent type:(NTESRecentSessionMarkType)type
  346. {
  347. NSDictionary *localExt = recent.localExt;
  348. NSString *key = [NTESSessionUtil keyForMarkType:type];
  349. return [localExt[key] boolValue] == YES;
  350. }
  351. + (NSString *)keyForMarkType:(NTESRecentSessionMarkType)type
  352. {
  353. static NSDictionary *keys;
  354. static dispatch_once_t onceToken;
  355. dispatch_once(&onceToken, ^{
  356. keys = @{
  357. @(NTESRecentSessionMarkTypeAt) : NTESRecentSessionAtMark,
  358. @(NTESRecentSessionMarkTypeTop) : NTESRecentSessionTopMark
  359. };
  360. });
  361. return [keys objectForKey:@(type)];
  362. }
  363. //+ (NSString *)onlineState:(NSString *)userId detail:(BOOL)detail
  364. //{
  365. // NSString *state = @"";
  366. // if (![NTESSubscribeManager sharedManager] || [[NIMSDK sharedSDK].loginManager.currentAccount isEqualToString:userId])
  367. // {
  368. // //没有开启订阅服务或是自己 不显示在线状态
  369. // return state;
  370. // }
  371. // if ([[NIMSDK sharedSDK].robotManager isValidRobot:userId]) {
  372. // return @"在线";
  373. // }
  374. //
  375. // NSDictionary *dict = [[NTESSubscribeManager sharedManager] eventsForType:NIMSubscribeSystemEventTypeOnline];
  376. // NIMSubscribeEvent *event = [dict objectForKey:userId];
  377. // NIMSubscribeOnlineInfo *info = event.subscribeInfo;
  378. // if ([info isKindOfClass:[NIMSubscribeOnlineInfo class]] && info.senderClientTypes.count)
  379. // {
  380. // NIMLoginClientType client = [self resolveShowClientType:info.senderClientTypes];
  381. //
  382. // switch (event.value) {
  383. // case NTESCustomStateValueOnlineExt:
  384. // case NIMSubscribeEventOnlineValueLogin:
  385. // case NIMSubscribeEventOnlineValueLogout:
  386. // case NIMSubscribeEventOnlineValueDisconnected:
  387. // {
  388. // NSString *ext = [event ext:client];
  389. // state = [self resolveOnlineState:ext client:client detail:detail];
  390. // }
  391. // break;
  392. //
  393. // default:
  394. // {
  395. // NSString *clientName = [self resolveOnlineClientName:client];
  396. // state = [NSString stringWithFormat:@"%@在线",clientName];
  397. // break;
  398. // }
  399. // }
  400. // }
  401. // else
  402. // {
  403. // state = @"离线";
  404. // }
  405. // return state;
  406. //}
  407. + (NIMLoginClientType)resolveShowClientType:(NSArray *)senderClientTypes
  408. {
  409. NSArray *clients = @[@(NIMLoginClientTypePC),@(NIMLoginClientTypemacOS),@(NIMLoginClientTypeiOS),@(NIMLoginClientTypeAOS),@(NIMLoginClientTypeWeb),@(NIMLoginClientTypeWP)]; //显示优先级
  410. for (NSNumber *type in clients) {
  411. NIMLoginClientType client = type.integerValue;
  412. if ([senderClientTypes containsObject:@(client)]) {
  413. return client;
  414. }
  415. }
  416. return NIMLoginClientTypeUnknown;
  417. }
  418. + (NSString *)resolveOnlineClientName:(NIMLoginClientType )client
  419. {
  420. NSDictionary *formats = @{
  421. @(NIMLoginClientTypePC) : @"PC",
  422. @(NIMLoginClientTypemacOS) : @"Mac",
  423. @(NIMLoginClientTypeiOS): @"iOS",
  424. @(NIMLoginClientTypeAOS): @"Android",
  425. @(NIMLoginClientTypeWeb): @"Web",
  426. @(NIMLoginClientTypeWP) : @"WP"
  427. };
  428. NSString *format = [formats objectForKey:@(client)];
  429. return format? format : @"";
  430. }
  431. //+ (void)addRecentSessionMark:(NIMSession *)session type:(NTESRecentSessionMarkType)type
  432. //{
  433. // NIMRecentSession *recent = [[NIMSDK sharedSDK].conversationManager recentSessionBySession:session];
  434. // if (recent)
  435. // {
  436. // NSDictionary *localExt = recent.localExt?:@{};
  437. // NSMutableDictionary *dict = [localExt mutableCopy];
  438. // NSString *key = [NTESSessionUtil keyForMarkType:type];
  439. // [dict setObject:@(YES) forKey:key];
  440. // [[NIMSDK sharedSDK].conversationManager updateRecentLocalExt:dict recentSession:recent];
  441. // }
  442. //
  443. //
  444. //}
  445. + (NSString *)formatAutoLoginMessage:(NSError *)error
  446. {
  447. NSString *message = [NSString stringWithFormat:@"自动登录失败 %@",error];
  448. NSString *domain = error.domain;
  449. NSInteger code = error.code;
  450. if ([domain isEqualToString:NIMLocalErrorDomain])
  451. {
  452. if (code == NIMLocalErrorCodeAutoLoginRetryLimit)
  453. {
  454. message = @"自动登录错误次数超限,请检查网络后重试";
  455. }
  456. }
  457. else if([domain isEqualToString:NIMRemoteErrorDomain])
  458. {
  459. if (code == NIMRemoteErrorCodeInvalidPass)
  460. {
  461. message = @"密码错误";
  462. }
  463. else if(code == NIMRemoteErrorCodeExist)
  464. {
  465. message = @"当前已经其他设备登录,请使用手动模式登录";
  466. }
  467. }
  468. return message;
  469. }
  470. @end