diff --git a/include/ircd/m/v1/invite.h b/include/ircd/m/v1/invite.h new file mode 100644 index 000000000..76da2604d --- /dev/null +++ b/include/ircd/m/v1/invite.h @@ -0,0 +1,40 @@ +// 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_INVITE_H + +namespace ircd::m::v1 +{ + struct invite; +}; + +struct ircd::m::v1::invite +:server::request +{ + struct opts; + + explicit operator json::array() const + { + return json::array{in.content}; + } + + invite(const room::id &, const id::event &, const json::object &, const mutable_buffer &, opts); + invite() = default; +}; + +struct ircd::m::v1::invite::opts +{ + net::hostport remote; + m::request request; + server::out out; + server::in in; + const struct server::request::opts *sopts {nullptr}; +}; diff --git a/include/ircd/m/v1/v1.h b/include/ircd/m/v1/v1.h index 82a1e5433..5867035f9 100644 --- a/include/ircd/m/v1/v1.h +++ b/include/ircd/m/v1/v1.h @@ -16,6 +16,7 @@ #include "user.h" #include "make_join.h" #include "send_join.h" +#include "invite.h" #include "event.h" #include "state.h" #include "backfill.h" diff --git a/ircd/m/v1.cc b/ircd/m/v1.cc index 23c7ab388..eaf2f9cc1 100644 --- a/ircd/m/v1.cc +++ b/ircd/m/v1.cc @@ -407,6 +407,66 @@ ircd::m::v1::event::event(const m::event::id &event_id, { } +/////////////////////////////////////////////////////////////////////////////// +// +// v1/invite.h +// + +ircd::m::v1::invite::invite(const room::id &room_id, + const id::event &event_id, + const json::object &content, + const mutable_buffer &buf, + opts opts) +:server::request{[&] +{ + assert(!!opts.remote); + + assert(!size(opts.out.content)); + opts.out.content = content; + + assert(!defined(json::get<"content"_>(opts.request))); + json::get<"content"_>(opts.request) = json::object{opts.out.content}; + + 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], ridbuf[768], eidbuf[768]; + json::get<"uri"_>(opts.request) = fmt::sprintf + { + urlbuf, "/_matrix/federation/v1/invite/%s/%s", + url::encode(room_id, ridbuf), + url::encode(event_id, eidbuf) + }; + } + + json::get<"method"_>(opts.request) = "PUT"; + 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 = mutable_buffer{}; + } + + return server::request + { + opts.remote, std::move(opts.out), std::move(opts.in), opts.sopts + }; +}()} +{ +} + /////////////////////////////////////////////////////////////////////////////// // // v1/send_join.h