From d0a447a4090911d895070e3ac8317b6c2f8d6844 Mon Sep 17 00:00:00 2001 From: lda Date: Wed, 1 May 2024 22:00:59 +0200 Subject: [PATCH] [WIP/ADD] Start basic room work --- src/Room.c | 43 +++++++++++++++++++++++++++++++----- src/Routes/RouteCreateRoom.c | 33 ++++++++++++++++++++++++++- src/include/Room.h | 4 +++- 3 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/Room.c b/src/Room.c index 9eb9328..1d327f2 100644 --- a/src/Room.c +++ b/src/Room.c @@ -31,6 +31,8 @@ #include +#include + struct Room { Db *db; @@ -40,12 +42,43 @@ struct Room int version; }; -Room * -RoomCreate(Db * db, RoomCreateRequest * req) +static char * +GenerateRoomId(void) { - (void) db; - (void) req; - return NULL; + 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.) */ +} + +Room * +RoomCreate(Db * db, User *user, RoomCreateRequest * req) +{ + Room *room; + char *version_string; + int version_num = 1; + if (!db || !req || !user) + { + return NULL; + } + + version_string = req->room_version; + if (version_string) + { + /* TODO: Eventually use something else than room version 1 by + * default, and maybe add a config parameter. */ + version_num = atoi(version_string); + version_num = version_num == 0 ? 1 : version_num; + } + room = Malloc(sizeof(Room)); + room->db = db; + room->id = GenerateRoomId(); /* TODO: Check config. */ + room->version = version_num; + + /* TODO: A room is *not just* its state? */ + room->ref = DbCreate(db, 3, "rooms", room->id, "state"); + + /* TODO: Populate room with information */ + return room; } Room * diff --git a/src/Routes/RouteCreateRoom.c b/src/Routes/RouteCreateRoom.c index 126f905..6fd2430 100644 --- a/src/Routes/RouteCreateRoom.c +++ b/src/Routes/RouteCreateRoom.c @@ -23,11 +23,13 @@ * SOFTWARE. */ +#include "Cytoplasm/Http.h" #include #include -#include +#include +#include ROUTE_IMPL(RouteCreateRoom, path, argp) { @@ -35,7 +37,12 @@ ROUTE_IMPL(RouteCreateRoom, path, argp) HashMap *request = NULL; HashMap *response; + Room *room = NULL; + Db *db = args->matrixArgs->db; RoomCreateRequest parsed; + User *user = NULL; + + char *token; char *err; (void) path; @@ -48,6 +55,18 @@ ROUTE_IMPL(RouteCreateRoom, path, argp) goto finish; } + response = MatrixGetAccessToken(args->context, &token); + if (response) + { + return response; + } + user = UserAuthenticate(db, token); + if (!user) + { + HttpResponseStatus(args->context, HTTP_UNAUTHORIZED); + return MatrixErrorCreate(M_UNKNOWN_TOKEN, NULL); + } + request = JsonDecode(HttpServerStream(args->context)); if (!request) { @@ -67,9 +86,21 @@ ROUTE_IMPL(RouteCreateRoom, path, argp) JsonFree(request); request = NULL; + if (!(room = RoomCreate(db, user, &parsed))) + { + err = "Couldn't create room."; + /* Consider another error status. */ + HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR); + response = MatrixErrorCreate(M_UNRECOGNIZED, err); + goto finish; + } + response = HashMapCreate(); + JsonSet(response, JsonValueString(RoomIdGet(room)), 1, "room_id"); finish: JsonFree(request); + RoomUnlock(room); + UserUnlock(user); return response; } diff --git a/src/include/Room.h b/src/include/Room.h index 1de979a..0ec6b20 100644 --- a/src/include/Room.h +++ b/src/include/Room.h @@ -42,6 +42,8 @@ #include +#include + /** * The functions in this API operate on an opaque structure. */ @@ -51,7 +53,7 @@ typedef struct Room Room; * Create a new room in the given database using the given * RoomCreateRequest. */ -extern Room * RoomCreate(Db *, RoomCreateRequest *); +extern Room * RoomCreate(Db *, User *, RoomCreateRequest *); /** * Lock the existing room in the specified database,