From fa0365fa3114cb260ac93ee5bb2f8465c5bbea18 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 18 Apr 2023 11:47:49 -0700 Subject: [PATCH] ircd::m: Add convenience interface for querying m.replace relation. --- include/ircd/m/m.h | 1 + include/ircd/m/replaced.h | 80 +++++++++++++++++++++++++++++++++++++++ matrix/room_messages.cc | 9 ++--- 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 include/ircd/m/replaced.h diff --git a/include/ircd/m/m.h b/include/ircd/m/m.h index b2a2b6c0a..a30841619 100644 --- a/include/ircd/m/m.h +++ b/include/ircd/m/m.h @@ -89,6 +89,7 @@ namespace ircd::m #include "direct_to_device.h" #include "visible.h" #include "redacted.h" +#include "replaced.h" #include "feds.h" #include "app.h" #include "bridge.h" diff --git a/include/ircd/m/replaced.h b/include/ircd/m/replaced.h new file mode 100644 index 000000000..7da29df5d --- /dev/null +++ b/include/ircd/m/replaced.h @@ -0,0 +1,80 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2023 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_M_REPLACED_H + +namespace ircd::m +{ + struct replaced; +} + +class ircd::m::replaced +:public returns +{ + m::relates relates; + + public: + IRCD_OVERLOAD(latest); + + replaced(const event::idx &, latest_t); + replaced(const event::idx &); + + replaced(const event::id &, latest_t); + replaced(const event::id &); + + explicit replaced(const event &, latest_t); + explicit replaced(const event &); +}; + +inline +ircd::m::replaced::replaced(const event &event) +:replaced{event.event_id} +{} + +inline +ircd::m::replaced::replaced(const event &event, + latest_t) +:replaced{event.event_id, latest} +{} + +inline +ircd::m::replaced::replaced(const event::id &event_id) +:replaced{index(std::nothrow, event_id)} +{} + +inline +ircd::m::replaced::replaced(const event::id &event_id, + latest_t) +:replaced{index(std::nothrow, event_id), latest} +{} + +inline +ircd::m::replaced::replaced(const event::idx &event_idx) +:relates +{ + .refs = event_idx, + .match_sender = true, +} +{ + this->returns::ret = relates.has("m.replace")? -1UL: 0UL; +} + +inline +ircd::m::replaced::replaced(const event::idx &event_idx, + latest_t) +:relates +{ + .refs = event_idx, + .match_sender = true, +} +{ + this->returns::ret = relates.latest("m.replace"); +} diff --git a/matrix/room_messages.cc b/matrix/room_messages.cc index 3bfb35ecb..885acaef3 100644 --- a/matrix/room_messages.cc +++ b/matrix/room_messages.cc @@ -54,15 +54,14 @@ ircd::m::replace(room::message &msg, const event::idx &event_idx) { // Find the latest edit of this; otherwise stick with the original. - const m::relates relates + const m::replaced replaced { - .refs = event_idx, - .match_sender = true, + event_idx, m::replaced::latest }; - const event::idx replace_idx + const event::idx &replace_idx { - relates.latest("m.replace") + replaced }; if(!replace_idx)