More API migration

● Added a template for posting a message to a server
● Minor changes to API structure to provide better stability with the different types
● Started fixing mistakes in the SocketBulkMessage request.
This commit is contained in:
JacobTech 2023-05-18 14:27:49 -04:00
parent 7a157fdc5d
commit 04d9dda073
15 changed files with 195 additions and 7 deletions

View File

@ -12,7 +12,7 @@ public class AppConfig
public byte DataId { get; set; } = 0;
[JsonInclude]
[JsonPropertyName("server_epoch")]
public long ServerEpoch { get; set; } = Luski.Info.GetTimestampFromEpoch(0);
public DateTime ServerEpoch { get; set; } = DateTime.UtcNow.Date;
[JsonInclude]
[JsonPropertyName("database")]
public string Database { get; set; } = "Some database name";

View File

@ -80,9 +80,9 @@ public static class Luski
// public static readonly DateTime ServerEpoch = new(2023, 1, 1, 0, 0, 0, 0);
public static long GetTimestampFromEpoch(long Epoch)
public static long GetTimestampFromEpoch(DateTime Epoch)
{
double ts = Math.Round(DateTime.UtcNow.Subtract(DateTime.UnixEpoch).TotalMilliseconds - Epoch, 0);
double ts = Math.Round(DateTime.UtcNow.Subtract(Epoch).TotalMilliseconds, 0);
return long.Parse(ts.ToString().Replace(".", string.Empty));
}
}
@ -97,7 +97,7 @@ public static class Luski
Timestamp = ID >> 20;
}
public static Snowflake GenerateSnowflake(long Epoch)
public static Snowflake GenerateSnowflake(DateTime Epoch)
{
i++;
if (i > 0b_1111_1111_1111) i = 0;

View File

@ -0,0 +1,23 @@
using System.Text.Json.Serialization;
using LuskiServer.Interfaces.ServerComs;
namespace LuskiServer.Classes.ServerComs;
public class MessageEvent : IServerEvent
{
[JsonInclude]
[JsonPropertyName("user_id")]
public long UserID { get; set; }
[JsonInclude]
[JsonPropertyName("channel_id")]
public long ChannelID { get; set; }
[JsonInclude]
[JsonPropertyName("context")]
public string Base64Context { get; set; }
[JsonInclude]
[JsonPropertyName("id")]
public long ID { get; set; }
[JsonInclude]
[JsonPropertyName("files")]
public long[] Files { get; set; }
}

View File

@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
using LuskiServer.Enums.ServerComs;
using LuskiServer.Interfaces.ServerComs;
namespace LuskiServer.Classes.ServerComs;
public class ServerEvent
{
[JsonInclude]
[JsonPropertyName("type")]
public DataType Type { get; set; }
[JsonInclude]
[JsonPropertyName("data")]
public IServerEvent Data { get; set; }
}

View File

@ -0,0 +1,23 @@
using System.Text.Json.Serialization;
using LuskiServer.Enums.ServerComs;
namespace LuskiServer.Classes.ServerComs;
public class ServerSendSendEvent
{
[JsonInclude]
[JsonPropertyName("data")]
public ServerEvent Data { get; set; }
[JsonInclude]
[JsonPropertyName("send_type")]
public SendType SendType { get; set; }
[JsonInclude]
[JsonPropertyName("id")]
public long? ID { get; set; } = null;
[JsonInclude]
[JsonPropertyName("ids")]
public long[]? IDS { get; set; } = null;
}

View File

@ -0,0 +1,18 @@
using LuskiServer.Enums.ServerComs;
namespace LuskiServer.Classes.ServerComs;
public class WSS
{
public static void SendData(SendType SendType, ServerEvent Event, params long[] IDS)
{
ServerSendSendEvent SSSE = new()
{
SendType = SendType,
Data = Event
};
if (SendType == SendType.ID_Group) SSSE.IDS = IDS;
else if (SendType == SendType.ID) SSSE.ID = IDS[0];
//TODO Start work on server send code
}
}

View File

@ -6,6 +6,7 @@ namespace LuskiServer.Classes.TableDef;
public static class Files
{
public static TableColumn<long> ID { get; } = new("id", true);
public static TableColumn<long> ChannelID { get; } = new("channel_id");
public static TableColumn<long> Size { get; } = new("size");
public static TableColumn<byte[]> Name { get; } = new("name");
public static TableColumn<byte[]> Hash { get; } = new("hash");

View File

@ -0,0 +1,24 @@
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace LuskiServer.Classes.v1.Incoming;
public class ClientSendMessage
{
[JsonInclude]
[BindRequired]
[JsonRequired]
[JsonPropertyName("channel_id")]
public long ChannelID { get; set; }
[JsonInclude]
[BindRequired]
[JsonRequired]
[JsonPropertyName("context")]
public string Base64Context { get; set; }
[JsonInclude]
[BindRequired]
[JsonRequired]
[JsonPropertyName("files")]
public long[] Files { get; set; }
}

View File

@ -15,6 +15,7 @@ namespace LuskiServer.Controllers.v1;
public class SocketBulkMessageController : ControllerBase
{
[HttpGet]
[Route(Luski.Info.Routes.Default.Base)]
public IActionResult Get()
{
try

View File

@ -0,0 +1,62 @@
using Asp.Versioning;
using JacobTechEncryption.Enums;
using LuskiServer.Classes;
using LuskiServer.Classes.ServerComs;
using LuskiServer.Classes.TableDef;
using LuskiServer.Classes.v1.Incoming;
using LuskiServer.Classes.WebTypes;
using LuskiServer.Enums;
using LuskiServer.Enums.ServerComs;
using Microsoft.AspNetCore.Mvc;
namespace LuskiServer.Controllers.v1;
[ApiVersion(1)]
[ApiController]
public class SocketMessageController : ControllerBase
{
[HttpPost]
[DisableRequestSizeLimit]
[Route(Luski.Info.Routes.Default.Base)]
public async Task<IActionResult> Post([FromBody]ClientSendMessage data)
{
try
{
if (!this.CanTokenRequest(out long ID, out IActionResult? toc) && toc != null) return toc;
//TODO Add a check system for channel access
//if (Luski.HasAccessToChannel(ID, data.ChannelID))
{
Luski.Snowflake Id = Luski.Snowflake.GenerateSnowflake(Tables.Channels.Read(Channels.Epoch, Channels.ID.CreateParameter(data.ChannelID)));
ChannelType type = Tables.Channels.Read(Channels.Type, Channels.ID.CreateParameter(data.ChannelID));
MessageEvent MessageEvent = new()
{
UserID = ID,
ChannelID = data.ChannelID,
Base64Context = data.Base64Context,
ID = Id.ID
};
Tables.Messages.Insert(
Messages.ChannelID.CreateParameter(data.ChannelID),
Messages.ID.CreateParameter(Id.ID),
Messages.AuthorID.CreateParameter(ID),
Messages.Context.CreateParameter(Convert.FromBase64String(data.Base64Context)),
Messages.TimeStamp.CreateParameter(Id.Timestamp),
Messages.Files.CreateParameter(data.Files),
Messages.EncoderType.CreateParameter(EncoderType.UTF8),
Messages.EncryptionType.CreateParameter(EncryptionType.RSA));
//TODO Get all ID for members to send to
WSS.SendData(SendType.ID_Group, new ServerEvent()
{
Type = DataType.MessageCreate,
Data = MessageEvent
}, Array.Empty<long>());
}
Response.StatusCode = 201;
return null!;
}
catch (Exception ex)
{
return this.ShowError(ex);
}
}
}

View File

@ -2,5 +2,5 @@ namespace LuskiServer.Enums;
public enum ChannelType : short
{
Default = 0
TextAndVoice = 0
}

View File

@ -0,0 +1,6 @@
namespace LuskiServer.Enums.ServerComs;
public enum DataType
{
MessageCreate
}

View File

@ -0,0 +1,8 @@
namespace LuskiServer.Enums.ServerComs;
public enum SendType
{
All,
ID_Group,
ID
}

View File

@ -0,0 +1,6 @@
namespace LuskiServer.Interfaces.ServerComs;
public interface IServerEvent
{
}

View File

@ -14,7 +14,7 @@ using Swashbuckle.AspNetCore.SwaggerGen;
[assembly: Microsoft.AspNetCore.Mvc.ApiController]
Luski.Config = Luski.GetSettings("/etc/luskiserver/app.json", AppConfigContext.Default.AppConfig, true);
Console.WriteLine($"Server starting with starting epoch of {DateTime.UnixEpoch.AddMilliseconds(Luski.Config.ServerEpoch).ToLocalTime()}");
Console.WriteLine($"Server starting with starting epoch of {Luski.Config.ServerEpoch.ToLocalTime()} and current time of {DateTime.UtcNow.ToLocalTime()}");
Luski.Database = new Database(Luski.Config.Address,
Luski.Config.Database,
Luski.Config.Username,
@ -99,7 +99,7 @@ if (!Tables.Categories.TryRead(Categories.ID, out _, Categories.ID.CreateParamet
);
Tables.Channels.Insert(
Channels.ID.CreateParameter(0),
Channels.Type.CreateParameter(ChannelType.Default),
Channels.Type.CreateParameter(ChannelType.TextAndVoice),
Channels.Description.CreateParameter(Encoding.UTF8.GetBytes("Default chat for you to use in your new server")),
Channels.Name.CreateParameter(Encoding.UTF8.GetBytes("Default Channel")),
Channels.Key.CreateParameter(Luski.Encryption.Keys.PublicKey),