2019-03-31 22:46:48 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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.
|
|
|
|
|
2020-03-23 22:30:18 +01:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
static resource::response
|
|
|
|
get_notifications(client &,
|
|
|
|
const resource::request &);
|
|
|
|
|
|
|
|
extern const event::keys notification_event_keys;
|
|
|
|
extern conf::item<size_t> notifications_limit_default;
|
|
|
|
extern resource::method notifications_method_get;
|
|
|
|
extern resource notifications_resource;
|
|
|
|
}
|
2019-03-31 22:46:48 +02:00
|
|
|
|
2020-03-23 22:30:18 +01:00
|
|
|
ircd::mapi::header
|
2019-03-31 22:46:48 +02:00
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2020-03-23 22:30:18 +01:00
|
|
|
"Client r0.6.0-13.13.1.3.1 :Listing Notifications"
|
2019-03-31 22:46:48 +02:00
|
|
|
};
|
|
|
|
|
2020-03-23 22:30:18 +01:00
|
|
|
decltype(ircd::m::notifications_resource)
|
|
|
|
ircd::m::notifications_resource
|
2019-03-31 22:46:48 +02:00
|
|
|
{
|
|
|
|
"/_matrix/client/r0/notifications",
|
|
|
|
{
|
2020-03-23 22:30:18 +01:00
|
|
|
"(r0.6.0-13.13.1.3.1) Listing Notifications"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::notifications_method_get)
|
|
|
|
ircd::m::notifications_method_get
|
|
|
|
{
|
|
|
|
notifications_resource, "GET", get_notifications,
|
|
|
|
{
|
|
|
|
notifications_method_get.REQUIRES_AUTH
|
2019-03-31 22:46:48 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-23 22:30:18 +01:00
|
|
|
decltype(ircd::m::notifications_limit_default)
|
|
|
|
ircd::m::notifications_limit_default
|
2019-03-31 22:46:48 +02:00
|
|
|
{
|
|
|
|
{ "name", "ircd.client.notifications.limit.default" },
|
2020-03-23 22:30:18 +01:00
|
|
|
{ "default", 12 }
|
2019-03-31 22:46:48 +02:00
|
|
|
};
|
|
|
|
|
2020-03-23 22:30:18 +01:00
|
|
|
decltype(ircd::m::notification_event_keys)
|
|
|
|
ircd::m::notification_event_keys
|
|
|
|
{
|
|
|
|
event::keys::include
|
|
|
|
{
|
|
|
|
"event_id",
|
|
|
|
"content",
|
|
|
|
"origin_server_ts",
|
|
|
|
"sender",
|
|
|
|
"state_key",
|
|
|
|
"type",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::m::resource::response
|
|
|
|
ircd::m::get_notifications(client &client,
|
|
|
|
const resource::request &request)
|
2019-03-31 22:46:48 +02:00
|
|
|
{
|
|
|
|
const string_view &from
|
|
|
|
{
|
|
|
|
request.query["from"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &only
|
|
|
|
{
|
|
|
|
request.query["only"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const ushort limit
|
|
|
|
{
|
2020-03-23 22:30:18 +01:00
|
|
|
request.query.get<ushort>("limit", size_t(notifications_limit_default))
|
|
|
|
};
|
|
|
|
|
|
|
|
m::user::notifications::opts opts;
|
|
|
|
opts.only = only;
|
|
|
|
opts.to = 0UL;
|
|
|
|
opts.from = from?
|
|
|
|
lex_cast<event::idx>(from):
|
|
|
|
0UL;
|
|
|
|
|
|
|
|
const m::user::notifications notifications
|
|
|
|
{
|
|
|
|
request.user_id
|
2019-03-31 22:46:48 +02:00
|
|
|
};
|
|
|
|
|
2019-08-12 13:22:52 +02:00
|
|
|
resource::response::chunked response
|
2019-03-31 22:46:48 +02:00
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
2019-08-12 13:22:52 +02:00
|
|
|
|
|
|
|
json::stack out
|
|
|
|
{
|
|
|
|
response.buf, response.flusher()
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
2020-03-23 22:30:18 +01:00
|
|
|
size_t count(0);
|
2020-03-25 01:49:33 +01:00
|
|
|
bool finished(true);
|
2020-03-23 22:30:18 +01:00
|
|
|
event::idx next_token(0);
|
2019-08-12 13:22:52 +02:00
|
|
|
{
|
2020-03-23 22:30:18 +01:00
|
|
|
json::stack::array array
|
2019-08-12 13:22:52 +02:00
|
|
|
{
|
|
|
|
top, "notifications"
|
|
|
|
};
|
2020-03-23 22:30:18 +01:00
|
|
|
|
2020-03-25 01:49:33 +01:00
|
|
|
finished = notifications.for_each(opts, [&]
|
2020-03-23 22:30:18 +01:00
|
|
|
(const event::idx ¬e_idx, const json::object ¬e)
|
|
|
|
{
|
|
|
|
assert(note_idx);
|
|
|
|
next_token = note_idx;
|
|
|
|
if(count >= limit)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const event::idx &event_idx
|
|
|
|
{
|
|
|
|
note.get<event::idx>("event_idx")
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx &rule_idx
|
|
|
|
{
|
|
|
|
note.get<event::idx>("rule_idx")
|
|
|
|
};
|
|
|
|
|
|
|
|
//if(m::redacted(event_idx))
|
|
|
|
// return true;
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow, event_idx
|
2020-03-23 22:30:18 +01:00
|
|
|
};
|
|
|
|
|
2020-03-25 01:49:33 +01:00
|
|
|
if(unlikely(!event.valid))
|
2020-03-23 22:30:18 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
array
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "room_id", json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "ts", json::value
|
|
|
|
{
|
|
|
|
m::get<time_t>(note_idx, "origin_server_ts")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::id::buf last_read;
|
|
|
|
m::receipt::get(last_read, json::get<"room_id"_>(event), request.user_id);
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "read", json::value
|
|
|
|
{
|
2020-04-02 01:12:41 +02:00
|
|
|
last_read && index(std::nothrow, last_read) >= event_idx
|
2020-03-23 22:30:18 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: get from rule_idx
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "actions", json::value
|
|
|
|
{
|
|
|
|
opts.only == "highlight"?
|
|
|
|
R"(["notify",{"set_tweak":"highlight","value":true}])":
|
|
|
|
R"(["notify"])"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: get from notification source
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "profile_tag", json::value
|
|
|
|
{
|
|
|
|
nullptr
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object event_object
|
|
|
|
{
|
|
|
|
object, "event"
|
|
|
|
};
|
|
|
|
|
|
|
|
m::event::append::opts opts;
|
|
|
|
opts.keys = ¬ification_event_keys;
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.query_redacted = false;
|
|
|
|
//opts.query_txnid = false;
|
|
|
|
//opts.query_prev_state = false;
|
|
|
|
m::event::append
|
|
|
|
{
|
|
|
|
event_object, event, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
++count;
|
|
|
|
return true;
|
|
|
|
});
|
2019-08-12 13:22:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 01:49:33 +01:00
|
|
|
if(!finished)
|
2019-08-12 13:22:52 +02:00
|
|
|
json::stack::member
|
|
|
|
{
|
2020-03-23 22:30:18 +01:00
|
|
|
top, "next_token", json::value
|
|
|
|
{
|
|
|
|
lex_cast(next_token), json::STRING
|
|
|
|
}
|
2019-08-12 13:22:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
top.~object();
|
2020-02-12 20:30:37 +01:00
|
|
|
return std::move(response);
|
2019-03-31 22:46:48 +02:00
|
|
|
}
|