From 7d2f847f44938a97711ae3fbdc7681360a226b9c Mon Sep 17 00:00:00 2001 From: ION606 Date: Sat, 11 Mar 2023 15:54:43 -0500 Subject: [PATCH] added typing presence - OUTGOING --- Channels/ChannelObj.cs | 31 ++++++++++++++++++++++++- Connections/MsgRequests.cs | 46 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/Channels/ChannelObj.cs b/Channels/ChannelObj.cs index 471ebd8..e4c8b6c 100644 --- a/Channels/ChannelObj.cs +++ b/Channels/ChannelObj.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Diagnostics; using System.Drawing; using System.Net; +using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Text.Json.Nodes; using static Discord_Client_Custom.client_internals.Client; @@ -48,6 +49,7 @@ namespace Discord_Client_Custom.Channels private static string? cicon; private static string? cownerId; private string lastSent; + private static System.Threading.Timer typingTimer; public ChannelObj() { } @@ -191,21 +193,48 @@ namespace Discord_Client_Custom.Channels } var response = await MsgRequests.sendMessage(txtbx.Text, ep); + if (response == null) + { + MessageBox.Show(response.ToJsonString(), "Message failed with the following reason"); + return; + } + //JsonNode[] arr = new JsonNode[1]; //arr[0] = response; //Add the message to chat in the app groupedMsgs.Add(new ChannelMsgGroup(response, dmFlowContent, uMainIcon, i + 1)); txtbx.Clear(); + typingTimer.Dispose(); i++; dmFlowContent.Controls.Remove(txtbx); dmFlowContent.Controls.Add(txtbx, 1, i + 1); dmFlowContent.SetColumnSpan(txtbx, 2); + dmFlowContent.ScrollControlIntoView(txtbx); } else { //Do typing intent stuff here - //Connection + string ep; + if (ctype == 1) + { + ep = "https://discord.com/api/channels/" + cid2 + "/typing"; + if (txtbx.Text.Length == 0) + { + typingTimer = new System.Threading.Timer((object state) => { + Debug.WriteLine("TYPING...."); + MsgRequests.sendTyping(ep); + }); + + typingTimer.Change(0, 10000); + } + } + else + { + //Deal with group message stuff here..... + Debug.WriteLine("Message Sending has not been implemented for group DMs (yet)"); + return; + } } }; diff --git a/Connections/MsgRequests.cs b/Connections/MsgRequests.cs index cb39bb5..cf0a033 100644 --- a/Connections/MsgRequests.cs +++ b/Connections/MsgRequests.cs @@ -23,8 +23,6 @@ namespace Discord_Client_Custom.Connections public async static Task sendMessage(string content, string ep) { - Debug.WriteLine(ep); - try { var client = new HttpClient(); @@ -68,6 +66,50 @@ namespace Discord_Client_Custom.Connections } + public static async Task sendTyping(string url) + { + try + { + var client = new HttpClient(); + + var request = new HttpRequestMessage() + { + RequestUri = new Uri(url), + Method = HttpMethod.Post, + }; + + request.Headers.Clear(); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + //CHANGE THIS TO CONFIG LATER + request.Headers.Add("Authorization", userToken); + request.Headers.Add("User-Agent", ".NET Foundation Repository Reporter"); + + + var taskResponse = await client.SendAsync(request); + var responseContent = await taskResponse.Content.ReadAsStringAsync(); + + //Typing + if (responseContent.Length == 0) return null; + + JsonNode responseJSON = JsonNode.Parse(responseContent); + + if (taskResponse.StatusCode != System.Net.HttpStatusCode.OK) + { + Debug.Write(responseJSON["message"]); + return null; + } + + return responseJSON; + } + catch (Exception e) + { + Debug.WriteLine(e); + return null; + } + } + + public static async Task getChannels() { var client = new HttpClient();