| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Chat.Channels; |
| | 4 | | using DCL.Chat.WebApi; |
| | 5 | | using DCL.Interface; |
| | 6 | | using JetBrains.Annotations; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Social.Chat |
| | 10 | | { |
| | 11 | | public class WebInterfaceChatBridge : MonoBehaviour, IChatApiBridge |
| | 12 | | { |
| | 13 | | public event Action<InitializeChatPayload> OnInitialized; |
| | 14 | | public event Action<ChatMessage[]> OnAddMessage; |
| | 15 | | public event Action<UpdateTotalUnseenMessagesPayload> OnTotalUnseenMessagesChanged; |
| | 16 | | public event Action<(string userId, int count)[]> OnUserUnseenMessagesChanged; |
| | 17 | | public event Action<(string channelId, int count)[]> OnChannelUnseenMessagesChanged; |
| | 18 | | public event Action<UpdateChannelMembersPayload> OnChannelMembersUpdated; |
| | 19 | | public event Action<ChannelInfoPayloads> OnChannelJoined; |
| | 20 | | public event Action<JoinChannelErrorPayload> OnChannelJoinFailed; |
| | 21 | | public event Action<JoinChannelErrorPayload> OnChannelLeaveFailed; |
| | 22 | | public event Action<ChannelInfoPayloads> OnChannelsUpdated; |
| | 23 | | public event Action<MuteChannelErrorPayload> OnMuteChannelFailed; |
| | 24 | | public event Action<ChannelSearchResultsPayload> OnChannelSearchResults; |
| | 25 | |
|
| | 26 | | [PublicAPI] |
| | 27 | | public void InitializeChat(string json) => |
| 0 | 28 | | OnInitialized?.Invoke(JsonUtility.FromJson<InitializeChatPayload>(json)); |
| | 29 | |
|
| | 30 | | [PublicAPI] |
| | 31 | | public void AddMessageToChatWindow(string jsonMessage) => |
| 0 | 32 | | OnAddMessage?.Invoke(new[] {JsonUtility.FromJson<ChatMessage>(jsonMessage)}); |
| | 33 | |
|
| | 34 | | [PublicAPI] |
| | 35 | | public void AddChatMessages(string jsonMessage) |
| | 36 | | { |
| 0 | 37 | | var msg = JsonUtility.FromJson<ChatMessageListPayload>(jsonMessage); |
| 0 | 38 | | if (msg == null) return; |
| 0 | 39 | | OnAddMessage?.Invoke(msg.messages); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | [PublicAPI] |
| | 43 | | public void UpdateTotalUnseenMessages(string json) => |
| 0 | 44 | | OnTotalUnseenMessagesChanged?.Invoke(JsonUtility.FromJson<UpdateTotalUnseenMessagesPayload>(json)); |
| | 45 | |
|
| | 46 | | [PublicAPI] |
| | 47 | | public void UpdateUserUnseenMessages(string json) |
| | 48 | | { |
| 0 | 49 | | var msg = JsonUtility.FromJson<UpdateUserUnseenMessagesPayload>(json); |
| 0 | 50 | | OnUserUnseenMessagesChanged?.Invoke(new[] {(msg.userId, msg.total)}); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | [PublicAPI] |
| | 54 | | public void UpdateTotalUnseenMessagesByUser(string json) |
| | 55 | | { |
| 0 | 56 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByUserPayload>(json); |
| 0 | 57 | | OnUserUnseenMessagesChanged?.Invoke(msg.unseenPrivateMessages |
| 0 | 58 | | .Select(unseenMessages => (unseenMessages.userId, unseenMessages.count)) |
| | 59 | | .ToArray()); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | [PublicAPI] |
| | 63 | | public void UpdateTotalUnseenMessagesByChannel(string json) |
| | 64 | | { |
| 0 | 65 | | var msg = JsonUtility.FromJson<UpdateTotalUnseenMessagesByChannelPayload>(json); |
| 0 | 66 | | OnChannelUnseenMessagesChanged?.Invoke(msg.unseenChannelMessages |
| 0 | 67 | | .Select(message => (message.channelId, message.count)) |
| | 68 | | .ToArray()); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | [PublicAPI] |
| | 72 | | public void UpdateChannelMembers(string json) => |
| 0 | 73 | | OnChannelMembersUpdated?.Invoke(JsonUtility.FromJson<UpdateChannelMembersPayload>(json)); |
| | 74 | |
|
| | 75 | | [PublicAPI] |
| | 76 | | public void JoinChannelConfirmation(string payload) => |
| 0 | 77 | | OnChannelJoined?.Invoke(JsonUtility.FromJson<ChannelInfoPayloads>(payload)); |
| | 78 | |
|
| | 79 | | [PublicAPI] |
| | 80 | | public void JoinChannelError(string payload) => |
| 0 | 81 | | OnChannelJoinFailed?.Invoke(JsonUtility.FromJson<JoinChannelErrorPayload>(payload)); |
| | 82 | |
|
| | 83 | | [PublicAPI] |
| | 84 | | public void LeaveChannelError(string payload) => |
| 0 | 85 | | OnChannelLeaveFailed?.Invoke(JsonUtility.FromJson<JoinChannelErrorPayload>(payload)); |
| | 86 | |
|
| | 87 | | [PublicAPI] |
| | 88 | | public void UpdateChannelInfo(string payload) => |
| 0 | 89 | | OnChannelsUpdated?.Invoke(JsonUtility.FromJson<ChannelInfoPayloads>(payload)); |
| | 90 | |
|
| | 91 | | [PublicAPI] |
| | 92 | | public void MuteChannelError(string payload) => |
| 0 | 93 | | OnMuteChannelFailed?.Invoke(JsonUtility.FromJson<MuteChannelErrorPayload>(payload)); |
| | 94 | |
|
| | 95 | | [PublicAPI] |
| | 96 | | public void UpdateChannelSearchResults(string payload) => |
| 0 | 97 | | OnChannelSearchResults?.Invoke(JsonUtility.FromJson<ChannelSearchResultsPayload>(payload)); |
| | 98 | |
|
| 0 | 99 | | public void LeaveChannel(string channelId) => WebInterface.LeaveChannel(channelId); |
| | 100 | |
|
| | 101 | | public void GetChannelMessages(string channelId, int limit, string fromMessageId) => |
| 0 | 102 | | WebInterface.GetChannelMessages(channelId, limit, fromMessageId); |
| | 103 | |
|
| 0 | 104 | | public void JoinOrCreateChannel(string channelId) => WebInterface.JoinOrCreateChannel(channelId); |
| | 105 | |
|
| 0 | 106 | | public void GetJoinedChannels(int limit, int skip) => WebInterface.GetJoinedChannels(limit, skip); |
| | 107 | |
|
| | 108 | | public void GetChannels(int limit, string paginationToken, string name) => |
| 0 | 109 | | WebInterface.GetChannels(limit, paginationToken, name); |
| | 110 | |
|
| | 111 | | public void MuteChannel(string channelId, bool muted) => |
| 0 | 112 | | WebInterface.MuteChannel(channelId, muted); |
| | 113 | |
|
| | 114 | | public void GetPrivateMessages(string userId, int limit, string fromMessageId) => |
| 0 | 115 | | WebInterface.GetPrivateMessages(userId, limit, fromMessageId); |
| | 116 | |
|
| | 117 | | public void MarkChannelMessagesAsSeen(string channelId) => |
| 0 | 118 | | WebInterface.MarkChannelMessagesAsSeen(channelId); |
| | 119 | |
|
| | 120 | | public void GetUnseenMessagesByUser() => |
| 0 | 121 | | WebInterface.GetUnseenMessagesByUser(); |
| | 122 | |
|
| | 123 | | public void GetUnseenMessagesByChannel() => |
| 0 | 124 | | WebInterface.GetUnseenMessagesByChannel(); |
| | 125 | |
|
| | 126 | | public void CreateChannel(string channelId) => |
| 0 | 127 | | WebInterface.CreateChannel(channelId); |
| | 128 | |
|
| | 129 | | public void GetChannelInfo(string[] channelIds) => |
| 0 | 130 | | WebInterface.GetChannelInfo(channelIds); |
| | 131 | |
|
| | 132 | | public void GetChannelMembers(string channelId, int limit, int skip, string name) => |
| 0 | 133 | | WebInterface.GetChannelMembers(channelId, limit, skip, name); |
| | 134 | |
|
| 0 | 135 | | public void SendChatMessage(ChatMessage message) => WebInterface.SendChatMessage(message); |
| | 136 | |
|
| 0 | 137 | | public void MarkMessagesAsSeen(string userId) => WebInterface.MarkMessagesAsSeen(userId); |
| | 138 | | } |
| | 139 | | } |