0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-29 18:22:50 +01:00

modules/client/pushrules: Add handler functionalities; rename namespace.

This commit is contained in:
Jason Volk 2020-03-18 14:28:41 -07:00
parent f897ebd134
commit 0ee313efc4

View file

@ -8,11 +8,14 @@
// copyright notice and this permission notice is present in all copies. The // copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file. // full license for this software is available in the LICENSE file.
namespace ircd::m::pushrules namespace ircd::m::push
{ {
static path params(mutable_buffer, const resource::request &);
static resource::response handle_delete(client &, const resource::request &); static resource::response handle_delete(client &, const resource::request &);
static resource::response handle_put(client &, const resource::request &); static resource::response handle_put(client &, const resource::request &);
static resource::response handle_get(client &, const resource::request &); static resource::response handle_get(client &, const resource::request &);
static const size_t PATH_BUFSIZE {256};
extern resource::method method_get; extern resource::method method_get;
extern resource::method method_put; extern resource::method method_put;
extern resource::method method_delete; extern resource::method method_delete;
@ -25,8 +28,8 @@ IRCD_MODULE
"Client 0.6.0-13.13.1.6 :Push Rules API" "Client 0.6.0-13.13.1.6 :Push Rules API"
}; };
decltype(ircd::m::pushrules::resource) decltype(ircd::m::push::resource)
ircd::m::pushrules::resource ircd::m::push::resource
{ {
"/_matrix/client/r0/pushrules", "/_matrix/client/r0/pushrules",
{ {
@ -36,8 +39,8 @@ ircd::m::pushrules::resource
} }
}; };
decltype(ircd::m::pushrules::method_get) decltype(ircd::m::push::method_get)
ircd::m::pushrules::method_get ircd::m::push::method_get
{ {
resource, "GET", handle_get, resource, "GET", handle_get,
{ {
@ -46,50 +49,152 @@ ircd::m::pushrules::method_get
}; };
ircd::m::resource::response ircd::m::resource::response
ircd::m::pushrules::handle_get(client &client, ircd::m::push::handle_get(client &client,
const resource::request &request) const resource::request &request)
{ {
char buf[3][64]; char buf[PATH_BUFSIZE];
const auto &scope const auto &path
{ {
request.parv.size() > 0? params(buf, request)
url::decode(buf[0], request.parv[0]):
string_view{},
}; };
const auto &kind const auto &[scope, kind, ruleid]
{ {
request.parv.size() > 1? path
url::decode(buf[1], request.parv[1]):
string_view{},
}; };
const auto &ruleid const user::pushrules pushrules
{ {
request.parv.size() > 2? request.user_id
url::decode(buf[2], request.parv[2]):
string_view{},
}; };
m::resource::response::chunked response
return resource::response
{ {
client, json::members client, http::OK
};
json::stack out
{
response.buf, response.flusher()
};
json::stack::object top
{
out
};
const auto append_rule{[]
(json::stack::array &_kind, const auto &path, const json::object &rule)
{
const auto &[scope, kind, ruleid]
{ {
{ "global", json::members path
};
json::stack::object object
{
_kind
};
json::stack::member
{
object, "rule_id", ruleid
};
for(const auto &[key, val] : rule)
json::stack::member
{ {
{ "content", json::array{} }, object, key, val
{ "override", json::array{} }, };
{ "room", json::array{} }, }};
{ "sender", json::array{} },
{ "underride", json::array{} }, if(ruleid)
}} {
} json::stack::object _scope
}; {
top, scope
};
json::stack::array _kind
{
_scope, kind
};
pushrules.get(std::nothrow, path, [&]
(const auto &path, const json::object &rule)
{
append_rule(_kind, path, rule);
});
return {};
}
if(kind)
{
json::stack::object _scope
{
top, scope
};
json::stack::array _kind
{
_scope, kind
};
pushrules.for_each(push::path{scope, kind, {}}, [&]
(const auto &path, const json::object &rule)
{
append_rule(_kind, path, rule);
return true;
});
return {};
}
const auto each_scope{[&]
(const auto &scope)
{
json::stack::object _scope
{
top, scope
};
const auto each_kind{[&]
(const string_view &kind)
{
json::stack::array _kind
{
_scope, kind
};
pushrules.for_each(push::path{scope, kind, {}}, [&]
(const auto &path, const json::object &rule)
{
append_rule(_kind, path, rule);
return true;
});
}};
each_kind("content");
each_kind("override");
each_kind("room");
each_kind("sender");
each_kind("underride");
}};
if(scope)
{
each_scope(scope);
return {};
}
//TODO: XXX device scopes
each_scope("global");
return {};
} }
decltype(ircd::m::pushrules::method_put) decltype(ircd::m::push::method_put)
ircd::m::pushrules::method_put ircd::m::push::method_put
{ {
resource, "PUT", handle_put, resource, "PUT", handle_put,
{ {
@ -98,39 +203,59 @@ ircd::m::pushrules::method_put
}; };
ircd::m::resource::response ircd::m::resource::response
ircd::m::pushrules::handle_put(client &client, ircd::m::push::handle_put(client &client,
const resource::request &request) const resource::request &request)
{ {
char buf[3][64]; char buf[PATH_BUFSIZE];
const auto &scope const auto &path
{ {
request.parv.size() > 0? params(buf, request)
url::decode(buf[0], request.parv[0]):
string_view{},
}; };
const auto &kind const auto &[scope, kind, ruleid]
{ {
request.parv.size() > 1? path
url::decode(buf[1], request.parv[1]):
string_view{},
}; };
const auto &ruleid if(!scope || !kind || !ruleid)
throw m::NEED_MORE_PARAMS
{
"Missing some path parameters; {scope}/{kind}/{ruleid} required."
};
const auto &before
{ {
request.parv.size() > 2? request.query["before"]
url::decode(buf[2], request.parv[2]): };
string_view{},
const auto &after
{
request.query["after"]
};
const user::pushrules pushrules
{
request.user_id
};
const json::object &rule
{
request
};
const auto res
{
pushrules.set(path, rule)
}; };
return resource::response return resource::response
{ {
client, json::object{} client, http::OK
}; };
} }
decltype(ircd::m::pushrules::method_delete) decltype(ircd::m::push::method_delete)
ircd::m::pushrules::method_delete ircd::m::push::method_delete
{ {
resource, "DELETE", handle_delete, resource, "DELETE", handle_delete,
{ {
@ -139,33 +264,73 @@ ircd::m::pushrules::method_delete
}; };
ircd::m::resource::response ircd::m::resource::response
ircd::m::pushrules::handle_delete(client &client, ircd::m::push::handle_delete(client &client,
const resource::request &request) const resource::request &request)
{ {
char buf[3][64]; char buf[PATH_BUFSIZE];
const auto &scope const auto &path
{ {
request.parv.size() > 0? params(buf, request)
url::decode(buf[0], request.parv[0]):
string_view{},
}; };
const auto &kind const auto &[scope, kind, ruleid]
{ {
request.parv.size() > 1? path
url::decode(buf[1], request.parv[1]):
string_view{},
}; };
const auto &ruleid if(!scope || !kind || !ruleid)
throw m::NEED_MORE_PARAMS
{
"Missing some path parameters; {scope}/{kind}/{ruleid} required."
};
const user::pushrules pushrules
{ {
request.parv.size() > 2? request.user_id
url::decode(buf[2], request.parv[2]): };
string_view{},
const auto res
{
pushrules.del(path)
}; };
return resource::response return resource::response
{ {
client, json::object{} client, res?
http::OK:
http::NOT_FOUND
};
}
ircd::m::push::path
ircd::m::push::params(mutable_buffer buf,
const resource::request &request)
{
const auto &scope
{
request.parv.size() > 0?
url::decode(buf, request.parv[0]):
string_view{},
};
consume(buf, size(scope));
const auto &kind
{
request.parv.size() > 1?
url::decode(buf, request.parv[1]):
string_view{},
};
consume(buf, size(kind));
const auto &ruleid
{
request.parv.size() > 2?
url::decode(buf, request.parv[2]):
string_view{},
};
return path
{
scope, kind, ruleid
}; };
} }