| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL.Chat.Channels; |
| | 5 | | using DCL.Chat.WebApi; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using DCL.Interface; |
| | 8 | | using JetBrains.Annotations; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Threading; |
| | 11 | | using UnityEngine; |
| | 12 | |
|
| | 13 | | namespace 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 | | { |
| 425 | 23 | | var bridgeObj = SceneReferences.i?.bridgeGameObject; |
| 425 | 24 | | 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 | |
|
| 425 | 42 | | private readonly Dictionary<string, IUniTaskSource> pendingTasks = new (); |
| | 43 | |
|
| | 44 | | [PublicAPI] |
| | 45 | | public void InitializeChat(string json) => |
| 0 | 46 | | OnInitialized?.Invoke(JsonUtility.FromJson<InitializeChatPayload>(json)); |
| | 47 | |
|
| | 48 | | [PublicAPI] |
| | 49 | | public void AddMessageToChatWindow(string jsonMessage) => |
| 0 | 50 | | OnAddMessage?.Invoke(new[] { JsonUtility.FromJson<ChatMessage>(jsonMessage) }); |
| | 51 | |
|
| | 52 | | [PublicAPI] |
| | 53 | | public void AddChatMessages(string jsonMessage) |
| | 54 | | { |
| 0 | 55 | | var msg = JsonUtility.FromJson<ChatMessageListPayload>(jsonMessage); |
| 0 | 56 | | if (msg == null) return; |
| 0 | 57 | | OnAddMessage?.Invoke(msg.messages); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | [PublicAPI] |
| | 61 | | public void UpdateTotalUnseenMessages(string json) => |
| 0 | 62 | | OnTotalUnseenMessagesChanged?.Invoke(JsonUtility.FromJson<UpdateTotalUnseenMessagesPayload>(json)); |
| | 63 | |
|
| | 64 | | [PublicAPI] |
| | 65 | | public void UpdateUserUnseenMessages(string json) |
| | 66 | | { |
| 0 | 67 | | var msg = JsonUtility.FromJson<UpdateUserUnseenMessagesPayload>(json); |
| 0 | 68 | | OnUserUnseenMessagesChanged?.Invoke(new[] { (msg.userId, msg.total) }); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | [PublicAPI] |
| | 72 | | public void UpdateTotalUnseenMessagesByUser(string json) |
| | 73 | | { |
| 0 | 74 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByUserPayload>(json); |
| | 75 | |
|
| 0 | 76 | | OnUserUnseenMessagesChanged?.Invoke(msg.unseenPrivateMessages |
| 0 | 77 | | .Select(unseenMessages => (unseenMessages.userId, unseenMessages.coun |
| | 78 | | .ToArray()); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | [PublicAPI] |
| | 82 | | public void UpdateTotalUnseenMessagesByChannel(string json) |
| | 83 | | { |
| 0 | 84 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByChannelPayload>(json); |
| | 85 | |
|
| 0 | 86 | | OnChannelUnseenMessagesChanged?.Invoke(msg.unseenChannelMessages |
| 0 | 87 | | .Select(message => (message.channelId, message.count)) |
| | 88 | | .ToArray()); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | [PublicAPI] |
| | 92 | | public void UpdateChannelMembers(string json) => |
| 0 | 93 | | OnChannelMembersUpdated?.Invoke(JsonUtility.FromJson<UpdateChannelMembersPayload>(json)); |
| | 94 | |
|
| | 95 | | [PublicAPI] |
| | 96 | | public void JoinChannelConfirmation(string payload) |
| | 97 | | { |
| 0 | 98 | | ChannelInfoPayloads payloads = JsonUtility.FromJson<ChannelInfoPayloads>(payload); |
| | 99 | |
|
| 0 | 100 | | if (pendingTasks.TryGetValue(JOIN_OR_CREATE_CHANNEL_TASK_ID, out var task)) |
| | 101 | | { |
| 0 | 102 | | pendingTasks.Remove(JOIN_OR_CREATE_CHANNEL_TASK_ID); |
| 0 | 103 | | UniTaskCompletionSource<ChannelInfoPayload> taskCompletionSource = (UniTaskCompletionSource<ChannelInfoP |
| 0 | 104 | | taskCompletionSource.TrySetResult(payloads.channelInfoPayload[0]); |
| | 105 | | } |
| | 106 | |
|
| 0 | 107 | | OnChannelJoined?.Invoke(payloads); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | [PublicAPI] |
| | 111 | | public void JoinChannelError(string payload) |
| | 112 | | { |
| 0 | 113 | | JoinChannelErrorPayload errorPayload = JsonUtility.FromJson<JoinChannelErrorPayload>(payload); |
| | 114 | |
|
| 0 | 115 | | if (pendingTasks.TryGetValue(JOIN_OR_CREATE_CHANNEL_TASK_ID, out var task)) |
| | 116 | | { |
| 0 | 117 | | pendingTasks.Remove(JOIN_OR_CREATE_CHANNEL_TASK_ID); |
| 0 | 118 | | UniTaskCompletionSource<ChannelInfoPayload> taskCompletionSource = (UniTaskCompletionSource<ChannelInfoP |
| 0 | 119 | | taskCompletionSource.TrySetException(new ChannelException(errorPayload.channelId, (ChannelErrorCode) err |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | OnChannelJoinFailed?.Invoke(errorPayload); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | [PublicAPI] |
| | 126 | | public void LeaveChannelError(string payload) => |
| 0 | 127 | | OnChannelLeaveFailed?.Invoke(JsonUtility.FromJson<JoinChannelErrorPayload>(payload)); |
| | 128 | |
|
| | 129 | | [PublicAPI] |
| | 130 | | public void UpdateChannelInfo(string payload) => |
| 0 | 131 | | OnChannelsUpdated?.Invoke(JsonUtility.FromJson<ChannelInfoPayloads>(payload)); |
| | 132 | |
|
| | 133 | | [PublicAPI] |
| | 134 | | public void MuteChannelError(string payload) => |
| 0 | 135 | | OnMuteChannelFailed?.Invoke(JsonUtility.FromJson<MuteChannelErrorPayload>(payload)); |
| | 136 | |
|
| | 137 | | [PublicAPI] |
| | 138 | | public void UpdateChannelSearchResults(string payload) |
| | 139 | | { |
| 0 | 140 | | ChannelSearchResultsPayload searchResultPayload = JsonUtility.FromJson<ChannelSearchResultsPayload>(payload) |
| | 141 | |
|
| 0 | 142 | | if (pendingTasks.TryGetValue(GET_CHANNELS_TASK_ID, out var task)) |
| | 143 | | { |
| 0 | 144 | | pendingTasks.Remove(GET_CHANNELS_TASK_ID); |
| 0 | 145 | | UniTaskCompletionSource<ChannelSearchResultsPayload> taskCompletionSource = (UniTaskCompletionSource<Cha |
| 0 | 146 | | taskCompletionSource.TrySetResult(searchResultPayload); |
| | 147 | | } |
| | 148 | |
|
| 0 | 149 | | OnChannelSearchResults?.Invoke(searchResultPayload); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | public void LeaveChannel(string channelId) => |
| 0 | 153 | | WebInterface.LeaveChannel(channelId); |
| | 154 | |
|
| | 155 | | public void GetChannelMessages(string channelId, int limit, string fromMessageId) => |
| 0 | 156 | | WebInterface.GetChannelMessages(channelId, limit, fromMessageId); |
| | 157 | |
|
| | 158 | | public UniTask<ChannelInfoPayload> JoinOrCreateChannelAsync(string channelId, CancellationToken cancellationToke |
| | 159 | | { |
| 0 | 160 | | if (pendingTasks.ContainsKey(JOIN_OR_CREATE_CHANNEL_TASK_ID)) |
| | 161 | | { |
| 0 | 162 | | UniTaskCompletionSource<ChannelInfoPayload> pendingTask = (UniTaskCompletionSource<ChannelInfoPayload>)p |
| 0 | 163 | | cancellationToken.RegisterWithoutCaptureExecutionContext(() => pendingTask.TrySetCanceled()); |
| 0 | 164 | | return pendingTask.Task; |
| | 165 | | } |
| | 166 | |
|
| 0 | 167 | | UniTaskCompletionSource<ChannelInfoPayload> task = new (); |
| 0 | 168 | | pendingTasks[JOIN_OR_CREATE_CHANNEL_TASK_ID] = task; |
| 0 | 169 | | cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled()); |
| | 170 | |
|
| 0 | 171 | | WebInterface.JoinOrCreateChannel(channelId); |
| | 172 | |
|
| 0 | 173 | | return task.Task.Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT_SECONDS)); |
| | 174 | | } |
| | 175 | |
|
| | 176 | | public void JoinOrCreateChannel(string channelId) => |
| 0 | 177 | | WebInterface.JoinOrCreateChannel(channelId); |
| | 178 | |
|
| | 179 | | public void GetJoinedChannels(int limit, int skip) => |
| 0 | 180 | | WebInterface.GetJoinedChannels(limit, skip); |
| | 181 | |
|
| | 182 | | public void GetChannels(int limit, string paginationToken, string name) => |
| 1 | 183 | | WebInterface.GetChannels(limit, paginationToken, name); |
| | 184 | |
|
| | 185 | | public UniTask<ChannelSearchResultsPayload> GetChannelsAsync(int limit, string name, string paginationToken, Can |
| | 186 | | { |
| 0 | 187 | | if (pendingTasks.ContainsKey(GET_CHANNELS_TASK_ID)) |
| | 188 | | { |
| 0 | 189 | | UniTaskCompletionSource<ChannelSearchResultsPayload> pendingTask = (UniTaskCompletionSource<ChannelSearc |
| 0 | 190 | | cancellationToken.RegisterWithoutCaptureExecutionContext(() => pendingTask.TrySetCanceled()); |
| 0 | 191 | | return pendingTask.Task; |
| | 192 | | } |
| | 193 | |
|
| 0 | 194 | | UniTaskCompletionSource<ChannelSearchResultsPayload> task = new (); |
| 0 | 195 | | pendingTasks[GET_CHANNELS_TASK_ID] = task; |
| 0 | 196 | | cancellationToken.RegisterWithoutCaptureExecutionContext(() => task.TrySetCanceled()); |
| | 197 | |
|
| 0 | 198 | | WebInterface.GetChannels(limit, paginationToken, name); |
| | 199 | |
|
| 0 | 200 | | return task.Task.Timeout(TimeSpan.FromSeconds(REQUEST_TIMEOUT_SECONDS)); |
| | 201 | | } |
| | 202 | |
|
| | 203 | | public void MuteChannel(string channelId, bool muted) => |
| 0 | 204 | | WebInterface.MuteChannel(channelId, muted); |
| | 205 | |
|
| | 206 | | public void GetPrivateMessages(string userId, int limit, string fromMessageId) => |
| 0 | 207 | | WebInterface.GetPrivateMessages(userId, limit, fromMessageId); |
| | 208 | |
|
| | 209 | | public void MarkChannelMessagesAsSeen(string channelId) => |
| 4 | 210 | | WebInterface.MarkChannelMessagesAsSeen(channelId); |
| | 211 | |
|
| | 212 | | public void GetUnseenMessagesByUser() => |
| 0 | 213 | | WebInterface.GetUnseenMessagesByUser(); |
| | 214 | |
|
| | 215 | | public void GetUnseenMessagesByChannel() => |
| 0 | 216 | | WebInterface.GetUnseenMessagesByChannel(); |
| | 217 | |
|
| | 218 | | public void CreateChannel(string channelId) => |
| 0 | 219 | | WebInterface.CreateChannel(channelId); |
| | 220 | |
|
| | 221 | | public void GetChannelInfo(string[] channelIds) => |
| 1 | 222 | | WebInterface.GetChannelInfo(channelIds); |
| | 223 | |
|
| | 224 | | public void GetChannelMembers(string channelId, int limit, int skip, string name) => |
| 1 | 225 | | WebInterface.GetChannelMembers(channelId, limit, skip, name); |
| | 226 | |
|
| | 227 | | public void SendChatMessage(ChatMessage message) => |
| 0 | 228 | | WebInterface.SendChatMessage(message); |
| | 229 | |
|
| | 230 | | public void MarkMessagesAsSeen(string userId) => |
| 1 | 231 | | WebInterface.MarkMessagesAsSeen(userId); |
| | 232 | | } |
| | 233 | | } |