NTESCustomAttachmentDecoder.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // NTESCustomAttachmentDecoder.m
  3. // NIM
  4. //
  5. // Created by amao on 7/2/15.
  6. // Copyright (c) 2015 Netease. All rights reserved.
  7. //
  8. #import "NTESCustomAttachmentDecoder.h"
  9. #import "NTESCustomAttachmentDefines.h"
  10. #import "NTESJanKenPonAttachment.h"
  11. #import "NTESSnapchatAttachment.h"
  12. #import "NTESChartletAttachment.h"
  13. #import "NTESGiftAttachment.h"
  14. #import "NTESWhiteboardAttachment.h"
  15. #import "NSDictionary+NTESJson.h"
  16. #import "NTESSessionUtil.h"
  17. @implementation NTESCustomAttachmentDecoder
  18. - (id<NIMCustomAttachment>)decodeAttachment:(NSString *)content
  19. {
  20. id<NIMCustomAttachment> attachment = nil;
  21. NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
  22. if (data) {
  23. NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
  24. options:0
  25. error:nil];
  26. if ([dict isKindOfClass:[NSDictionary class]])
  27. {
  28. NSInteger type = [dict jsonInteger:CMType];
  29. NSDictionary *data = [dict jsonDict:CMData];
  30. switch (type) {
  31. case CustomMessageTypeJanKenPon:
  32. {
  33. attachment = [[NTESJanKenPonAttachment alloc] init];
  34. ((NTESJanKenPonAttachment *)attachment).value = [data jsonInteger:CMValue];
  35. }
  36. break;
  37. case CustomMessageTypeSnapchat:
  38. {
  39. attachment = [[NTESSnapchatAttachment alloc] init];
  40. ((NTESSnapchatAttachment *)attachment).md5 = [data jsonString:CMMD5];
  41. ((NTESSnapchatAttachment *)attachment).url = [data jsonString:CMURL];
  42. ((NTESSnapchatAttachment *)attachment).isFired = [data jsonBool:CMFIRE];
  43. }
  44. break;
  45. case CustomMessageTypeChartlet:
  46. {
  47. attachment = [[NTESChartletAttachment alloc] init];
  48. ((NTESChartletAttachment *)attachment).chartletCatalog = [data jsonString:CMCatalog];
  49. ((NTESChartletAttachment *)attachment).chartletId = [data jsonString:CMChartlet];
  50. }
  51. break;
  52. case CustomMessageTypeWhiteboard:
  53. {
  54. attachment = [[NTESWhiteboardAttachment alloc] init];
  55. ((NTESWhiteboardAttachment *)attachment).flag = [data jsonInteger:CMFlag];
  56. }
  57. break;
  58. case CustomMessageTypeGift:
  59. {
  60. attachment = [[NTESGiftAttachment alloc] init];
  61. [((NTESGiftAttachment *)attachment) handleGiftMessage:data];
  62. }
  63. break;
  64. default:
  65. break;
  66. }
  67. attachment = [self checkAttachment:attachment] ? attachment : nil;
  68. }
  69. }
  70. return attachment;
  71. }
  72. - (BOOL)checkAttachment:(id<NIMCustomAttachment>)attachment{
  73. BOOL check = NO;
  74. if ([attachment isKindOfClass:[NTESJanKenPonAttachment class]]) {
  75. NSInteger value = [((NTESJanKenPonAttachment *)attachment) value];
  76. check = (value>=CustomJanKenPonValueKen && value<=CustomJanKenPonValuePon) ? YES : NO;
  77. }
  78. else if ([attachment isKindOfClass:[NTESSnapchatAttachment class]]) {
  79. check = YES;
  80. }
  81. else if ([attachment isKindOfClass:[NTESChartletAttachment class]]) {
  82. NSString *chartletCatalog = ((NTESChartletAttachment *)attachment).chartletCatalog;
  83. NSString *chartletId =((NTESChartletAttachment *)attachment).chartletId;
  84. check = chartletCatalog.length&&chartletId.length ? YES : NO;
  85. }
  86. else if ([attachment isKindOfClass:[NTESWhiteboardAttachment class]]) {
  87. NSInteger flag = [((NTESWhiteboardAttachment *)attachment) flag];
  88. check = ((flag >= CustomWhiteboardFlagInvite) && (flag <= CustomWhiteboardFlagClose)) ? YES : NO;
  89. }
  90. else if ([attachment isKindOfClass:[NTESGiftAttachment class]]) {
  91. check = YES;
  92. }
  93. return check;
  94. }
  95. @end