Skip to content

Commit

Permalink
Implement profile pic uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePinkUnicorn6 committed Jan 5, 2024
1 parent 007c679 commit 3af66f5
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Security;
using System.Dynamic;
using System.Net.Mime;
using System.Windows.Markup;

class MessageServer
{
Expand All @@ -31,7 +32,7 @@ static void Main(string[] args)
log("DEBUG", "Using SQLite version: " + con.ServerVersion);
}
createDB(connectionString);
const string url = "http://+:8080/"; // Sets up http server
const string url = "http://localhost:8080/"; // Sets up http server
// TODO: log and give error if port is already in use.
HttpListener listener = new HttpListener();
listener.Prefixes.Add(url);
Expand Down Expand Up @@ -377,6 +378,12 @@ static void apiSendMessage(HttpListenerContext context)
else
{
token = jsonBodyObject.token;
}
if (string.IsNullOrEmpty((string)jsonBodyObject.channelID) | string.IsNullOrEmpty((string)jsonBodyObject.content) | string.IsNullOrEmpty(token)) //TODO: check for message type
{returnMissingParameterError(out responseMessage, out code);}
else if (!tokenValid(token)) returnInvalidTokenError(out responseMessage, out code);
else
{
message = new Message
{
ID = Guid.NewGuid().ToString(),
Expand All @@ -385,12 +392,6 @@ static void apiSendMessage(HttpListenerContext context)
Content = jsonBodyObject.content,
IV = jsonBodyObject.iv,
};
}
if (string.IsNullOrEmpty(message.ChannelID) | string.IsNullOrEmpty(message.Content) | string.IsNullOrEmpty(token))
{returnMissingParameterError(out responseMessage, out code);}
else if (!tokenValid(token)) returnInvalidTokenError(out responseMessage, out code);
else
{
message.UserID = getUserIDFromToken(token);
if (checkUserChannelPerms(message.ChannelID, message.UserID) > readOnly) // Has to have higher privilages than read only
{
Expand Down Expand Up @@ -670,7 +671,7 @@ FROM tblUsers
{
string userID = Guid.NewGuid().ToString();
cmd.CommandText = @"INSERT INTO tblUsers (UserID, UserName, PassHash, PublicKey, Picture)
VALUES (@UserID, @UserName, @PassHash, @PublicKey, 'default')";
VALUES (@UserID, @UserName, @PassHash, @PublicKey, '')";
cmd.Parameters.AddWithValue("UserID", userID);
cmd.Parameters.AddWithValue("UserName", userName);
cmd.Parameters.AddWithValue("PassHash", passHash);
Expand All @@ -679,7 +680,7 @@ FROM tblUsers
var responseJson = new { token = createToken(userID) };
responseMessage = JsonConvert.SerializeObject(responseJson);
code = 200;
}
}
}
}
sendResponse(context, typeJson, code, responseMessage);
Expand All @@ -689,6 +690,7 @@ static void apiSetPicture(HttpListenerContext context)
string responseMessage;
int code;
string? token;
string? imageBase64;
dynamic jsonBodyObject = parseJsonPost(context);
if (jsonBodyObject == null)
{
Expand All @@ -700,7 +702,28 @@ static void apiSetPicture(HttpListenerContext context)
else
{
token = jsonBodyObject.token;
imageBase64 = jsonBodyObject.image;
}
if (string.IsNullOrEmpty(imageBase64)) returnMissingParameterError(out responseMessage, out code);
else if (!tokenValid(token)) returnInvalidTokenError(out responseMessage, out code);
else
{
string userID = getUserIDFromToken(token);
using (var con = new SQLiteConnection(connectionString))
using (var cmd = new SQLiteCommand(con))
{
con.Open();
cmd.CommandText = @"UPDATE tblUsers
SET Picture = @Picture
WHERE UserID = @UserID";
cmd.Parameters.AddWithValue("UserID", userID);
cmd.Parameters.AddWithValue("Picture", imageBase64);
cmd.ExecuteNonQuery();
code = 200;
responseMessage = null;
}
}
sendResponse(context, typeJson, code, responseMessage);
}
static void apiLogin(HttpListenerContext context) // Checks if the supplied username and password are correct, and returns a token if they are
{
Expand Down Expand Up @@ -740,7 +763,7 @@ FROM tblUsers
{
var responseJson = new { token = createToken(userID) };
responseMessage = JsonConvert.SerializeObject(responseJson);
code = 200;
code = 200;
}
else
{
Expand Down Expand Up @@ -809,19 +832,26 @@ static void apiCreateChannel(HttpListenerContext context, bool isDM)
}
else
{
bool guildExists = checkGuildExists(guildID);
if (guildExists)
{
createChannel(channelName, guildID, (int)channelType);
responseMessage = null;
code = 200;
}
else

int userPermission = checkUserGuildPerms(userID1, guildID);
if (userPermission <= guildNotExist)
{
var responseJson = new { error = "Invalid GuildID", errcode = "INVALID_GUILDID" };
responseMessage = JsonConvert.SerializeObject(responseJson);
code = 400;
}
else if (userPermission < admin) // Reject the request if the user has lower permissions than admin
{
var responseJson = new { error = "You do not have permissions to carry out this action", errcode = "FORBIDDEN" };
responseMessage = JsonConvert.SerializeObject(responseJson);
code = 403;
}
else
{
createChannel(channelName, guildID, (int)channelType);
responseMessage = null;
code = 200;
}
}
}
sendResponse(context, typeJson, code, responseMessage);
Expand Down

0 comments on commit 3af66f5

Please sign in to comment.