Skip to content

Commit 4406a17

Browse files
authored
Merge pull request #198 from GetStream/feature/uni-93-cleanup-after-release
Refactor to reduce nesting
2 parents 53b9a84 + a1e59c3 commit 4406a17

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

Assets/Plugins/StreamChat/Core/StatefulModels/StreamChannel.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -840,34 +840,34 @@ private void AssertCid(string cid)
840840
private bool InternalAppendOrUpdateMessage(MessageInternalDTO dto, out StreamMessage streamMessage)
841841
{
842842
streamMessage = Cache.TryCreateOrUpdate(dto, out var wasCreated);
843-
if (wasCreated)
843+
var isNewMessage = wasCreated && !_messages.ContainsNoAlloc(streamMessage);
844+
if (!isNewMessage)
844845
{
845-
if (!_messages.ContainsNoAlloc(streamMessage))
846-
{
847-
var lastMessage = _messages.LastOrDefault();
848-
849-
try
850-
{
851-
_messages.Add(streamMessage);
852-
}
853-
catch
854-
{
855-
streamMessage = null;
856-
return false;
857-
}
846+
return true;
847+
}
848+
849+
var lastMessage = _messages.LastOrDefault();
858850

859-
// If local user sends message during the sync operation.
860-
// It is possible that the locally sent message will be added before the /sync endpoint returns past message events
861-
if (lastMessage != null && streamMessage.CreatedAt < lastMessage.CreatedAt)
862-
{
863-
//StreamTodo: test this more. One way is to toggle Ethernet on PC and send messages from Android client
864-
_messages.Sort(_messageCreateAtComparer);
865-
}
851+
try
852+
{
853+
// Adding message can fail. For example, shadowed messages are ignored
854+
_messages.Add(streamMessage);
855+
}
856+
catch
857+
{
858+
streamMessage = null;
859+
return false;
860+
}
866861

867-
MessageReceived?.Invoke(this, streamMessage);
868-
}
862+
// If local user sends message during the sync operation.
863+
// It is possible that the locally sent message will be added before the /sync endpoint returns past message events
864+
if (lastMessage != null && streamMessage.CreatedAt < lastMessage.CreatedAt)
865+
{
866+
//StreamTodo: test this more. One way is to toggle Ethernet on PC and send messages from Android client
867+
_messages.Sort(_messageCreateAtComparer);
869868
}
870869

870+
MessageReceived?.Invoke(this, streamMessage);
871871
return true;
872872
}
873873

Assets/Plugins/StreamChat/Tests/StatefulClient/ChannelsModerationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private async Task When_client_sends_message_shadowed_by_ai_moderation_expect_ot
3737
Assert.IsTrue(channel.Messages.Contains(normalMessage));
3838

3939
// Wait for other client to receive the message
40-
await WaitWhileFalseAsync(() => otherClientChannel.Messages.Any(m => m.Id == normalMessage.Id));
40+
await WaitWhileFalseAsync(() => otherClientChannel.Messages.Any(m => m.Id == normalMessage.Id), maxSeconds: 20);
4141

4242
Assert.IsNotNull(otherClientChannel.Messages.Single(m => m.Id == normalMessage.Id));
4343

@@ -54,7 +54,7 @@ private async Task When_client_sends_message_shadowed_by_ai_moderation_expect_ot
5454
Assert.IsTrue(channel.Messages.Contains(offensiveMessage));
5555

5656
// Wait for other client to receive the offensive message
57-
await WaitWhileFalseAsync(() => messagesReceivedOnOtherClient.Any(m => m.Message.Id == offensiveMessage.Id));
57+
await WaitWhileFalseAsync(() => messagesReceivedOnOtherClient.Any(m => m.Message.Id == offensiveMessage.Id), maxSeconds: 20);
5858

5959
// Other client should have the offensive message shadowed
6060
Assert.IsFalse(otherClientChannel.Messages.Any(m => m.Id == offensiveMessage.Id));

0 commit comments

Comments
 (0)