From 7b38d216b4319b5e473e316fa3a0646bd81561e1 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 2 Apr 2018 18:24:57 -0700 Subject: [PATCH] ircd::m::v1: Fix user devices query and associated console cmd. --- include/ircd/m/v1/user.h | 48 +++++++++++++++++++++++++++++++++ include/ircd/m/v1/v1.h | 1 + ircd/m/v1.cc | 57 ++++++++++++++++++++++++++++++++++++++++ modules/console.cc | 23 ++++++++++++---- 4 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 include/ircd/m/v1/user.h diff --git a/include/ircd/m/v1/user.h b/include/ircd/m/v1/user.h new file mode 100644 index 000000000..7e2ed3df6 --- /dev/null +++ b/include/ircd/m/v1/user.h @@ -0,0 +1,48 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// 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. + +#pragma once +#define HAVE_IRCD_M_V1_USER_H + +namespace ircd::m::v1::user +{ + struct opts; + struct devices; +} + +struct ircd::m::v1::user::devices +:server::request +{ + using opts = v1::user::opts; + + explicit operator json::object() const + { + const json::object object{in.content}; + return object; + } + + devices(const id::user &user_id, const mutable_buffer &, opts); + devices() = default; +}; + +struct ircd::m::v1::user::opts +{ + net::hostport remote; + m::request request; + server::out out; + server::in in; + const struct server::request::opts *sopts {nullptr}; + + opts(const net::hostport &remote) + :remote{remote} + {} + + opts() = default; +}; diff --git a/include/ircd/m/v1/v1.h b/include/ircd/m/v1/v1.h index be384b52e..82a1e5433 100644 --- a/include/ircd/m/v1/v1.h +++ b/include/ircd/m/v1/v1.h @@ -13,6 +13,7 @@ #include "version.h" #include "query.h" +#include "user.h" #include "make_join.h" #include "send_join.h" #include "event.h" diff --git a/ircd/m/v1.cc b/ircd/m/v1.cc index 729c3ea8f..23c7ab388 100644 --- a/ircd/m/v1.cc +++ b/ircd/m/v1.cc @@ -537,6 +537,63 @@ ircd::m::v1::make_join::make_join(const room::id &room_id, { } +/////////////////////////////////////////////////////////////////////////////// +// +// v1/user.h +// + +ircd::m::v1::user::devices::devices(const id::user &user_id, + const mutable_buffer &buf, + opts opts) +:server::request{[&] +{ + if(!opts.remote) + opts.remote = user_id.host(); + + if(!defined(json::get<"origin"_>(opts.request))) + json::get<"origin"_>(opts.request) = my_host(); + + if(!defined(json::get<"destination"_>(opts.request))) + json::get<"destination"_>(opts.request) = host(opts.remote); + + if(!defined(json::get<"uri"_>(opts.request))) + { + thread_local char urlbuf[2048], uidbuf[768]; + json::get<"uri"_>(opts.request) = fmt::sprintf + { + urlbuf, "/_matrix/federation/v1/user/devices/%s", + url::encode(user_id, uidbuf) + }; + } + + if(defined(json::get<"content"_>(opts.request))) + opts.out.content = json::get<"content"_>(opts.request); + + if(!defined(json::get<"method"_>(opts.request))) + json::get<"method"_>(opts.request) = "GET"; + + opts.out.head = opts.request(buf); + + if(!size(opts.in)) + { + const auto in_max + { + std::max(ssize_t(size(buf) - size(opts.out.head)), ssize_t(0)) + }; + + assert(in_max >= ssize_t(size(buf) / 2)); + opts.in.head = { data(buf) + size(opts.out.head), size_t(in_max) }; + opts.in.content = opts.in.head; + } + + return server::request + { + opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts + }; +}()} +{ +} + /////////////////////////////////////////////////////////////////////////////// // // v1/query.h diff --git a/modules/console.cc b/modules/console.cc index 46102edcc..7b4a93236 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -2275,7 +2275,7 @@ console_cmd__fed__query__directory(opt &out, const string_view &line) } bool -console_cmd__fed__query__user_devices(opt &out, const string_view &line) +console_cmd__fed__user__devices(opt &out, const string_view &line) { const m::id::user &user_id { @@ -2287,7 +2287,7 @@ console_cmd__fed__query__user_devices(opt &out, const string_view &line) token(line, ' ', 1, user_id.host()) }; - m::v1::query::opts opts; + m::v1::user::devices::opts opts; opts.remote = remote; const unique_buffer buf @@ -2295,12 +2295,12 @@ console_cmd__fed__query__user_devices(opt &out, const string_view &line) 32_KiB }; - m::v1::query::user_devices request + m::v1::user::devices request { user_id, buf, std::move(opts) }; - request.wait(seconds(10)); + request.wait(seconds(15)); const auto code { request.get() @@ -2311,7 +2311,20 @@ console_cmd__fed__query__user_devices(opt &out, const string_view &line) request }; - out << string_view{response} << std::endl; + const string_view stream_id + { + unquote(response["stream_id"]) + }; + + const json::array &devices + { + response["devices"] + }; + + for(const json::object &device : devices) + out << string_view{device} << std::endl; + + out << "-- " << size(devices) << " devices." << std::endl; return true; }