public async Task <bool> SendChats( IList <Chat> chats, string sessionName, string sessionId, ILogger log) { if (chats.Count == 0) { return(true); } foreach (var chat in chats) { chat.SessionName = null; } chats.First().SessionName = sessionName; var list = new ListOfChats(); list.Chats.AddRange(chats); var json = JsonConvert.SerializeObject(list); //_log.LogDebug($"json: {json}"); var chatsUrl = $"{_hostNameFree}/chats"; var httpRequest = new HttpRequestMessage(HttpMethod.Post, chatsUrl); httpRequest.Headers.Add(Constants.GroupIdHeaderKey, sessionId); httpRequest.Content = new StringContent(json); var response = await _http.SendAsync(httpRequest); if (!response.IsSuccessStatusCode) { IsInError = true; log.LogError($"Issue sending chat: {response.StatusCode} / {response.ReasonPhrase}"); ErrorStatus = "Error connecting to Chat service, try to refresh the page and send again"; return(false); } IsInError = false; ErrorStatus = null; Status = "Chat sent"; return(true); }
public async Task ReceiveChats( Action raiseUpdateEvent, Func <Task> saveChats, ListOfChats receivedChats, IList <Chat> allChats, string peerId, ILogger log) { log.LogTrace("-> ChatProxy.ReceiveChat(ListOfChats)"); foreach (var receivedChat in receivedChats.Chats.OrderBy(c => c.MessageDateTime)) { log.LogDebug($"Received chat with MessageMarkdown '{receivedChat.MessageMarkdown}' and ID {receivedChat.UniqueId}"); if (receivedChat.Key != SecretKey) { log.LogError("Received chat with invalid key"); return; } UpdateChatStyles(receivedChat, peerId); if (!string.IsNullOrEmpty(receivedChat.SessionName)) { foreach (var chat in allChats.Where(c => c.SessionName != null)) { chat.SessionName = null; } } if (!allChats.Any(c => c.UniqueId == receivedChat.UniqueId)) { var nextChat = allChats .FirstOrDefault(c => receivedChat.MessageDateTime > c.MessageDateTime); if (nextChat == null) { allChats.Insert(0, receivedChat); } else { var index = allChats.IndexOf(nextChat); allChats.Insert(index, receivedChat); } } if (!string.IsNullOrEmpty(receivedChat.SessionName)) { var firstChat = allChats.FirstOrDefault(); if (firstChat != null) { firstChat.SessionName = receivedChat.SessionName; } } } if (raiseUpdateEvent != null) { raiseUpdateEvent(); } if (saveChats != null) { await saveChats(); } }