[ADD/WIP] Start adding room ID -> alias relation

This commit is contained in:
lda 2023-11-29 19:30:40 +01:00
parent c1933a2184
commit 48ffd86553
2 changed files with 29 additions and 8 deletions

View file

@ -25,6 +25,7 @@
#include <Routes.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Str.h>
@ -35,7 +36,7 @@
ROUTE_IMPL(RouteAliasDirectory, path, argp)
{
RouteArgs *args = argp;
char *alias = ArrayGet(path, 0);
char *alias = HttpUrlDecode(ArrayGet(path, 0));
HashMap *request = NULL;
HashMap *response;
@ -43,7 +44,9 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
Db *db = args->matrixArgs->db;
DbRef *ref = NULL;
HashMap *aliases;
HashMap *idObject;
JsonValue *val;
Array *arr;
char *token;
char *msg;
@ -67,6 +70,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
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);
@ -120,14 +124,17 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
if (HttpRequestMethodGet(args->context) == HTTP_PUT)
{
HashMap *newAlias;
char *id;
/* Check for server name.
* TODO: Take the port into account, that might need a
* refactor for it to use a ServerPart */
if (!StrEquals(aliasID.server.hostname, config->serverName))
{
Log(LOG_INFO, "'%s' != '%s'", aliasID.server.hostname, config->serverName);
msg = "Invalid server name.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_INVALID_PARAM, "Invalid servername");
response = MatrixErrorCreate(M_INVALID_PARAM, msg);
goto finish;
}
@ -146,16 +153,15 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
goto finish;
}
if (!JsonValueAsString(HashMapGet(request, "room_id")))
id = JsonValueAsString(HashMapGet(request, "room_id"));
if (!id)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, "Missing or invalid room_id.");
goto finish;
}
/* TODO: Validate room ID to make sure it is well
* formed. */
if (!ParseCommonID(JsonValueAsString(HashMapGet(request, "room_id")), &roomID) || aliasID.sigil != '!')
if (!ParseCommonID(id, &roomID) || roomID.sigil != '!')
{
msg = "Invalid room ID.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
@ -165,10 +171,22 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
newAlias = HashMapCreate();
HashMapSet(newAlias, "createdBy", JsonValueString(UserGetName(user)));
HashMapSet(newAlias, "id", JsonValueDuplicate(HashMapGet(request, "room_id")));
HashMapSet(newAlias, "id", JsonValueString(id));
HashMapSet(newAlias, "servers", JsonValueArray(ArrayCreate()));
JsonSet(aliases, JsonValueObject(newAlias), 2, "alias", alias);
if (!(idObject = JsonValueAsObject(JsonGet(aliases, 2, "id", id))))
{
arr = ArrayCreate();
idObject = HashMapCreate();
HashMapSet(idObject, "aliases", JsonValueArray(arr));
JsonSet(aliases, JsonValueObject(idObject), 2, "id", id);
}
val = HashMapGet(idObject, "aliases");
arr = JsonValueAsArray(val);
ArrayAdd(arr, JsonValueString(alias));
}
else
{
@ -187,6 +205,7 @@ ROUTE_IMPL(RouteAliasDirectory, path, argp)
}
JsonValueFree(HashMapDelete(JsonValueAsObject(HashMapGet(aliases, "alias")), alias));
/* TODO: Delete from "id" objects when putting aliases. */
}
response = HashMapCreate();
@ -204,5 +223,6 @@ finish:
UserUnlock(user);
DbUnlock(db, ref);
JsonFree(request);
Free(alias);
return response;
}

View file

@ -38,6 +38,7 @@ ROUTE_IMPL(RouteVersions, path, argp)
#define DECLARE_SPEC_VERSION(x) ArrayAdd(versions, JsonValueString(x))
DECLARE_SPEC_VERSION("v1.5");
DECLARE_SPEC_VERSION("v1.7");
/* Declare additional spec version support here. */