< Summary

Class:DCL.Social.Chat.WebInterfaceChatBridge
Assembly:ChatController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ChatController/WebInterfaceChatBridge.cs
Covered lines:8
Uncovered lines:71
Coverable lines:79
Total lines:233
Line coverage:10.1% (8 of 79)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:33
Method coverage:21.2% (7 of 33)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetOrCreate()0%440100%
WebInterfaceChatBridge()0%110100%
InitializeChat(...)0%6200%
AddMessageToChatWindow(...)0%6200%
AddChatMessages(...)0%12300%
UpdateTotalUnseenMessages(...)0%6200%
UpdateUserUnseenMessages(...)0%6200%
UpdateTotalUnseenMessagesByUser(...)0%12300%
UpdateTotalUnseenMessagesByChannel(...)0%12300%
UpdateChannelMembers(...)0%6200%
JoinChannelConfirmation(...)0%12300%
JoinChannelError(...)0%12300%
LeaveChannelError(...)0%6200%
UpdateChannelInfo(...)0%6200%
MuteChannelError(...)0%6200%
UpdateChannelSearchResults(...)0%12300%
LeaveChannel(...)0%2100%
GetChannelMessages(...)0%2100%
JoinOrCreateChannelAsync(...)0%6200%
JoinOrCreateChannel(...)0%2100%
GetJoinedChannels(...)0%2100%
GetChannels(...)0%110100%
GetChannelsAsync(...)0%6200%
MuteChannel(...)0%2100%
GetPrivateMessages(...)0%2100%
MarkChannelMessagesAsSeen(...)0%110100%
GetUnseenMessagesByUser()0%2100%
GetUnseenMessagesByChannel()0%2100%
CreateChannel(...)0%2100%
GetChannelInfo(...)0%110100%
GetChannelMembers(...)0%110100%
SendChatMessage(...)0%2100%
MarkMessagesAsSeen(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ChatController/WebInterfaceChatBridge.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Linq;
 4using DCL.Chat.Channels;
 5using DCL.Chat.WebApi;
 6using DCL.Helpers;
 7using DCL.Interface;
 8using JetBrains.Annotations;
 9using System.Collections.Generic;
 10using System.Threading;
 11using UnityEngine;
 12
 13namespace DCL.Social.Chat
 14{
 15    public class WebInterfaceChatBridge : MonoBehaviour, IChatApiBridge
 16    {
 17        private const string JOIN_OR_CREATE_CHANNEL_TASK_ID = "JoinOrCreateChannelAsync";
 18        private const string GET_CHANNELS_TASK_ID = "GetChannelsAsync";
 19        private const double REQUEST_TIMEOUT_SECONDS = 30;
 20
 21        public static IChatApiBridge GetOrCreate()
 22        {
 42523            var bridgeObj = SceneReferences.i?.bridgeGameObject;
 42524            return bridgeObj == null
 25                ? new GameObject("Bridges").AddComponent<WebInterfaceChatBridge>()
 26                : bridgeObj.GetOrCreateComponent<WebInterfaceChatBridge>();
 27        }
 28
 29        public event Action<InitializeChatPayload> OnInitialized;
 30        public event Action<ChatMessage[]> OnAddMessage;
 31        public event Action<UpdateTotalUnseenMessagesPayload> OnTotalUnseenMessagesChanged;
 32        public event Action<(string userId, int count)[]> OnUserUnseenMessagesChanged;
 33        public event Action<(string channelId, int count)[]> OnChannelUnseenMessagesChanged;
 34        public event Action<UpdateChannelMembersPayload> OnChannelMembersUpdated;
 35        public event Action<ChannelInfoPayloads> OnChannelJoined;
 36        public event Action<JoinChannelErrorPayload> OnChannelJoinFailed;
 37        public event Action<JoinChannelErrorPayload> OnChannelLeaveFailed;
 38        public event Action<ChannelInfoPayloads> OnChannelsUpdated;
 39        public event Action<MuteChannelErrorPayload> OnMuteChannelFailed;
 40        public event Action<ChannelSearchResultsPayload> OnChannelSearchResults;
 41
 42542        private readonly Dictionary<string, IUniTaskSource> pendingTasks = new ();
 43
 44        [PublicAPI]
 45        public void InitializeChat(string json) =>
 046            OnInitialized?.Invoke(JsonUtility.FromJson<InitializeChatPayload>(json));
 47
 48        [PublicAPI]
 49        public void AddMessageToChatWindow(string jsonMessage) =>
 050            OnAddMessage?.Invoke(new[] { JsonUtility.FromJson<ChatMessage>(jsonMessage) });
 51
 52        [PublicAPI]
 53        public void AddChatMessages(string jsonMessage)
 54        {
 055            var msg = JsonUtility.FromJson<ChatMessageListPayload>(jsonMessage);
 056            if (msg == null) return;
 057            OnAddMessage?.Invoke(msg.messages);
 058        }
 59
 60        [PublicAPI]
 61        public void UpdateTotalUnseenMessages(string json) =>
 062            OnTotalUnseenMessagesChanged?.Invoke(JsonUtility.FromJson<UpdateTotalUnseenMessagesPayload>(json));
 63
 64        [PublicAPI]
 65        public void UpdateUserUnseenMessages(string json)
 66        {
 067            var msg = JsonUtility.FromJson<UpdateUserUnseenMessagesPayload>(json);
 068            OnUserUnseenMessagesChanged?.Invoke(new[] { (msg.userId, msg.total) });
 069        }
 70
 71        [PublicAPI]
 72        public void UpdateTotalUnseenMessagesByUser(string json)
 73        {
 074            var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByUserPayload>(json);
 75
 076            OnUserUnseenMessagesChanged?.Invoke(msg.unseenPrivateMessages
 077                                                   .Select(unseenMessages => (unseenMessages.userId, unseenMessages.coun
 78                                                   .ToArray());
 079        }
 80
 81        [PublicAPI]
 82        public void UpdateTotalUnseenMessagesByChannel(string json)
 83        {
 084            var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByChannelPayload>(json);
 85
 086            OnChannelUnseenMessagesChanged?.Invoke(msg.unseenChannelMessages
 087                                                      .Select(message => (message.channelId, message.count))
 88                                                      .ToArray());
 089        }
 90
 91        [PublicAPI]
 92        public void UpdateChannelMembers(string json) =>
 093            OnChannelMembersUpdated?.Invoke(JsonUtility.FromJson<UpdateChannelMembersPayload>(json));
 94
 95        [PublicAPI]
 96        public void JoinChannelConfirmation(string payload)
 97        {
 098            ChannelInfoPayloads payloads = JsonUtility.FromJson<ChannelInfoPayloads>(payload);
 99
 0100            if (pendingTasks.TryGetValue(JOIN_OR_CREATE_CHANNEL_TASK_ID, out var task))
 101            {
 0102                pendingTasks.Remove(JOIN_OR_CREATE_CHANNEL_TASK_ID);
 0103                UniTaskCompletionSource<ChannelInfoPayload> taskCompletionSource = (UniTaskCompletionSource<ChannelInfoP
 0104                taskCompletionSource.TrySetResult(payloads.channelInfoPayload[0]);
 105            }
 106
 0107            OnChannelJoined?.Invoke(payloads);
 0108        }
 109
 110        [PublicAPI]
 111        public void JoinChannelError(string payload)
 112        {
 0113            JoinChannelErrorPayload errorPayload = JsonUtility.FromJson<JoinChannelErrorPayload>(payload);
 114
 0115            if (pendingTasks.TryGetValue(JOIN_OR_CREATE_CHANNEL_TASK_ID, out var task))
 116            {
 0117                pendingTasks.Remove(JOIN_OR_CREATE_CHANNEL_TASK_ID);
 0118                UniTaskCompletionSource<ChannelInfoPayload> taskCompletionSource = (UniTaskCompletionSource<ChannelInfoP
 0119                taskCompletionSource.TrySetException(new ChannelException(errorPayload.channelId, (ChannelErrorCode) err
 120            }
 121
 0122            OnChannelJoinFailed?.Invoke(errorPayload);
 0123        }
 124
 125        [PublicAPI]
 126        public void LeaveChannelError(string payload) =>
 0127            OnChannelLeaveFailed?.Invoke(JsonUtility.FromJson<JoinChannelErrorPayload>(payload));
 128
 129        [PublicAPI]
 130        public void UpdateChannelInfo(string payload) =>
 0131            OnChannelsUpdated?.Invoke(JsonUtility.FromJson<ChannelInfoPayloads>(payload));
 132
 133        [PublicAPI]
 134        public void MuteChannelError(string payload) =>
 0135            OnMuteChannelFailed?.Invoke(JsonUtility.FromJson<MuteChannelErrorPayload>(payload));
 136
 137        [PublicAPI]
 138        public void UpdateChannelSearchResults(string payload)
 139        {
 0140            ChannelSearchResultsPayload searchResultPayload = JsonUtility.FromJson<ChannelSearchResultsPayload>(payload)
 141
 0142            if (pendingTasks.TryGetValue(GET_CHANNELS_TASK_ID, out var task))
 143            {
 0144                pendingTasks.Remove(GET_CHANNELS_TASK_ID);
 0145                UniTaskCompletionSource<ChannelSearchResultsPayload> taskCompletionSource = (UniTaskCompletionSource<Cha
 0146                taskCompletionSource.TrySetResult(searchResultPayload);
 147            }
 148
 0149            OnChannelSearchResults?.Invoke(searchResultPayload);
 0150        }
 151
 152        public void LeaveChannel(string channelId) =>
 0153            WebInterface.LeaveChannel(channelId);
 154
 155        public void GetChannelMessages(string channelId, int limit, string fromMessageId) =>
 0156            WebInterface.GetChannelMessages(channelId, limit, fromMessageId);
 157
 158        public UniTask<ChannelInfoPayload> JoinOrCreateChannelAsync(string channelId, CancellationToken cancellationToke
 159        {
 0160            if (pendingTasks.ContainsKey(JOIN_OR_CREATE_CHANNEL_TASK_ID))
 161            {
 0162                UniTaskCompletionSource<ChannelInfoPayload> pendingTask = (UniTaskCompletionSource<ChannelInfoPayload>)p
 0163                cancellationToken.RegisterWithoutCaptureExecutionContext(() => pendingTask.TrySetCanceled());
 0164                return pendingTask.Task;
 165            }
 166
 0167            UniTaskCompletionSource<ChannelInfoPayload> task = new ();
 0168            pendingTasks[JOIN_OR_CREATE_CHANNEL_TASK_ID] = task;
 0169            cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled());
 170
 0171            WebInterface.JoinOrCreateChannel(channelId);
 172
 0173            return task.Task.Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT_SECONDS));
 174        }
 175
 176        public void JoinOrCreateChannel(string channelId) =>
 0177            WebInterface.JoinOrCreateChannel(channelId);
 178
 179        public void GetJoinedChannels(int limit, int skip) =>
 0180            WebInterface.GetJoinedChannels(limit, skip);
 181
 182        public void GetChannels(int limit, string paginationToken, string name) =>
 1183            WebInterface.GetChannels(limit, paginationToken, name);
 184
 185        public UniTask<ChannelSearchResultsPayload> GetChannelsAsync(int limit, string name, string paginationToken, Can
 186        {
 0187            if (pendingTasks.ContainsKey(GET_CHANNELS_TASK_ID))
 188            {
 0189                UniTaskCompletionSource<ChannelSearchResultsPayload> pendingTask = (UniTaskCompletionSource<ChannelSearc
 0190                cancellationToken.RegisterWithoutCaptureExecutionContext(() => pendingTask.TrySetCanceled());
 0191                return pendingTask.Task;
 192            }
 193
 0194            UniTaskCompletionSource<ChannelSearchResultsPayload> task = new ();
 0195            pendingTasks[GET_CHANNELS_TASK_ID] = task;
 0196            cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled());
 197
 0198            WebInterface.GetChannels(limit, paginationToken, name);
 199
 0200            return task.Task.Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT_SECONDS));
 201        }
 202
 203        public void MuteChannel(string channelId, bool muted) =>
 0204            WebInterface.MuteChannel(channelId, muted);
 205
 206        public void GetPrivateMessages(string userId, int limit, string fromMessageId) =>
 0207            WebInterface.GetPrivateMessages(userId, limit, fromMessageId);
 208
 209        public void MarkChannelMessagesAsSeen(string channelId) =>
 4210            WebInterface.MarkChannelMessagesAsSeen(channelId);
 211
 212        public void GetUnseenMessagesByUser() =>
 0213            WebInterface.GetUnseenMessagesByUser();
 214
 215        public void GetUnseenMessagesByChannel() =>
 0216            WebInterface.GetUnseenMessagesByChannel();
 217
 218        public void CreateChannel(string channelId) =>
 0219            WebInterface.CreateChannel(channelId);
 220
 221        public void GetChannelInfo(string[] channelIds) =>
 1222            WebInterface.GetChannelInfo(channelIds);
 223
 224        public void GetChannelMembers(string channelId, int limit, int skip, string name) =>
 1225            WebInterface.GetChannelMembers(channelId, limit, skip, name);
 226
 227        public void SendChatMessage(ChatMessage message) =>
 0228            WebInterface.SendChatMessage(message);
 229
 230        public void MarkMessagesAsSeen(string userId) =>
 1231            WebInterface.MarkMessagesAsSeen(userId);
 232    }
 233}