WFCUFavoriteItem.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // WFCUFavoriteItem.m
  3. // WFChatUIKit
  4. //
  5. // Created by Tom Lee on 2020/11/1.
  6. // Copyright © 2020 Wildfirechat. All rights reserved.
  7. //
  8. #import "WFCUFavoriteItem.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. @implementation WFCUFavoriteItem
  11. + (WFCUFavoriteItem *)itemFromMessage:(WFCCMessage *)message {
  12. WFCUFavoriteItem *item = [[WFCUFavoriteItem alloc] init];
  13. item.messageUid = message.messageUid;
  14. item.conversation = message.conversation;
  15. item.timestamp = message.serverTime;
  16. item.sender = message.fromUser;
  17. if(message.conversation.type == Single_Type) {
  18. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:message.conversation.target refresh:NO];
  19. item.origin = userInfo.displayName;
  20. } else if(message.conversation.type == Group_Type) {
  21. WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:message.conversation.target refresh:NO];
  22. item.origin = groupInfo.displayName;
  23. } else if(message.conversation.type == Channel_Type) {
  24. WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:message.conversation.target refresh:NO];
  25. item.origin = channelInfo.name;
  26. } else if(message.conversation.type == SecretChat_Type) {
  27. NSString *userId = [[WFCCIMService sharedWFCIMService] getSecretChatInfo:message.conversation.target].userId;
  28. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId refresh:NO];
  29. item.origin = userInfo.displayName;
  30. }
  31. WFCCMessageContent *content = message.content;
  32. if ([content isKindOfClass:[WFCCTextMessageContent class]]) {
  33. WFCCTextMessageContent *textContent = (WFCCTextMessageContent *)content;
  34. item.favType = MESSAGE_CONTENT_TYPE_TEXT;
  35. item.title = textContent.text;
  36. } else if ([content isKindOfClass:[WFCCSoundMessageContent class]]) {
  37. WFCCSoundMessageContent *soundContent = (WFCCSoundMessageContent *)content;
  38. item.favType = MESSAGE_CONTENT_TYPE_SOUND;
  39. item.url = soundContent.remoteUrl;
  40. NSDictionary *dict = @{@"duration":@(soundContent.duration)};
  41. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  42. options:kNilOptions
  43. error:nil];
  44. item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  45. } else if ([content isKindOfClass:[WFCCImageMessageContent class]]) {
  46. WFCCImageMessageContent *imgContent = (WFCCImageMessageContent *)content;
  47. item.favType = MESSAGE_CONTENT_TYPE_IMAGE;
  48. item.url = imgContent.remoteUrl;
  49. NSData *thumbData = UIImageJPEGRepresentation(imgContent.thumbnail, 0.45);
  50. NSDictionary *dict = @{@"thumb":[thumbData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]};
  51. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  52. options:kNilOptions
  53. error:nil];
  54. item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  55. } else if ([content isKindOfClass:[WFCCVideoMessageContent class]]) {
  56. WFCCVideoMessageContent *imgContent = (WFCCVideoMessageContent *)content;
  57. item.favType = MESSAGE_CONTENT_TYPE_VIDEO;
  58. item.url = imgContent.remoteUrl;
  59. NSData *thumbData = UIImageJPEGRepresentation(imgContent.thumbnail, 0.45);
  60. NSDictionary *dict = @{@"thumb":[thumbData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed], @"duration":@(imgContent.duration)};
  61. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  62. options:kNilOptions
  63. error:nil];
  64. item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  65. } else if ([content isKindOfClass:[WFCCLocationMessageContent class]]) {
  66. WFCCLocationMessageContent *locContent = (WFCCLocationMessageContent *)content;
  67. item.favType = MESSAGE_CONTENT_TYPE_LOCATION;
  68. item.title = locContent.title;
  69. NSData *thumbData = UIImageJPEGRepresentation(locContent.thumbnail, 0.45);
  70. NSDictionary *dict = @{@"thumb":[thumbData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed], @"long":@(locContent.coordinate.longitude), @"lat":@(locContent.coordinate.latitude)};
  71. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  72. options:kNilOptions
  73. error:nil];
  74. item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  75. } else if ([content isKindOfClass:[WFCCLinkMessageContent class]]) {
  76. WFCCLinkMessageContent *linkContent = (WFCCLinkMessageContent *)content;
  77. item.favType = MESSAGE_CONTENT_TYPE_LINK;
  78. item.title = linkContent.title;
  79. item.thumbUrl = linkContent.thumbnailUrl;
  80. item.url = linkContent.url;
  81. } else if ([content isKindOfClass:[WFCCCompositeMessageContent class]]) {
  82. WFCCCompositeMessageContent *compositeContent = (WFCCCompositeMessageContent *)content;
  83. item.favType = MESSAGE_CONTENT_TYPE_COMPOSITE_MESSAGE;
  84. item.title = compositeContent.title;
  85. WFCCMessagePayload *payload = [compositeContent encode];
  86. if (compositeContent.remoteUrl.length) {
  87. NSError *__error = nil;
  88. NSMutableDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:payload.binaryContent
  89. options:kNilOptions
  90. error:&__error] mutableCopy];
  91. [dictionary setObject:compositeContent.remoteUrl forKey:@"remote_url"];
  92. payload.binaryContent = [NSJSONSerialization dataWithJSONObject:dictionary
  93. options:kNilOptions
  94. error:nil];
  95. }
  96. item.data = [payload.binaryContent base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
  97. } else if ([content isKindOfClass:[WFCCFileMessageContent class]]) {
  98. WFCCFileMessageContent *fileContent = (WFCCFileMessageContent *)content;
  99. item.favType = MESSAGE_CONTENT_TYPE_FILE;
  100. item.title = fileContent.name;
  101. item.url = fileContent.remoteUrl;
  102. NSDictionary *dict = @{@"size":@(fileContent.size)};
  103. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  104. options:kNilOptions
  105. error:nil];
  106. item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  107. } else {
  108. NSLog(@"Error, not implement!!!!");
  109. return nil;
  110. }
  111. return item;
  112. }
  113. - (WFCCMessage *)toMessage {
  114. WFCCMessage *msg = [[WFCCMessage alloc] init];
  115. msg.conversation = self.conversation;
  116. msg.fromUser = self.sender;
  117. msg.serverTime = self.timestamp;
  118. switch (self.favType) {
  119. case MESSAGE_CONTENT_TYPE_TEXT:
  120. {
  121. msg.content = [WFCCTextMessageContent contentWith:self.title];
  122. break;
  123. }
  124. case MESSAGE_CONTENT_TYPE_SOUND:
  125. {
  126. WFCCSoundMessageContent *soundCnt = [[WFCCSoundMessageContent alloc] init];
  127. soundCnt.remoteUrl = self.url;
  128. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  129. soundCnt.duration = [dict[@"duration"] intValue];
  130. msg.content = soundCnt;
  131. break;
  132. }
  133. case MESSAGE_CONTENT_TYPE_IMAGE:
  134. {
  135. WFCCImageMessageContent *imageCnt = [[WFCCImageMessageContent alloc] init];
  136. imageCnt.remoteUrl = self.url;
  137. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  138. NSString *thumbStr = dict[@"thumb"];
  139. NSData *thumbData = [[NSData alloc] initWithBase64EncodedString:thumbStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
  140. imageCnt.thumbnail = [UIImage imageWithData:thumbData];
  141. msg.content = imageCnt;
  142. break;
  143. }
  144. case MESSAGE_CONTENT_TYPE_VIDEO:
  145. {
  146. WFCCVideoMessageContent *videoCnt = [[WFCCVideoMessageContent alloc] init];
  147. videoCnt.remoteUrl = self.url;
  148. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  149. NSString *thumbStr = dict[@"thumb"];
  150. NSData *thumbData = [[NSData alloc] initWithBase64EncodedString:thumbStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
  151. videoCnt.thumbnail = [UIImage imageWithData:thumbData];
  152. videoCnt.duration = [dict[@"duration"] intValue];
  153. msg.content = videoCnt;
  154. break;
  155. }
  156. case MESSAGE_CONTENT_TYPE_LOCATION:
  157. {
  158. WFCCLocationMessageContent *locationCnt = [[WFCCLocationMessageContent alloc] init];
  159. locationCnt.title = self.title;
  160. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  161. NSString *thumbStr = dict[@"thumb"];
  162. NSData *thumbData = [[NSData alloc] initWithBase64EncodedString:thumbStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
  163. locationCnt.thumbnail = [UIImage imageWithData:thumbData];
  164. double latitude = [dict[@"lat"] doubleValue];
  165. double longitude = [dict[@"long"] doubleValue];
  166. locationCnt.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  167. msg.content = locationCnt;
  168. break;
  169. }
  170. case MESSAGE_CONTENT_TYPE_LINK:
  171. {
  172. WFCCLinkMessageContent *linkCnt = [[WFCCLinkMessageContent alloc] init];
  173. linkCnt.url = self.url;
  174. linkCnt.thumbnailUrl = self.thumbUrl;
  175. linkCnt.title = self.title;
  176. msg.content = linkCnt;
  177. break;
  178. }
  179. case MESSAGE_CONTENT_TYPE_COMPOSITE_MESSAGE:
  180. {
  181. WFCCCompositeMessageContent *compositeCnt = [[WFCCCompositeMessageContent alloc] init];
  182. compositeCnt.title = self.title;
  183. NSData *binaryData = [[NSData alloc] initWithBase64EncodedString:self.data options:NSDataBase64DecodingIgnoreUnknownCharacters];
  184. NSError *__error = nil;
  185. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:binaryData
  186. options:kNilOptions
  187. error:&__error];
  188. NSString *remoteUrl = dictionary[@"remote_url"];
  189. WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
  190. payload.binaryContent = binaryData;
  191. payload.remoteMediaUrl = remoteUrl;
  192. [compositeCnt decode:payload];
  193. msg.content = compositeCnt;
  194. break;
  195. }
  196. case MESSAGE_CONTENT_TYPE_FILE:
  197. {
  198. WFCCFileMessageContent *fileCnt = [[WFCCFileMessageContent alloc] init];
  199. fileCnt.remoteUrl = self.url;
  200. fileCnt.name = self.title;
  201. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  202. fileCnt.size = [dict[@"size"] intValue];
  203. msg.content = fileCnt;
  204. break;
  205. }
  206. default:
  207. {
  208. WFCCUnknownMessageContent *unknownCnt = [[WFCCUnknownMessageContent alloc] init];
  209. unknownCnt.orignalType = self.favType;
  210. msg.content = unknownCnt;
  211. break;
  212. }
  213. }
  214. return msg;
  215. }
  216. @end