Skip to content

Commit

Permalink
ircd::m::receipt: Reorg/rename interface; simplify impl; add options …
Browse files Browse the repository at this point in the history
…argument.

ircd::m::receipt: Remove central linkage cruft for interface.
  • Loading branch information
jevolk committed Sep 6, 2019
1 parent 7d0c44e commit 6a59036
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 159 deletions.
20 changes: 16 additions & 4 deletions include/ircd/m/receipt.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ namespace ircd::m::receipt
bool ignoring(const m::user &, const id::room &);

// [GET] Get the last event the user has read in the room.
bool read(const id::room &, const id::user &, const id::event::closure &);
id::event read(id::event::buf &out, const id::room &, const id::user &);
bool get(const id::room &, const id::user &, const id::event::closure &);
id::event get(id::event::buf &out, const id::room &, const id::user &);

// [SET] Indicate that the user has read the event in the room.
id::event::buf read(const id::room &, const id::user &, const id::event &, const time_t &);
id::event::buf read(const id::room &, const id::user &, const id::event &); // now
id::event::buf read(const id::room &, const id::user &, const id::event &, const json::object & = {});
};

struct ircd::m::edu::m_receipt
Expand All @@ -50,3 +49,16 @@ struct ircd::m::edu::m_receipt::m_read
using super_type::tuple;
using super_type::operator=;
};

inline ircd::m::event::id
ircd::m::receipt::get(event::id::buf &out,
const room::id &room_id,
const user::id &user_id)
{
const event::id::closure copy
{
[&out](const auto &event_id) { out = event_id; }
};

return get(room_id, user_id, copy)? event::id{out} : event::id{};
}
118 changes: 0 additions & 118 deletions ircd/m.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1666,124 +1666,6 @@ ircd::m::visible(const event &event,
return call(event, mxid);
}

///////////////////////////////////////////////////////////////////////////////
//
// m/receipt.h
//

ircd::m::event::id::buf
ircd::m::receipt::read(const id::room &room_id,
const id::user &user_id,
const id::event &event_id)
{
return read(room_id, user_id, event_id, ircd::time<milliseconds>());
}

ircd::m::event::id::buf
ircd::m::receipt::read(const room::id &room_id,
const user::id &user_id,
const event::id &event_id,
const time_t &ms)
{
using prototype = event::id::buf (const room::id &, const user::id &, const event::id &, const time_t &);

static mods::import<prototype> function
{
"m_receipt", "ircd::m::receipt::read"
};

return function(room_id, user_id, event_id, ms);
}

ircd::m::event::id
ircd::m::receipt::read(event::id::buf &out,
const room::id &room_id,
const user::id &user_id)
{
const event::id::closure copy{[&out]
(const event::id &event_id)
{
out = event_id;
}};

return read(room_id, user_id, copy)?
event::id{out}:
event::id{};
}

bool
ircd::m::receipt::read(const room::id &room_id,
const user::id &user_id,
const event::id::closure &closure)
{
using prototype = bool (const room::id &, const user::id &, const event::id::closure &);

static mods::import<prototype> function
{
"m_receipt", "ircd::m::receipt::read"
};

return function(room_id, user_id, closure);
}

bool
ircd::m::receipt::ignoring(const user &user,
const room::id &room_id)
{
using prototype = bool (const m::user &, const m::room::id &);

static mods::import<prototype> function
{
"m_receipt", "ircd::m::receipt::ignoring"
};

return function(user, room_id);
}

bool
ircd::m::receipt::ignoring(const user &user,
const event::id &event_id)
{
using prototype = bool (const m::user &, const m::event::id &);

static mods::import<prototype> function
{
"m_receipt", "ircd::m::receipt::ignoring"
};

return function(user, event_id);
}

bool
ircd::m::receipt::freshest(const room::id &room_id,
const user::id &user_id,
const event::id &event_id)
{
using prototype = bool (const m::room::id &, const m::user::id &, const m::event::id &);

static mods::import<prototype> function
{
"m_receipt", "ircd::m::receipt::freshest"
};

return function(room_id, user_id, event_id);
}

bool
ircd::m::receipt::exists(const room::id &room_id,
const user::id &user_id,
const event::id &event_id)
{
using prototype = bool (const m::room::id &, const m::user::id &, const m::event::id &);

static mods::import<prototype> function
{
"m_receipt", "ircd::m::receipt::exists"
};

return function(room_id, user_id, event_id);
}

///////////////////////////////////////////////////////////////////////////////
//
// m/presence.h
Expand Down
2 changes: 1 addition & 1 deletion modules/client/rooms/read_markers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ handle_m_fully_read(client &client,
m::log, "Ignoring duplicate m.fully_read marker for %s in %s by %s",
string_view{event_id},
string_view{room_id},
request.user_id,
string_view{request.user_id},
};

return;
Expand Down
12 changes: 11 additions & 1 deletion modules/client/rooms/receipt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,15 @@ handle_receipt_m_read(client &client,
if(m::receipt::ignoring(request.user_id, event_id))
return;

m::receipt::read(room_id, request.user_id, event_id);
// The options object starts with anything in the request content, which
// differs depending on whether this is being called from a /receipt or
// /read_markers resource handler. The receipt::read() implementation
// looks for properties knowing this call pattern, thus it's best to just
// convey the whole content here for forward compat.
const json::object &options
{
request
};

m::receipt::read(room_id, request.user_id, event_id, options);
}
4 changes: 2 additions & 2 deletions modules/client/sync/rooms/unread_notifications.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ircd::m::sync::room_unread_notifications_linear(data &data)

m::event::id::buf last_read;
if(likely(!is_self_read))
if(!m::receipt::read(last_read, room.room_id, data.user))
if(!m::receipt::get(last_read, room.room_id, data.user))
return false;

