working messages!!@!@
All checks were successful
Server Build / publish (push) Successful in 30s
Voice Build / publish (push) Successful in 26s

This commit is contained in:
olcxja 2026-06-02 12:53:32 +02:00
commit e33554f369
4 changed files with 238 additions and 45 deletions

View file

@ -647,6 +647,12 @@ public static async Task Auth(HttpContext context, Func<Task> next, IQueryCollec
case "user/dm/create":
await context.Response.WriteAsync(Encryption.Encryption.EncryptString(await Room.Requests.DmCreate(id, serializedBody.string2), password));
break;
case "dm/message/send":
await context.Response.WriteAsync(
Encryption.Encryption.EncryptString(
await Room.Requests.DmMessageSend(id, serializedBody.string2)
, password));
break;
case "dm/messages/get":
{
Universal3String msgGet = JsonSerializer.Deserialize(

View file

@ -58,6 +58,17 @@ public class Program
switch (path)
{
case "/_larpix/ws":
if (context.WebSockets.IsWebSocketRequest)
{
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await WsHandler(context, webSocket, query);
}
else
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
return;
case "/_larpix/serverinfo":
ServerInfo serverInfo = new ServerInfo();
serverInfo.domain = DOMAIN;

View file

@ -133,11 +133,6 @@ public class Requests
{
return "error:cant.invite.urself";
}
SemaphoreSlim userLock2 = Account.Utils.GetUserLock(id2);
await userLock2.WaitAsync();
try
{
string inviteFile = ACCOUNTS_DATA_DIR + $"/{id2}/dminvites/recv/{id}";
if (Fs.Exists(inviteFile))
{
@ -145,13 +140,21 @@ public class Requests
}
byte[] timestamp = Encoding.UTF8.GetBytes(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
await Fs.WriteFile(inviteFile, timestamp);
await Fs.WriteFile(ACCOUNTS_DATA_DIR + $"/{id}/dminvites/sent/{id2}", timestamp); //we should also save that this user invited someone, because listing sent invites is cool
_ = Task.Run(async () =>
{
SemaphoreSlim userLock2 = Account.Utils.GetUserLock(id2);
await userLock2.WaitAsync();
try
{
await Fs.WriteFile(inviteFile, timestamp);
}
finally
{
userLock2.Release();
}
});
return "success:user.invited";
}
@ -170,17 +173,21 @@ public class Requests
string id2 = tId;
if (id2 == "0" || string.IsNullOrEmpty(id2)) return "error:user.not.found";
Fs.DeleteFile(ACCOUNTS_DATA_DIR + $"/{id}/dminvites/sent/{id2}");
_ = Task.Run(async () =>
{
SemaphoreSlim userLock2 = Account.Utils.GetUserLock(id2);
await userLock2.WaitAsync();
try
{
Fs.DeleteFile(ACCOUNTS_DATA_DIR + $"/{id2}/dminvites/recv/{id}");
Fs.DeleteFile(ACCOUNTS_DATA_DIR + $"/{id}/dminvites/sent/{id2}");
}
finally
{
userLock2.Release();
}
});
return "success:invite.revoked";
}
@ -199,17 +206,21 @@ public class Requests
string id2 = tId;
if (id2 == "0" || string.IsNullOrEmpty(id2)) return "error:user.not.found";
Fs.DeleteFile(ACCOUNTS_DATA_DIR + $"/{id}/dminvites/recv/{id2}");
_ = Task.Run(async () =>
{
SemaphoreSlim userLock2 = Account.Utils.GetUserLock(id2);
await userLock2.WaitAsync();
try
{
Fs.DeleteFile(ACCOUNTS_DATA_DIR + $"/{id}/dminvites/recv/{id2}");
Fs.DeleteFile(ACCOUNTS_DATA_DIR + $"/{id2}/dminvites/sent/{id}");
}
finally
{
userLock2.Release();
}
});
return "success:invite.declined";
}
@ -308,6 +319,8 @@ public class Requests
startingMessage.timestamp);
if (isUserLocal)
{
_ = Task.Run(async () =>
{
SemaphoreSlim userLock = Account.Utils.GetUserLock(id2);
await userLock.WaitAsync();
@ -320,6 +333,7 @@ public class Requests
{
userLock.Release();
}
});
}
else //federacja
{
@ -344,4 +358,70 @@ public class Requests
}
return "error:cant.create.dm.without.invitation";
}
public static async Task<string> DmMessageSend(string id, string body)
{
Universal3String serializedBody = JsonSerializer.Deserialize(
body,
AppJsonSerializerContext.Default.Universal3String
);
string dmId = serializedBody.string1;
string content = serializedBody.string2;
string attachment = serializedBody.string3;
if (!await IsMemberOfDm(id, dmId)) return "error:forbidden";
string msgDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/messages";
long last = 0;
if (Fs.Exists($"{msgDir}/last"))
{
last = long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{msgDir}/last")));
}
long nextId = last + 1;
Message msg = new Message();
msg.author = id;
msg.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
msg.type = "larp.text";
msg.content = content;
msg.attachment = attachment;
msg.key = "0"; // Flag for encrypted content
msg.pervious = "";
await Fs.WriteFile($"{msgDir}/{nextId}", Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message)));
await Fs.WriteFile($"{msgDir}/last", Encoding.UTF8.GetBytes(nextId.ToString()));
string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members");
foreach(string memberWD in members)
{
bool isLocal = Account.Utils.IsUserLocal(memberWD, out string domain);
string memberId = Account.Utils.GetValidIdOrZero(memberWD);
if (isLocal)
{
if (memberId == id)
{
await Account.Utils.UpdateUserDm(memberId, dmId, "true", msg.timestamp);
await Utils.Utils.SendWsMessage(memberId, $"dm_message:{dmId}");
}
else
{
_ = Task.Run(async () =>
{
SemaphoreSlim userLock = Account.Utils.GetUserLock(memberId);
await userLock.WaitAsync();
try
{
await Account.Utils.UpdateUserDm(memberId, dmId, "false", msg.timestamp);
}
finally
{
userLock.Release();
}
await Utils.Utils.SendWsMessage(memberId, $"dm_message:{dmId}");
});
}
}
}
return "success:message.sent";
}
}

