[ADD/WIP] Two-way replies

Call me Seija Kijin the way I apply my Reverse Ideology.
This commit is contained in:
LDA 2024-06-29 22:00:29 +02:00
commit c5df636bdb
5 changed files with 44 additions and 1 deletions

View file

@ -192,3 +192,14 @@ MatrixCreateReact(char *event, char *body)
return map; return map;
} }
void
MatrixSetReply(HashMap *map, char *event)
{
if (!map || !event)
{
return;
}
JsonValueFree(JsonSet(map, JsonValueString(event),
3, "m.relates_to", "m.in_reply_to", "event_id"
));
}

View file

@ -268,3 +268,14 @@ XMPPGetReplacedID(XMLElement *stanza)
); );
return replace ? HashMapGet(replace->attrs, "id") : NULL; return replace ? HashMapGet(replace->attrs, "id") : NULL;
} }
char *
XMPPGetReply(XMLElement *elem)
{
XMLElement *rep = XMLookForTKV(elem, "reply", "xmlns", "urn:xmpp:reply:0");
if (!rep)
{
return NULL;
}
return HashMapGet(rep->attrs, "id");
}

View file

@ -90,6 +90,7 @@ MessageStanza(ParseeData *args, XMLElement *stanza)
char *id_str = HashMapGet(stanza->attrs, "id"); char *id_str = HashMapGet(stanza->attrs, "id");
char *event_id = NULL; char *event_id = NULL;
char *replaced = XMPPGetReplacedID(stanza); char *replaced = XMPPGetReplacedID(stanza);
char *reply_to = XMPPGetReply(stanza);
if (ParseeVerifyStanza(args, chat_id, s_id_str) && !replaced) if (ParseeVerifyStanza(args, chat_id, s_id_str) && !replaced)
{ {
@ -145,9 +146,23 @@ MessageStanza(ParseeData *args, XMLElement *stanza)
} }
else else
{ {
/* TODO: Use HTML-formatted bodies, and respect the fallback
* trims the stanza provides us if possible. Element does
* not like raw bodies on replies. Go figure. */
size_t off =
reply_to ? ParseeFindDatastart(data->data) : 0;
HashMap *ev = MatrixCreateMessage(data->data + off);
if (reply_to)
{
char *reply_id = ParseeEventFromSID(
args, chat_id, reply_to
);
MatrixSetReply(ev, reply_id);
Free(reply_id);
}
event_id = ASSend( event_id = ASSend(
args->config, mroom_id, encoded, args->config, mroom_id, encoded,
"m.room.message", MatrixCreateMessage(data->data) "m.room.message", ev
); );
} }
ParseePushStanza(args, chat_id, s_id_str, id_str, event_id, from); ParseePushStanza(args, chat_id, s_id_str, id_str, event_id, from);

View file

@ -26,6 +26,9 @@ extern HashMap * MatrixCreateNameState(char *name);
/* Creates a join membership with a specific nickname */ /* Creates a join membership with a specific nickname */
extern HashMap * MatrixCreateNickChange(char *nick); extern HashMap * MatrixCreateNickChange(char *nick);
/* Modifies a hashmap to create a reply event */
extern void MatrixSetReply(HashMap *e, char *event);
/* Get the event ID of the reply if existent */ /* Get the event ID of the reply if existent */
extern char * MatrixGetReply(HashMap *event); extern char * MatrixGetReply(HashMap *event);
extern char * MatrixGetEdit(HashMap *event); extern char * MatrixGetEdit(HashMap *event);

View file

@ -70,4 +70,7 @@ extern char * XMPPGetOriginID(XMLElement *);
* is a replacement notice */ * is a replacement notice */
extern char * XMPPGetReplacedID(XMLElement *); extern char * XMPPGetReplacedID(XMLElement *);
/* Get the replied-to stanza ID, if existent. */
extern char * XMPPGetReply(XMLElement *elem);
#endif #endif