IMACustomConversation.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // IMACustomConversation.m
  3. // TIMChat
  4. //
  5. // Created by AlexiChen on 16/4/7.
  6. // Copyright © 2016年 AlexiChen. All rights reserved.
  7. //
  8. #import "IMACustomConversation.h"
  9. @implementation IMACustomConversation
  10. + (NSString *)getCustomConversationID:(IMAMsg *)msg
  11. {
  12. NSString *receiver = nil;
  13. if (msg.type == EIMAMSG_SNSSystem)
  14. {
  15. // 新朋友消息
  16. receiver = [NSString stringWithFormat:ASLocalizedString(@"新朋友")];
  17. }
  18. else if (msg.type == EIMAMSG_GroupSystem)
  19. {
  20. // 群消息
  21. receiver = [NSString stringWithFormat:ASLocalizedString(@"群系统消息")];
  22. }
  23. return receiver;
  24. }
  25. + (IMAConType)getTypOfSystemConversation:(TIMConversation *)conv
  26. {
  27. NSString *receiver = [conv getReceiver];
  28. IMAConType type = IMA_Unknow;
  29. if ([receiver isEqualToString:ASLocalizedString(@"新朋友")])
  30. {
  31. type = IMA_Sys_NewFriend;
  32. }
  33. else if ([receiver isEqualToString:ASLocalizedString(@"群系统消息")])
  34. {
  35. type = IMA_Sys_GroupTip;
  36. }
  37. return type;
  38. }
  39. - (instancetype)initWith:(TIMConversation *)conv
  40. {
  41. IMAConType type = [IMACustomConversation getTypOfSystemConversation:conv];
  42. if (type == IMA_Unknow)
  43. {
  44. return nil;
  45. }
  46. if (self = [super initWith:conv])
  47. {
  48. _imaConType = type;
  49. }
  50. return self;
  51. }
  52. - (instancetype)initWith:(TIMConversation *)conv andMsg:(IMAMsg *)msg
  53. {
  54. IMAConType type = [IMACustomConversation getTypOfSystemConversation:conv];
  55. if (type == IMA_Unknow)
  56. {
  57. return nil;
  58. }
  59. if (self = [super initWith:conv])
  60. {
  61. _imaConType = type;
  62. }
  63. return self;
  64. }
  65. - (void)saveMessage:(IMAMsg *)msg succ:(SaveMsgSucc)succ
  66. {
  67. __weak IMACustomConversation *ws = self;
  68. __weak TIMConversation *wc = _conversation;
  69. if (_imaConType == IMA_Sys_NewFriend)
  70. {
  71. TIMFriendFutureMeta *meta = [[TIMFriendFutureMeta alloc] init];
  72. meta.reqNum = 30;
  73. meta.timestamp = 0;
  74. [[IMAPlatform sharedInstance] asyncGetAllFriendPendency:meta succ:^(TIMFriendFutureMeta *meta, NSArray *items) {
  75. NSInteger curUnRead = ws.unReadMsgCount;
  76. NSInteger realUnRead = meta.pendencyUnReadCnt;
  77. if (curUnRead != realUnRead)
  78. {
  79. [wc saveMessage:msg.msg sender:[msg.msg sender] isReaded:NO];
  80. ws.unReadMsgCount = realUnRead;
  81. ws.lastMessage = msg;
  82. }
  83. if (succ)
  84. {
  85. succ((int)(realUnRead-curUnRead));
  86. }
  87. } fail:nil];
  88. }
  89. if (_imaConType == IMA_Sys_GroupTip)
  90. {
  91. [[IMAPlatform sharedInstance].contactMgr asyncGetGroupPendencyList:^(TIMGroupPendencyMeta *meta, NSArray *pendencies) {
  92. NSInteger curUnRead = ws.unReadMsgCount;
  93. NSInteger realUnRead = meta.unReadCnt;
  94. if (curUnRead != realUnRead)
  95. {
  96. [wc saveMessage:msg.msg sender:[msg.msg sender] isReaded:NO];
  97. ws.unReadMsgCount = realUnRead;
  98. ws.lastMessage = msg;
  99. }
  100. if (succ)
  101. {
  102. succ((int)(realUnRead-curUnRead));
  103. }
  104. } fail:nil];
  105. }
  106. }
  107. - (IMAConType)imaType
  108. {
  109. return _imaConType;
  110. }
  111. - (BOOL)isEqualTo:(IMAConversation *)conv
  112. {
  113. return ([self imaType] == [conv imaType]) && ([[self receiver] isEqualToString:[conv receiver]]);
  114. }
  115. - (void)setReadAllMsg
  116. {
  117. //2.0之前的版本不支持 getConversationList 接口
  118. NSArray *conversationList = [[TIMManager sharedInstance] getConversationList];
  119. for (TIMConversation * conversation in conversationList)
  120. {
  121. if ([conversation getType] == TIM_SYSTEM)
  122. {
  123. if (_imaConType == [IMACustomConversation getTypOfSystemConversation:conversation])
  124. {
  125. [conversation setReadMessage:nil succ:nil fail:nil];
  126. }
  127. }
  128. }
  129. [_conversation setReadMessage:nil succ:nil fail:nil];
  130. [IMAPlatform sharedInstance].conversationMgr.unReadMessageCount -= self.unReadMsgCount;
  131. self.unReadMsgCount = 0;
  132. }
  133. - (NSInteger)unReadCount
  134. {
  135. return self.unReadMsgCount;
  136. }
  137. @end