forked from Telodendria/Telodendria
[ADD] Add /devices
It was easy to add, so I did. Still want to store more information about devices(and maybe make the Cytoplasm HTTP server be able to actually show address information)
This commit is contained in:
parent
68c47e1ae6
commit
276b7fecd7
3 changed files with 107 additions and 0 deletions
|
@ -54,6 +54,7 @@ RouterBuild(void)
|
|||
R("/_matrix/client/v3/auth/(.*)/fallback/web", RouteUiaFallback);
|
||||
|
||||
R("/_matrix/client/v3/capabilities", RouteCapabilities);
|
||||
R("/_matrix/client/v3/devices", RouteDevices);
|
||||
R("/_matrix/client/v3/login", RouteLogin);
|
||||
R("/_matrix/client/v3/logout", RouteLogout);
|
||||
R("/_matrix/client/v3/logout/(all)", RouteLogout);
|
||||
|
|
105
src/Routes/RouteDevices.c
Normal file
105
src/Routes/RouteDevices.c
Normal file
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright (C) 2022-2024 Jordan Bancino <@jordan:bancino.net> with
|
||||
* other valuable contributors. See CONTRIBUTORS.txt for the full list.
|
||||
*
|
||||
* 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 <string.h>
|
||||
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/HashMap.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <User.h>
|
||||
|
||||
ROUTE_IMPL(RouteDevices, path, argp)
|
||||
{
|
||||
RouteArgs *args = argp;
|
||||
HashMap *response = NULL;
|
||||
HashMap *devices;
|
||||
|
||||
char *tokenstr, *deviceID;
|
||||
JsonValue *deviceVal;
|
||||
|
||||
char *msg;
|
||||
|
||||
size_t i = 0;
|
||||
Array *responseDevices;
|
||||
|
||||
Db *db = args->matrixArgs->db;
|
||||
|
||||
User *user;
|
||||
|
||||
if (HttpRequestMethodGet(args->context) != HTTP_GET)
|
||||
{
|
||||
msg = "This route only accepts GET.";
|
||||
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||
return MatrixErrorCreate(M_UNRECOGNIZED, msg);
|
||||
}
|
||||
|
||||
response = MatrixGetAccessToken(args->context, &tokenstr);
|
||||
if (response)
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
user = UserAuthenticate(db, tokenstr);
|
||||
if (!user)
|
||||
{
|
||||
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
|
||||
return MatrixErrorCreate(M_UNKNOWN_TOKEN, NULL);
|
||||
}
|
||||
if (!(devices = UserGetDevices(user)))
|
||||
{
|
||||
msg = "Couldn't get user's devices. This is not right.";
|
||||
HttpResponseStatus(args->context, HTTP_INTERNAL_SERVER_ERROR);
|
||||
response = MatrixErrorCreate(M_UNKNOWN, NULL);
|
||||
goto finish;
|
||||
}
|
||||
|
||||
response = HashMapCreate();
|
||||
HashMapSet(response,
|
||||
"devices", JsonValueArray((responseDevices = ArrayCreate()))
|
||||
);
|
||||
i = 0;
|
||||
while (HashMapIterateReentrant(devices, &deviceID, (void **) &deviceVal, &i))
|
||||
{
|
||||
HashMap *deviceInfo = HashMapCreate();
|
||||
HashMap *deviceObj = JsonValueAsObject(deviceVal);
|
||||
|
||||
JsonValue *potentialDisplay = HashMapGet(deviceObj, "displayName");
|
||||
|
||||
HashMapSet(deviceInfo, "device_id", JsonValueString(deviceID));
|
||||
HashMapSet(deviceInfo, "display_name", JsonValueDuplicate(potentialDisplay));
|
||||
/* TODO: As of now, Telodendria does not store enough information to figure out
|
||||
* last timestamp and IP address. */
|
||||
|
||||
ArrayAdd(responseDevices, JsonValueObject(deviceInfo));
|
||||
}
|
||||
|
||||
/* We do not need to free the device object as it lives with the user */
|
||||
finish:
|
||||
UserUnlock(user);
|
||||
(void) path;
|
||||
return response;
|
||||
}
|
|
@ -77,6 +77,7 @@ ROUTE(RouteVersions);
|
|||
ROUTE(RouteWellKnown);
|
||||
|
||||
ROUTE(RouteCapabilities);
|
||||
ROUTE(RouteDevices);
|
||||
ROUTE(RouteLogin);
|
||||
ROUTE(RouteLogout);
|
||||
ROUTE(RouteRegister);
|
||||
|
|
Loading…
Reference in a new issue