const auto start_idx
Expand Down Expand Up @@ -160,7 +160,7 @@ ircd::m::sync::room_unread_notifications_polylog(data &data)
m::event::id::buf last_read_buf;
const auto last_read
{
m::receipt::read(last_read_buf, room.room_id, data.user)
m::receipt::get(last_read_buf, room.room_id, data.user)
};

const auto start_idx
Expand Down
5 changes: 4 additions & 1 deletion modules/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10761,7 +10761,10 @@ console_cmd__user__read__receipt(opt &out, const string_view &line)

const auto eid
{
m::receipt::read(room_id, user_id, event_id, ms)
m::receipt::read(room_id, user_id, event_id, json::strung{json::members
{
{ "ts", ms },
}})
};

out << eid << std::endl;
Expand Down
18 changes: 15 additions & 3 deletions modules/m_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ command__read(const mutable_buffer &buf,
m::head(room)
};

m::receipt::read(room, user, event_id, ms);
m::receipt::read(room, user, event_id, json::strung{json::members
{
{ "ts", ms },
}});

return {};
}
else if(m::valid(m::id::ROOM, arg) || m::valid(m::id::ROOM_ALIAS, arg))
Expand All @@ -388,7 +392,11 @@ command__read(const mutable_buffer &buf,
m::head(room)
};

m::receipt::read(room, user, event_id, ms);
m::receipt::read(room, user, event_id, json::strung{json::members
{
{ "ts", ms },
}});

return {};
}

Expand Down Expand Up @@ -492,7 +500,11 @@ command__read(const mutable_buffer &buf,
}

// Commit the receipt.
m::receipt::read(room_id, user, event_id, ms);
m::receipt::read(room_id, user, event_id, json::strung{json::members
{
{ "ts", ms },
}});

put(room_id, event_id);
++matched;
});
Expand Down
59 changes: 30 additions & 29 deletions modules/m_receipt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern m::hookfn<m::vm::eval &> _ircd_read_eval;
static void handle_implicit_receipt(const m::event &, m::vm::eval &);
extern m::hookfn<m::vm::eval &> _implicit_receipt;

static void handle_m_receipt_m_read(const m::room::id &, const m::user::id &, const m::event::id &, const time_t &);
static void handle_m_receipt_m_read(const m::room::id &, const m::user::id &, const m::event::id &, const json::object &data);
static void handle_m_receipt_m_read(const m::room::id &, const m::user::id &, const m::edu::m_receipt::m_read &);
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 &);
Expand Down Expand Up @@ -172,14 +172,9 @@ handle_m_receipt_m_read(const m::room::id &room_id,
json::get<"data"_>(receipt)
};

const time_t &ts
{
data.get<time_t>("ts")
};

for(const json::string &event_id : event_ids) try
{
handle_m_receipt_m_read(room_id, user_id, event_id, ts);
handle_m_receipt_m_read(room_id, user_id, event_id, data);
}
catch(const std::exception &e)
{
Expand All @@ -198,7 +193,7 @@ void
handle_m_receipt_m_read(const m::room::id &room_id,
const m::user::id &user_id,
const m::event::id &event_id,
const time_t &ts)
const json::object &data)
try
{
const m::user user
Expand Down Expand Up @@ -227,7 +222,7 @@ try

const auto evid
{
m::receipt::read(room_id, user_id, event_id, ts)
m::receipt::read(room_id, user_id, event_id, data)
};
}
catch(const std::exception &e)
Expand All @@ -251,13 +246,18 @@ 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,
const time_t &ms)
const json::object &options)
{
const m::user::room user_room
{
user_id
};

const time_t &ms
{
options.get("ts", ircd::time<milliseconds>())
};

const auto evid
{
send(user_room, user_id, "ircd.read", room_id,
Expand All @@ -269,41 +269,38 @@ ircd::m::receipt::read(const m::room::id &room_id,

log::info
{
receipt_log, "%s read by %s in %s @ %zd",
receipt_log, "%s read by %s in %s options:%s",
string_view{event_id},
string_view{user_id},
string_view{room_id},
ms
string_view{options},
};

return evid;
}

bool
IRCD_MODULE_EXPORT
ircd::m::receipt::read(const m::room::id &room_id,
const m::user::id &user_id,
const m::event::id::closure &closure)
ircd::m::receipt::get(const m::room::id &room_id,
const m::user::id &user_id,
const m::event::id::closure &closure)
{
static const m::event::fetch::opts fopts
const m::user::room user_room
{
m::event::keys::include
{
"content"
}
user_id
};

const m::user::room user_room
const auto event_idx
{
user_id, nullptr, &fopts
user_room.get(std::nothrow, "ircd.read", room_id)
};

return user_room.get(std::nothrow, "ircd.read", room_id, [&closure]
(const m::event &event)
return m::get(std::nothrow, event_idx, "content", [&closure]
(const json::object &content)
{
const m::event::id &event_id
const json::string &event_id
{
unquote(at<"content"_>(event).get("event_id"))
content["event_id"]
};

closure(event_id);
Expand Down Expand Up @@ -471,14 +468,18 @@ try
event.event_id
};

const time_t &ms
char databuf[64];
const json::object &data
{
at<"origin_server_ts"_>(event)
json::stringify(mutable_buffer{databuf}, json::members
{
{ "ts", at<"origin_server_ts"_>(event) },
})
};

const auto receipt_event_id
{
m::receipt::read(room_id, user_id, event_id, ms)
m::receipt::read(room_id, user_id, event_id, data)
};
}
catch(const std::exception &e)
Expand Down

0 comments on commit 6a59036

Please sign in to comment.