From 334aa10007c7b8ed11a94cdb0f2fda5c73f1fd81 Mon Sep 17 00:00:00 2001 From: ThePinkUnicorn6 Date: Thu, 14 Dec 2023 15:09:11 +0000 Subject: [PATCH] First implementation of key requests --- Program.cs | 63 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/Program.cs b/Program.cs index f4f1407..26d0c80 100644 --- a/Program.cs +++ b/Program.cs @@ -2,6 +2,7 @@ using System.Text; using Newtonsoft.Json; using System.Data.SQLite; +using System.Windows.Markup; class MessageServer { @@ -155,11 +156,13 @@ FOREIGN KEY('OwnerID') REFERENCES 'tblUsers'('UserID') );"; cmd.ExecuteNonQuery(); cmd.CommandText = @"CREATE TABLE IF NOT EXISTS 'tblKeyRequests' ( - 'UserID' CHAR(36), - 'GuildID' CHAR(36), - FOREIGN KEY('UserID') REFERENCES 'tblUsers'('UserID'), + 'RequesterUserID' CHAR(36), + 'GuildID' CHAR(36), + 'EncryptedKey' TEXT, + 'ResponderUserID' CHAR(36), + FOREIGN KEY('RequesterUserID') REFERENCES 'tblUsers'('UserID'), FOREIGN KEY('GuildID') REFERENCES 'tblGuilds'('GuildID'), - PRIMARY KEY('UserID', 'GuildID') + PRIMARY KEY('RequesterUserID', 'GuildID') );"; cmd.ExecuteNonQuery(); cmd.CommandText = @"CREATE TABLE IF NOT EXISTS 'tblInvites' ( @@ -1211,14 +1214,54 @@ static void apiRequestKeys(HttpListenerContext context) using (var cmd = new SQLiteCommand(con)) { con.Open(); - cmd.CommandText = @"IF NOT EXISTS (SELECT * FROM tblKeyRequests WHERE UserID = @UserID AND GuildID = @GuildID) - INSERT INTO tblKeyRequests(UserID, GuildID) - VALUES (@UserID, @GuildID);"; + cmd.CommandText = @"SELECT EXISTS( + SELECT 1 + FROM tblKeyRequests + WHERE RequesterUserID = @UserID + AND GuildID = @GuildID + );"; cmd.Parameters.AddWithValue("UserID", userID); cmd.Parameters.AddWithValue("GuildID", guildID); + bool alreadyRequested = (Int64)cmd.ExecuteScalar() > 0; + if (alreadyRequested) + { + cmd.CommandText = @"SELECT EncryptedKey, ResponderUserID + FROM tblKeyRequestes + WHERE RequesterUserID = @UserID + AND GuildID = @GuildID;"; + cmd.Parameters.AddWithValue("UserID", userID); + cmd.Parameters.AddWithValue("GuildID", guildID); + if (cmd.ExecuteReader().Read()) + { + var keys = new { + returned = true, + key = cmd.ExecuteReader().GetString(0), + userID = cmd.ExecuteReader().GetString(1), + }; + responseMessage = JsonConvert.SerializeObject(keys); + code = 200; + } + else + { + var keys = new { + returned = false, + }; + responseMessage = JsonConvert.SerializeObject(keys); + code = 425; + } + } + else + { + cmd.CommandText = @"INSERT INTO tblKeyRequests(UserID, GuildID) + VALUES (@UserID, @GuildID);"; + cmd.Parameters.AddWithValue("UserID", userID); + cmd.Parameters.AddWithValue("GuildID", guildID); + cmd.ExecuteNonQuery(); + code = 200; + responseMessage = null; + } } - code = 200; - responseMessage = null; + } sendResponse(context, typeJson, code, responseMessage); } @@ -1263,7 +1306,7 @@ static bool tokenValid(string? token) SELECT 1 FROM tblTokens WHERE Token = @Token - )"; + );"; cmd.Parameters.AddWithValue("Token", token); valid = (Int64)cmd.ExecuteScalar() > 0; }