2018-10-24 23:01:21 +02: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.
|
|
|
|
|
|
|
|
namespace ircd::m::rooms
|
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
static string_view make_state_key(const mutable_buffer &out, const m::room::id &);
|
|
|
|
static m::room::id::buf unmake_state_key(const string_view &);
|
|
|
|
|
|
|
|
static void remote_summary_chunk(const m::room &room, json::stack::object &obj);
|
|
|
|
static void local_summary_chunk(const m::room &room, json::stack::object &obj);
|
2019-06-18 08:05:31 +02:00
|
|
|
static bool for_each_public(const string_view &room_id_lb, const room::id::closure_bool &);
|
2018-10-24 23:01:21 +02:00
|
|
|
|
2019-06-18 08:05:31 +02:00
|
|
|
extern conf::item<size_t> fetch_limit;
|
|
|
|
extern conf::item<seconds> fetch_timeout;
|
2019-06-17 23:47:31 +02:00
|
|
|
extern m::hookfn<vm::eval &> create_public_room;
|
2018-10-25 02:51:33 +02:00
|
|
|
extern const room::id::buf public_room_id;
|
2018-10-24 23:01:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix rooms interface; modular components"
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::rooms::public_room_id)
|
|
|
|
ircd::m::rooms::public_room_id
|
|
|
|
{
|
|
|
|
"public", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Create the public rooms room during initial database bootstrap.
|
|
|
|
/// This hooks the creation of the !ircd room which is a fundamental
|
|
|
|
/// event indicating the database has just been created.
|
2019-06-17 23:47:31 +02:00
|
|
|
decltype(ircd::m::rooms::create_public_room)
|
|
|
|
ircd::m::rooms::create_public_room
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.create" },
|
2019-06-17 23:47:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
[](const m::event &, m::vm::eval &)
|
|
|
|
{
|
|
|
|
m::create(public_room_id, m::me.user_id);
|
2018-10-24 23:01:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-24 23:08:14 +02:00
|
|
|
bool
|
2019-04-08 05:15:00 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-04-18 05:35:34 +02:00
|
|
|
ircd::m::rooms::for_each(const each_opts &opts)
|
2018-10-24 23:08:14 +02:00
|
|
|
{
|
2019-04-18 05:35:34 +02:00
|
|
|
if(opts.public_rooms)
|
2019-06-18 08:05:31 +02:00
|
|
|
return for_each_public(opts.key, opts.closure);
|
2019-04-18 05:35:34 +02:00
|
|
|
|
2018-10-24 23:08:14 +02:00
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
my_room
|
|
|
|
};
|
|
|
|
|
2019-04-18 05:35:34 +02:00
|
|
|
const room::state::keys_bool keys{[&opts]
|
2018-10-24 23:08:14 +02:00
|
|
|
(const string_view &room_id) -> bool
|
|
|
|
{
|
2019-04-18 05:35:34 +02:00
|
|
|
return opts.closure(room_id);
|
2018-10-24 23:08:14 +02:00
|
|
|
}};
|
|
|
|
|
2019-04-18 05:35:34 +02:00
|
|
|
return state.for_each("ircd.room", opts.key, keys);
|
2018-10-24 23:08:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 23:51:30 +01:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::is_public(const room::id &room_id)
|
|
|
|
{
|
|
|
|
const m::room room{public_room_id};
|
|
|
|
const m::room::state state{room};
|
|
|
|
return state.has("ircd.room", room_id);
|
|
|
|
}
|
|
|
|
|
2018-10-24 23:22:10 +02:00
|
|
|
size_t
|
2019-05-06 20:46:05 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::count_public(const string_view &server)
|
2018-10-24 23:22:10 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
size_t ret{0};
|
|
|
|
const auto count{[&ret]
|
|
|
|
(const m::room::id &room_id)
|
2018-10-24 23:22:10 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
}};
|
2018-10-24 23:22:10 +02:00
|
|
|
|
2019-06-18 08:05:31 +02:00
|
|
|
for_each_public(server, count);
|
2018-10-25 01:50:54 +02:00
|
|
|
return ret;
|
2018-10-24 23:22:10 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 23:15:36 +02:00
|
|
|
bool
|
2019-06-18 08:05:31 +02:00
|
|
|
ircd::m::rooms::for_each_public(const string_view &key,
|
|
|
|
const room::id::closure_bool &closure)
|
2018-10-24 23:15:36 +02:00
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
public_room_id
|
|
|
|
};
|
|
|
|
|
2018-10-25 01:50:54 +02:00
|
|
|
const bool is_room_id
|
|
|
|
{
|
|
|
|
m::valid(m::id::ROOM, key)
|
|
|
|
};
|
|
|
|
|
|
|
|
char state_key_buf[256];
|
|
|
|
const auto state_key
|
|
|
|
{
|
|
|
|
is_room_id?
|
|
|
|
make_state_key(state_key_buf, key):
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &server
|
|
|
|
{
|
|
|
|
is_room_id?
|
|
|
|
room::id(key).host():
|
|
|
|
key
|
|
|
|
};
|
|
|
|
|
|
|
|
const room::state::keys_bool keys{[&closure, &server]
|
|
|
|
(const string_view &state_key) -> bool
|
2018-10-24 23:15:36 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
const auto room_id
|
|
|
|
{
|
|
|
|
unmake_state_key(state_key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(server && room_id.host() != server)
|
|
|
|
return false;
|
|
|
|
|
2018-10-24 23:15:36 +02:00
|
|
|
return closure(room_id);
|
|
|
|
}};
|
|
|
|
|
2018-10-25 01:50:54 +02:00
|
|
|
return state.for_each("ircd.rooms", state_key, keys);
|
2018-10-24 23:15:36 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 23:01:21 +02:00
|
|
|
void
|
2019-05-06 20:46:05 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::summary_chunk(const m::room &room,
|
|
|
|
json::stack::object &obj)
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
return exists(room)?
|
|
|
|
local_summary_chunk(room, obj):
|
|
|
|
remote_summary_chunk(room, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::rooms::remote_summary_chunk(const m::room &room,
|
|
|
|
json::stack::object &obj)
|
|
|
|
{
|
|
|
|
const m::room publix
|
2018-10-25 00:18:07 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
public_room_id
|
|
|
|
};
|
2018-10-25 00:18:07 +02:00
|
|
|
|
2018-10-25 01:50:54 +02:00
|
|
|
char state_key_buf[256];
|
|
|
|
const auto state_key
|
|
|
|
{
|
|
|
|
make_state_key(state_key_buf, room.room_id)
|
|
|
|
};
|
2018-10-25 00:18:07 +02:00
|
|
|
|
2018-10-25 01:50:54 +02:00
|
|
|
publix.get("ircd.rooms", state_key, [&obj]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const json::object &summary
|
|
|
|
{
|
|
|
|
json::at<"content"_>(event)
|
|
|
|
};
|
2018-10-25 00:18:07 +02:00
|
|
|
|
2018-10-25 01:50:54 +02:00
|
|
|
for(const auto &member : summary)
|
|
|
|
json::stack::member m{obj, member.first, member.second};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::rooms::local_summary_chunk(const m::room &room,
|
|
|
|
json::stack::object &obj)
|
|
|
|
{
|
2019-01-24 00:36:02 +01:00
|
|
|
static const event::keys::include keys
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2019-01-24 00:36:02 +01:00
|
|
|
"content",
|
2018-10-24 23:01:21 +02:00
|
|
|
};
|
|
|
|
|
2018-12-07 04:20:30 +01:00
|
|
|
const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
keys, room.fopts? room.fopts->gopts : db::gopts{}
|
|
|
|
};
|
2018-10-24 23:01:21 +02:00
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
// Closure over boilerplate primary room state queries; i.e matrix
|
|
|
|
// m.room.$type events with state key "" where the content is directly
|
|
|
|
// presented to the closure.
|
|
|
|
const auto query{[&state]
|
|
|
|
(const string_view &type, const string_view &content_key, const auto &closure)
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, type, "", [&content_key, &closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content(json::get<"content"_>(event));
|
|
|
|
const auto &value(unquote(content.get(content_key)));
|
|
|
|
closure(value);
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Aliases array
|
|
|
|
{
|
|
|
|
json::stack::member aliases_m{obj, "aliases"};
|
|
|
|
json::stack::array array{aliases_m};
|
|
|
|
state.for_each("m.room.aliases", [&array]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const json::array aliases
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event).get("aliases")
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const string_view &alias : aliases)
|
|
|
|
array.append(unquote(alias));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
query("m.room.avatar_url", "url", [&obj]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "avatar_url", value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
query("m.room.canonical_alias", "alias", [&obj]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "canonical_alias", value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
query("m.room.guest_access", "guest_access", [&obj]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "guest_can_join", json::value
|
|
|
|
{
|
|
|
|
value == "can_join"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
query("m.room.name", "name", [&obj]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "name", value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// num_join_members
|
|
|
|
{
|
|
|
|
const m::room::members members{room};
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "num_joined_members", json::value
|
|
|
|
{
|
|
|
|
long(members.count("join"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// room_id
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "room_id", room.room_id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
query("m.room.topic", "topic", [&obj]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "topic", value
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
query("m.room.history_visibility", "history_visibility", [&obj]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
obj, "world_readable", json::value
|
|
|
|
{
|
|
|
|
value == "world_readable"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2018-10-25 00:18:07 +02:00
|
|
|
|
|
|
|
decltype(ircd::m::rooms::fetch_timeout)
|
|
|
|
ircd::m::rooms::fetch_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.rooms.fetch.timeout" },
|
|
|
|
{ "default", 45L /* matrix.org :-/ */ },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::rooms::fetch_limit)
|
|
|
|
ircd::m::rooms::fetch_limit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.rooms.fetch.limit" },
|
|
|
|
{ "default", 64L },
|
|
|
|
};
|
|
|
|
|
|
|
|
std::pair<size_t, std::string>
|
2019-05-06 20:46:05 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::fetch_update(const net::hostport &hp,
|
|
|
|
const string_view &since,
|
2019-06-18 08:05:31 +02:00
|
|
|
const size_t &limit)
|
2018-10-25 00:18:07 +02:00
|
|
|
{
|
|
|
|
m::v1::public_rooms::opts opts;
|
|
|
|
opts.limit = limit;
|
|
|
|
opts.since = since;
|
|
|
|
opts.include_all_networks = true;
|
|
|
|
opts.dynamic = true;
|
|
|
|
|
|
|
|
// Buffer for headers and send content only; received content is dynamic
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::public_rooms request
|
|
|
|
{
|
|
|
|
hp, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
2019-06-18 08:05:31 +02:00
|
|
|
request.wait(seconds(fetch_timeout));
|
|
|
|
|
2018-10-25 00:18:07 +02:00
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
request.get()
|
|
|
|
};
|
|
|
|
|
2019-06-23 01:12:14 +02:00
|
|
|
const json::object response
|
2018-10-25 00:18:07 +02:00
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &chunk
|
|
|
|
{
|
|
|
|
response.get("chunk")
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::object &summary : chunk)
|
|
|
|
{
|
|
|
|
const m::room::id &room_id
|
|
|
|
{
|
|
|
|
unquote(summary.at("room_id"))
|
|
|
|
};
|
|
|
|
|
2019-05-06 20:46:05 +02:00
|
|
|
summary_set(room_id, summary);
|
2018-10-25 00:18:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
response.get("total_room_count_estimate", 0UL),
|
2018-10-25 01:50:54 +02:00
|
|
|
unquote(response.get("next_batch", string_view{}))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:07:19 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::summary_del(const m::room &room)
|
|
|
|
{
|
|
|
|
char state_key_buf[m::event::STATE_KEY_MAX_SIZE];
|
|
|
|
const m::room::state state{public_room_id};
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
2019-04-18 06:05:31 +02:00
|
|
|
state.get(std::nothrow, "ircd.rooms", make_state_key(state_key_buf, room.room_id))
|
2019-03-14 23:07:19 +01:00
|
|
|
};
|
|
|
|
|
2019-04-18 06:05:31 +02:00
|
|
|
if(!event_idx)
|
|
|
|
return {};
|
|
|
|
|
2019-03-14 23:07:19 +01:00
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
m::event_id(event_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
return redact(public_room_id, m::me, event_id, "delisted");
|
|
|
|
}
|
|
|
|
|
2018-10-25 02:51:33 +02:00
|
|
|
ircd::m::event::id::buf
|
2019-05-06 20:46:05 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::summary_set(const m::room::id &room_id,
|
|
|
|
const json::object &summary)
|
2018-10-25 02:51:33 +02:00
|
|
|
{
|
|
|
|
char state_key_buf[256];
|
|
|
|
const auto state_key
|
|
|
|
{
|
|
|
|
make_state_key(state_key_buf, room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
return send(public_room_id, m::me, "ircd.rooms", state_key, summary);
|
|
|
|
}
|
|
|
|
|
2018-10-25 01:50:54 +02:00
|
|
|
ircd::m::room::id::buf
|
|
|
|
ircd::m::rooms::unmake_state_key(const string_view &key)
|
|
|
|
{
|
|
|
|
const auto s
|
|
|
|
{
|
|
|
|
split(key, '!')
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::room::id::buf
|
|
|
|
{
|
|
|
|
s.second, s.first
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::rooms::make_state_key(const mutable_buffer &buf,
|
|
|
|
const m::room::id &room_id)
|
|
|
|
{
|
|
|
|
mutable_buffer out{buf};
|
|
|
|
consume(out, copy(out, room_id.host()));
|
|
|
|
consume(out, copy(out, room_id.local()));
|
|
|
|
return string_view
|
|
|
|
{
|
|
|
|
data(buf), data(out)
|
2018-10-25 00:18:07 +02:00
|
|
|
};
|
|
|
|
}
|