WFCCQuitGroupNotificationContent.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // WFCCQuitGroupNotificationContent.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/9/20.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCQuitGroupNotificationContent.h"
  9. #import "WFCCIMService.h"
  10. #import "WFCCNetworkService.h"
  11. #import "Common.h"
  12. @implementation WFCCQuitGroupNotificationContent
  13. - (WFCCMessagePayload *)encode {
  14. WFCCMessagePayload *payload = [super encode];
  15. payload.contentType = [self.class getContentType];
  16. NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
  17. if (self.quitMember) {
  18. [dataDict setObject:self.quitMember forKey:@"o"];
  19. }
  20. if (self.groupId) {
  21. [dataDict setObject:self.groupId forKey:@"g"];
  22. }
  23. payload.binaryContent = [NSJSONSerialization dataWithJSONObject:dataDict
  24. options:kNilOptions
  25. error:nil];
  26. return payload;
  27. }
  28. - (void)decode:(WFCCMessagePayload *)payload {
  29. [super decode:payload];
  30. NSError *__error = nil;
  31. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:payload.binaryContent
  32. options:kNilOptions
  33. error:&__error];
  34. if (!__error) {
  35. self.quitMember = dictionary[@"o"];
  36. self.groupId = dictionary[@"g"];
  37. }
  38. }
  39. + (int)getContentType {
  40. return MESSAGE_CONTENT_TYPE_QUIT_GROUP;
  41. }
  42. + (int)getContentFlags {
  43. return WFCCPersistFlag_PERSIST;
  44. }
  45. + (void)load {
  46. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  47. }
  48. - (NSString *)digest:(WFCCMessage *)message {
  49. return [self formatNotification:message];
  50. }
  51. - (NSString *)formatNotification:(WFCCMessage *)message {
  52. NSString *formatMsg;
  53. if ([[WFCCNetworkService sharedInstance].userId isEqualToString:self.quitMember]) {
  54. formatMsg = @"你退出了群聊";
  55. } else {
  56. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.quitMember inGroup:self.groupId refresh:NO];
  57. if (userInfo.friendAlias.length > 0) {
  58. formatMsg = [NSString stringWithFormat:@"%@退出了群聊", userInfo.friendAlias];
  59. } else if(userInfo.groupAlias.length > 0) {
  60. formatMsg = [NSString stringWithFormat:@"%@退出了群聊", userInfo.groupAlias];
  61. } else if (userInfo.displayName.length > 0) {
  62. formatMsg = [NSString stringWithFormat:@"%@退出了群聊", userInfo.displayName];
  63. } else {
  64. formatMsg = [NSString stringWithFormat:@"用户<%@>退出了群聊", self.quitMember];
  65. }
  66. }
  67. return formatMsg;
  68. }
  69. @end