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

View file

@ -25,25 +25,58 @@
#include <Routes.h> #include <Routes.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h> #include <Cytoplasm/Json.h>
#include <Cytoplasm/Db.h>
#include <Matrix.h>
#include <User.h>
ROUTE_IMPL(RouteRoomAliases, path, argp) ROUTE_IMPL(RouteRoomAliases, path, argp)
{ {
RouteArgs *args = 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 *response = NULL;
HashMap *aliases = NULL;
HashMap *reversealias = NULL;
JsonValue *val;
Db *db = args->matrixArgs->db; Db *db = args->matrixArgs->db;
DbRef *ref = NULL; DbRef *ref = NULL;
(void) roomId; User *user = NULL;
/* TODO: Placeholder; remove. */ /* TODO: Also check permissions. */
response = MatrixGetAccessToken(args->context, &token);
if (response)
{
goto finish; 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: finish:
DbUnlock(db, ref); DbUnlock(db, ref);
JsonFree(request); UserUnlock(user);
Free(roomId);
return response; return response;
} }

View file

@ -58,9 +58,14 @@ typedef struct CommonID {
* by the [matrix] specification. * by the [matrix] specification.
*/ */
extern int ParseCommonID(char *, CommonID *); 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); extern void CommonIDFree(CommonID id);