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"
|
|
|
|
|
2018-02-23 00:52:43 +01:00
|
|
|
using namespace ircd::m;
|
2018-02-14 21:23:20 +01:00
|
|
|
using namespace ircd;
|
|
|
|
|
2018-03-27 12:00:09 +02:00
|
|
|
static event::id::buf
|
2018-06-12 09:29:00 +02:00
|
|
|
bootstrap(const string_view &host,
|
2018-03-27 12:00:09 +02:00
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id);
|
|
|
|
|
2018-02-23 00:52:43 +01:00
|
|
|
extern "C" event::id::buf
|
|
|
|
join__room_user(const room &room,
|
|
|
|
const id::user &user_id);
|
|
|
|
|
2018-03-27 12:00:09 +02:00
|
|
|
extern "C" event::id::buf
|
|
|
|
join__alias_user(const m::room::alias &room_alias,
|
|
|
|
const m::user::id &user_id);
|
|
|
|
|
2018-02-14 21:23:20 +01:00
|
|
|
resource::response
|
|
|
|
post__join(client &client,
|
|
|
|
const resource::request &request,
|
2018-02-23 00:52:43 +01:00
|
|
|
const room::id &room_id)
|
2018-02-14 21:23:20 +01:00
|
|
|
{
|
2018-02-22 10:35:00 +01:00
|
|
|
const string_view &third_party_signed
|
|
|
|
{
|
|
|
|
unquote(request["third_party_signed"])
|
|
|
|
};
|
|
|
|
|
2018-06-12 09:29:00 +02:00
|
|
|
const string_view &server_name
|
|
|
|
{
|
|
|
|
unquote(request["server_name"])
|
|
|
|
};
|
|
|
|
|
2018-02-23 00:52:43 +01:00
|
|
|
join__room_user(room_id, request.user_id);
|
2018-02-14 21:23:20 +01:00
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, json::members
|
|
|
|
{
|
|
|
|
{ "room_id", room_id }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-02-23 00:52:43 +01:00
|
|
|
|
|
|
|
event::id::buf
|
|
|
|
join__room_user(const room &room,
|
|
|
|
const id::user &user_id)
|
|
|
|
{
|
2018-03-09 01:32:31 +01:00
|
|
|
if(!exists(room))
|
2018-06-12 09:29:00 +02:00
|
|
|
{
|
|
|
|
const auto &room_id(room.room_id);
|
|
|
|
return bootstrap(room_id.host(), room_id, user_id); //TODO: host
|
|
|
|
}
|
2018-03-09 01:32:31 +01:00
|
|
|
|
2018-02-23 00:52:43 +01:00
|
|
|
json::iov event;
|
|
|
|
json::iov content;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.room.member" }},
|
|
|
|
{ event, { "sender", user_id }},
|
|
|
|
{ event, { "state_key", user_id }},
|
|
|
|
{ event, { "membership", "join" }},
|
|
|
|
{ content, { "membership", "join" }},
|
|
|
|
};
|
|
|
|
|
2018-03-23 00:28:43 +01:00
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
char displayname_buf[256];
|
|
|
|
const string_view displayname
|
|
|
|
{
|
|
|
|
user.profile(displayname_buf, "displayname")
|
|
|
|
};
|
|
|
|
|
|
|
|
char avatar_url_buf[256];
|
|
|
|
const string_view avatar_url
|
|
|
|
{
|
|
|
|
user.profile(avatar_url_buf, "avatar_url")
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add _displayname
|
2018-03-23 00:28:43 +01:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
content, !empty(displayname),
|
|
|
|
{
|
|
|
|
"displayname", [&displayname]() -> json::value
|
|
|
|
{
|
|
|
|
return displayname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add _avatar_url
|
|
|
|
{
|
|
|
|
content, !empty(avatar_url),
|
|
|
|
{
|
|
|
|
"avatar_url", [&avatar_url]() -> json::value
|
|
|
|
{
|
|
|
|
return avatar_url;
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 00:28:43 +01:00
|
|
|
};
|
|
|
|
|
2018-02-23 00:52:43 +01:00
|
|
|
return commit(room, event, content);
|
|
|
|
}
|
2018-03-27 12:00:09 +02:00
|
|
|
|
|
|
|
event::id::buf
|
|
|
|
join__alias_user(const m::room::alias &room_alias,
|
|
|
|
const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
const room::id::buf room_id
|
|
|
|
{
|
|
|
|
m::room_id(room_alias)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!exists(room_id))
|
2018-06-12 09:29:00 +02:00
|
|
|
return bootstrap(room_alias.host(), room_id, user_id);
|
2018-03-27 12:00:09 +02:00
|
|
|
|
|
|
|
return join__room_user(room_id, user_id);
|
|
|
|
}
|
|
|
|
|
2018-09-05 08:19:26 +02:00
|
|
|
conf::item<seconds>
|
|
|
|
make_join_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.join.make_join.timeout" },
|
|
|
|
{ "default", 15L },
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<seconds>
|
|
|
|
send_join_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.rooms.join_send_join.timeout" },
|
|
|
|
{ "default", 45L /* spinappse */ },
|
|
|
|
};
|
|
|
|
|
2018-03-27 12:00:09 +02:00
|
|
|
static event::id::buf
|
2018-06-12 09:29:00 +02:00
|
|
|
bootstrap(const string_view &host,
|
2018-03-27 12:00:09 +02:00
|
|
|
const m::room::id &room_id,
|
|
|
|
const m::user::id &user_id)
|
|
|
|
{
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::make_join request
|
|
|
|
{
|
2018-06-12 09:29:00 +02:00
|
|
|
room_id, user_id, buf, { host }
|
2018-03-27 12:00:09 +02:00
|
|
|
};
|
|
|
|
|
2018-09-05 08:19:26 +02:00
|
|
|
request.wait(seconds(make_join_timeout));
|
2018-03-27 12:00:09 +02:00
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
request.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
request.in.content
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &proto
|
|
|
|
{
|
|
|
|
response.at("event")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto auth_events
|
|
|
|
{
|
|
|
|
replace(std::string{proto.get("auth_events")}, "\\/", "/")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto prev_events
|
|
|
|
{
|
|
|
|
replace(std::string{proto.get("prev_events")}, "\\/", "/")
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov event;
|
|
|
|
json::iov content;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.room.member" }},
|
|
|
|
{ event, { "sender", user_id }},
|
|
|
|
{ event, { "state_key", user_id }},
|
|
|
|
{ event, { "membership", "join" }},
|
|
|
|
{ content, { "membership", "join" }},
|
|
|
|
{ event, { "prev_events", prev_events }},
|
|
|
|
{ event, { "auth_events", auth_events }},
|
|
|
|
{ event, { "prev_state", "[]" }},
|
|
|
|
{ event, { "depth", proto.get<long>("depth") }},
|
|
|
|
{ event, { "room_id", room_id }},
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user user
|
|
|
|
{
|
|
|
|
user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
char displayname_buf[256];
|
|
|
|
const string_view displayname
|
|
|
|
{
|
|
|
|
user.profile(displayname_buf, "displayname")
|
|
|
|
};
|
|
|
|
|
|
|
|
char avatar_url_buf[256];
|
|
|
|
const string_view avatar_url
|
|
|
|
{
|
|
|
|
user.profile(avatar_url_buf, "avatar_url")
|
|
|
|
};
|
|
|
|
|
2018-06-05 20:19:40 +02:00
|
|
|
const json::iov::add _displayname
|
2018-03-27 12:00:09 +02:00
|
|
|
{
|
2018-06-05 20:19:40 +02:00
|
|
|
content, !empty(displayname),
|
|
|
|
{
|
|
|
|
"displayname", [&displayname]() -> json::value
|
|
|
|
{
|
|
|
|
return displayname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add _avatar_url
|
|
|
|
{
|
|
|
|
content, !empty(avatar_url),
|
|
|
|
{
|
|
|
|
"avatar_url", [&avatar_url]() -> json::value
|
|
|
|
{
|
|
|
|
return avatar_url;
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 12:00:09 +02:00
|
|
|
};
|
|
|
|
|
2018-05-07 01:04:51 +02:00
|
|
|
m::vm::copts opts;
|
2018-03-27 12:00:09 +02:00
|
|
|
opts.non_conform.set(m::event::conforms::MISSING_MEMBERSHIP);
|
|
|
|
opts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
|
|
|
opts.prev_check_exists = false;
|
2018-06-12 09:29:00 +02:00
|
|
|
opts.head_must_exist = false;
|
2018-03-27 12:00:09 +02:00
|
|
|
opts.history = false;
|
|
|
|
opts.infolog_accept = true;
|
2018-05-07 01:04:51 +02:00
|
|
|
const m::event::id::buf event_id
|
2018-03-27 12:00:09 +02:00
|
|
|
{
|
2018-05-07 01:04:51 +02:00
|
|
|
m::vm::eval
|
|
|
|
{
|
|
|
|
event, content, opts
|
|
|
|
}
|
2018-03-27 12:00:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> ebuf
|
|
|
|
{
|
|
|
|
64_KiB
|
|
|
|
};
|
|
|
|
|
2019-01-16 23:35:10 +01:00
|
|
|
const m::event::fetch mevent
|
2018-03-27 12:00:09 +02:00
|
|
|
{
|
2019-01-16 23:35:10 +01:00
|
|
|
event_id
|
2018-03-27 12:00:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const string_view strung
|
|
|
|
{
|
2019-01-16 23:35:10 +01:00
|
|
|
mevent.source?
|
|
|
|
string_view{mevent.source}:
|
|
|
|
string_view{data(ebuf), serialized(mevent)}
|
2018-03-27 12:00:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf2
|
|
|
|
{
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::send_join sj
|
|
|
|
{
|
2018-06-12 09:29:00 +02:00
|
|
|
room_id, event_id, strung, buf2, { host }
|
2018-03-27 12:00:09 +02:00
|
|
|
};
|
|
|
|
|
2018-09-05 08:19:26 +02:00
|
|
|
sj.wait(seconds(send_join_timeout));
|
2018-03-27 12:00:09 +02:00
|
|
|
|
|
|
|
const auto sjcode
|
|
|
|
{
|
|
|
|
sj.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
return event_id;
|
|
|
|
}
|