[ADD/WIP] Basic PDU depth system.
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

NOTE: This is *not* a good way to compute depth.
This commit is contained in:
lda 2024-05-18 20:17:54 +02:00
parent 5ca36396a2
commit 327730c2c6
2 changed files with 63 additions and 0 deletions

View file

@ -91,6 +91,8 @@ RoomCreate(Db * db, User *user, RoomCreateRequest * req, ServerPart s)
char *sender_str, *key; char *sender_str, *key;
JsonValue *val; JsonValue *val;
HashMap *override; HashMap *override;
Array *initial_states;
size_t i;
if (!db || !req || !user) if (!db || !req || !user)
{ {
return NULL; return NULL;
@ -164,6 +166,21 @@ RoomCreate(Db * db, User *user, RoomCreateRequest * req, ServerPart s)
/* TODO: Events set by the preset */ /* TODO: Events set by the preset */
/* TODO: Events in initial_state */ /* TODO: Events in initial_state */
initial_states = req->initial_state;
for (i = 0; i < ArraySize(initial_states); i++)
{
RoomStateEvent *rse = ArrayGet(initial_states, i);
HashMap *rseObject = RoomStateEventToJson(rse);
if (!HashMapGet(rseObject, "state_key"))
{
HashMapSet(rseObject, "state_key", JsonValueString(""));
}
HashMapSet(rseObject, "sender", JsonValueString(sender_str));
JsonFree(RoomEventSend(room, rseObject));
JsonFree(rseObject);
}
if (req->name) if (req->name)
{ {
content = HashMapCreate(); content = HashMapCreate();
@ -481,6 +498,7 @@ PopulateEventV1(Room * room, HashMap * event, PduV1 * pdu, ServerPart serv)
JsonDuplicate(JsonValueAsObject(JsonGet(event, 1, "content"))); JsonDuplicate(JsonValueAsObject(JsonGet(event, 1, "content")));
pdu->room_id = StrDuplicate(room->id); pdu->room_id = StrDuplicate(room->id);
pdu->signatures = HashMapCreate(); pdu->signatures = HashMapCreate();
pdu->depth = RoomGetDepth(room) + 1; /* TODO: Clamp this value. */
/* Create a random event ID for this PDU. /* Create a random event ID for this PDU.
* TODO: Optionally verify whenever it's already used, but that * TODO: Optionally verify whenever it's already used, but that
@ -1661,3 +1679,42 @@ RoomEventCreate(char *sender, char *type, char *key, HashMap *c)
return event; return event;
} }
/* TODO: This has it's fair share of problems, as a malicious
* homserver could *easily* fake a PDU depth and put this
* function to the depth limit. */
uint64_t
RoomGetDepth(Room *room)
{
Array *leaves;
HashMap *pdu;
size_t i;
uint64_t max;
if (!room)
{
return 0;
}
leaves = RoomPrevEventsGet(room);
if (!leaves)
{
return 0;
}
max = 0;
for (i = 0; i < ArraySize(leaves); i++)
{
uint64_t depth;
pdu = JsonValueAsObject(ArrayGet(leaves, i));
depth = JsonValueAsInteger(HashMapGet(pdu, "depth"));
if (depth > max)
{
max = depth;
}
}
(void) i;
(void) pdu;
return max;
}

View file

@ -170,4 +170,10 @@ extern bool RoomAddEventV1(Room *, PduV1);
*/ */
extern HashMap * RoomEventCreate(char *, char *, char *, HashMap *); extern HashMap * RoomEventCreate(char *, char *, char *, HashMap *);
/**
* Computes an approximation of the PDU depth by looking at
* the leaves stored in the room data.
*/
extern uint64_t RoomGetDepth(Room *);
#endif /* TELODENDRIA_ROOM_H */ #endif /* TELODENDRIA_ROOM_H */