ifish/Ifish/controllers/IfishTabControllers/消息/IfishIMFiles/Sessions/Model/Object/NTESCustomAttachmentDecoder.m

107 lines
4.4 KiB
Objective-C

//
// NTESCustomAttachmentDecoder.m
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import "NTESCustomAttachmentDecoder.h"
#import "NTESCustomAttachmentDefines.h"
#import "NTESJanKenPonAttachment.h"
#import "NTESSnapchatAttachment.h"
#import "NTESChartletAttachment.h"
#import "NTESWhiteboardAttachment.h"
#import "NSDictionary+NTESJson.h"
#import "NTESSessionUtil.h"
#import "IfishAttachment.h"
@implementation NTESCustomAttachmentDecoder
- (id<NIMCustomAttachment>)decodeAttachment:(NSString *)content
{
id<NIMCustomAttachment> attachment = nil;
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
if ([dict isKindOfClass:[NSDictionary class]])
{
NSInteger type = [dict jsonInteger:CMType];
NSDictionary *data = [dict jsonDict:CMData];
switch (type) {
case CustomMessageTypeJanKenPon:
{
attachment = [[NTESJanKenPonAttachment alloc] init];
((NTESJanKenPonAttachment *)attachment).value = [data jsonInteger:CMValue];
}
break;
case CustomMessageTypeSnapchat:
{
attachment = [[NTESSnapchatAttachment alloc] init];
((NTESSnapchatAttachment *)attachment).md5 = [data jsonString:CMMD5];
((NTESSnapchatAttachment *)attachment).url = [data jsonString:CMURL];
((NTESSnapchatAttachment *)attachment).isFired = [data jsonBool:CMFIRE];
}
break;
case CustomMessageTypeChartlet:
{
attachment = [[NTESChartletAttachment alloc] init];
((NTESChartletAttachment *)attachment).chartletCatalog = [data jsonString:CMCatalog];
((NTESChartletAttachment *)attachment).chartletId = [data jsonString:CMChartlet];
}
break;
case CustomMessageTypeWhiteboard:
{
attachment = [[NTESWhiteboardAttachment alloc] init];
((NTESWhiteboardAttachment *)attachment).flag = [data jsonInteger:CMFlag];
}
break;
case CustomMessageTypeIfishPushMassage:
{
//爱鱼奇自定义推送消息
attachment = [[IfishAttachment alloc] init];
((IfishAttachment *)attachment).pushlink=[data jsonString:CMIfishPushlink];
((IfishAttachment *)attachment).timestamp=[data jsonString:CMIfishTimestamp];
((IfishAttachment *)attachment).pushType=[data jsonString:CMIfishPushType];
((IfishAttachment *)attachment).pushId=[data jsonString:CMIfishPushId];
}
break;
default:
break;
}
attachment = [self checkAttachment:attachment] ? attachment : nil;
}
}
return attachment;
}
- (BOOL)checkAttachment:(id<NIMCustomAttachment>)attachment{
BOOL check = NO;
if ([attachment isKindOfClass:[NTESJanKenPonAttachment class]]) {
NSInteger value = [((NTESJanKenPonAttachment *)attachment) value];
check = (value>=CustomJanKenPonValueKen && value<=CustomJanKenPonValuePon) ? YES : NO;
}
else if ([attachment isKindOfClass:[NTESSnapchatAttachment class]]) {
check = YES;
}
else if ([attachment isKindOfClass:[NTESChartletAttachment class]]) {
NSString *chartletCatalog = ((NTESChartletAttachment *)attachment).chartletCatalog;
NSString *chartletId =((NTESChartletAttachment *)attachment).chartletId;
check = chartletCatalog.length&&chartletId.length ? YES : NO;
}
else if ([attachment isKindOfClass:[NTESWhiteboardAttachment class]]) {
NSInteger flag = [((NTESWhiteboardAttachment *)attachment) flag];
check = ((flag >= CustomWhiteboardFlagInvite) && (flag <= CustomWhiteboardFlagClose)) ? YES : NO;
}
return check;
}
@end