View file

@ -1,5 +1,7 @@
using System.Collections.Concurrent;
using System.Text;
using System.Text.Json;
using LarpixServer.Utils.Jsons;
namespace LarpixServer.Utils;
@ -96,6 +98,100 @@ public class Utils
["pptx"] = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
};
public static ConcurrentDictionary<string, List<System.Net.WebSockets.WebSocket>> ActiveWebSockets = new();
public static async Task WsHandler(HttpContext context, System.Net.WebSockets.WebSocket webSocket, IQueryCollection query)
{
var buffer = new byte[8192];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == System.Net.WebSockets.WebSocketMessageType.Close)
{
await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
return;
}
string authMessage = Encoding.UTF8.GetString(buffer, 0, result.Count);
Universal2String authParams;
try {
authParams = JsonSerializer.Deserialize<Universal2String>(authMessage, AppJsonSerializerContext.Default.Universal2String);
} catch {
await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.InvalidMessageType, "Invalid auth", CancellationToken.None);
return;
}
string id = Account.Utils.GetValidIdOrZero(authParams.string1);
string password = await Account.Utils.GetPassword(id);
string secret = await Account.Utils.NonceDecryptBody(id, password, authParams.string2);
string auth = await Account.Utils.Auth(id, password, secret);
if (auth != Account.Utils.LOGIN_SUCCESS)
{
await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.PolicyViolation, "Unauthorized", CancellationToken.None);
return;
}
ActiveWebSockets.AddOrUpdate(id, new List<System.Net.WebSockets.WebSocket> { webSocket }, (k, list) => {
lock(list) {
list.Add(webSocket);
}
return list;
});
try
{
while (webSocket.State == System.Net.WebSockets.WebSocketState.Open)
{
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == System.Net.WebSockets.WebSocketMessageType.Close)
{
break;
}
}
}
catch { }
finally
{
if (ActiveWebSockets.TryGetValue(id, out var list))
{
lock(list) {
list.Remove(webSocket);
}
}
if (webSocket.State == System.Net.WebSockets.WebSocketState.Open)
{
await webSocket.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
}
}
public static async Task SendWsMessage(string id, string message)
{
if (ActiveWebSockets.TryGetValue(id, out var list))
{
List<System.Net.WebSockets.WebSocket> toRemove = null;
var bytes = Encoding.UTF8.GetBytes(message);
lock(list) {
foreach(var ws in list)
{
if (ws.State == System.Net.WebSockets.WebSocketState.Open)
{
_ = ws.SendAsync(new ArraySegment<byte>(bytes), System.Net.WebSockets.WebSocketMessageType.Text, true, CancellationToken.None);
}
else
{
if (toRemove == null) toRemove = new();
toRemove.Add(ws);
}
}
if (toRemove != null)
{
foreach(var ws in toRemove) list.Remove(ws);
}
}
}
}
public static async Task<string> LoadBody(StreamReader reader)
{
StringBuilder bodyBuilder = new();