From 5c58492b60c3107814c1664c5f95e267c3fe15b3 Mon Sep 17 00:00:00 2001 From: olcxja Date: Sat, 25 Jul 2026 13:55:34 +0200 Subject: [PATCH] fix few locks and unpin message --- LarpixServer/Account/BackgroundDelete.cs | 4 +- LarpixServer/Room/Requests.cs | 156 ++++++++++++++--------- 2 files changed, 95 insertions(+), 65 deletions(-) diff --git a/LarpixServer/Account/BackgroundDelete.cs b/LarpixServer/Account/BackgroundDelete.cs index abbfdde..93cfa93 100644 --- a/LarpixServer/Account/BackgroundDelete.cs +++ b/LarpixServer/Account/BackgroundDelete.cs @@ -40,8 +40,8 @@ public class BackgroundDelete : BackgroundService { string id = Path.GetFileName(account); SemaphoreSlim userLock = Utils.GetUserLock(id); - await userLock.WaitAsync(); await Requests.createLock.WaitAsync(); + await userLock.WaitAsync(); try { string username = await Account.Utils.NameFromId(id); @@ -55,8 +55,8 @@ public class BackgroundDelete : BackgroundService } finally { - Requests.createLock.Release(); userLock.Release(); + Requests.createLock.Release(); } } } diff --git a/LarpixServer/Room/Requests.cs b/LarpixServer/Room/Requests.cs index 59a1a4e..affcca6 100644 --- a/LarpixServer/Room/Requests.cs +++ b/LarpixServer/Room/Requests.cs @@ -427,61 +427,71 @@ public class Requests } 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; - + + long nextId = 0; 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"; //empty = no encryption, value = encryption key index - msg.pervious = ""; - msg.responded = responded ?? ""; - msg.reactions = ""; - - if (!string.IsNullOrEmpty(msg.attachment) && msg.attachment != "[]") + await createLock.WaitAsync(); + try { - try + long last = 0; + if (Fs.Exists($"{msgDir}/last")) { - var attachmentsNode = System.Text.Json.Nodes.JsonNode.Parse(msg.attachment); - if (attachmentsNode is System.Text.Json.Nodes.JsonArray arr) + last = long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{msgDir}/last"))); + } + nextId = last + 1; + + msg.author = id; + msg.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); + msg.type = "larp.text"; + msg.content = content; + msg.attachment = attachment; + msg.key = "0"; //empty = no encryption, value = encryption key index + msg.pervious = ""; + msg.responded = responded ?? ""; + msg.reactions = ""; + + if (!string.IsNullOrEmpty(msg.attachment) && msg.attachment != "[]") + { + try { - string attDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/attachments/{nextId}"; - if (!Fs.Exists(attDir)) Fs.CreateDirectory(attDir); - - for (int i = 0; i < arr.Count; i++) + var attachmentsNode = System.Text.Json.Nodes.JsonNode.Parse(msg.attachment); + if (attachmentsNode is System.Text.Json.Nodes.JsonArray arr) { - var attObj = arr[i] as System.Text.Json.Nodes.JsonObject; - if (attObj != null && attObj.ContainsKey("staging_id")) + string attDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/attachments/{nextId}"; + if (!Fs.Exists(attDir)) Fs.CreateDirectory(attDir); + + for (int i = 0; i < arr.Count; i++) { - string stagingId = attObj["staging_id"].ToString(); - string stagingPath = $"{ROOMS_DIR}/dms_staging/{stagingId}"; - if (Fs.Exists(stagingPath)) + var attObj = arr[i] as System.Text.Json.Nodes.JsonObject; + if (attObj != null && attObj.ContainsKey("staging_id")) { - string attId = attObj["id"].ToString(); - string destPath = $"{attDir}/{attId}"; - Fs.MoveFile(stagingPath, destPath); + string stagingId = attObj["staging_id"].ToString(); + string stagingPath = $"{ROOMS_DIR}/dms_staging/{stagingId}"; + if (Fs.Exists(stagingPath)) + { + string attId = attObj["id"].ToString(); + string destPath = $"{attDir}/{attId}"; + Fs.MoveFile(stagingPath, destPath); + } + attObj.Remove("staging_id"); // Clean it up before saving } - attObj.Remove("staging_id"); // Clean it up before saving } + msg.attachment = attachmentsNode.ToJsonString(); } - msg.attachment = attachmentsNode.ToJsonString(); + } + catch (Exception ex) + { + Console.WriteLine($"Error processing attachments: {ex}"); } } - catch (Exception ex) - { - Console.WriteLine($"Error processing attachments: {ex}"); - } - } - await Fs.WriteFile($"{msgDir}/{nextId}", Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message))); - await Fs.WriteFile($"{msgDir}/last", Encoding.UTF8.GetBytes(nextId.ToString())); + await Fs.WriteFile($"{msgDir}/{nextId}", Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message))); + await Fs.WriteFile($"{msgDir}/last", Encoding.UTF8.GetBytes(nextId.ToString())); + } + finally + { + createLock.Release(); + } string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members"); foreach(string memberWD in members) @@ -548,6 +558,18 @@ public class Requests await Fs.WriteFile(msgPath, Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message))); + string reactDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/reactions/{msgId}"; + if (Fs.Exists(reactDir)) + { + Fs.DeleteDirectory(reactDir); + } + + string attDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/attachments/{msgId}"; + if (Fs.Exists(attDir)) + { + Fs.DeleteDirectory(attDir); + } + string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members"); foreach(string memberWD in members) { @@ -593,15 +615,23 @@ public class Requests string reactDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/reactions/{msgId}"; if (!Fs.Exists(reactDir)) Fs.CreateDirectory(reactDir); - long last = 0; - if (Fs.Exists($"{reactDir}/last")) + await createLock.WaitAsync(); + try { - last = long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{reactDir}/last"))); - } - long nextId = last + 1; + long last = 0; + if (Fs.Exists($"{reactDir}/last")) + { + last = long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{reactDir}/last"))); + } + long nextId = last + 1; - await Fs.WriteFile($"{reactDir}/{nextId}", Encoding.UTF8.GetBytes($"{id}\n{reaction}")); - await Fs.WriteFile($"{reactDir}/last", Encoding.UTF8.GetBytes(nextId.ToString())); + await Fs.WriteFile($"{reactDir}/{nextId}", Encoding.UTF8.GetBytes($"{id}\n{reaction}")); + await Fs.WriteFile($"{reactDir}/last", Encoding.UTF8.GetBytes(nextId.ToString())); + } + finally + { + createLock.Release(); + } string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members"); foreach(string memberWD in members) @@ -680,22 +710,22 @@ public class Requests string pinnedFilePath = $"{pinnedDir}/{msgId}"; bool isNowPinned = false; - if (Fs.Exists(pinnedFilePath)) - { - Fs.DeleteFile(pinnedFilePath); - isNowPinned = false; - } - else - { - await Fs.WriteFile(pinnedFilePath, new byte[0]); - isNowPinned = true; - } - - // Send larp.info.pin message - long nextId = 0; await createLock.WaitAsync(); try { + if (Fs.Exists(pinnedFilePath)) + { + Fs.DeleteFile(pinnedFilePath); + isNowPinned = false; + } + else + { + await Fs.WriteFile(pinnedFilePath, new byte[0]); + isNowPinned = true; + } + + // Send larp.info.pin message + long nextId = 0; if (Fs.Exists($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/messages/last")) { string lastStr = Encoding.UTF8.GetString(await Fs.ReadFile($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/messages/last")); @@ -709,7 +739,7 @@ public class Requests infoMessage.attachment = ""; infoMessage.author = id; infoMessage.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); - infoMessage.type = "larp.info.pin"; + infoMessage.type = isNowPinned ? "larp.info.pin" : "larp.info.unpin"; infoMessage.content = isNowPinned ? "{blah(message.pinned.by)}" : "{blah(message.unpinned.by)}"; infoMessage.key = ""; infoMessage.pervious = msgId;