Change dm list indexing
This commit is contained in:
parent
9249ca3f89
commit
9abd013e91
3 changed files with 55 additions and 68 deletions
|
|
@ -1,9 +1,10 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using LarpixServer.Filesystem;
|
||||
|
||||
using LarpixServer.Utils.Jsons;
|
||||
using static LarpixServer.Utils.Utils;
|
||||
|
||||
namespace LarpixServer.Account;
|
||||
|
|
@ -208,14 +209,51 @@ public class Utils
|
|||
return await Fs.ReadFile(path);
|
||||
}
|
||||
|
||||
public static async Task UpdateUserDm(string id, string dmId, string isRead = "false", string timestamp = "")
|
||||
{
|
||||
|
||||
if (await Account.Utils.NameFromId(id) == "") //if user account just got deleted
|
||||
{
|
||||
return;
|
||||
}
|
||||
string dmPath = $"{ACCOUNTS_DATA_DIR}/{id}/dms/{dmId}";
|
||||
Universal3String fileDm = new Universal3String();
|
||||
if (Fs.Exists(dmPath))
|
||||
{
|
||||
fileDm = JsonSerializer.Deserialize(
|
||||
Encoding.UTF8.GetString(await Fs.ReadFile(dmPath)),
|
||||
AppJsonSerializerContext.Default.Universal3String
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileDm.string1 = "";
|
||||
fileDm.string2 = "";
|
||||
fileDm.string3 = "";
|
||||
}
|
||||
|
||||
fileDm.string1 = dmId;
|
||||
fileDm.string2 = timestamp;
|
||||
fileDm.string3 = isRead;
|
||||
|
||||
await Fs.WriteFile(dmPath, Encoding.UTF8.GetBytes(JsonSerializer.Serialize(fileDm, AppJsonSerializerContext.Default.Universal3String)));
|
||||
}
|
||||
|
||||
public static async Task<string> GetUserDms(string id)
|
||||
{
|
||||
string path = $"{ACCOUNTS_DATA_DIR}/{id}/dms";
|
||||
if (!Fs.Exists(path))
|
||||
{
|
||||
return "";
|
||||
return "{}";
|
||||
}
|
||||
return Encoding.UTF8.GetString(await Fs.ReadFile(path));
|
||||
|
||||
string dms = "{";
|
||||
foreach (var dmfile in Fs.ReadDirectory(path))
|
||||
{
|
||||
dms += $"{Encoding.UTF8.GetString(await Fs.ReadFile(dmfile))},";
|
||||
}
|
||||
|
||||
return dms.Substring(0, dms.Length - 1) + "}";
|
||||
}
|
||||
|
||||
public static async Task RemoveOldestDmIndex(string id) //i wont implement this, client should just warn users that they have like 99999999 dms and should leave some
|
||||
|
|
|
|||
|
|
@ -94,23 +94,14 @@ public class Receiver
|
|||
users.Sort();
|
||||
string dmId = $"{users[0]}_{users[1]}";
|
||||
|
||||
string dms2path = $"{ACCOUNTS_DATA_DIR}/{ids[0]}/dms";
|
||||
string dms2;
|
||||
if (await Account.Utils.NameFromId(ids[0]) == "") //if user account just got deleted
|
||||
{
|
||||
await context.Response.WriteAsync("DM accepted");
|
||||
return;
|
||||
}
|
||||
if (!Fs.Exists(dms2path))
|
||||
{
|
||||
dms2 = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
dms2 = Encoding.UTF8.GetString(await Fs.ReadFile(dms2path));
|
||||
}
|
||||
|
||||
await Fs.WriteFile(dms2path, Encoding.UTF8.GetBytes($"{dmId};{dms2}"));
|
||||
await Account.Utils.UpdateUserDm(ids[0], dmId, "false",
|
||||
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString());
|
||||
|
||||
|
||||
await context.Response.WriteAsync("DM accepted");
|
||||
return;
|
||||
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
|
|||
|
|
@ -101,9 +101,7 @@ public class Requests
|
|||
if (Fs.Exists(inviteFile))
|
||||
{
|
||||
|
||||
List<(string file, string todo, string content)> restoreList = new();
|
||||
|
||||
try //try because federation is involved, if it fails we should just clean all written files
|
||||
try
|
||||
{
|
||||
Fs.DeleteFile(inviteFile); //remove invite bc now its accepted (error = no invite & no dm)
|
||||
|
||||
|
|
@ -145,20 +143,8 @@ public class Requests
|
|||
JsonSerializer.Serialize(startingMessage, AppJsonSerializerContext.Default.Message)
|
||||
));
|
||||
|
||||
restoreList.Add(($"{ROOMS_DIR}/dms/{DOMAIN}/{dmId}", "deletef", ""));
|
||||
|
||||
string dmspath = $"{ACCOUNTS_DATA_DIR}/{id}/dms";
|
||||
string dms;
|
||||
if (!Fs.Exists(dmspath))
|
||||
{
|
||||
dms = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
dms = Encoding.UTF8.GetString(await Fs.ReadFile(dmspath));
|
||||
}
|
||||
restoreList.Add((dmspath, "write", dms));
|
||||
await Fs.WriteFile(dmspath, Encoding.UTF8.GetBytes($"{dmId};{dms}"));
|
||||
await Account.Utils.UpdateUserDm(id, dmId, "false",
|
||||
startingMessage.timestamp);
|
||||
|
||||
if (isUserLocal)
|
||||
{
|
||||
|
|
@ -166,22 +152,8 @@ public class Requests
|
|||
await userLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
string dms2path = $"{ACCOUNTS_DATA_DIR}/{id2}/dms";
|
||||
string dms2;
|
||||
if (await Account.Utils.NameFromId(id2) == "") //if user2 account just got deleted
|
||||
{
|
||||
return "DM accepted";
|
||||
}
|
||||
if (!Fs.Exists(dms2path))
|
||||
{
|
||||
dms2 = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
dms2 = Encoding.UTF8.GetString(await Fs.ReadFile(dms2path));
|
||||
}
|
||||
|
||||
await Fs.WriteFile(dms2path, Encoding.UTF8.GetBytes($"{dmId};{dms2}"));
|
||||
await Account.Utils.UpdateUserDm(id2, dmId, "false",
|
||||
startingMessage.timestamp);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -196,21 +168,7 @@ public class Requests
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
foreach (var entry in restoreList)
|
||||
{
|
||||
if (entry.todo == "delete")
|
||||
{
|
||||
Fs.DeleteFile(entry.file);
|
||||
}
|
||||
if (entry.todo == "deletef")
|
||||
{
|
||||
Fs.DeleteDirectory(entry.file);
|
||||
}
|
||||
if (entry.todo == "write")
|
||||
{
|
||||
await Fs.WriteFile(entry.file, Encoding.UTF8.GetBytes(entry.content));
|
||||
}
|
||||
}
|
||||
|
||||
return "Failed to accept DM. Ask for another invite";
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue