WFCCQuoteInfo.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // WFCCQuoteInfo.m
  3. // WFChatClient
  4. //
  5. // Created by dali on 2020/10/4.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCQuoteInfo.h"
  9. #import "WFCCUserInfo.h"
  10. #import "WFCCMessage.h"
  11. #import "WFCCIMService.h"
  12. @implementation WFCCQuoteInfo
  13. - (instancetype)initWithMessage:(WFCCMessage *)msg {
  14. self = [super init];
  15. if (self) {
  16. if (msg.messageUid) {
  17. self.messageUid = msg.messageUid;
  18. self.userId = msg.fromUser;
  19. NSString *groupId = msg.conversation.type == Group_Type ? msg.conversation.target : nil;
  20. WFCCUserInfo *user = [[WFCCIMService sharedWFCIMService] getUserInfo:msg.fromUser inGroup:groupId refresh:NO];
  21. self.userDisplayName = user.displayName;
  22. if (user.groupAlias.length) {
  23. self.userDisplayName = user.groupAlias;
  24. }
  25. self.messageDigest = [msg.content digest:msg];
  26. NSUInteger maxBytes = 120;
  27. if (self.messageDigest.length > 48) {
  28. __block NSUInteger seenBytes = 0;
  29. __block NSUInteger truncLength = 0;
  30. NSRange fullLength = (NSRange){0, [self.messageDigest length]};
  31. [self.messageDigest enumerateSubstringsInRange:fullLength
  32. options:NSStringEnumerationByComposedCharacterSequences
  33. usingBlock:
  34. ^(NSString *substring, NSRange substringRange,
  35. NSRange _, BOOL *stop)
  36. {
  37. seenBytes += [substring lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  38. if( seenBytes > maxBytes ){
  39. *stop = YES;
  40. return;
  41. }
  42. else {
  43. truncLength += substringRange.length;
  44. }
  45. }];
  46. self.messageDigest = [self.messageDigest substringToIndex:truncLength];
  47. }
  48. } else {
  49. return nil;
  50. }
  51. }
  52. return self;
  53. }
  54. - (NSDictionary *)encode {
  55. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  56. [dict setObject:@(self.messageUid) forKey:@"u"];
  57. if (self.userId.length) {
  58. [dict setObject:self.userId forKey:@"i"];
  59. }
  60. if (self.userDisplayName.length) {
  61. [dict setObject:self.userDisplayName forKey:@"n"];
  62. }
  63. if (self.messageDigest.length) {
  64. [dict setObject:self.messageDigest forKey:@"d"];
  65. }
  66. return [dict copy];
  67. }
  68. - (void)decode:(NSDictionary *)dictData {
  69. if(dictData[@"messageUid"]) {
  70. self.messageUid = [dictData[@"messageUid"] longLongValue];
  71. self.userId = dictData[@"userId"];
  72. self.userDisplayName = dictData[@"userDisplayName"];
  73. self.messageDigest = dictData[@"messageDigest"];
  74. } else {
  75. self.messageUid = [dictData[@"u"] longLongValue];
  76. self.userId = dictData[@"i"];
  77. self.userDisplayName = dictData[@"n"];
  78. self.messageDigest = dictData[@"d"];
  79. }
  80. }
  81. -(id)toJsonObj {
  82. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  83. dict[@"userId"] = self.userId;
  84. dict[@"userDisplayName"] = self.userDisplayName;
  85. dict[@"messageDigest"] = self.messageDigest;
  86. [self setDict:dict key:@"messageUid" longlongValue:self.messageUid];
  87. return dict;
  88. }
  89. @end