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.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix Receipts"
|
|
|
|
};
|
|
|
|
|
2018-09-07 15:22:09 +02:00
|
|
|
extern "C" bool last_receipt__event_id(const m::room::id &, const m::user::id &, const m::event::id::closure &);
|
2018-03-26 11:23:20 +02:00
|
|
|
static void handle_m_receipt_m_read(const m::room::id &, const m::user::id &, const m::event::id &, const time_t &);
|
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 &);
|
|
|
|
static void handle_m_receipt_m_read(const m::room::id &, const json::object &);
|
|
|
|
static void handle_m_receipt(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 &);
|
2018-03-15 05:05:08 +01:00
|
|
|
|
2018-09-14 04:11:40 +02:00
|
|
|
const m::hookfn<m::vm::eval &>
|
2018-03-15 05:05:08 +01:00
|
|
|
_m_receipt_eval
|
|
|
|
{
|
|
|
|
handle_edu_m_receipt,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.eval" },
|
|
|
|
{ "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;
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_m_receipt(room_id, member.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_m_receipt(const m::room::id &room_id,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
for(const auto &member : content)
|
|
|
|
{
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
unquote(member.first)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type == "m.read")
|
|
|
|
{
|
|
|
|
handle_m_receipt_m_read(room_id, member.second);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:26:29 +01:00
|
|
|
log::dwarning
|
2018-03-15 05:05:08 +01:00
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::log, "Unhandled m.receipt type '%s' to room '%s'",
|
2018-03-15 05:05:08 +01:00
|
|
|
type,
|
|
|
|
string_view{room_id}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_m_receipt_m_read(const m::room::id &room_id,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
for(const auto &member : content)
|
|
|
|
{
|
|
|
|
const m::user::id user_id
|
|
|
|
{
|
|
|
|
unquote(member.first)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
member.second
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_m_receipt_m_read(room_id, user_id, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
const time_t &ts
|
|
|
|
{
|
|
|
|
data.get<time_t>("ts")
|
|
|
|
};
|
2018-03-26 11:23:20 +02:00
|
|
|
|
|
|
|
for(const string_view &event_id : event_ids)
|
|
|
|
handle_m_receipt_m_read(room_id, user_id, unquote(event_id), ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!exists(user))
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto evid
|
|
|
|
{
|
2018-04-22 01:42:46 +02:00
|
|
|
send(user_room, user_id, "ircd.read", room_id,
|
2018-04-07 17:24:59 +02:00
|
|
|
{
|
|
|
|
{ "event_id", event_id },
|
|
|
|
{ "ts", ts }
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-04-22 01:42:46 +02:00
|
|
|
m::event receipt;
|
2018-05-23 10:32:21 +02:00
|
|
|
json::get<"depth"_>(receipt) = json::undefined_number;
|
2018-06-12 09:45:18 +02:00
|
|
|
json::get<"type"_>(receipt) = "m.receipt"_sv;
|
2018-04-22 01:42:46 +02:00
|
|
|
json::get<"room_id"_>(receipt) = room_id;
|
|
|
|
json::get<"sender"_>(receipt) = user_id;
|
|
|
|
json::get<"origin"_>(receipt) = user_id.host();
|
|
|
|
|
|
|
|
char buf[768];
|
|
|
|
json::get<"content"_>(receipt) = json::stringify(mutable_buffer{buf}, json::members
|
|
|
|
{
|
|
|
|
{ event_id,
|
|
|
|
{
|
|
|
|
{ "m.read",
|
|
|
|
{
|
|
|
|
{ user_id,
|
|
|
|
{
|
|
|
|
{ "ts", ts }
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
});
|
|
|
|
|
|
|
|
m::vm::opts vmopts;
|
2018-04-28 11:46:11 +02:00
|
|
|
vmopts.conforming = false;
|
2018-04-22 01:42:46 +02:00
|
|
|
m::vm::eval eval
|
|
|
|
{
|
|
|
|
receipt, vmopts
|
|
|
|
};
|
|
|
|
|
2018-04-07 17:24:59 +02:00
|
|
|
log::info
|
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::log, "%s read by %s in %s @ %zd",
|
2018-04-07 17:24:59 +02:00
|
|
|
string_view{event_id},
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
2018-08-29 00:59:52 +02:00
|
|
|
ts
|
2018-04-07 17:24:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
2018-12-23 02:44:18 +01:00
|
|
|
m::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
|
|
|
|
|
|
|
bool
|
|
|
|
last_receipt__event_id(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
|
|
|
|
{
|
|
|
|
m::event::keys::include
|
|
|
|
{
|
|
|
|
"content"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id, nullptr, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
return user_room.get(std::nothrow, "ircd.read", room_id, [&closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const m::event::id &event_id
|
|
|
|
{
|
|
|
|
unquote(at<"content"_>(event).get("event_id"))
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(event_id);
|
|
|
|
});
|
|
|
|
}
|