forked from Telodendria/Telodendria
[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
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:
parent
b72f18538d
commit
d0a447a409
3 changed files with 73 additions and 7 deletions
41
src/Room.c
41
src/Room.c
|
@ -31,6 +31,8 @@
|
||||||
|
|
||||||
#include <Schema/RoomCreateRequest.h>
|
#include <Schema/RoomCreateRequest.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct Room
|
struct Room
|
||||||
{
|
{
|
||||||
Db *db;
|
Db *db;
|
||||||
|
@ -40,14 +42,45 @@ struct Room
|
||||||
int version;
|
int version;
|
||||||
};
|
};
|
||||||
|
|
||||||
Room *
|
static char *
|
||||||
RoomCreate(Db * db, RoomCreateRequest * req)
|
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;
|
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 *
|
Room *
|
||||||
RoomLock(Db * db, char *id)
|
RoomLock(Db * db, char *id)
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,11 +23,13 @@
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "Cytoplasm/Http.h"
|
||||||
#include <Routes.h>
|
#include <Routes.h>
|
||||||
|
|
||||||
#include <Cytoplasm/Json.h>
|
#include <Cytoplasm/Json.h>
|
||||||
|
|
||||||
#include <Schema/RoomCreateRequest.h>
|
#include <Room.h>
|
||||||
|
#include <User.h>
|
||||||
|
|
||||||
ROUTE_IMPL(RouteCreateRoom, path, argp)
|
ROUTE_IMPL(RouteCreateRoom, path, argp)
|
||||||
{
|
{
|
||||||
|
@ -35,7 +37,12 @@ ROUTE_IMPL(RouteCreateRoom, path, argp)
|
||||||
|
|
||||||
HashMap *request = NULL;
|
HashMap *request = NULL;
|
||||||
HashMap *response;
|
HashMap *response;
|
||||||
|
Room *room = NULL;
|
||||||
|
Db *db = args->matrixArgs->db;
|
||||||
RoomCreateRequest parsed;
|
RoomCreateRequest parsed;
|
||||||
|
User *user = NULL;
|
||||||
|
|
||||||
|
char *token;
|
||||||
char *err;
|
char *err;
|
||||||
|
|
||||||
(void) path;
|
(void) path;
|
||||||
|
@ -48,6 +55,18 @@ ROUTE_IMPL(RouteCreateRoom, path, argp)
|
||||||
goto finish;
|
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));
|
request = JsonDecode(HttpServerStream(args->context));
|
||||||
if (!request)
|
if (!request)
|
||||||
{
|
{
|
||||||
|
@ -67,9 +86,21 @@ ROUTE_IMPL(RouteCreateRoom, path, argp)
|
||||||
JsonFree(request);
|
JsonFree(request);
|
||||||
request = NULL;
|
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();
|
response = HashMapCreate();
|
||||||
|
JsonSet(response, JsonValueString(RoomIdGet(room)), 1, "room_id");
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
JsonFree(request);
|
JsonFree(request);
|
||||||
|
RoomUnlock(room);
|
||||||
|
UserUnlock(user);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,8 @@
|
||||||
|
|
||||||
#include <Schema/RoomCreateRequest.h>
|
#include <Schema/RoomCreateRequest.h>
|
||||||
|
|
||||||
|
#include <User.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The functions in this API operate on an opaque structure.
|
* 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
|
* Create a new room in the given database using the given
|
||||||
* RoomCreateRequest.
|
* RoomCreateRequest.
|
||||||
*/
|
*/
|
||||||
extern Room * RoomCreate(Db *, RoomCreateRequest *);
|
extern Room * RoomCreate(Db *, User *, RoomCreateRequest *);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lock the existing room in the specified database,
|
* Lock the existing room in the specified database,
|
||||||
|
|
Loading…
Reference in a new issue