forked from Telodendria/Telodendria
Compare commits
3 commits
6f045083a2
...
55652eaa15
Author | SHA1 | Date | |
---|---|---|---|
|
55652eaa15 | ||
|
3d700de17d | ||
|
c25db688ba |
4 changed files with 164 additions and 68 deletions
113
src/Filter.c
113
src/Filter.c
|
@ -24,12 +24,121 @@
|
|||
*/
|
||||
#include <Filter.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <Schema/Filter.h>
|
||||
|
||||
|
||||
bool
|
||||
IsRoomFiltered(Filter *filter, char *roomID)
|
||||
{
|
||||
size_t i, count;
|
||||
if (!filter || !roomID)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
count = ArraySize(filter->room.not_rooms);
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
char *notRoom = ArrayGet(filter->room.not_rooms, i);
|
||||
if (StrEquals(roomID, notRoom))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
count = ArraySize(filter->room.rooms);
|
||||
if (count)
|
||||
{
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
char *room = ArrayGet(filter->room.rooms, i);
|
||||
if (StrEquals(roomID, room))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
HashMap *
|
||||
FilterApply(Filter * filter, HashMap * event)
|
||||
{
|
||||
(void) filter;
|
||||
(void) event;
|
||||
HashMap *copy;
|
||||
char *sender, *room;
|
||||
if (!event)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (!filter)
|
||||
{
|
||||
/* Do NOT filter anything out */
|
||||
return JsonDuplicate(event);
|
||||
}
|
||||
|
||||
sender = JsonValueAsString(HashMapGet(event, "sender"));
|
||||
room = JsonValueAsString(HashMapGet(event, "room_id"));
|
||||
|
||||
if (IsRoomFiltered(filter, room))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
copy = JsonDuplicate(event);
|
||||
|
||||
(void) sender;
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include <Cytoplasm/Util.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
#include <Filter.h>
|
||||
#include <State.h>
|
||||
#include <User.h>
|
||||
#include <Room.h>
|
||||
|
@ -59,42 +60,6 @@ StripStateEventSync(HashMap *pdu)
|
|||
return ret;
|
||||
}
|
||||
|
||||
/* Verify whenever a room is filtered out by a filter given in. */
|
||||
static bool
|
||||
IsRoomFiltered(Filter *filter, char *roomID)
|
||||
{
|
||||
size_t i, count;
|
||||
if (!filter || !roomID)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
count = ArraySize(filter->room.not_rooms);
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
char *notRoom = ArrayGet(filter->room.not_rooms, i);
|
||||
if (StrEquals(roomID, notRoom))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
count = ArraySize(filter->room.rooms);
|
||||
if (count)
|
||||
{
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
char *room = ArrayGet(filter->room.rooms, i);
|
||||
if (StrEquals(roomID, room))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ROUTE_IMPL(RouteSync, path, argp)
|
||||
{
|
||||
RouteArgs *args = argp;
|
||||
|
@ -103,7 +68,7 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
HashMap *params = NULL;
|
||||
HashMap *response = NULL;
|
||||
SyncResponse sync = { 0 };
|
||||
Filter filterData = { 0 };
|
||||
Filter *filterData = NULL;
|
||||
|
||||
Array *invites;
|
||||
Array *joins;
|
||||
|
@ -153,25 +118,15 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
filter = HashMapGet(params, "filter");
|
||||
timeoutDuration = timeout ? atoi(timeout) : 0;
|
||||
|
||||
if (filter && *filter != '{')
|
||||
if (filter)
|
||||
{
|
||||
/* The filter must be located in the database */
|
||||
DbRef *filterRef = DbLock(db, 3,
|
||||
"filters", UserGetName(user), filter
|
||||
);
|
||||
HashMap *filterJson = DbJson(filterRef);
|
||||
|
||||
if (!FilterFromJson(filterJson, &filterData, &err))
|
||||
char *userName = UserGetName(user);
|
||||
if (!(filterData = FilterDecode(db, userName, filter)))
|
||||
{
|
||||
err = "Couldn't decode filter given in";
|
||||
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||
response = MatrixErrorCreate(M_BAD_JSON, err);
|
||||
|
||||
DbUnlock(db, filterRef);
|
||||
goto finish;
|
||||
}
|
||||
|
||||
/* We now grabbed a filter JSON */
|
||||
DbUnlock(db, filterRef);
|
||||
}
|
||||
|
||||
if (!prevBatch)
|
||||
|
@ -188,8 +143,9 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
UserUnlock(user);
|
||||
|
||||
/* TODO: Using pthreads' condition variables with a timeout would be
|
||||
* the best way to proceed (as in Parsee's ParseeAwaitStanza function)
|
||||
*/
|
||||
* the best way to proceed (as in Parsee's ParseeAwaitStanza function).
|
||||
* Also, how would Cytoplasm proceed if the all of HTTP threads are used
|
||||
* for syncing? That sounds like a really bad bottleneck... */
|
||||
while (passed < timeoutDuration)
|
||||
{
|
||||
if (UserGetNotification(name))
|
||||
|
@ -217,7 +173,7 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
InvitedRooms invited = { 0 };
|
||||
HashMap *invitedObject;
|
||||
|
||||
if (IsRoomFiltered(&filterData, roomId))
|
||||
if (IsRoomFiltered(filterData, roomId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -248,7 +204,7 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
size_t j;
|
||||
Room *r;
|
||||
|
||||
if (IsRoomFiltered(&filterData, roomId))
|
||||
if (IsRoomFiltered(filterData, roomId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -262,20 +218,26 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
for (j = 0; j < ArraySize(el); j++)
|
||||
{
|
||||
char *event = ArrayGet(el, j);
|
||||
HashMap *e = RoomEventFetch(r, event);
|
||||
ClientEventWithoutRoomID rc = ClientfyEventSync(e);
|
||||
HashMap *eventObj = RoomEventFetch(r, event);
|
||||
HashMap *filteredObj = FilterApply(filterData, eventObj);
|
||||
|
||||
if (filteredObj)
|
||||
{
|
||||
ClientEventWithoutRoomID rc = ClientfyEventSync(filteredObj);
|
||||
ClientEventWithoutRoomID *c = Malloc(sizeof(*c));
|
||||
memcpy(c, &rc, sizeof(*c));
|
||||
|
||||
JsonFree(e);
|
||||
|
||||
if (j == 0)
|
||||
if (!firstEvent)
|
||||
{
|
||||
firstEvent = c->event_id;
|
||||
}
|
||||
|
||||
ArrayAdd(joined.timeline.events, c);
|
||||
}
|
||||
|
||||
JsonFree(eventObj);
|
||||
JsonFree(filteredObj);
|
||||
}
|
||||
joined.timeline.prev_batch = UserNewMessageToken(
|
||||
user, roomId, firstEvent
|
||||
);
|
||||
|
@ -316,7 +278,7 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
response = SyncResponseToJson(&sync);
|
||||
SyncResponseFree(&sync);
|
||||
finish:
|
||||
FilterFree(&filterData);
|
||||
FilterDestroy(filterData);
|
||||
UserUnlock(user);
|
||||
(void) path;
|
||||
return response;
|
||||
|
|
|
@ -379,7 +379,13 @@ StateCurrent(Room *room)
|
|||
|
||||
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;
|
||||
bool ret;
|
||||
|
@ -400,6 +406,7 @@ bool StateIterate(HashMap *state, char **type, char **key, void **event)
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *
|
||||
StateGet(HashMap *state, char *type, char *key)
|
||||
{
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include <Schema/Filter.h>
|
||||
|
||||
#include <Cytoplasm/HashMap.h>
|
||||
#include <Cytoplasm/Db.h>
|
||||
|
||||
/***
|
||||
* @Nm Filter
|
||||
|
@ -47,4 +48,21 @@
|
|||
extern HashMap *
|
||||
FilterApply(Filter *, HashMap *);
|
||||
|
||||
/**
|
||||
* Verifies if a room would be filtered out by a filter given in.
|
||||
*/
|
||||
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 */
|
||||
|
|
Loading…
Reference in a new issue