Skip to content

Commit

Permalink
refactor: limit ack
Browse files Browse the repository at this point in the history
  • Loading branch information
zapcannon87 committed Jan 22, 2021
1 parent 4a3e685 commit 6584c30
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
1 change: 1 addition & 0 deletions AVOS/AVOSCloudIM/AVIMCommon_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static AVIMConversationKey const AVIMConversationKeyLastMessageMentionPids

/// Use this enum to match command's value(`convType`)
typedef NS_ENUM(NSUInteger, LCIMConvType) {
LCIMConvTypeUnknown = 0,
LCIMConvTypeNormal = 1,
LCIMConvTypeTransient = 2,
LCIMConvTypeSystem = 3,
Expand Down
67 changes: 36 additions & 31 deletions AVOS/AVOSCloudIM/Client/AVIMClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ - (void)LCRTMConnection:(LCRTMConnection *)connection didReceiveCommand:(AVIMGen
} break;
case AVIMCommandType_Direct:
{
[self process_direct:inCommand];
[self processDirect:inCommand];
} break;
case AVIMCommandType_Rcp:
{
Expand Down Expand Up @@ -1482,46 +1482,51 @@ - (void)process_unread:(AVIMGenericCommand *)inCommand
}];
}

- (void)process_direct:(AVIMGenericCommand *)inCommand
- (void)processDirect:(AVIMGenericCommand *)inCommand
{
AssertRunInQueue(self->_internalSerialQueue);

AssertRunInQueue(self.internalSerialQueue);
AVIMDirectCommand *directCommand = (inCommand.hasDirectMessage ? inCommand.directMessage : nil);

NSString *conversationId = (directCommand.hasCid ? directCommand.cid : nil);
NSString *messageId = (directCommand.hasId_p ? directCommand.id_p : nil);
if (!conversationId || !messageId) {
NSString *conversationID = (directCommand.hasCid ? directCommand.cid : nil);
NSString *messageID = (directCommand.hasId_p ? directCommand.id_p : nil);
if (!conversationID ||
!messageID) {
return;
}
BOOL isTransientMsg = (directCommand.hasTransient ? directCommand.transient : false);

if (!isTransientMsg) {
LCIMProtobufCommandWrapper *ackCommandWrapper = ({
AVIMGenericCommand *outCommand = [AVIMGenericCommand new];
AVIMAckCommand *ackCommand = [AVIMAckCommand new];
outCommand.cmd = AVIMCommandType_Ack;
outCommand.ackMessage = ackCommand;
ackCommand.cid = conversationId;
ackCommand.mid = messageId;
LCIMProtobufCommandWrapper *commandWrapper = [LCIMProtobufCommandWrapper new];
commandWrapper.outCommand = outCommand;
commandWrapper;
});
[self sendCommandWrapper:ackCommandWrapper];
}

[self->_conversationManager queryConversationWithId:conversationId callback:^(AVIMConversation *conversation, NSError *error) {
if (error) { return; }
AVIMMessage *message = [conversation process_direct:directCommand messageId:messageId isTransientMsg:isTransientMsg];
id <AVIMClientDelegate> delegate = self->_delegate;
[self.conversationManager queryConversationWithId:conversationID callback:^(AVIMConversation *conversation, NSError *error) {
if (error) {
AVLoggerError(AVLoggerDomainIM, @"%@", error);
return;
}
BOOL isTransientMsg = (directCommand.hasTransient ? directCommand.transient : false);
if ((conversation.convType != LCIMConvTypeTransient) &&
!isTransientMsg) {
LCIMProtobufCommandWrapper *ackCommandWrapper = ({
AVIMGenericCommand *outCommand = [AVIMGenericCommand new];
AVIMAckCommand *ackCommand = [AVIMAckCommand new];
outCommand.cmd = AVIMCommandType_Ack;
outCommand.ackMessage = ackCommand;
ackCommand.cid = conversationID;
ackCommand.mid = messageID;
LCIMProtobufCommandWrapper *commandWrapper = [LCIMProtobufCommandWrapper new];
commandWrapper.outCommand = outCommand;
commandWrapper;
});
[self sendCommandWrapper:ackCommandWrapper];
}
AVIMMessage *message = [conversation process_direct:directCommand
messageId:messageID
isTransientMsg:isTransientMsg];
id <AVIMClientDelegate> delegate = self.delegate;
if (message && delegate) {
SEL selType = @selector(conversation:didReceiveTypedMessage:);
SEL selCommon = @selector(conversation:didReceiveCommonMessage:);
if ([message isKindOfClass:[AVIMTypedMessage class]] && [delegate respondsToSelector:selType]) {
if ([message isKindOfClass:[AVIMTypedMessage class]] &&
[delegate respondsToSelector:selType]) {
[self invokeInUserInteractQueue:^{
[delegate conversation:conversation didReceiveTypedMessage:(AVIMTypedMessage *)message];
}];
} else if ([message isKindOfClass:AVIMMessage.class] && [delegate respondsToSelector:selCommon]) {
} else if ([message isKindOfClass:[AVIMMessage class]] &&
[delegate respondsToSelector:selCommon]) {
[self invokeInUserInteractQueue:^{
[delegate conversation:conversation didReceiveCommonMessage:message];
}];
Expand Down
3 changes: 1 addition & 2 deletions AVOS/AVOSCloudIM/Conversation/AVIMConversation.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ @implementation AVIMConversation {
// public immutable
NSString *_clientId;
NSString *_conversationId;
LCIMConvType _convType;

// public mutable
AVIMMessage *_lastMessage;
Expand Down Expand Up @@ -1424,7 +1423,7 @@ - (void)sendRealMessage:(AVIMMessage *)message
return;
}

BOOL transientConv = (self->_convType == LCIMConvTypeTransient);
BOOL transientConv = (self.convType == LCIMConvTypeTransient);
BOOL transientMsg = option.transient;
BOOL receipt = option.receipt;
BOOL will = option.will;
Expand Down
2 changes: 2 additions & 0 deletions AVOS/AVOSCloudIM/Conversation/AVIMConversation_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

@interface AVIMConversation ()

@property (nonatomic, readonly) LCIMConvType convType;

+ (instancetype)conversationWithRawJSONData:(NSMutableDictionary *)rawJSONData
client:(AVIMClient *)client;

Expand Down

0 comments on commit 6584c30

Please sign in to comment.