@@ -864,45 +864,135 @@ private void OnMarkReadNotification(NotificationMarkReadEventInternalDTO eventDt
864864
865865 private void OnAddedToChannelNotification ( NotificationAddedToChannelEventInternalDTO eventDto )
866866 {
867- var channel = _cache . TryCreateOrUpdate ( eventDto . Channel ) ;
867+ var channel = _cache . TryCreateOrUpdate ( eventDto . Channel , out var wasCreated ) ;
868868 var member = _cache . TryCreateOrUpdate ( eventDto . Member ) ;
869869 _cache . TryCreateOrUpdate ( eventDto . Member . User ) ;
870+
871+ if ( ! wasCreated )
872+ {
873+ AddedToChannelAsMember ? . Invoke ( channel , member ) ;
874+ return ;
875+ }
870876
871- AddedToChannelAsMember ? . Invoke ( channel , member ) ;
877+ // Watch channel, otherwise WS events won't be received
878+ GetOrCreateChannelWithIdAsync ( channel . Type , channel . Id ) . ContinueWith ( t =>
879+ {
880+ if ( t . IsFaulted )
881+ {
882+ _logs . Error ( $ "Failed to watch channel with type: { channel . Type } & id: { channel . Id } " +
883+ $ "before triggering the { nameof ( AddedToChannelAsMember ) } event. Inspect the following exception.") ;
884+ _logs . Exception ( t . Exception ) ;
885+ return ;
886+ }
887+
888+ AddedToChannelAsMember ? . Invoke ( channel , member ) ;
889+ } ) ;
872890 }
873891
874892 private void OnRemovedFromChannelNotification (
875893 NotificationRemovedFromChannelEventInternalDTO eventDto )
876894 {
877- var channel = _cache . TryCreateOrUpdate ( eventDto . Channel ) ;
895+ var channel = _cache . TryCreateOrUpdate ( eventDto . Channel , out var wasCreated ) ;
878896 var member = _cache . TryCreateOrUpdate ( eventDto . Member ) ;
879897 _cache . TryCreateOrUpdate ( eventDto . Member . User ) ;
880898
881- RemovedFromChannelAsMember ? . Invoke ( channel , member ) ;
899+ if ( ! wasCreated )
900+ {
901+ RemovedFromChannelAsMember ? . Invoke ( channel , member ) ;
902+ return ;
903+ }
904+
905+ // Watch channel, otherwise WS events won't be received
906+ GetOrCreateChannelWithIdAsync ( channel . Type , channel . Id ) . ContinueWith ( t =>
907+ {
908+ if ( t . IsFaulted )
909+ {
910+ _logs . Error ( $ "Failed to watch channel with type: { channel . Type } & id: { channel . Id } " +
911+ $ "before triggering the { nameof ( RemovedFromChannelAsMember ) } event. Inspect the following exception.") ;
912+ _logs . Exception ( t . Exception ) ;
913+ return ;
914+ }
915+
916+ RemovedFromChannelAsMember ? . Invoke ( channel , member ) ;
917+ } ) ;
882918 }
883919
884920 private void OnInvitedNotification ( NotificationInvitedEventInternalDTO eventDto )
885921 {
886- var channel = _cache . TryCreateOrUpdate ( eventDto . Channel ) ;
922+ var channel = _cache . TryCreateOrUpdate ( eventDto . Channel , out var wasCreated ) ;
887923 var user = _cache . TryCreateOrUpdate ( eventDto . User ) ;
924+
925+ if ( ! wasCreated )
926+ {
927+ ChannelInviteReceived ? . Invoke ( channel , user ) ;
928+ return ;
929+ }
930+
931+ // Watch channel, otherwise WS events won't be received
932+ GetOrCreateChannelWithIdAsync ( channel . Type , channel . Id ) . ContinueWith ( t =>
933+ {
934+ if ( t . IsFaulted )
935+ {
936+ _logs . Error ( $ "Failed to watch channel with type: { channel . Type } & id: { channel . Id } " +
937+ $ "before triggering the { nameof ( ChannelInviteReceived ) } event. Inspect the following exception.") ;
938+ _logs . Exception ( t . Exception ) ;
939+ return ;
940+ }
888941
889- ChannelInviteReceived ? . Invoke ( channel , user ) ;
942+ ChannelInviteReceived ? . Invoke ( channel , user ) ;
943+ } ) ;
890944 }
891945
892946 private void OnInviteAcceptedNotification ( NotificationInviteAcceptedEventInternalDTO eventDto )
893947 {
894- var channel = _cache . TryCreateOrUpdate ( eventDto . Channel ) ;
948+ var channel = _cache . TryCreateOrUpdate ( eventDto . Channel , out var wasCreated ) ;
895949 var user = _cache . TryCreateOrUpdate ( eventDto . User ) ;
950+
951+ if ( ! wasCreated )
952+ {
953+ ChannelInviteAccepted ? . Invoke ( channel , user ) ;
954+ return ;
955+ }
956+
957+ // Watch channel, otherwise WS events won't be received
958+ GetOrCreateChannelWithIdAsync ( channel . Type , channel . Id ) . ContinueWith ( t =>
959+ {
960+ if ( t . IsFaulted )
961+ {
962+ _logs . Error ( $ "Failed to watch channel with type: { channel . Type } & id: { channel . Id } " +
963+ $ "before triggering the { nameof ( ChannelInviteAccepted ) } event. Inspect the following exception.") ;
964+ _logs . Exception ( t . Exception ) ;
965+ return ;
966+ }
896967
897- ChannelInviteAccepted ? . Invoke ( channel , user ) ;
968+ ChannelInviteAccepted ? . Invoke ( channel , user ) ;
969+ } ) ;
898970 }
899971
900972 private void OnInviteRejectedNotification ( NotificationInviteRejectedEventInternalDTO eventDto )
901973 {
902- var channel = _cache . TryCreateOrUpdate ( eventDto . Channel ) ;
974+ var channel = _cache . TryCreateOrUpdate ( eventDto . Channel , out var wasCreated ) ;
903975 var user = _cache . TryCreateOrUpdate ( eventDto . User ) ;
976+
977+ if ( ! wasCreated )
978+ {
979+ ChannelInviteRejected ? . Invoke ( channel , user ) ;
980+ return ;
981+ }
982+
983+ // Watch channel, otherwise WS events won't be received
984+ GetOrCreateChannelWithIdAsync ( channel . Type , channel . Id ) . ContinueWith ( t =>
985+ {
986+ if ( t . IsFaulted )
987+ {
988+ _logs . Error ( $ "Failed to watch channel with type: { channel . Type } & id: { channel . Id } " +
989+ $ "before triggering the { nameof ( ChannelInviteRejected ) } event. Inspect the following exception.") ;
990+ _logs . Exception ( t . Exception ) ;
991+ return ;
992+ }
904993
905- ChannelInviteRejected ? . Invoke ( channel , user ) ;
994+ ChannelInviteRejected ? . Invoke ( channel , user ) ;
995+ } ) ;
906996 }
907997
908998 private void OnReactionReceived ( ReactionNewEventInternalDTO eventDto )
0 commit comments