diff --git a/LarpixServer/Account/Requests.cs b/LarpixServer/Account/Requests.cs index 4d1857b..a8b4cc7 100644 --- a/LarpixServer/Account/Requests.cs +++ b/LarpixServer/Account/Requests.cs @@ -852,6 +852,15 @@ public static async Task Auth(HttpContext context, Func next, IQueryCollec await context.Response.WriteAsync(Encryption.Encryption.EncryptString(await Room.Groups.GroupJoin(id, payload.string1, payload.string2), password)); break; } + case "group/leave": + { + Universal2String payload = JsonSerializer.Deserialize( + serializedBody.string2, + AppJsonSerializerContext.Default.Universal2String + ); + await context.Response.WriteAsync(Encryption.Encryption.EncryptString(await Room.Groups.GroupLeave(id, payload.string1), password)); + break; + } case "group/members": await context.Response.WriteAsync(Encryption.Encryption.EncryptString(await Room.Groups.GroupMembers(id, serializedBody.string2), password)); break; diff --git a/LarpixServer/Room/Groups.cs b/LarpixServer/Room/Groups.cs index 2be3e63..d7e355c 100644 --- a/LarpixServer/Room/Groups.cs +++ b/LarpixServer/Room/Groups.cs @@ -519,4 +519,62 @@ public class Groups await Fs.WriteFile(path, contentBytes); return "success:storage.entry.updated"; } + + public static async Task GroupLeave(string userId, string groupId) + { + string[] parts = groupId.Split(';'); + string id = parts[0]; + string domain = parts.Length > 1 ? parts[1] : Utils.Utils.DOMAIN; + string groupPath = $"{Utils.Utils.ROOMS_DIR}/groups/{domain}/{id}"; + + if (!Fs.Exists(groupPath)) + return "error:group.not.found"; + + string memberPath = $"{groupPath}/members/{userId};{Utils.Utils.DOMAIN}"; + if (!Fs.Exists(memberPath)) + return "error:not.a.member"; + + await Room.Requests.createLock.WaitAsync(); + try + { + // Remove from members + Fs.DeleteFile(memberPath); + + // Remove group from user's groups + string userGroupPath = $"{Utils.Utils.ACCOUNTS_DATA_DIR}/{userId}/groups/{id};{domain}"; + if (Fs.Exists(userGroupPath)) + { + Fs.DeleteFile(userGroupPath); + } + + // Require new key + await Fs.WriteFile($"{groupPath}/keys/newrequired", Encoding.UTF8.GetBytes("true")); + + // Add info message + Message msg = new(); + msg.attachment = ""; + msg.author = userId; + msg.timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); + msg.type = "larp.info.leave"; + msg.content = "{blah(group.left)}"; + msg.key = ""; + msg.pervious = ""; + msg.responded = ""; + + ulong msgId = 0; + if (Fs.Exists($"{groupPath}/messages/last")) + { + msgId = ulong.Parse(Encoding.UTF8.GetString(await Fs.ReadFile($"{groupPath}/messages/last"))); + msgId++; + } + await Fs.WriteFile($"{groupPath}/messages/last", Encoding.UTF8.GetBytes(msgId.ToString())); + await Fs.WriteFile($"{groupPath}/messages/{msgId}", Encoding.UTF8.GetBytes(JsonSerializer.Serialize(msg, AppJsonSerializerContext.Default.Message))); + } + finally + { + Room.Requests.createLock.Release(); + } + + return "success"; + } }