[WIP/ADD] Start basic room work
Some checks are pending
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run

This commit is contained in:
lda 2024-05-01 22:00:59 +02:00
parent b72f18538d
commit d0a447a409
3 changed files with 73 additions and 7 deletions

View file

@ -31,6 +31,8 @@
#include <Schema/RoomCreateRequest.h>
#include <stdlib.h>
struct Room
{
Db *db;
@ -40,14 +42,45 @@ struct Room
int version;
};
Room *
RoomCreate(Db * db, RoomCreateRequest * req)
static char *
GenerateRoomId(void)
{
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)
{
(void) db;
(void) req;
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 *
RoomLock(Db * db, char *id)
{

View file

@ -23,11 +23,13 @@
* SOFTWARE.
*/
#include "Cytoplasm/Http.h"
#include <Routes.h>
#include <Cytoplasm/Json.h>
#include <Schema/RoomCreateRequest.h>
#include <Room.h>
#include <User.h>
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;
}

View file

@ -42,6 +42,8 @@
#include <Schema/RoomCreateRequest.h>
#include <User.h>
/**
* 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,