fix few locks and unpin message
This commit is contained in:
parent
0a0e79180a
commit
5c58492b60
2 changed files with 95 additions and 65 deletions
|
|
@ -40,8 +40,8 @@ public class BackgroundDelete : BackgroundService
|
||||||
{
|
{
|
||||||
string id = Path.GetFileName(account);
|
string id = Path.GetFileName(account);
|
||||||
SemaphoreSlim userLock = Utils.GetUserLock(id);
|
SemaphoreSlim userLock = Utils.GetUserLock(id);
|
||||||
await userLock.WaitAsync();
|
|
||||||
await Requests.createLock.WaitAsync();
|
await Requests.createLock.WaitAsync();
|
||||||
|
await userLock.WaitAsync();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string username = await Account.Utils.NameFromId(id);
|
string username = await Account.Utils.NameFromId(id);
|
||||||
|
|
@ -55,8 +55,8 @@ public class BackgroundDelete : BackgroundService
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
Requests.createLock.Release();
|
|
||||||
userLock.Release();
|
userLock.Release();
|
||||||
|
Requests.createLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -427,61 +427,71 @@ public class Requests
|
||||||
}
|
}
|
||||||
|
|
||||||
string msgDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/messages";
|
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();
|
Message msg = new Message();
|
||||||
msg.author = id;
|
await createLock.WaitAsync();
|
||||||
msg.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
try
|
||||||
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
|
long last = 0;
|
||||||
|
if (Fs.Exists($"{msgDir}/last"))
|
||||||
{
|
{
|
||||||
var attachmentsNode = System.Text.Json.Nodes.JsonNode.Parse(msg.attachment);
|
last = long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{msgDir}/last")));
|
||||||
if (attachmentsNode is System.Text.Json.Nodes.JsonArray arr)
|
}
|
||||||
{
|
nextId = last + 1;
|
||||||
string attDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/attachments/{nextId}";
|
|
||||||
if (!Fs.Exists(attDir)) Fs.CreateDirectory(attDir);
|
|
||||||
|
|
||||||
for (int i = 0; i < arr.Count; i++)
|
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
|
||||||
|
{
|
||||||
|
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;
|
string attDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/attachments/{nextId}";
|
||||||
if (attObj != null && attObj.ContainsKey("staging_id"))
|
if (!Fs.Exists(attDir)) Fs.CreateDirectory(attDir);
|
||||||
|
|
||||||
|
for (int i = 0; i < arr.Count; i++)
|
||||||
{
|
{
|
||||||
string stagingId = attObj["staging_id"].ToString();
|
var attObj = arr[i] as System.Text.Json.Nodes.JsonObject;
|
||||||
string stagingPath = $"{ROOMS_DIR}/dms_staging/{stagingId}";
|
if (attObj != null && attObj.ContainsKey("staging_id"))
|
||||||
if (Fs.Exists(stagingPath))
|
|
||||||
{
|
{
|
||||||
string attId = attObj["id"].ToString();
|
string stagingId = attObj["staging_id"].ToString();
|
||||||
string destPath = $"{attDir}/{attId}";
|
string stagingPath = $"{ROOMS_DIR}/dms_staging/{stagingId}";
|
||||||
Fs.MoveFile(stagingPath, destPath);
|
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}/{nextId}", Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message)));
|
||||||
await Fs.WriteFile($"{msgDir}/last", Encoding.UTF8.GetBytes(nextId.ToString()));
|
await Fs.WriteFile($"{msgDir}/last", Encoding.UTF8.GetBytes(nextId.ToString()));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
createLock.Release();
|
||||||
|
}
|
||||||
|
|
||||||
string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members");
|
string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members");
|
||||||
foreach(string memberWD in 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)));
|
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");
|
string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members");
|
||||||
foreach(string memberWD in members)
|
foreach(string memberWD in members)
|
||||||
{
|
{
|
||||||
|
|
@ -593,15 +615,23 @@ public class Requests
|
||||||
string reactDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/reactions/{msgId}";
|
string reactDir = $"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/reactions/{msgId}";
|
||||||
if (!Fs.Exists(reactDir)) Fs.CreateDirectory(reactDir);
|
if (!Fs.Exists(reactDir)) Fs.CreateDirectory(reactDir);
|
||||||
|
|
||||||
long last = 0;
|
await createLock.WaitAsync();
|
||||||
if (Fs.Exists($"{reactDir}/last"))
|
try
|
||||||
{
|
{
|
||||||
last = long.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{reactDir}/last")));
|
long last = 0;
|
||||||
}
|
if (Fs.Exists($"{reactDir}/last"))
|
||||||
long nextId = last + 1;
|
{
|
||||||
|
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}/{nextId}", Encoding.UTF8.GetBytes($"{id}\n{reaction}"));
|
||||||
await Fs.WriteFile($"{reactDir}/last", Encoding.UTF8.GetBytes(nextId.ToString()));
|
await Fs.WriteFile($"{reactDir}/last", Encoding.UTF8.GetBytes(nextId.ToString()));
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
createLock.Release();
|
||||||
|
}
|
||||||
|
|
||||||
string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members");
|
string[] members = Fs.ReadDirectory($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}/members");
|
||||||
foreach(string memberWD in members)
|
foreach(string memberWD in members)
|
||||||
|
|
@ -680,22 +710,22 @@ public class Requests
|
||||||
string pinnedFilePath = $"{pinnedDir}/{msgId}";
|
string pinnedFilePath = $"{pinnedDir}/{msgId}";
|
||||||
bool isNowPinned = false;
|
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();
|
await createLock.WaitAsync();
|
||||||
try
|
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"))
|
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"));
|
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.attachment = "";
|
||||||
infoMessage.author = id;
|
infoMessage.author = id;
|
||||||
infoMessage.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
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.content = isNowPinned ? "{blah(message.pinned.by)}" : "{blah(message.unpinned.by)}";
|
||||||
infoMessage.key = "";
|
infoMessage.key = "";
|
||||||
infoMessage.pervious = msgId;
|
infoMessage.pervious = msgId;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue