[MOD] Use j2s.

This commit is contained in:
lda 2023-11-02 15:44:04 +01:00
parent 5295e0295b
commit 4674621637
Signed by: lda
GPG key ID: 6898757653ABE3E6
2 changed files with 50 additions and 51 deletions

View file

@ -0,0 +1,13 @@
{
"header": "Schema\/UserDirectoryRequest.h",
"types": {
"UserDirectoryRequest": {
"fields": {
"search_term": { "type": "string" },
"limit": { "type": "integer" }
},
"type": "struct"
}
},
"guard": "TELODENDRIA_SCHEMA_USERDIRECTORYREQUEST_H"
}

View file

@ -4,7 +4,7 @@
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files * obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, * (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, * including without dirRequest.limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, * publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, * and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions: * subject to the following conditions:
@ -30,6 +30,8 @@
#include <Cytoplasm/Memory.h> #include <Cytoplasm/Memory.h>
#include <Cytoplasm/Db.h> #include <Cytoplasm/Db.h>
#include <Schema/UserDirectoryRequest.h>
#include <User.h> #include <User.h>
ROUTE_IMPL(RouteUserDirectory, path, argp) ROUTE_IMPL(RouteUserDirectory, path, argp)
@ -47,19 +49,19 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
User *user = NULL; User *user = NULL;
JsonValue *val = NULL;
char *token = NULL; char *token = NULL;
char *searchTerm = NULL;
char *requesterName = NULL; char *requesterName = NULL;
char *msg = NULL; char *msg = NULL;
size_t limit = 10; UserDirectoryRequest dirRequest;
size_t i, included; size_t i, included;
(void) path; (void) path;
dirRequest.search_term = NULL;
dirRequest.limit = Int64Create(0, 10);
if (HttpRequestMethodGet(args->context) != HTTP_POST) if (HttpRequestMethodGet(args->context) != HTTP_POST)
{ {
@ -76,6 +78,20 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
response = MatrixErrorCreate(M_NOT_JSON, NULL); response = MatrixErrorCreate(M_NOT_JSON, NULL);
goto finish; goto finish;
} }
if (!UserDirectoryRequestFromJson(request, &dirRequest, &msg))
{
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish;
}
if (!dirRequest.search_term)
{
msg = "Field 'search_term' not set.";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
goto finish;
}
response = MatrixGetAccessToken(args->context, &token); response = MatrixGetAccessToken(args->context, &token);
if (response) if (response)
@ -93,25 +109,6 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
} }
requesterName = UserGetName(user); requesterName = UserGetName(user);
/* TODO: Use j2s instead of an hardcoded parser */
if (!(val = JsonGet(request, 1, "search_term")) ||
JsonValueType(val) != JSON_STRING)
{
/* The Spec requires search_term to be set to a string. */
msg = "search_term must be set to a string";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, msg);
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(); response = HashMapCreate();
results = ArrayCreate(); results = ArrayCreate();
@ -131,7 +128,9 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
goto finish; goto finish;
} }
for (i = 0, included = 0; i < ArraySize(users) && included < limit; i++) #define IncludedLtLimit (Int64Lt(Int64Create(0, included), dirRequest.limit))
for (i = 0, included = 0; i < ArraySize(users) && IncludedLtLimit; i++)
#undef IncludedLtLimit
{ {
HashMap *obj; HashMap *obj;
User *currentUser; User *currentUser;
@ -154,8 +153,9 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
avatarUrl = UserGetProfile(currentUser, "avatar_url"); avatarUrl = UserGetProfile(currentUser, "avatar_url");
/* Check for the user ID and display name. */ /* Check for the user ID and display name. */
if (strstr(name, searchTerm) || if (strstr(name, dirRequest.search_term) ||
(lowerDisplayName && strstr(lowerDisplayName, searchTerm))) (lowerDisplayName &&
strstr(lowerDisplayName, dirRequest.search_term)))
{ {
included++; included++;
@ -170,9 +170,9 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
} }
if (name) if (name)
{ {
char *userID = StrConcat(4, "@", name, ":", config->serverName); char *uID = StrConcat(4, "@", name, ":", config->serverName);
JsonSet(obj, JsonValueString(userID), 1, "user_id"); JsonSet(obj, JsonValueString(uID), 1, "user_id");
Free(userID); Free(uID);
} }
ArrayAdd(results, JsonValueObject(obj)); ArrayAdd(results, JsonValueObject(obj));
} }
@ -186,28 +186,14 @@ ROUTE_IMPL(RouteUserDirectory, path, argp)
} }
} }
JsonSet(response, JsonValueArray(results), 1, "results"); JsonSet(response, JsonValueArray(results), 1, "results");
JsonSet(response, JsonValueBoolean(included == limit), 1, "limited"); JsonSet(response, JsonValueBoolean(Int64Eq(included, dirRequest.limit)),
1, "limited");
finish: finish:
if (user)
{
UserUnlock(user); UserUnlock(user);
}
if (request)
{
JsonFree(request); JsonFree(request);
}
if (searchTerm)
{
Free(searchTerm);
}
if (users)
{
DbListFree(users); DbListFree(users);
}
if (config)
{
ConfigUnlock(config); ConfigUnlock(config);
} UserDirectoryRequestFree(&dirRequest);
return response; return response;
} }