2018-03-15 05:05:08 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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.
|
|
|
|
|
2019-09-06 19:56:49 +02:00
|
|
|
/// There are three principal component sections in this unit:
|
|
|
|
///
|
|
|
|
/// ---------------------------------- _
|
|
|
|
/// | 1. Incoming federation EDU hook | |
|
|
|
|
/// ---------------------------------- |
|
|
|
|
/// ---------------------------------- |
|
|
|
|
/// | 2. ircd::m::receipt API | |
|
|
|
|
/// ---------------------------------- |
|
|
|
|
/// ---------------------------------- |
|
|
|
|
/// | 3. Outgoing federation EDU hook | V
|
|
|
|
/// ----------------------------------
|
|
|
|
///
|
|
|
|
/// This unit parses and accepts m.receipt EDU's from the federation (1); then
|
|
|
|
/// it calls the m::receipt API (2) which generates internal PDU's sent to
|
|
|
|
/// user rooms. Hooks on these events sent to user rooms (3) turn the events
|
|
|
|
/// into federation EDU's for broadcast (for local users). Note that there are
|
|
|
|
/// other reactives for these internal events in client/sync, etc.
|
|
|
|
|
2018-03-15 05:05:08 +01:00
|
|
|
using namespace ircd;
|
|
|
|
|
2019-08-23 23:54:37 +02:00
|
|
|
static void handle_ircd_read(const m::event &, m::vm::eval &);
|
2019-08-10 06:27:12 +02:00
|
|
|
extern m::hookfn<m::vm::eval &> _ircd_read_eval;
|
2019-03-28 21:45:35 +01:00
|
|
|
|
2019-03-28 22:04:34 +01:00
|
|
|
static void handle_implicit_receipt(const m::event &, m::vm::eval &);
|
2019-08-10 06:27:12 +02:00
|
|
|
extern m::hookfn<m::vm::eval &> _implicit_receipt;
|
2019-03-28 22:04:34 +01:00
|
|
|
|
2019-09-06 20:13:58 +02:00
|
|
|
static void handle_m_receipt_m_read(const m::room::id &, const m::user::id &, const m::event::id &, const json::object &data);
|
2018-03-15 05:05:08 +01:00
|
|
|
static void handle_m_receipt_m_read(const m::room::id &, const m::user::id &, const m::edu::m_receipt::m_read &);
|
2019-05-27 05:25:58 +02:00
|
|
|
static void handle_m_receipt_m_read(const m::event &, const m::room::id &, const json::object &);
|
|
|
|
static void handle_m_receipt(const m::event &, const m::room::id &, const json::object &);
|
2018-09-14 04:11:40 +02:00
|
|
|
static void handle_edu_m_receipt(const m::event &, m::vm::eval &);
|
2019-08-10 06:27:12 +02:00
|
|
|
extern m::hookfn<m::vm::eval &> _m_receipt_eval;
|
2018-03-15 05:05:08 +01:00
|
|
|
|
2019-02-12 00:23:43 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix Receipts"
|
|
|
|
};
|
|
|
|
|
2019-04-05 23:07:52 +02:00
|
|
|
log::log
|
|
|
|
receipt_log
|
|
|
|
{
|
2019-08-01 04:40:18 +02:00
|
|
|
"m.receipt"
|
2019-04-05 23:07:52 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 19:56:49 +02:00
|
|
|
//
|
|
|
|
// EDU handler.
|
|
|
|
//
|
|
|
|
|
2019-03-28 21:45:35 +01:00
|
|
|
decltype(_m_receipt_eval)
|
2018-03-15 05:05:08 +01:00
|
|
|
_m_receipt_eval
|
|
|
|
{
|
|
|
|
handle_edu_m_receipt,
|
|
|
|
{
|
2019-03-28 22:31:24 +01:00
|
|
|
{ "_site", "vm.effect" },
|
2018-03-15 05:05:08 +01:00
|
|
|
{ "type", "m.receipt" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2018-09-14 04:11:40 +02:00
|
|
|
handle_edu_m_receipt(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
2018-04-22 01:42:46 +02:00
|
|
|
if(json::get<"room_id"_>(event))
|
|
|
|
return;
|
|
|
|
|
2019-05-27 05:25:58 +02:00
|
|
|
const auto &origin
|
|
|
|
{
|
|
|
|
json::get<"origin"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(my_host(origin))
|
2019-03-28 22:31:24 +01:00
|
|
|
return;
|
|
|
|
|
2018-03-15 05:05:08 +01:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const auto &member : content)
|
|
|
|
{
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
unquote(member.first)
|
|
|
|
};
|
|
|
|
|
2019-05-27 05:36:26 +02:00
|
|
|
if(m::room::server_acl::enable_write && !m::room::server_acl::check(room_id, origin))
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
receipt_log, "Ignoring m.receipt from '%s' in %s :denied by m.room.server_acl.",
|
|
|
|
json::get<"origin"_>(event),
|
|
|
|
string_view{room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-27 05:25:58 +02:00
|
|
|
handle_m_receipt(event, room_id, member.second);
|
2018-03-15 05:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-27 05:25:58 +02:00
|
|
|
handle_m_receipt(const m::event &event,
|
|
|
|
const m::room::id &room_id,
|
2019-07-21 23:14:08 +02:00
|
|
|
const json::object &content_)
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
for(const auto &[type, content] : content_)
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
|
|
|
if(type == "m.read")
|
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
handle_m_receipt_m_read(event, room_id, content);
|
2018-03-15 05:05:08 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
2019-04-05 23:07:52 +02:00
|
|
|
receipt_log, "Unhandled m.receipt type '%s' to room '%s'",
|
2018-03-15 05:05:08 +01:00
|
|
|
type,
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-27 05:25:58 +02:00
|
|
|
handle_m_receipt_m_read(const m::event &event,
|
|
|
|
const m::room::id &room_id,
|
2019-07-21 23:14:08 +02:00
|
|
|
const json::object &content_)
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
for(const auto &[user_id_, content] : content_)
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
const m::user::id user_id{user_id_};
|
2019-05-27 05:25:58 +02:00
|
|
|
if(user_id.host() != json::get<"origin"_>(event))
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
receipt_log, "Ignoring m.receipt m.read from '%s' in %s for alien %s.",
|
2019-05-27 05:25:58 +02:00
|
|
|
json::get<"origin"_>(event),
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{user_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-21 23:14:08 +02:00
|
|
|
handle_m_receipt_m_read(room_id, user_id, json::object{content});
|
2018-03-15 05:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_m_receipt_m_read(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::edu::m_receipt::m_read &receipt)
|
|
|
|
{
|
|
|
|
const json::array &event_ids
|
|
|
|
{
|
|
|
|
json::get<"event_ids"_>(receipt)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &data
|
|
|
|
{
|
|
|
|
json::get<"data"_>(receipt)
|
|
|
|
};
|
|
|
|
|
2019-07-21 23:20:38 +02:00
|
|
|
for(const json::string &event_id : event_ids) try
|
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
handle_m_receipt_m_read(room_id, user_id, event_id, data);
|
2019-07-21 23:20:38 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
receipt_log, "Failed to handle m.receipt m.read for %s in %s for '%s' :%s",
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{event_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-03-26 11:23:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_m_receipt_m_read(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::event::id &event_id,
|
2019-09-06 20:13:58 +02:00
|
|
|
const json::object &data)
|
2018-04-07 17:24:59 +02:00
|
|
|
try
|
2018-03-26 11:23:20 +02:00
|
|
|
{
|
2018-04-07 17:24:59 +02:00
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
2019-03-28 22:31:24 +01:00
|
|
|
// This handler only cares about remote users.
|
|
|
|
if(my(user))
|
|
|
|
return;
|
|
|
|
|
2019-07-21 23:14:08 +02:00
|
|
|
// If the user isn't known to us yet we drop that here; in the future
|
|
|
|
// we might be able to use this to trigger a resynchronization of the room.
|
2018-04-07 17:24:59 +02:00
|
|
|
if(!exists(user))
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
receipt_log, "Ignoring m.receipt m.read for unknown %s in %s for %s",
|
2018-04-07 17:24:59 +02:00
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{event_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:31:24 +01:00
|
|
|
const auto evid
|
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
m::receipt::read(room_id, user_id, event_id, data)
|
2019-03-28 22:31:24 +01:00
|
|
|
};
|
2018-04-07 17:24:59 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2019-07-21 23:14:08 +02:00
|
|
|
receipt_log, "Failed to save m.receipt m.read for %s in %s for %s :%s",
|
2018-04-07 17:24:59 +02:00
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{event_id},
|
|
|
|
e.what()
|
|
|
|
};
|
2018-03-15 05:05:08 +01:00
|
|
|
}
|
2018-09-07 15:22:09 +02:00
|
|
|
|
2019-09-06 19:56:49 +02:00
|
|
|
//
|
|
|
|
// m::receipt API -> Internal
|
|
|
|
//
|
|
|
|
|
2019-02-12 00:23:43 +01:00
|
|
|
m::event::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::receipt::read(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::event::id &event_id,
|
2019-09-06 20:13:58 +02:00
|
|
|
const json::object &options)
|
2019-02-12 00:23:43 +01:00
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto evid
|
|
|
|
{
|
|
|
|
send(user_room, user_id, "ircd.read", room_id,
|
|
|
|
{
|
2019-09-06 20:51:45 +02:00
|
|
|
{ "event_id", event_id },
|
|
|
|
{ "ts", options.get("ts", ircd::time<milliseconds>()) },
|
|
|
|
{ "m.hidden", options.get("m.hidden", false) },
|
2019-02-12 00:23:43 +01:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
receipt_log, "%s read by %s in %s options:%s",
|
2019-02-12 00:23:43 +01:00
|
|
|
string_view{event_id},
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
2019-09-06 20:13:58 +02:00
|
|
|
string_view{options},
|
2019-02-12 00:23:43 +01:00
|
|
|
};
|
|
|
|
|
2019-03-28 21:45:35 +01:00
|
|
|
return evid;
|
2019-02-12 00:23:43 +01:00
|
|
|
}
|
|
|
|
|
2018-09-07 15:22:09 +02:00
|
|
|
bool
|
2019-02-12 00:23:43 +01:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-09-06 20:13:58 +02:00
|
|
|
ircd::m::receipt::get(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::event::id::closure &closure)
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
const m::user::room user_room
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
user_id
|
2018-09-07 15:22:09 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 20:13:58 +02:00
|
|
|
const auto event_idx
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
user_room.get(std::nothrow, "ircd.read", room_id)
|
2018-09-07 15:22:09 +02:00
|
|
|
};
|
|
|
|
|
2019-09-06 20:13:58 +02:00
|
|
|
return m::get(std::nothrow, event_idx, "content", [&closure]
|
|
|
|
(const json::object &content)
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
const json::string &event_id
|
2018-09-07 15:22:09 +02:00
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
content["event_id"]
|
2018-09-07 15:22:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
closure(event_id);
|
|
|
|
});
|
|
|
|
}
|
2019-02-12 00:23:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
/// Does the user wish to not send receipts for events sent by its specific
|
|
|
|
/// sender?
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::receipt::ignoring(const m::user &user,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
m::get(std::nothrow, event_id, "sender", [&ret, &user]
|
|
|
|
(const string_view &sender)
|
|
|
|
{
|
|
|
|
const m::user::room user_room{user};
|
|
|
|
ret = user_room.has("ircd.read.ignore", sender);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Does the user wish to not send receipts for events for this entire room?
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::receipt::ignoring(const m::user &user,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
const m::user::room user_room{user};
|
|
|
|
return user_room.has("ircd.read.ignore", room_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::receipt::freshest(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{true};
|
|
|
|
user_room.get("ircd.read", room_id, [&ret, &event_id]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id &previous_id
|
|
|
|
{
|
|
|
|
unquote(content.get("event_id"))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(event_id == previous_id)
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const m::event::idx &previous_idx
|
|
|
|
{
|
|
|
|
index(previous_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
|
|
|
index(event_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = event_idx > previous_idx;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2019-04-05 23:07:52 +02:00
|
|
|
receipt_log, "Freshness of receipt in %s from %s for %s :%s",
|
2019-02-12 00:23:43 +01:00
|
|
|
string_view{room_id},
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{event_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::receipt::exists(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
user_room.get(std::nothrow, "ircd.read", room_id, [&ret, &event_id]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = unquote(content.get("event_id")) == event_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-03-28 21:45:35 +01:00
|
|
|
|
2019-09-06 19:56:49 +02:00
|
|
|
//
|
|
|
|
// Internal -> Federation
|
|
|
|
//
|
|
|
|
|
2019-03-28 22:04:34 +01:00
|
|
|
decltype(_implicit_receipt)
|
|
|
|
_implicit_receipt
|
|
|
|
{
|
|
|
|
handle_implicit_receipt,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "type", "m.room.message" },
|
|
|
|
{ "origin", my_host() },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This handler generates receipts for messages sent by that user. These are
|
|
|
|
/// required for notification counts. They're not broadcast, we just keep state
|
|
|
|
/// for them.
|
|
|
|
void
|
|
|
|
handle_implicit_receipt(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
|
|
|
try
|
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
if(!event.event_id)
|
2019-03-28 22:31:24 +01:00
|
|
|
return;
|
|
|
|
|
2019-03-28 22:04:34 +01:00
|
|
|
const m::user::id &user_id
|
|
|
|
{
|
|
|
|
at<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// This handler does not care about remote users.
|
|
|
|
if(!my(user_id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
2019-07-10 09:26:25 +02:00
|
|
|
event.event_id
|
2019-03-28 22:04:34 +01:00
|
|
|
};
|
|
|
|
|
2019-09-06 20:13:58 +02:00
|
|
|
char databuf[64];
|
|
|
|
const json::object &data
|
2019-03-28 22:04:34 +01:00
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
json::stringify(mutable_buffer{databuf}, json::members
|
|
|
|
{
|
|
|
|
{ "ts", at<"origin_server_ts"_>(event) },
|
|
|
|
})
|
2019-03-28 22:04:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto receipt_event_id
|
|
|
|
{
|
2019-09-06 20:13:58 +02:00
|
|
|
m::receipt::read(room_id, user_id, event_id, data)
|
2019-03-28 22:04:34 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-04-05 23:07:52 +02:00
|
|
|
receipt_log, "Implicit receipt hook for %s :%s",
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-03-28 22:04:34 +01:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-28 21:45:35 +01:00
|
|
|
decltype(_ircd_read_eval)
|
|
|
|
_ircd_read_eval
|
|
|
|
{
|
2019-08-23 23:54:37 +02:00
|
|
|
handle_ircd_read,
|
2019-03-28 21:45:35 +01:00
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "type", "ircd.read" },
|
|
|
|
{ "origin", my_host() },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This handler looks for ircd.read events and conducts a federation
|
|
|
|
/// broadcast of the m.receipt edu.
|
|
|
|
void
|
2019-08-23 23:54:37 +02:00
|
|
|
handle_ircd_read(const m::event &event,
|
|
|
|
m::vm::eval &eval)
|
2019-03-28 21:45:35 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if(!json::get<"state_key"_>(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The state_key of an ircd.read event is the target room_id
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
at<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// This handler does federation broadcasts of receipts from
|
|
|
|
// this server only.
|
|
|
|
if(!my(user))
|
|
|
|
return;
|
|
|
|
|
2019-08-12 12:40:56 +02:00
|
|
|
// Only broadcast if the user is joined to the room.
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!membership(m::room(room_id), user, "join"))
|
2019-08-12 12:40:56 +02:00
|
|
|
return;
|
|
|
|
|
2019-03-28 21:45:35 +01:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
at<"sender"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Ignore anybody that creates an ircd.read event in some other room.
|
|
|
|
if(json::get<"room_id"_>(event) != user_room.room_id)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const json::string &event_id
|
|
|
|
{
|
2019-07-12 01:20:26 +02:00
|
|
|
at<"content"_>(event).at("event_id")
|
2019-03-28 21:45:35 +01:00
|
|
|
};
|
|
|
|
|
2019-09-06 20:51:45 +02:00
|
|
|
// MSC2285; if m.hidden is set here we don't broadcast this receipt
|
|
|
|
// to the federation; nothing further to do here then.
|
|
|
|
if(at<"content"_>(event).get("m.hidden", false))
|
|
|
|
return;
|
|
|
|
|
2019-03-28 21:50:19 +01:00
|
|
|
// Lastly, we elide broadcasts of receipts for a user's own message.
|
|
|
|
m::user::id::buf sender_buf;
|
|
|
|
if(m::get(std::nothrow, event_id, "sender", sender_buf) == user.user_id)
|
|
|
|
return;
|
|
|
|
|
2019-03-28 21:45:35 +01:00
|
|
|
const time_t &ms
|
|
|
|
{
|
|
|
|
at<"content"_>(event).get<time_t>("ts", 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::value event_ids[]
|
|
|
|
{
|
|
|
|
{ event_id }
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::members m_read
|
|
|
|
{
|
|
|
|
{ "data",
|
|
|
|
{
|
|
|
|
{ "ts", ms }
|
|
|
|
}},
|
|
|
|
{ "event_ids",
|
|
|
|
{
|
|
|
|
event_ids, 1
|
|
|
|
}},
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov edu_event, content;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ edu_event, { "type", "m.receipt" } },
|
|
|
|
{ edu_event, { "room_id", room_id } },
|
|
|
|
{ content, { room_id,
|
|
|
|
{
|
|
|
|
{ "m.read",
|
|
|
|
{
|
|
|
|
{ user.user_id, m_read }
|
|
|
|
}}
|
|
|
|
}}}
|
|
|
|
};
|
|
|
|
|
|
|
|
// EDU options
|
2019-08-17 09:44:14 +02:00
|
|
|
m::vm::copts opts;
|
2019-03-28 21:45:35 +01:00
|
|
|
opts.conforming = false;
|
2019-08-17 09:44:14 +02:00
|
|
|
opts.prop_mask.reset();
|
2019-03-28 21:45:35 +01:00
|
|
|
|
|
|
|
// Don't need to notify clients, the /sync system understood the
|
|
|
|
// `ircd.read` directly. The federation sender is what we're hitting here.
|
|
|
|
opts.notify_clients = false;
|
|
|
|
|
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
edu_event, content, opts
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2019-04-05 23:07:52 +02:00
|
|
|
receipt_log, "ircd.read hook on %s for federation broadcast :%s",
|
2019-07-06 07:01:34 +02:00
|
|
|
string_view{event.event_id},
|
2019-03-28 21:45:35 +01:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|