2018-02-14 21:23:20 +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.
|
|
|
|
|
|
|
|
#include "rooms.h"
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-03-29 04:47:41 +02:00
|
|
|
extern "C" m::event::id::buf
|
|
|
|
commit__m_receipt_m_read(const m::room::id &,
|
|
|
|
const m::user::id &,
|
|
|
|
const m::event::id &,
|
|
|
|
const time_t &);
|
|
|
|
|
2018-04-07 17:26:29 +02:00
|
|
|
extern "C" bool
|
|
|
|
exists__m_receipt_m_read(const m::room::id &,
|
|
|
|
const m::user::id &,
|
|
|
|
const m::event::id &);
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
resource::response
|
|
|
|
post__receipt(client &client,
|
|
|
|
const resource::request &request,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
2018-02-15 06:52:19 +01:00
|
|
|
if(request.parv.size() < 3)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"receipt type required"
|
|
|
|
};
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
if(request.parv.size() < 4)
|
2018-02-15 06:52:19 +01:00
|
|
|
throw m::NEED_MORE_PARAMS
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-02-15 06:52:19 +01:00
|
|
|
"event_id required"
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &receipt_type
|
|
|
|
{
|
|
|
|
request.parv[2]
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
url::decode(request.parv[3], event_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
2018-03-29 04:47:41 +02:00
|
|
|
|
|
|
|
m::event::id::buf
|
|
|
|
commit__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 &ms)
|
|
|
|
{
|
2018-04-07 17:26:29 +02:00
|
|
|
if(exists__m_receipt_m_read(room_id, user_id, event_id))
|
|
|
|
return {};
|
|
|
|
|
2018-03-29 04:47:41 +02:00
|
|
|
const json::value event_ids[]
|
|
|
|
{
|
|
|
|
{ event_id }
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::members m_read
|
|
|
|
{
|
|
|
|
{ "data",
|
|
|
|
{
|
|
|
|
{ "ts", ms }
|
|
|
|
}},
|
|
|
|
{ "event_ids", { event_ids, 1 } },
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov event, content;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.receipt" } },
|
|
|
|
{ event, { "room_id", room_id } },
|
|
|
|
{ content, { room_id,
|
|
|
|
{
|
|
|
|
{ "m.read",
|
|
|
|
{
|
|
|
|
{ user_id, m_read }
|
|
|
|
}}
|
|
|
|
}}}
|
|
|
|
};
|
|
|
|
|
2018-04-06 08:07:55 +02:00
|
|
|
m::vm::opts::commit opts;
|
2018-03-29 04:47:41 +02:00
|
|
|
opts.hash = false;
|
|
|
|
opts.sign = false;
|
|
|
|
opts.event_id = false;
|
|
|
|
opts.origin = true;
|
|
|
|
opts.origin_server_ts = false;
|
|
|
|
opts.conforming = false;
|
|
|
|
|
|
|
|
return m::vm::commit(event, content, opts);
|
|
|
|
}
|
2018-04-07 17:26:29 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
exists__m_receipt_m_read(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, "m.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;
|
|
|
|
}
|