2022-08-14 05:06:18 +02:00
|
|
|
// The Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) The Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2022 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
decltype(ircd::m::room::messages::fopts)
|
|
|
|
ircd::m::room::messages::fopts
|
|
|
|
{
|
|
|
|
event::keys::include
|
|
|
|
{
|
|
|
|
"content",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::messages::for_each(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2022-09-28 20:47:56 +02:00
|
|
|
event::fetch event{fopts}, replace{fopts};
|
|
|
|
return events.for_each([this, &closure, &event, &replace]
|
2022-08-14 05:06:18 +02:00
|
|
|
(const string_view &type, const uint64_t &depth, event::idx event_idx)
|
|
|
|
{
|
|
|
|
assert(type == "m.room.message");
|
2022-09-29 00:02:02 +02:00
|
|
|
if(!redacted && m::redacted(event_idx))
|
2022-08-14 05:06:18 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!seek(std::nothrow, event, event_idx))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
room::message msg
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Don't show messages which edit other messages, since we will or
|
|
|
|
// already have rendered it at the edited message, if we have it,
|
|
|
|
// otherwise ignored.
|
|
|
|
if(msg.replace_event())
|
|
|
|
return true;
|
|
|
|
|
2022-09-28 20:47:56 +02:00
|
|
|
m::replace(msg, replace, event_idx);
|
2022-08-14 05:06:18 +02:00
|
|
|
return closure(msg, depth, event_idx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-09-28 20:47:56 +02:00
|
|
|
ircd::m::replace(room::message &msg,
|
|
|
|
event::fetch &replace,
|
|
|
|
const event::idx &event_idx)
|
2022-08-14 05:06:18 +02:00
|
|
|
{
|
|
|
|
// Find the latest edit of this; otherwise stick with the original.
|
|
|
|
const m::relates relates
|
|
|
|
{
|
|
|
|
.refs = event_idx,
|
|
|
|
.match_sender = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx replace_idx
|
|
|
|
{
|
|
|
|
relates.latest("m.replace")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!replace_idx)
|
|
|
|
return false;
|
|
|
|
|
2022-09-28 20:47:56 +02:00
|
|
|
if(!seek(std::nothrow, replace, replace_idx))
|
2022-08-14 05:06:18 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const json::object replace_content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(replace)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object new_content
|
|
|
|
{
|
|
|
|
replace_content.get("m.new_content")
|
|
|
|
};
|
|
|
|
|
|
|
|
// XXX When m.new_content is undefined the edit does not take place.
|
|
|
|
if(!new_content && !replace_content.has("m.new_content"))
|
|
|
|
return false;
|
|
|
|
|
2022-09-28 20:47:56 +02:00
|
|
|
// If the content was replaced msg has to be updated.
|
|
|
|
msg = new_content;
|
2022-08-14 05:06:18 +02:00
|
|
|
return true;
|
|
|
|
}
|