Files
yinmeng-ios/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageParser.m

378 lines
19 KiB
Mathematica
Raw Normal View History

2021-11-04 16:27:39 +08:00
//
// XPRoomMessageParser.m
// xplan-ios
//
// Created by on 2021/10/26.
//
#import "XPRoomMessageParser.h"
#import <YYText/YYText.h>
#import <NIMSDK/NIMSDK.h>
///Tool
#import "ThemeColor+Room.h"
#import "XPRoomMessageConstant.h"
#import "AccountInfoStorage.h"
#import "XPMacro.h"
2021-11-17 19:29:14 +08:00
#import "XPGiftStorage.h"
2021-11-04 16:27:39 +08:00
///Model
#import "XPMessageRemoteExtModel.h"
2021-11-17 19:29:14 +08:00
#import "AttachMentModel.h"
#import "GiftReceiveInfoModel.h"
2021-11-04 16:27:39 +08:00
#import "NetImageView.h"
@implementation XPRoomMessageParser
+ (NSAttributedString*)parseMessageAttribute:(NIMMessage *)message {
NIMMessageType messageType = message.messageType;
switch (messageType) {
case NIMMessageTypeText:
return [self makeChatAttribute:message];
case NIMMessageTypeTip:
return [self makeTipsAttribute:message];
case NIMMessageTypeNotification:
return [self makeNotificationAttribute:message];
2021-11-17 19:29:14 +08:00
case NIMMessageTypeCustom:
return [self makeCustomAttribute:message];
2021-11-04 16:27:39 +08:00
default:
return nil;
}
}
///
2021-11-17 19:29:14 +08:00
+ (NSAttributedString *)makeCustomAttribute:(NIMMessage *)message {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
XPMessageRemoteExtModel * model = [XPMessageRemoteExtModel modelWithJSON:message.remoteExt[message.from]];
int first = attachment.first;
if (first == CustomMessageType_Gift) {///
return [self createSendGiftAttribute:attachment sendInfo:model];
} else if(first == CustomMessageType_ALLMicroSend) {///
return [self createBatchMicroSendGiftAttribute:attachment sendInfo:model];
2021-11-24 14:58:53 +08:00
} else if(first == CustomMessageType_Room_Tip) {////
return [self createShareOrAttentionRoomAttribute:attachment sendInfo:model];
2021-11-17 19:29:14 +08:00
}
return nil;
}
2021-11-04 16:27:39 +08:00
/// @param message
+ (NSAttributedString*)makeChatAttribute:(NIMMessage *)message{
NSString * uid = [AccountInfoStorage instance].getUid;
XPMessageRemoteExtModel * model = [XPMessageRemoteExtModel modelWithJSON:message.remoteExt[message.from]];
NSString * nick = [NSString stringWithFormat:@"%@:", ((NIMMessageChatroomExtension *)message.messageExt).roomNickname];
if ([message.from isEqualToString:uid]) {
nick = @"我:";
}
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
2021-11-24 14:58:53 +08:00
///
[attribute appendAttributedString:[self createOfficalAndNewuserAttribute:model.defUser newUser:model.newUser]];
2021-11-04 16:27:39 +08:00
if (model.experUrl) {
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createUrlImageAttribute:model.experUrl]];
2021-11-04 16:27:39 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:nick color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-04 16:27:39 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:message.text color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-04 16:27:39 +08:00
return attribute;
}
/// tips
/// @param message
+ (NSAttributedString*)makeTipsAttribute:(NIMMessage *)message {
return [self createTextAttribute:message.text color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont];
}
///
/// @param message
+ (NSAttributedString*)makeNotificationAttribute:(NIMMessage *)message {
NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject;
NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content;
NIMChatroomNotificationMember *member = content.targets[0];
switch (content.eventType) {
case NIMChatroomEventTypeEnter:///
{
NSString* nick = member.nick.length > 0 ? member.nick : @"";
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
[attribute appendAttributedString:[self createTextAttribute:@"进入了房间" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-04 16:27:39 +08:00
return attribute;
}
case NIMChatroomEventTypeInfoUpdated:///
return [self createRoomInfoUpdateAttribute:content.notifyExt];
default:
return nil;
}
}
2021-11-17 19:29:14 +08:00
#pragma mark -
2021-11-04 16:27:39 +08:00
///
+ (NSAttributedString*)createRoomInfoUpdateAttribute:(NSString *)notifyExt {
return nil;
}
2021-11-24 14:58:53 +08:00
#pragma mark -
+ (NSAttributedString *)createShareOrAttentionRoomAttribute:(AttachmentModel *)attachment sendInfo:(XPMessageRemoteExtModel *)sendInfo {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
NSDictionary *data = attachment.data[@"data"];
NSString *nick = [NSString stringWithFormat:@"%@",data[@"nick"]];
[attribute appendAttributedString:[self createTextAttribute:nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
NSString *tipString = @"";
if (attachment.second == Custom_Message_Sub_Room_Tip_ShareRoom) {
tipString = @"分享了房间";
}else if (attachment.second == Custom_Message_Sub_Room_Tip_Attention_Owner) {
tipString = @"关注了房主";
}
[attribute appendAttributedString:[self createTextAttribute:tipString color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
return attribute;
}
2021-11-17 19:29:14 +08:00
#pragma mark -
+ (NSAttributedString *)createBatchMicroSendGiftAttribute:(AttachmentModel *)attachment sendInfo:(XPMessageRemoteExtModel *)sendInfo {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
GiftReceiveInfoModel *info = [GiftReceiveInfoModel modelWithJSON:attachment.data];
GiftInfoModel *giftInfo = info.gift == nil ? info.giftInfo : info.gift;
if (giftInfo == nil) {
giftInfo = [[XPGiftStorage shareStorage] findGiftInfo:info.giftId];
}
2021-11-24 14:58:53 +08:00
///
[attribute appendAttributedString:[self createOfficalAndNewuserAttribute:sendInfo.defUser newUser:sendInfo.newUser]];
2021-11-17 19:29:14 +08:00
//nick
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:info.nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
if (attachment.second == Custom_Message_Sub_AllBatchSend || attachment.second == Custom_Message_Sub_AllMicroSend) {///
if (attachment.second == Custom_Message_Sub_AllBatchSend) {
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@" 打赏 " color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
for (int i = 0; i < info.targetUsers.count; i++) {
GiftReceiveUserInfoModel *targetInfo = [info.targetUsers objectAtIndex:i];
//nick
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:targetInfo.nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
if (i < (info.targetUsers.count -1)) {
//nick
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"," color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
}
} else {
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@" 全麦打赏 " color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
//img
if (giftInfo.giftUrl) {
[attribute appendAttributedString:[self createUrlImageAttribute:giftInfo.giftUrl]];
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
//x N
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:[NSString stringWithFormat:@" X%ld",info.giftNum] color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
} else if(attachment.second == Custom_Message_Sub_AllMicroLuckySend || attachment.second == Custom_Message_Sub_AllBatchMicroLuckySend) {///
//action
NSString * sendTitle = @" 送出 ";
if (attachment.second == Custom_Message_Sub_AllMicroLuckySend) {
sendTitle = @" 全麦送出 ";
}
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:sendTitle color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:info.giftName color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@" 给 " color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
if (info.luckyGiftList) {
//target
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:info.luckyGiftList.user.nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@" 爆出了 " color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
[info.luckyGiftList.giftList enumerateObjectsUsingBlock:^(GiftListsInfo * _Nonnull giftListInfo, NSUInteger idx, BOOL * _Nonnull stop) {
GiftInfoModel *luckGiftInfo = [[XPGiftStorage shareStorage] findGiftInfo:[NSString stringWithFormat:@"%ld", giftListInfo.giftId]];;
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"价值" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:[NSString stringWithFormat:@"%ld",(NSInteger)giftInfo.goldPrice] color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"钻石的礼物" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
//img
[attribute appendAttributedString:[self createUrlImageAttribute:luckGiftInfo.giftUrl]];
[attribute appendAttributedString:[self createSapceAttribute:2]];
//x N
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute: [NSString stringWithFormat:@" X%ld",giftListInfo.giftNum] color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
if (info.luckyGiftList.giftList.count-1 != idx) {
//,
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"," color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
}
}];
}
}
return attribute;
}
+ (NSAttributedString *)createSendGiftAttribute:(AttachmentModel *)attachment sendInfo:(XPMessageRemoteExtModel *)sendInfo {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
GiftReceiveInfoModel *info = [GiftReceiveInfoModel modelWithJSON:attachment.data];
GiftInfoModel *giftInfo = info.gift == nil ? info.giftInfo : info.gift;
if (giftInfo == nil) {
giftInfo = [[XPGiftStorage shareStorage] findGiftInfo:info.giftId];
}
2021-11-24 14:58:53 +08:00
///
[attribute appendAttributedString:[self createOfficalAndNewuserAttribute:sendInfo.defUser newUser:sendInfo.newUser]];
2021-11-17 19:29:14 +08:00
//nick
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:info.nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];;
if (attachment.second == Custom_Message_Sub_Gift_LuckySend) {
//action
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"送出" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:info.giftName color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"给" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
//target
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:info.luckyGiftList.user.nick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//lucky
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@"爆出了" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[info.luckyGiftList.giftList enumerateObjectsUsingBlock:^(GiftListsInfo * _Nonnull giftListInfo, NSUInteger idx, BOOL * _Nonnull stop) {
GiftInfoModel *luckyGiftInfo = [[XPGiftStorage shareStorage] findGiftInfo:[NSString stringWithFormat:@"%ld", giftListInfo.giftId]];
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString: [self createTextAttribute:@"价值" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:[NSString stringWithFormat:@"%ld",(NSInteger)giftInfo.goldPrice] color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString: [self createTextAttribute:@"钻石的礼物" color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
//img
[attribute appendAttributedString:[self createUrlImageAttribute:luckyGiftInfo.giftUrl]];
[attribute appendAttributedString:[self createSapceAttribute:2]];
//x N
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute: [NSString stringWithFormat:@" X%ld",giftListInfo.giftNum] color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
if (info.luckyGiftList.giftList.count-1 != idx) {
//,
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString: [self createTextAttribute:@"," color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
}
}];
} else {
//
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:@" 打赏 " color:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//target
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString: [self createTextAttribute:info.targetNick color:[ThemeColor messageNickColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
[attribute appendAttributedString:[self createSapceAttribute:2]];
//img
[attribute appendAttributedString:[self createUrlImageAttribute:giftInfo.giftUrl]];
[attribute appendAttributedString:[self createSapceAttribute:2]];
//x N
2021-11-24 14:58:53 +08:00
[attribute appendAttributedString:[self createTextAttribute:[NSString stringWithFormat:@" X%ld",info.giftNum] color:[ThemeColor messageTextColor] font:kRoomMessageDefalutFont]];
2021-11-17 19:29:14 +08:00
}
return attribute;
}
2021-11-24 14:58:53 +08:00
#pragma mark - Commond Mehotd
+ (NSAttributedString *)createOfficalAndNewuserAttribute:(UserLevelType)levelType newUser:(BOOL)newUser {
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init];
//offical
if (levelType == UserLevelType_Offical) {
[attribute appendAttributedString: [self createLocalImageAttribute:@"common_offical"]];
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
if (newUser) {
[attribute appendAttributedString: [self createLocalImageAttribute:@"common_new_user"]];
[attribute appendAttributedString:[self createSapceAttribute:2]];
}
return attribute;
}
2021-11-17 19:29:14 +08:00
#pragma mark - private base methods
2021-11-04 16:27:39 +08:00
///
/// @param imageUrl
+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl {
2021-11-25 17:35:31 +08:00
NetImageConfig *config = [[NetImageConfig alloc]init];
config.autoLoad = NO;
NetImageView *imageView = [[NetImageView alloc]initWithUrl:imageUrl config:config];
2021-11-17 19:29:14 +08:00
UIImage* image = imageView.image;
if (image) {
CGFloat scale = image.size.width / image.size.height;
imageView.bounds = CGRectMake(0, 0, 20 * scale, 20);
} else {
imageView.bounds = CGRectMake(0, 0, 20, 20);
}
imageView.layer.masksToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return attrString;
2021-11-04 16:27:39 +08:00
}
2021-11-17 19:29:14 +08:00
2021-11-04 16:27:39 +08:00
///
/// @param imageName
+ (NSMutableAttributedString *)createLocalImageAttribute:(NSString *)imageName {
2021-11-17 19:29:14 +08:00
if (imageName == nil && imageName.length <= 0) {
return nil;
}
2021-11-04 16:27:39 +08:00
return [self createImageAttribute:[UIImage imageNamed:imageName]];
}
///
/// @param image
+ (NSMutableAttributedString *)createImageAttribute:(UIImage *)image {
UIImageView *imaveView = [[UIImageView alloc]init];
imaveView.image = image;
CGFloat scale = (CGFloat)imaveView.image.size.width / (CGFloat)imaveView.image.size.height;
imaveView.bounds = CGRectMake(0, 0, 16 * scale, 16);
NSMutableAttributedString * attrString = [NSMutableAttributedString yy_attachmentStringWithContent:imaveView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(imaveView.frame.size.width, imaveView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return attrString;
}
///
/// @param text
/// @param color
/// @param font
+ (NSMutableAttributedString *)createTextAttribute:(NSString *)text color:(UIColor *)color font:(CGFloat)font {
2021-11-17 19:29:14 +08:00
if (text == nil || text.length <= 0) {
text = @"";
}
2021-11-04 16:27:39 +08:00
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
attribute.yy_font = [UIFont systemFontOfSize:font];
attribute.yy_color = color;
attribute.yy_paragraphStyle = [self paragraphStyle];
return attribute;
}
/// ...
+ (NSMutableParagraphStyle *)paragraphStyle {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineSpacing = 4.0f;//
// ()
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
return paraStyle;
}
///
/// @param width
+ (NSMutableAttributedString *)createSapceAttribute:(CGFloat)width {
UIView *spaceView = [[UIView alloc]init];
spaceView.backgroundColor = [UIColor clearColor];
spaceView.bounds = CGRectMake(0, 0, width, 10);
NSMutableAttributedString * attribute = [NSMutableAttributedString yy_attachmentStringWithContent:spaceView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(spaceView.frame.size.width, spaceView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter];
return attribute;
}
@end