[FIX] Remove RouteUserDirectory from lda-dev

This commit is contained in:
lda 2023-09-11 16:22:14 +02:00
parent e0327b8769
commit e23bd500e6
Signed by: lda
GPG key ID: 6898757653ABE3E6

View file

@ -1,213 +0,0 @@
/*
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Routes.h>
#include <Array.h>
#include <HashMap.h>
#include <Json.h>
#include <Str.h>
#include <Memory.h>
#include <User.h>
#include <Db.h>
ROUTE_IMPL(RouteUserDirectory, path, argp)
{
RouteArgs *args = argp;
HashMap *response = NULL;
HashMap *request = NULL;
Array *users = NULL;
Array *results = NULL;
Db *db = args->matrixArgs->db;
Config *config = NULL;
User *user = NULL;
JsonValue *val = NULL;
char *token = NULL;
char *searchTerm = NULL;
char *requesterName = NULL;
char *error;
size_t limit = 10;
size_t i, included;
(void) path;
if (HttpRequestMethodGet(args->context) != HTTP_POST)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNRECOGNIZED, NULL);
goto finish;
}
request = JsonDecode(HttpServerStream(args->context));
if (!request)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_NOT_JSON, NULL);
goto finish;
}
response = MatrixGetAccessToken(args->context, &token);
if (response)
{
return response;
}
/* TODO: Actually use information related to the user. */
user = UserAuthenticate(db, token);
if (!user)
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_UNKNOWN_TOKEN, NULL);
goto finish;
}
requesterName = UserGetName(user);
/* Parse limit and search_term */
if (!(val = JsonGet(request, 1, "search_term")) ||
JsonValueType(val) != JSON_STRING)
{
/* The Spec requires search_term to be set to a string. */
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
error = "search_term is not a string, or is non-existent";
response = MatrixErrorCreate(M_BAD_JSON, error);
goto finish;
}
searchTerm = StrLower(JsonValueAsString(val));
if ((val = JsonGet(request, 1, "limit")) &&
JsonValueType(val) == JSON_INTEGER)
{
/* It is however far more leinent on limit, with 10 by default. */
limit = JsonValueAsInteger(val);
}
response = HashMapCreate();
results = ArrayCreate();
/* TODO: Check for users matching search term and users outside our
* local server. */
users = DbList(db, 1, "users");
/* Offending line? */
config = ConfigLock(db);
if (!config)
{
error = "Directory endpoint failed to lock configuration.";
Log(LOG_ERR, error);
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
response = MatrixErrorCreate(M_UNKNOWN, error);
goto finish;
}
for (i = 0, included = 0; i < ArraySize(users) && included < limit; i++)
{
HashMap *obj;
User *currentUser;
char *name = ArrayGet(users, i);
char *displayName;
char *lowerDisplayName;
char *avatarUrl;
if (!StrEquals(name, requesterName))
{
currentUser = UserLock(db, name);
}
else
{
currentUser = user;
}
displayName = UserGetProfile(currentUser, "displayname");
lowerDisplayName = StrLower(displayName);
avatarUrl = UserGetProfile(currentUser, "avatar_url");
/* Check for the user ID and display name. */
if (strstr(name, searchTerm) ||
(lowerDisplayName && strstr(lowerDisplayName, searchTerm)))
{
included++;
obj = HashMapCreate();
if (displayName)
{
JsonSet(obj, JsonValueString(displayName), 1, "display_name");
}
if (avatarUrl)
{
JsonSet(obj, JsonValueString(displayName), 1, "avatar_url");
}
if (name)
{
char *userID = StrConcat(4, "@", name, ":", config->serverName);
JsonSet(obj, JsonValueString(userID), 1, "user_id");
Free(userID);
}
ArrayAdd(results, JsonValueObject(obj));
}
if (lowerDisplayName)
{
Free(lowerDisplayName);
}
if (!StrEquals(name, requesterName))
{
UserUnlock(currentUser);
}
}
JsonSet(response, JsonValueArray(results), 1, "results");
JsonSet(response, JsonValueBoolean(included == limit), 1, "limited");
finish:
if (user)
{
UserUnlock(user);
}
if (request)
{
JsonFree(request);
}
if (searchTerm)
{
Free(searchTerm);
}
if (users)
{
DbListFree(users);
}
if (config)
{
ConfigUnlock(config);
}
return response;
}