First commit
This commit is contained in:
commit
0ac6ff9196
26 changed files with 2836 additions and 0 deletions
198
LarpixServer/Room/Requests.cs
Normal file
198
LarpixServer/Room/Requests.cs
Normal file
|
|
@ -0,0 +1,198 @@
|
|||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using LarpixServer.Filesystem;
|
||||
using LarpixServer.Utils.Jsons;
|
||||
using static LarpixServer.Utils.Utils;
|
||||
|
||||
namespace LarpixServer.Room;
|
||||
|
||||
public class Requests
|
||||
{
|
||||
public static SemaphoreSlim createLock = new SemaphoreSlim(1, 1);
|
||||
public static async Task<string> DmInvite(string id, string username2)
|
||||
{
|
||||
if (!Account.Utils.IsUserLocal(username2, out string domain)) //federation
|
||||
{
|
||||
return "";
|
||||
}
|
||||
username2 = username2.Split(":")[0];
|
||||
string id2 = await Account.Utils.IdFromName(username2);
|
||||
|
||||
SemaphoreSlim userLock = Account.Utils.GetUserLock(id2);
|
||||
await userLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
|
||||
string inviteFile = ACCOUNTS_DATA_DIR + $"/{id2}/dminvites/recv/{id};{DOMAIN}";
|
||||
if (Fs.Exists(inviteFile))
|
||||
{
|
||||
string inviteState = Encoding.UTF8.GetString(await Fs.ReadFile(inviteFile));
|
||||
switch (inviteState)
|
||||
{
|
||||
case "0": //invited
|
||||
return "User already invited";
|
||||
case "1": //accepted
|
||||
return "You already have a DM with this user";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
await Fs.WriteFile(inviteFile, Encoding.UTF8.GetBytes("0"));
|
||||
await Fs.WriteFile(ACCOUNTS_DATA_DIR + $"/{id}/dminvites/sent/{id2};{domain}", []); //we should also save that this user invited someone, because this will act as dm accept verification while federating
|
||||
}
|
||||
finally
|
||||
{
|
||||
userLock.Release();
|
||||
}
|
||||
|
||||
return "User invited";
|
||||
}
|
||||
|
||||
public static async Task<string> DmCreate(string id, string body)
|
||||
{
|
||||
await createLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
Universal3String serializedBody = JsonSerializer.Deserialize( //1 is ID2 nameWD, 2 is creators dm key, 3 is key for ID2
|
||||
body,
|
||||
AppJsonSerializerContext.Default.Universal3String
|
||||
);
|
||||
|
||||
string username2 = serializedBody.string1;
|
||||
bool isUserLocal = Account.Utils.IsUserLocal(username2, out string domain);
|
||||
string id2;
|
||||
username2 = username2.Split(":")[0];
|
||||
if (isUserLocal)
|
||||
{
|
||||
id2 = await Account.Utils.IdFromName(username2);
|
||||
}
|
||||
else //id z federacji
|
||||
{
|
||||
id2 = "?";
|
||||
}
|
||||
|
||||
string inviteFile = ACCOUNTS_DATA_DIR + $"/{id}/dminvites/recv/{id2};{domain}";
|
||||
if (Fs.Exists(inviteFile))
|
||||
{
|
||||
string inviteState = Encoding.UTF8.GetString(await Fs.ReadFile(inviteFile));
|
||||
switch (inviteState)
|
||||
{
|
||||
case "0": //invited
|
||||
await Fs.WriteFile(inviteFile, Encoding.UTF8.GetBytes("1")); //we are setting this to 1,
|
||||
//so now we should have an option to delete id from dmslist and still search it
|
||||
|
||||
|
||||
BigInteger dmId =
|
||||
BigInteger.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{ROOMS_DIR}/lastdm")));
|
||||
dmId++;
|
||||
var freeid =
|
||||
Path.GetFileName(Directory.EnumerateFiles(ROOMS_DIR + "/freedms").FirstOrDefault());
|
||||
if (!isUserLocal) //federacja
|
||||
{
|
||||
//jezeli freeid tez jest wolne na innym serwerze (tez jako freeid) to wszystko zostawiamy tak jak jest
|
||||
//w przeciwnym razie porownojemy lastid z 2 serwerow i bierzemy to większe + 1 jako docelowe id dma
|
||||
|
||||
//no ale moze to byc problematyczne
|
||||
}
|
||||
|
||||
if (freeid != null)
|
||||
{
|
||||
dmId = BigInteger.Parse(freeid);
|
||||
Fs.DeleteFile($"{ROOMS_DIR}/freedms/{freeid}");
|
||||
}
|
||||
else
|
||||
{
|
||||
await Fs.WriteFile($"{ROOMS_DIR}/lastdm", Encoding.UTF8.GetBytes(dmId.ToString()));
|
||||
}
|
||||
|
||||
await Fs.WriteFile($"{ROOMS_DIR}/dms/{dmId}/members",
|
||||
Encoding.UTF8.GetBytes($"{id}:{DOMAIN};{id2}:{domain}"));
|
||||
await Fs.WriteFile($"{ROOMS_DIR}/dms/{dmId}/messages/last", Encoding.UTF8.GetBytes($"0"));
|
||||
|
||||
Message startingMessage = new();
|
||||
startingMessage.attachment = "";
|
||||
startingMessage.author = "0";
|
||||
startingMessage.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||||
startingMessage.type = "larp.info";
|
||||
startingMessage.content =
|
||||
"At the beginning of a DM, you should always verify that the person you're talking to is the intended recipient";
|
||||
startingMessage.key = ""; //no key == NOT encrypted
|
||||
startingMessage.pervious = "";
|
||||
|
||||
await Fs.WriteFile($"{ROOMS_DIR}/dms/{dmId}/keys/0/{id};{DOMAIN}", //klucz dla uzytkownika tworzącego jest juz gotowy i ma zwykly zapis
|
||||
Encoding.UTF8.GetBytes(serializedBody.string2));
|
||||
|
||||
|
||||
Universal2String keys = JsonSerializer.Deserialize(
|
||||
await Account.Utils.GetUserKeys(id),
|
||||
AppJsonSerializerContext.Default.Universal2String
|
||||
);
|
||||
await Fs.WriteFile($"{ROOMS_DIR}/dms/{dmId}/keys/0/{id2};{domain}",
|
||||
Encoding.UTF8.GetBytes($"SETUP:{keys.string2};{serializedBody.string3}")); //jezeli mamy setup to [1] to jest publiczny klucz drugiej osoby,
|
||||
//a string 3 to zaszyfrowany klucz pokoju ktory musi odszyfrowac za pomoca swoich kluczy i tego publicznego
|
||||
|
||||
|
||||
await Fs.WriteFile($"{ROOMS_DIR}/dms/{dmId}/messages/0", Encoding.UTF8.GetBytes(
|
||||
JsonSerializer.Serialize(startingMessage, AppJsonSerializerContext.Default.Message)
|
||||
));
|
||||
|
||||
string dmspath = $"{ACCOUNTS_DATA_DIR}/{id}/dms";
|
||||
string dms;
|
||||
if (!Fs.Exists(dmspath))
|
||||
{
|
||||
dms = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
dms = Encoding.UTF8.GetString(await Fs.ReadFile(dmspath));
|
||||
}
|
||||
|
||||
await Fs.WriteFile(dmspath, Encoding.UTF8.GetBytes($"{dmId};{dms}"));
|
||||
|
||||
if (isUserLocal)
|
||||
{
|
||||
SemaphoreSlim userLock = Account.Utils.GetUserLock(id2);
|
||||
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}"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
userLock.Release();
|
||||
}
|
||||
}
|
||||
else //federacja
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return "DM accepted";
|
||||
case "1": //accepted
|
||||
return "You already have a DM with this user";
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
createLock.Release();
|
||||
}
|
||||
return "You can't create a DM without an invitation";
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue