[ADD] Implement barebones of GET /rooms/{id}/alias

This commit is contained in:
lda 2023-11-29 22:42:23 +01:00
parent ad1901017f
commit 56d348454e
4 changed files with 62 additions and 18 deletions

View file

@ -327,3 +327,19 @@ CommonIDFree(CommonID id)
Free(id.server.port);
}
}
int
ValidCommonID(char *str, char sigil)
{
CommonID id;
int ret;
memset(&id, 0, sizeof(CommonID));
if (!str)
{
return 0;
}
ret = ParseCommonID(str, &id) && id.sigil == sigil;
CommonIDFree(id);
return ret;
}

View file

@ -53,7 +53,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
User *user = NULL;
CommonID aliasID;
CommonID roomID;
Config *config;
aliasID.sigil = '\0';
@ -61,16 +60,10 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
aliasID.server.hostname = NULL;
aliasID.server.port = NULL;
roomID.sigil = '\0';
roomID.local = NULL;
roomID.server.hostname = NULL;
roomID.server.port = NULL;
config = ConfigLock(db);
if (!ParseCommonID(alias, &aliasID) || aliasID.sigil != '#')
{
Log(LOG_INFO, "sigil: ' %c '", aliasID.sigil);
msg = "Invalid room alias.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
@ -161,7 +154,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
goto finish;
}
if (!ParseCommonID(id, &roomID) || roomID.sigil != '!')
if (!ValidCommonID(id, '!'))
{
msg = "Invalid room ID.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
@ -201,7 +194,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
}
roomAlias = JsonValueAsObject(val);
id = StrDuplicate(JsonValueAsString(HashMapGet(roomAlias, "id")));
Log(LOG_INFO, "id: %s", id);
if (!(UserGetPrivileges(user) & USER_ALIAS) && !StrEquals(UserGetName(user), JsonValueAsString(JsonGet(roomAlias, 1, "createdBy"))))
{
@ -229,7 +221,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
}
}
Free(id);
}
response = HashMapCreate();
@ -242,7 +233,6 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
finish:
CommonIDFree(aliasID);
CommonIDFree(roomID);
ConfigUnlock(config);
UserUnlock(user);
DbUnlock(db, ref);

View file

@ -25,25 +25,58 @@
#include <Routes.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Db.h>
#include <Matrix.h>
#include <User.h>
ROUTE_IMPL(RouteRoomAliases, path, argp)
{
RouteArgs *args = argp;
char *roomId = ArrayGet(path, 0);
char *roomId = HttpUrlDecode(ArrayGet(path, 0));
char *token;
char *msg;
HashMap *request = NULL;
HashMap *response = NULL;
HashMap *aliases = NULL;
HashMap *reversealias = NULL;
JsonValue *val;
Db *db = args->matrixArgs->db;
DbRef *ref = NULL;
(void) roomId;
User *user = NULL;
/* TODO: Placeholder; remove. */
goto finish;
/* TODO: Also check permissions. */
response = MatrixGetAccessToken(args->context, &token);
if (response)
{
goto finish;
}
user = UserAuthenticate(db, token);
/* TODO: Check if user is authorised. */
ref = DbLock(db, 1, "aliases");
aliases = DbJson(ref);
reversealias = JsonValueAsObject(JsonGet(aliases, 2, "id", roomId));
if (!reversealias)
{
/* We do not know about the room ID. */
msg = "Unknown room ID.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
response = HashMapCreate();
val = JsonGet(reversealias, 1, "aliases");
HashMapSet(response, "aliases", JsonValueDuplicate(val));
finish:
DbUnlock(db, ref);
JsonFree(request);
UserUnlock(user);
Free(roomId);
return response;
}

View file

@ -58,9 +58,14 @@ typedef struct CommonID {
* by the [matrix] specification.
*/
extern int ParseCommonID(char *, CommonID *);
/**
* Checks whenever the string is a valid common ID with the correct sigil.
*/
extern int ValidCommonID(char *, char);
/**
* Frees a CommonID's values. Note that it doesn't free the CommonID itself. */
* Frees a CommonID's values. Note that it doesn't free the CommonID itself.
*/
extern void CommonIDFree(CommonID id);