From c22d628c8608430db095b263efa62abdc0d1c27f Mon Sep 17 00:00:00 2001 From: lda Date: Mon, 20 May 2024 16:37:41 +0200 Subject: [PATCH] [ADD/WIP] Start implementing invites. I'll need to add the direct flag somewhere in the room leaves. Also, it seems like there's a giant bottleneck in my code... --- src/Room.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Room.c b/src/Room.c index ce99bae..222917f 100644 --- a/src/Room.c +++ b/src/Room.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -76,7 +77,6 @@ GenerateRoomId(ServerPart s) string = ParserRecomposeCommonID(cid); Free(cid.local); - return string; } static void @@ -84,7 +84,7 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s) { CommonID sender; char *sender_str, *key, *version; - HashMap *content, *event, *override; + HashMap *content, *event, *override, *pl_content; Array *initial_states; JsonValue *val; size_t i; @@ -92,7 +92,7 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s) char *join_rules_preset = NULL; char *history_visibility_preset = NULL; char *guest_access_preset = NULL; - /* TODO: Manage trusted private chat */ + bool trusted_room = false; sender.sigil = '@'; sender.local = UserGetName(user); @@ -150,6 +150,7 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s) } HashMapSet(content, key, JsonValueDuplicate(val)); } + pl_content = JsonDuplicate(content); event = RoomEventCreate(sender_str, "m.room.power_levels", "", content); JsonFree(RoomEventSend(room, event)); JsonFree(event); @@ -163,7 +164,7 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s) guest_access_preset = "forbidden"; break; case ROOM_CREATE_TRUSTED: - /* TODO */ + trusted_room = true; /* Fallthrough */ case ROOM_CREATE_PRIVATE: join_rules_preset = "invite"; @@ -272,6 +273,40 @@ RoomPopulate(Room *room, User *user, RoomCreateRequest *req, ServerPart s) Free(fullStr); Free(serverStr); } + /* Invites */ + for (i = 0; i < ArraySize(req->invite); i++) + { + char *user_id = ArrayGet(req->invite, i); + + if (!user_id || !ValidCommonID(user_id, '@')) + { + /* TODO: Raise error. */ + break; + } + + content = HashMapCreate(); + if (req->is_direct) + { + JsonSet(content, JsonValueBoolean(true), 1, "is_direct"); + } + JsonSet(content, JsonValueString("invite"), 1, "membership"); + event = RoomEventCreate(sender_str, "m.room.member", user_id, content); + JsonFree(RoomEventSend(room, event)); + JsonFree(event); + + if (trusted_room) + { + JsonValue *own = JsonGet(pl_content, 2, "users", sender_str); + JsonValueFree(JsonSet( + pl_content, JsonValueDuplicate(own), + 2, "users", user_id)); + } + + } + event = RoomEventCreate(sender_str, "m.room.power_levels", "", pl_content); + JsonFree(RoomEventSend(room, event)); + JsonFree(event); + /* TODO: The rest of the events mandated by the specification on * POST /createRoom, and error management. */