Skip to content

Commit 3cb9e1d

Browse files
authored
Fix Notification DTOs sometimes having empty channel.ID and channel.Type while having this passed as channel_id & channel_type (#168)
1 parent 1587472 commit 3cb9e1d

5 files changed

Lines changed: 152 additions & 14 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using StreamChat.Core.InternalDTO.Events;
2+
3+
namespace StreamChat.Core.Helpers
4+
{
5+
/// <summary>
6+
/// This class fixes the case where sometimes response contains empty channel.id and channel.type but instead has channel_id and channel_type
7+
/// </summary>
8+
internal static class InternalNotificationsHelper
9+
{
10+
public static void FixMissingChannelTypeAndId(NotificationAddedToChannelEventInternalDTO dto)
11+
{
12+
if (string.IsNullOrEmpty(dto.Channel.Id))
13+
{
14+
dto.Channel.Id = dto.ChannelId;
15+
}
16+
17+
if (string.IsNullOrEmpty(dto.Channel.Type))
18+
{
19+
dto.Channel.Type = dto.ChannelType;
20+
}
21+
}
22+
23+
public static void FixMissingChannelTypeAndId(NotificationChannelDeletedEventInternalDTO dto)
24+
{
25+
if (string.IsNullOrEmpty(dto.Channel.Id))
26+
{
27+
dto.Channel.Id = dto.ChannelId;
28+
}
29+
30+
if (string.IsNullOrEmpty(dto.Channel.Type))
31+
{
32+
dto.Channel.Type = dto.ChannelType;
33+
}
34+
}
35+
36+
public static void FixMissingChannelTypeAndId(NotificationChannelTruncatedEventInternalDTO dto)
37+
{
38+
if (string.IsNullOrEmpty(dto.Channel.Id))
39+
{
40+
dto.Channel.Id = dto.ChannelId;
41+
}
42+
43+
if (string.IsNullOrEmpty(dto.Channel.Type))
44+
{
45+
dto.Channel.Type = dto.ChannelType;
46+
}
47+
}
48+
49+
public static void FixMissingChannelTypeAndId(NotificationInviteAcceptedEventInternalDTO dto)
50+
{
51+
if (string.IsNullOrEmpty(dto.Channel.Id))
52+
{
53+
dto.Channel.Id = dto.ChannelId;
54+
}
55+
56+
if (string.IsNullOrEmpty(dto.Channel.Type))
57+
{
58+
dto.Channel.Type = dto.ChannelType;
59+
}
60+
}
61+
62+
public static void FixMissingChannelTypeAndId(NotificationInvitedEventInternalDTO dto)
63+
{
64+
if (string.IsNullOrEmpty(dto.Channel.Id))
65+
{
66+
dto.Channel.Id = dto.ChannelId;
67+
}
68+
69+
if (string.IsNullOrEmpty(dto.Channel.Type))
70+
{
71+
dto.Channel.Type = dto.ChannelType;
72+
}
73+
}
74+
75+
public static void FixMissingChannelTypeAndId(NotificationInviteRejectedEventInternalDTO dto)
76+
{
77+
if (string.IsNullOrEmpty(dto.Channel.Id))
78+
{
79+
dto.Channel.Id = dto.ChannelId;
80+
}
81+
82+
if (string.IsNullOrEmpty(dto.Channel.Type))
83+
{
84+
dto.Channel.Type = dto.ChannelType;
85+
}
86+
}
87+
88+
public static void FixMissingChannelTypeAndId(NotificationNewMessageEventInternalDTO dto)
89+
{
90+
if (string.IsNullOrEmpty(dto.Channel.Id))
91+
{
92+
dto.Channel.Id = dto.ChannelId;
93+
}
94+
95+
if (string.IsNullOrEmpty(dto.Channel.Type))
96+
{
97+
dto.Channel.Type = dto.ChannelType;
98+
}
99+
}
100+
101+
public static void FixMissingChannelTypeAndId(NotificationRemovedFromChannelEventInternalDTO dto)
102+
{
103+
if (string.IsNullOrEmpty(dto.Channel.Id))
104+
{
105+
dto.Channel.Id = dto.ChannelId;
106+
}
107+
108+
if (string.IsNullOrEmpty(dto.Channel.Type))
109+
{
110+
dto.Channel.Type = dto.ChannelType;
111+
}
112+
}
113+
114+
public static void FixMissingChannelTypeAndId(NotificationMarkReadEventInternalDTO dto)
115+
{
116+
if (string.IsNullOrEmpty(dto.Channel.Id))
117+
{
118+
dto.Channel.Id = dto.ChannelId;
119+
}
120+
121+
if (string.IsNullOrEmpty(dto.Channel.Type))
122+
{
123+
dto.Channel.Type = dto.ChannelType;
124+
}
125+
}
126+
127+
128+
}
129+
}

Assets/Plugins/StreamChat/Core/Helpers/InternalNotificationsHelper.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/StreamChat/Core/LowLevelClient/Events/EventNotificationMarkRead.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace StreamChat.Core.LowLevelClient.Events
1010
/// Trigger: when the total count of unread messages (across all channels the user is a member) changes
1111
/// Recipients: clients from the user removed that are not watching the channel
1212
/// </summary>
13-
public partial class EventNotificationMarkRead : EventBase, ILoadableFrom<NotificationMarkReadEventInternalDTO, EventNotificationMarkRead>
13+
public partial class EventNotificationMarkRead : EventBase,
14+
ILoadableFrom<NotificationMarkReadEventInternalDTO, EventNotificationMarkRead>
1415
{
1516
public Channel Channel { get; set; }
1617

@@ -33,7 +34,8 @@ public partial class EventNotificationMarkRead : EventBase, ILoadableFrom<Notifi
3334

3435
public User User { get; set; }
3536

36-
EventNotificationMarkRead ILoadableFrom<NotificationMarkReadEventInternalDTO, EventNotificationMarkRead>.LoadFromDto(NotificationMarkReadEventInternalDTO dto)
37+
EventNotificationMarkRead ILoadableFrom<NotificationMarkReadEventInternalDTO, EventNotificationMarkRead>.
38+
LoadFromDto(NotificationMarkReadEventInternalDTO dto)
3739
{
3840
Channel = Channel.TryLoadFromDto(dto.Channel);
3941
ChannelId = dto.ChannelId;

Assets/Plugins/StreamChat/Core/LowLevelClient/Events/EventNotificationMessageNew.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
namespace StreamChat.Core.LowLevelClient.Events
77
{
8-
public partial class EventNotificationMessageNew : EventBase, ILoadableFrom<NotificationNewMessageEventInternalDTO, EventNotificationMessageNew>
8+
public partial class EventNotificationMessageNew : EventBase,
9+
ILoadableFrom<NotificationNewMessageEventInternalDTO, EventNotificationMessageNew>
910
{
1011
public Channel Channel { get; set; }
1112

@@ -21,7 +22,8 @@ public partial class EventNotificationMessageNew : EventBase, ILoadableFrom<Noti
2122

2223
public string Type { get; set; }
2324

24-
EventNotificationMessageNew ILoadableFrom<NotificationNewMessageEventInternalDTO, EventNotificationMessageNew>.LoadFromDto(NotificationNewMessageEventInternalDTO dto)
25+
EventNotificationMessageNew ILoadableFrom<NotificationNewMessageEventInternalDTO, EventNotificationMessageNew>.
26+
LoadFromDto(NotificationNewMessageEventInternalDTO dto)
2527
{
2628
Channel = Channel.TryLoadFromDto(dto.Channel);
2729
ChannelId = dto.ChannelId;

Assets/Plugins/StreamChat/Core/LowLevelClient/StreamChatLowLevelClient.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using StreamChat.Libs.Websockets;
2828
using StreamChat.Core.LowLevelClient.Requests;
2929
using System.Linq;
30+
using StreamChat.Core.Helpers;
3031

3132
#if STREAM_TESTS_ENABLED
3233
using System.Runtime.CompilerServices;
@@ -752,45 +753,45 @@ private void RegisterEventHandlers()
752753

753754
RegisterEventType<NotificationMarkReadEventInternalDTO, EventNotificationMarkRead>(
754755
WSEventType.NotificationMarkRead,
755-
(e, dto) => NotificationMarkRead?.Invoke(e), dto => InternalNotificationMarkRead?.Invoke(dto));
756+
(e, dto) => NotificationMarkRead?.Invoke(e), dto => InternalNotificationMarkRead?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
756757
RegisterEventType<NotificationNewMessageEventInternalDTO, EventNotificationMessageNew>(
757758
WSEventType.NotificationMessageNew,
758759
(e, dto) => NotificationMessageReceived?.Invoke(e),
759-
dto => InternalNotificationMessageReceived?.Invoke(dto));
760+
dto => InternalNotificationMessageReceived?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
760761

761762
RegisterEventType<NotificationChannelDeletedEventInternalDTO, EventNotificationChannelDeleted>(
762763
WSEventType.NotificationChannelDeleted,
763764
(e, dto) => NotificationChannelDeleted?.Invoke(e),
764-
dto => InternalNotificationChannelDeleted?.Invoke(dto));
765+
dto => InternalNotificationChannelDeleted?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
765766
RegisterEventType<NotificationChannelTruncatedEventInternalDTO, EventNotificationChannelTruncated>(
766767
WSEventType.NotificationChannelTruncated,
767768
(e, dto) => NotificationChannelTruncated?.Invoke(e),
768-
dto => InternalNotificationChannelTruncated?.Invoke(dto));
769+
dto => InternalNotificationChannelTruncated?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
769770

770771
RegisterEventType<NotificationAddedToChannelEventInternalDTO, EventNotificationAddedToChannel>(
771772
WSEventType.NotificationAddedToChannel,
772773
(e, dto) => NotificationAddedToChannel?.Invoke(e),
773-
dto => InternalNotificationAddedToChannel?.Invoke(dto));
774+
dto => InternalNotificationAddedToChannel?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
774775
RegisterEventType<NotificationRemovedFromChannelEventInternalDTO, EventNotificationRemovedFromChannel>(
775776
WSEventType.NotificationRemovedFromChannel,
776777
(e, dto) => NotificationRemovedFromChannel?.Invoke(e),
777-
dto => InternalNotificationRemovedFromChannel?.Invoke(dto));
778+
dto => InternalNotificationRemovedFromChannel?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
778779

779780
RegisterEventType<NotificationInvitedEventInternalDTO, EventNotificationInvited>(
780781
WSEventType.NotificationInvited,
781-
(e, dto) => NotificationInvited?.Invoke(e), dto => InternalNotificationInvited?.Invoke(dto));
782+
(e, dto) => NotificationInvited?.Invoke(e), dto => InternalNotificationInvited?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
782783
RegisterEventType<NotificationInviteAcceptedEventInternalDTO, EventNotificationInviteAccepted>(
783784
WSEventType.NotificationInviteAccepted,
784785
(e, dto) => NotificationInviteAccepted?.Invoke(e),
785-
dto => InternalNotificationInviteAccepted?.Invoke(dto));
786+
dto => InternalNotificationInviteAccepted?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
786787
RegisterEventType<NotificationInviteRejectedEventInternalDTO, EventNotificationInviteRejected>(
787788
WSEventType.NotificationInviteRejected,
788789
(e, dto) => NotificationInviteRejected?.Invoke(e),
789-
dto => InternalNotificationInviteRejected?.Invoke(dto));
790+
dto => InternalNotificationInviteRejected?.Invoke(dto), InternalNotificationsHelper.FixMissingChannelTypeAndId);
790791
}
791792

792793
private void RegisterEventType<TDto, TEvent>(string key,
793-
Action<TEvent, TDto> handler, Action<TDto> internalHandler = null)
794+
Action<TEvent, TDto> handler, Action<TDto> internalHandler = null, Action<TDto> postprocess = null)
794795
where TEvent : EventBase, ILoadableFrom<TDto, TEvent>, new()
795796
{
796797
if (_eventKeyToHandler.ContainsKey(key))
@@ -804,6 +805,7 @@ private void RegisterEventType<TDto, TEvent>(string key,
804805
try
805806
{
806807
var eventObj = DeserializeEvent<TDto, TEvent>(serializedContent, out var dto);
808+
postprocess?.Invoke(dto);
807809
_lastEventReceivedAt = eventObj.CreatedAt;
808810
handler?.Invoke(eventObj, dto);
809811
internalHandler?.Invoke(dto);

0 commit comments

Comments
 (0)