123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- //
- // WFCUFavoriteItem.m
- // WFChatUIKit
- //
- // Created by Tom Lee on 2020/11/1.
- // Copyright © 2020 Wildfirechat. All rights reserved.
- //
- #import "WFCUFavoriteItem.h"
- #import <WFChatClient/WFCChatClient.h>
- @implementation WFCUFavoriteItem
- + (WFCUFavoriteItem *)itemFromMessage:(WFCCMessage *)message {
- WFCUFavoriteItem *item = [[WFCUFavoriteItem alloc] init];
- item.messageUid = message.messageUid;
- item.conversation = message.conversation;
- item.timestamp = message.serverTime;
- item.sender = message.fromUser;
-
- if(message.conversation.type == Single_Type) {
- WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:message.conversation.target refresh:NO];
- item.origin = userInfo.displayName;
- } else if(message.conversation.type == Group_Type) {
- WFCCGroupInfo *groupInfo = [[WFCCIMService sharedWFCIMService] getGroupInfo:message.conversation.target refresh:NO];
- item.origin = groupInfo.displayName;
- } else if(message.conversation.type == Channel_Type) {
- WFCCChannelInfo *channelInfo = [[WFCCIMService sharedWFCIMService] getChannelInfo:message.conversation.target refresh:NO];
- item.origin = channelInfo.name;
- } else if(message.conversation.type == SecretChat_Type) {
- NSString *userId = [[WFCCIMService sharedWFCIMService] getSecretChatInfo:message.conversation.target].userId;
- WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:userId refresh:NO];
- item.origin = userInfo.displayName;
- }
-
- WFCCMessageContent *content = message.content;
- if ([content isKindOfClass:[WFCCTextMessageContent class]]) {
- WFCCTextMessageContent *textContent = (WFCCTextMessageContent *)content;
-
- item.favType = MESSAGE_CONTENT_TYPE_TEXT;
- item.title = textContent.text;
- } else if ([content isKindOfClass:[WFCCSoundMessageContent class]]) {
- WFCCSoundMessageContent *soundContent = (WFCCSoundMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_SOUND;
- item.url = soundContent.remoteUrl;
- NSDictionary *dict = @{@"duration":@(soundContent.duration)};
- NSData *data = [NSJSONSerialization dataWithJSONObject:dict
- options:kNilOptions
- error:nil];
- item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- } else if ([content isKindOfClass:[WFCCImageMessageContent class]]) {
- WFCCImageMessageContent *imgContent = (WFCCImageMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_IMAGE;
- item.url = imgContent.remoteUrl;
- NSData *thumbData = UIImageJPEGRepresentation(imgContent.thumbnail, 0.45);
- NSDictionary *dict = @{@"thumb":[thumbData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]};
- NSData *data = [NSJSONSerialization dataWithJSONObject:dict
- options:kNilOptions
- error:nil];
- item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- } else if ([content isKindOfClass:[WFCCVideoMessageContent class]]) {
- WFCCVideoMessageContent *imgContent = (WFCCVideoMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_VIDEO;
- item.url = imgContent.remoteUrl;
- NSData *thumbData = UIImageJPEGRepresentation(imgContent.thumbnail, 0.45);
- NSDictionary *dict = @{@"thumb":[thumbData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed], @"duration":@(imgContent.duration)};
- NSData *data = [NSJSONSerialization dataWithJSONObject:dict
- options:kNilOptions
- error:nil];
- item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- } else if ([content isKindOfClass:[WFCCLocationMessageContent class]]) {
- WFCCLocationMessageContent *locContent = (WFCCLocationMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_LOCATION;
- item.title = locContent.title;
- NSData *thumbData = UIImageJPEGRepresentation(locContent.thumbnail, 0.45);
- NSDictionary *dict = @{@"thumb":[thumbData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed], @"long":@(locContent.coordinate.longitude), @"lat":@(locContent.coordinate.latitude)};
- NSData *data = [NSJSONSerialization dataWithJSONObject:dict
- options:kNilOptions
- error:nil];
- item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- } else if ([content isKindOfClass:[WFCCLinkMessageContent class]]) {
- WFCCLinkMessageContent *linkContent = (WFCCLinkMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_LINK;
- item.title = linkContent.title;
- item.thumbUrl = linkContent.thumbnailUrl;
- item.url = linkContent.url;
- } else if ([content isKindOfClass:[WFCCCompositeMessageContent class]]) {
- WFCCCompositeMessageContent *compositeContent = (WFCCCompositeMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_COMPOSITE_MESSAGE;
- item.title = compositeContent.title;
-
- WFCCMessagePayload *payload = [compositeContent encode];
- if (compositeContent.remoteUrl.length) {
- NSError *__error = nil;
- NSMutableDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:payload.binaryContent
- options:kNilOptions
- error:&__error] mutableCopy];
- [dictionary setObject:compositeContent.remoteUrl forKey:@"remote_url"];
- payload.binaryContent = [NSJSONSerialization dataWithJSONObject:dictionary
- options:kNilOptions
- error:nil];
-
- }
- item.data = [payload.binaryContent base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
- } else if ([content isKindOfClass:[WFCCFileMessageContent class]]) {
- WFCCFileMessageContent *fileContent = (WFCCFileMessageContent *)content;
- item.favType = MESSAGE_CONTENT_TYPE_FILE;
- item.title = fileContent.name;
- item.url = fileContent.remoteUrl;
- NSDictionary *dict = @{@"size":@(fileContent.size)};
- NSData *data = [NSJSONSerialization dataWithJSONObject:dict
- options:kNilOptions
- error:nil];
- item.data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- } else {
- NSLog(@"Error, not implement!!!!");
- return nil;
- }
-
- return item;
- }
- - (WFCCMessage *)toMessage {
- WFCCMessage *msg = [[WFCCMessage alloc] init];
- msg.conversation = self.conversation;
- msg.fromUser = self.sender;
- msg.serverTime = self.timestamp;
- switch (self.favType) {
- case MESSAGE_CONTENT_TYPE_TEXT:
- {
- msg.content = [WFCCTextMessageContent contentWith:self.title];
- break;
- }
- case MESSAGE_CONTENT_TYPE_SOUND:
- {
- WFCCSoundMessageContent *soundCnt = [[WFCCSoundMessageContent alloc] init];
- soundCnt.remoteUrl = self.url;
-
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
- soundCnt.duration = [dict[@"duration"] intValue];
-
- msg.content = soundCnt;
- break;
- }
- case MESSAGE_CONTENT_TYPE_IMAGE:
- {
- WFCCImageMessageContent *imageCnt = [[WFCCImageMessageContent alloc] init];
- imageCnt.remoteUrl = self.url;
-
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
-
- NSString *thumbStr = dict[@"thumb"];
- NSData *thumbData = [[NSData alloc] initWithBase64EncodedString:thumbStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
- imageCnt.thumbnail = [UIImage imageWithData:thumbData];
-
- msg.content = imageCnt;
- break;
- }
- case MESSAGE_CONTENT_TYPE_VIDEO:
- {
- WFCCVideoMessageContent *videoCnt = [[WFCCVideoMessageContent alloc] init];
- videoCnt.remoteUrl = self.url;
-
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
-
- NSString *thumbStr = dict[@"thumb"];
- NSData *thumbData = [[NSData alloc] initWithBase64EncodedString:thumbStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
- videoCnt.thumbnail = [UIImage imageWithData:thumbData];
- videoCnt.duration = [dict[@"duration"] intValue];
-
- msg.content = videoCnt;
- break;
- }
- case MESSAGE_CONTENT_TYPE_LOCATION:
- {
- WFCCLocationMessageContent *locationCnt = [[WFCCLocationMessageContent alloc] init];
- locationCnt.title = self.title;
-
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
-
- NSString *thumbStr = dict[@"thumb"];
- NSData *thumbData = [[NSData alloc] initWithBase64EncodedString:thumbStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
- locationCnt.thumbnail = [UIImage imageWithData:thumbData];
-
- double latitude = [dict[@"lat"] doubleValue];
- double longitude = [dict[@"long"] doubleValue];
- locationCnt.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
-
- msg.content = locationCnt;
- break;
- }
- case MESSAGE_CONTENT_TYPE_LINK:
- {
- WFCCLinkMessageContent *linkCnt = [[WFCCLinkMessageContent alloc] init];
- linkCnt.url = self.url;
- linkCnt.thumbnailUrl = self.thumbUrl;
- linkCnt.title = self.title;
- msg.content = linkCnt;
- break;
- }
- case MESSAGE_CONTENT_TYPE_COMPOSITE_MESSAGE:
- {
- WFCCCompositeMessageContent *compositeCnt = [[WFCCCompositeMessageContent alloc] init];
- compositeCnt.title = self.title;
-
- NSData *binaryData = [[NSData alloc] initWithBase64EncodedString:self.data options:NSDataBase64DecodingIgnoreUnknownCharacters];
- NSError *__error = nil;
- NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:binaryData
- options:kNilOptions
- error:&__error];
- NSString *remoteUrl = dictionary[@"remote_url"];
-
- WFCCMediaMessagePayload *payload = [[WFCCMediaMessagePayload alloc] init];
- payload.binaryContent = binaryData;
- payload.remoteMediaUrl = remoteUrl;
- [compositeCnt decode:payload];
-
- msg.content = compositeCnt;
- break;
- }
- case MESSAGE_CONTENT_TYPE_FILE:
- {
- WFCCFileMessageContent *fileCnt = [[WFCCFileMessageContent alloc] init];
- fileCnt.remoteUrl = self.url;
- fileCnt.name = self.title;
-
- NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[self.data dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
- fileCnt.size = [dict[@"size"] intValue];
-
- msg.content = fileCnt;
- break;
- }
- default:
- {
- WFCCUnknownMessageContent *unknownCnt = [[WFCCUnknownMessageContent alloc] init];
- unknownCnt.orignalType = self.favType;
- msg.content = unknownCnt;
- break;
- }
-
- }
- return msg;
- }
- @end
|