forked from Telodendria/Telodendria
[MOD] Separate IsRoomFiltered
This commit is contained in:
parent
6f045083a2
commit
c25db688ba
5 changed files with 86 additions and 50 deletions
BIN
src/.Filter.c.swp
Normal file
BIN
src/.Filter.c.swp
Normal file
Binary file not shown.
62
src/Filter.c
62
src/Filter.c
|
@ -24,12 +24,70 @@
|
|||
*/
|
||||
#include <Filter.h>
|
||||
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Str.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;
|
||||
}
|
||||
|
|
BIN
src/Routes/.RouteSync.c.swp
Normal file
BIN
src/Routes/.RouteSync.c.swp
Normal file
Binary file not shown.
|
@ -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;
|
||||
|
@ -153,6 +118,7 @@ ROUTE_IMPL(RouteSync, path, argp)
|
|||
filter = HashMapGet(params, "filter");
|
||||
timeoutDuration = timeout ? atoi(timeout) : 0;
|
||||
|
||||
/* TODO: Abstract this into a DecodeFilter function */
|
||||
if (filter && *filter != '{')
|
||||
{
|
||||
/* The filter must be located in the database */
|
||||
|
@ -188,8 +154,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))
|
||||
|
@ -262,20 +229,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
|
||||
);
|
||||
|
|
|
@ -47,4 +47,9 @@
|
|||
extern HashMap *
|
||||
FilterApply(Filter *, HashMap *);
|
||||
|
||||
/**
|
||||
* Verifies if a room would be filtered out by a filter given in.
|
||||
*/
|
||||
extern bool IsRoomFiltered(Filter *, char *);
|
||||
|
||||
#endif /* TELODENDRIA_FILTER_H */
|
||||
|
|
Loading…
Reference in a new issue