From 1753c2188bd15328d33c7805aaedd81f4129914f Mon Sep 17 00:00:00 2001 From: lda Date: Fri, 17 May 2024 13:52:25 +0200 Subject: [PATCH] [ADD/UNTESTED] Set ServerPart on room creation Not tested as of now! --- src/Room.c | 26 ++++++++++++++++++-------- src/Routes/RouteCreateRoom.c | 9 ++++++++- src/include/Room.h | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/Room.c b/src/Room.c index efdf526..8a6bb70 100644 --- a/src/Room.c +++ b/src/Room.c @@ -63,15 +63,22 @@ struct Room }; static char * -GenerateRoomId(void) +GenerateRoomId(ServerPart s) { - return StrRandom(32); /* TODO: Add extra room info somehow - * (I don't feel like theres really a way to - * get the server info where we're doing that.) */ + CommonID cid; + char *string; + + cid.sigil = '!'; + cid.local = StrRandom(32); + cid.server = s; + string = ParserRecomposeCommonID(cid); + Free(cid.local); + + return string; } Room * -RoomCreate(Db * db, User *user, RoomCreateRequest * req) +RoomCreate(Db * db, User *user, RoomCreateRequest * req, ServerPart s) { Room *room; char *version_string, *full_creator; @@ -92,9 +99,9 @@ RoomCreate(Db * db, User *user, RoomCreateRequest * req) } room = Malloc(sizeof(Room)); room->db = db; - room->id = GenerateRoomId(); /* TODO: Check config. */ - room->creator.hostname = NULL; - room->creator.port = NULL; + room->creator.hostname = s.hostname ? StrDuplicate(s.hostname) : NULL; + room->creator.port = s.port ? StrDuplicate(s.port) : NULL; + room->id = GenerateRoomId(s); room->version = version_num; room->state_ref = DbCreate(db, 3, "rooms", room->id, "state"); @@ -1350,6 +1357,9 @@ RoomEventSendV1(Room * room, HashMap * event) RoomAddEventV1(room, pdu); valid = true; + /* If it is a client event, we should make sure that we shout at + * every other homeserver about our new event. */ + finish: if (state) { diff --git a/src/Routes/RouteCreateRoom.c b/src/Routes/RouteCreateRoom.c index 6fd2430..733e4e6 100644 --- a/src/Routes/RouteCreateRoom.c +++ b/src/Routes/RouteCreateRoom.c @@ -41,12 +41,17 @@ ROUTE_IMPL(RouteCreateRoom, path, argp) Db *db = args->matrixArgs->db; RoomCreateRequest parsed; User *user = NULL; + Config cfg; + ServerPart server; char *token; char *err; (void) path; + ConfigLock(db, &cfg); + ParseServerPart(cfg.serverName, &server); + if (HttpRequestMethodGet(args->context) != HTTP_POST) { err = "Unknown request method."; @@ -86,7 +91,7 @@ ROUTE_IMPL(RouteCreateRoom, path, argp) JsonFree(request); request = NULL; - if (!(room = RoomCreate(db, user, &parsed))) + if (!(room = RoomCreate(db, user, &parsed, server))) { err = "Couldn't create room."; /* Consider another error status. */ @@ -102,5 +107,7 @@ finish: JsonFree(request); RoomUnlock(room); UserUnlock(user); + ConfigUnlock(&cfg); + ServerPartFree(server); return response; } diff --git a/src/include/Room.h b/src/include/Room.h index 94c6794..be1ef1a 100644 --- a/src/include/Room.h +++ b/src/include/Room.h @@ -55,7 +55,7 @@ typedef struct Room Room; * Create a new room in the given database using the given * RoomCreateRequest. */ -extern Room * RoomCreate(Db *, User *, RoomCreateRequest *); +extern Room * RoomCreate(Db *, User *, RoomCreateRequest *, ServerPart); /** * Lock the existing room in the specified database,