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-04-22 01:42:46 +02:00
|
|
|
extern "C" bool
|
|
|
|
fresher__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
|
|
|
|
{
|
2018-12-07 01:41:47 +01:00
|
|
|
url::decode(event_id, request.parv[3])
|
2018-02-14 21:23:20 +01:00
|
|
|
};
|
|
|
|
|
2018-05-21 09:01:28 +02:00
|
|
|
const auto eid
|
|
|
|
{
|
|
|
|
m::receipt::read(room_id, request.user_id, event_id)
|
|
|
|
};
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
2018-03-29 04:47:41 +02:00
|
|
|
|
2018-05-30 21:07:03 +02:00
|
|
|
/// Does the user wish to not send receipts for events sent by its specific
|
|
|
|
/// sender?
|
|
|
|
static bool
|
|
|
|
user_ignoring_receipts_sender(const m::user::room &user_room,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
m::get(std::nothrow, event_id, "sender", [&ret, &user_room]
|
|
|
|
(const string_view &sender)
|
|
|
|
{
|
|
|
|
ret = user_room.has("ircd.read.ignore", sender);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Does the user wish to not send receipts for events for this entire room?
|
|
|
|
static bool
|
|
|
|
user_ignoring_receipts_room(const m::user::room &user_room,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
return user_room.has("ircd.read.ignore", room_id);
|
|
|
|
}
|
|
|
|
|
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-22 01:42:46 +02:00
|
|
|
if(!fresher__m_receipt_m_read(room_id, user_id, event_id))
|
2018-04-07 17:26:29 +02:00
|
|
|
return {};
|
|
|
|
|
2018-04-28 11:46:11 +02:00
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
2018-05-30 21:07:03 +02:00
|
|
|
// Check if receipts for the room are disabled first because we have the
|
|
|
|
// room_id on the stack, whereas the event sender requires column queries
|
|
|
|
// from the event_id.
|
|
|
|
const bool ignored
|
2018-05-26 14:27:43 +02:00
|
|
|
{
|
2018-05-30 21:07:03 +02:00
|
|
|
user_ignoring_receipts_room(user_room, room_id) ||
|
|
|
|
user_ignoring_receipts_sender(user_room, event_id)
|
|
|
|
};
|
2018-05-26 14:27:43 +02:00
|
|
|
|
|
|
|
if(ignored)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"no receipt for %s by %s in %s @ %zd (ircd.read.ignore)",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
|
|
|
ms
|
|
|
|
};
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-04-28 11:46:11 +02:00
|
|
|
const auto evid
|
|
|
|
{
|
|
|
|
send(user_room, user_id, "ircd.read", room_id,
|
|
|
|
{
|
|
|
|
{ "event_id", event_id },
|
|
|
|
{ "ts", ms }
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-05-26 13:24:12 +02:00
|
|
|
log::info
|
|
|
|
{
|
|
|
|
"%s read by %s in %s @ %zd => %s (local)",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{room_id},
|
|
|
|
ms,
|
|
|
|
string_view{evid}
|
|
|
|
};
|
|
|
|
|
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" } },
|
2018-04-28 11:46:11 +02:00
|
|
|
{ event, { "room_id", room_id } },
|
2018-03-29 04:47:41 +02:00
|
|
|
{ content, { room_id,
|
|
|
|
{
|
|
|
|
{ "m.read",
|
|
|
|
{
|
|
|
|
{ user_id, m_read }
|
|
|
|
}}
|
|
|
|
}}}
|
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
m::vm::copts opts;
|
2018-05-12 06:21:15 +02:00
|
|
|
opts.add_hash = false;
|
|
|
|
opts.add_sig = false;
|
|
|
|
opts.add_event_id = false;
|
|
|
|
opts.add_origin = true;
|
|
|
|
opts.add_origin_server_ts = false;
|
2018-03-29 04:47:41 +02:00
|
|
|
opts.conforming = false;
|
2018-05-07 01:04:51 +02:00
|
|
|
return m::vm::eval
|
|
|
|
{
|
|
|
|
event, content, opts
|
|
|
|
};
|
2018-03-29 04:47:41 +02:00
|
|
|
}
|
2018-04-07 17:26:29 +02:00
|
|
|
|
2018-04-22 01:42:46 +02:00
|
|
|
bool
|
|
|
|
fresher__m_receipt_m_read(const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id,
|
|
|
|
const m::event::id &event_id)
|
2018-05-09 04:01:11 +02:00
|
|
|
try
|
2018-04-22 01:42:46 +02:00
|
|
|
{
|
|
|
|
const m::user::room user_room
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{true};
|
2018-05-09 04:01:11 +02:00
|
|
|
user_room.get("ircd.read", room_id, [&ret, &event_id]
|
2018-04-22 01:42:46 +02:00
|
|
|
(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
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
index(previous_id)
|
2018-04-22 01:42:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
index(event_id)
|
2018-04-22 01:42:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = event_idx > previous_idx;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-09 04:01:11 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
m::log, "Freshness of receipt in %s from %s for %s :%s",
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{user_id},
|
|
|
|
string_view{event_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-22 01:42:46 +02:00
|
|
|
|
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};
|
2018-04-22 01:42:46 +02:00
|
|
|
user_room.get(std::nothrow, "ircd.read", room_id, [&ret, &event_id]
|
2018-04-07 17:26:29 +02:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
at<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = unquote(content.get("event_id")) == event_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|