[MOD] Make a dedicated FilterDecode function
Some checks are pending
Compile Telodendria / Compile Telodendria (x86, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, alpine-v3.19) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, debian-v12.4) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, freebsd-v14.0) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, netbsd-v9.3) (push) Waiting to run
Compile Telodendria / Compile Telodendria (x86_64, openbsd-v7.4) (push) Waiting to run

I'll need to parse the JSON from a string, however, which means a stream
abstraction for being able to read from a string(or just a raw
bytestream). Guess I'll have to reimplement that away...
This commit is contained in:
LDA 2024-07-23 12:27:52 +02:00
parent 3d700de17d
commit 55652eaa15
4 changed files with 81 additions and 21 deletions

View file

@ -24,11 +24,15 @@
*/ */
#include <Filter.h> #include <Filter.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h> #include <Cytoplasm/Json.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <string.h>
#include <Schema/Filter.h> #include <Schema/Filter.h>
bool bool
IsRoomFiltered(Filter *filter, char *roomID) IsRoomFiltered(Filter *filter, char *roomID)
{ {
@ -91,3 +95,50 @@ FilterApply(Filter * filter, HashMap * event)
(void) sender; (void) sender;
return copy; return copy;
} }
Filter *
FilterDecode(Db *db, char *user, char *filterStr)
{
Filter *ret;
DbRef *filterRef;
HashMap *filterObj;
if (!db || !user || !filterStr)
{
return NULL;
}
if (*filterStr != '{')
{
char *err; /* TODO: use error status somewhere... */
filterRef = DbLock(db, 3, "filters", user, filterStr);
filterObj = DbJson(filterRef);
ret = Malloc(sizeof(*ret));
memset(ret, 0, sizeof(*ret));
if (!FilterFromJson(filterObj, ret, &err))
{
DbUnlock(db, filterRef);
Free(ret);
return NULL;
}
DbUnlock(db, filterRef);
(void) err;
return ret;
}
/* TODO: Decode JSON here... */
return NULL;
}
void
FilterDestroy(Filter *filter)
{
if (!filter)
{
return;
}
FilterFree(filter);
Free(filter);
}

View file

@ -68,7 +68,7 @@ ROUTE_IMPL(RouteSync, path, argp)
HashMap *params = NULL; HashMap *params = NULL;
HashMap *response = NULL; HashMap *response = NULL;
SyncResponse sync = { 0 }; SyncResponse sync = { 0 };
Filter filterData = { 0 }; Filter *filterData = NULL;
Array *invites; Array *invites;
Array *joins; Array *joins;
@ -118,26 +118,15 @@ ROUTE_IMPL(RouteSync, path, argp)
filter = HashMapGet(params, "filter"); filter = HashMapGet(params, "filter");
timeoutDuration = timeout ? atoi(timeout) : 0; timeoutDuration = timeout ? atoi(timeout) : 0;
/* TODO: Abstract this into a DecodeFilter function */ if (filter)
if (filter && *filter != '{')
{ {
/* The filter must be located in the database */ char *userName = UserGetName(user);
DbRef *filterRef = DbLock(db, 3, if (!(filterData = FilterDecode(db, userName, filter)))
"filters", UserGetName(user), filter
);
HashMap *filterJson = DbJson(filterRef);
if (!FilterFromJson(filterJson, &filterData, &err))
{ {
err = "Couldn't decode filter given in";
HttpResponseStatus(args->context, HTTP_BAD_REQUEST); HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
response = MatrixErrorCreate(M_BAD_JSON, err); response = MatrixErrorCreate(M_BAD_JSON, err);
DbUnlock(db, filterRef);
goto finish;
} }
/* We now grabbed a filter JSON */
DbUnlock(db, filterRef);
} }
if (!prevBatch) if (!prevBatch)
@ -184,7 +173,7 @@ ROUTE_IMPL(RouteSync, path, argp)
InvitedRooms invited = { 0 }; InvitedRooms invited = { 0 };
HashMap *invitedObject; HashMap *invitedObject;
if (IsRoomFiltered(&filterData, roomId)) if (IsRoomFiltered(filterData, roomId))
{ {
continue; continue;
} }
@ -215,7 +204,7 @@ ROUTE_IMPL(RouteSync, path, argp)
size_t j; size_t j;
Room *r; Room *r;
if (IsRoomFiltered(&filterData, roomId)) if (IsRoomFiltered(filterData, roomId))
{ {
continue; continue;
} }
@ -230,7 +219,7 @@ ROUTE_IMPL(RouteSync, path, argp)
{ {
char *event = ArrayGet(el, j); char *event = ArrayGet(el, j);
HashMap *eventObj = RoomEventFetch(r, event); HashMap *eventObj = RoomEventFetch(r, event);
HashMap *filteredObj = FilterApply(&filterData, eventObj); HashMap *filteredObj = FilterApply(filterData, eventObj);
if (filteredObj) if (filteredObj)
{ {
@ -289,7 +278,7 @@ ROUTE_IMPL(RouteSync, path, argp)
response = SyncResponseToJson(&sync); response = SyncResponseToJson(&sync);
SyncResponseFree(&sync); SyncResponseFree(&sync);
finish: finish:
FilterFree(&filterData); FilterDestroy(filterData);
UserUnlock(user); UserUnlock(user);
(void) path; (void) path;
return response; return response;

View file

@ -379,7 +379,13 @@ StateCurrent(Room *room)
return ret; return ret;
} }
bool StateIterate(HashMap *state, char **type, char **key, void **event)
/* TODO: USING A ',' DELIMITED HASHMAP IS A _BAD_ IDEA!
* I'll need to change these functions, and some things in the room
* implementation, thankfully _that_ was abstracted away, so I don't have
* to think about it too much... */
bool
StateIterate(HashMap *state, char **type, char **key, void **event)
{ {
char *tuple; char *tuple;
bool ret; bool ret;
@ -400,6 +406,7 @@ bool StateIterate(HashMap *state, char **type, char **key, void **event)
return ret; return ret;
} }
char * char *
StateGet(HashMap *state, char *type, char *key) StateGet(HashMap *state, char *type, char *key)
{ {

View file

@ -28,6 +28,7 @@
#include <Schema/Filter.h> #include <Schema/Filter.h>
#include <Cytoplasm/HashMap.h> #include <Cytoplasm/HashMap.h>
#include <Cytoplasm/Db.h>
/*** /***
* @Nm Filter * @Nm Filter
@ -52,4 +53,16 @@ FilterApply(Filter *, HashMap *);
*/ */
extern bool IsRoomFiltered(Filter *, char *); extern bool IsRoomFiltered(Filter *, char *);
/**
* Decodes a filter from a JSON stream to decode or an ID that was
* registered using the filter API.
*/
extern Filter * FilterDecode(Db *, char *, char *);
/**
* Frees all memory created by
* .Fn FilterDecode .
*/
extern void FilterDestroy(Filter *);
#endif /* TELODENDRIA_FILTER_H */ #endif /* TELODENDRIA_FILTER_H */