Skip to content

Commit

Permalink
First implementation of key requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePinkUnicorn6 committed Dec 14, 2023
1 parent 8011c21 commit 334aa10
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;
using Newtonsoft.Json;
using System.Data.SQLite;
using System.Windows.Markup;

class MessageServer
{
Expand Down Expand Up @@ -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' (
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 334aa10

Please sign in to comment.