[MOD] Separate IsRoomFiltered

This commit is contained in:
LDA 2024-07-23 11:58:46 +02:00
parent 6f045083a2
commit c25db688ba
5 changed files with 86 additions and 50 deletions

BIN
src/.Filter.c.swp Normal file

Binary file not shown.

View file

@ -24,12 +24,70 @@
*/ */
#include <Filter.h> #include <Filter.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Str.h>
#include <Schema/Filter.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 * HashMap *
FilterApply(Filter * filter, HashMap * event) FilterApply(Filter * filter, HashMap * event)
{ {
(void) filter; HashMap *copy;
(void) event; char *sender, *room;
return NULL; 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

Binary file not shown.

View file

@ -34,6 +34,7 @@
#include <Cytoplasm/Util.h> #include <Cytoplasm/Util.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Filter.h>
#include <State.h> #include <State.h>
#include <User.h> #include <User.h>
#include <Room.h> #include <Room.h>
@ -59,42 +60,6 @@ StripStateEventSync(HashMap *pdu)
return ret; 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) ROUTE_IMPL(RouteSync, path, argp)
{ {
RouteArgs *args = argp; RouteArgs *args = argp;
@ -153,6 +118,7 @@ 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 && *filter != '{') if (filter && *filter != '{')
{ {
/* The filter must be located in the database */ /* The filter must be located in the database */
@ -188,8 +154,9 @@ ROUTE_IMPL(RouteSync, path, argp)
UserUnlock(user); UserUnlock(user);
/* TODO: Using pthreads' condition variables with a timeout would be /* 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) while (passed < timeoutDuration)
{ {
if (UserGetNotification(name)) if (UserGetNotification(name))
@ -262,19 +229,25 @@ ROUTE_IMPL(RouteSync, path, argp)
for (j = 0; j < ArraySize(el); j++) for (j = 0; j < ArraySize(el); j++)
{ {
char *event = ArrayGet(el, j); char *event = ArrayGet(el, j);
HashMap *e = RoomEventFetch(r, event); HashMap *eventObj = RoomEventFetch(r, event);
ClientEventWithoutRoomID rc = ClientfyEventSync(e); HashMap *filteredObj = FilterApply(&filterData, eventObj);
ClientEventWithoutRoomID *c = Malloc(sizeof(*c));
memcpy(c, &rc, sizeof(*c));
JsonFree(e); if (filteredObj)
if (j == 0)
{ {
firstEvent = c->event_id; ClientEventWithoutRoomID rc = ClientfyEventSync(filteredObj);
ClientEventWithoutRoomID *c = Malloc(sizeof(*c));
memcpy(c, &rc, sizeof(*c));
if (!firstEvent)
{
firstEvent = c->event_id;
}
ArrayAdd(joined.timeline.events, c);
} }
ArrayAdd(joined.timeline.events, c); JsonFree(eventObj);
JsonFree(filteredObj);
} }
joined.timeline.prev_batch = UserNewMessageToken( joined.timeline.prev_batch = UserNewMessageToken(
user, roomId, firstEvent user, roomId, firstEvent

View file

@ -47,4 +47,9 @@
extern HashMap * extern HashMap *
FilterApply(Filter *, 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 */ #endif /* TELODENDRIA_FILTER_H */