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.
|
|
|
|
|
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Matrix rooms interface; modular components"
|
|
|
|
};
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
decltype(ircd::m::rooms::opts_default)
|
|
|
|
ircd::m::rooms::opts_default;
|
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-08-13 07:59:27 +02:00
|
|
|
ircd::m::rooms::has(const opts &opts)
|
2018-10-24 23:08:14 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
return !for_each(opts, []
|
|
|
|
(const m::room::id &)
|
2018-10-24 23:08:14 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
// false to break; for_each() returns false
|
|
|
|
return false;
|
|
|
|
});
|
2019-03-01 23:51:30 +01:00
|
|
|
}
|
|
|
|
|
2018-10-24 23:22:10 +02:00
|
|
|
size_t
|
2019-05-06 20:46:05 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-08-13 07:59:27 +02:00
|
|
|
ircd::m::rooms::count(const opts &opts)
|
2018-10-24 23:22:10 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
size_t ret{0};
|
2019-08-13 07:59:27 +02:00
|
|
|
for_each(opts, [&ret]
|
|
|
|
(const m::room::id &)
|
2018-10-24 23:22:10 +02:00
|
|
|
{
|
2018-10-25 01:50:54 +02:00
|
|
|
++ret;
|
|
|
|
return true;
|
2019-08-13 07:59:27 +02:00
|
|
|
});
|
2018-10-24 23:22:10 +02:00
|
|
|
|
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-05-06 20:46:05 +02:00
|
|
|
IRCD_MODULE_EXPORT
|
2019-08-13 07:59:27 +02:00
|
|
|
ircd::m::rooms::for_each(const room::id::closure_bool &closure)
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
return for_each(opts_default, closure);
|
2018-10-25 01:50:54 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
|
|
|
ircd::m::rooms::for_each(const opts &opts,
|
|
|
|
const room::id::closure_bool &closure)
|
2018-10-25 01:50:54 +02:00
|
|
|
{
|
2019-09-06 05:28:43 +02:00
|
|
|
const auto match_alias_prefix{[&opts]
|
|
|
|
(const auto &alias)
|
|
|
|
{
|
|
|
|
return !startswith(alias, opts.search_term);
|
|
|
|
}};
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
bool ret{true};
|
2019-09-06 05:28:43 +02:00
|
|
|
const auto proffer{[&opts, &closure, &ret, &match_alias_prefix]
|
2019-08-13 07:59:27 +02:00
|
|
|
(const m::room::id &room_id)
|
2018-10-25 01:50:54 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
if(opts.room_id && !opts.lower_bound)
|
2018-10-25 01:50:54 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
ret = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-10-25 00:18:07 +02:00
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
if(opts.room_id)
|
|
|
|
if(room_id < opts.room_id)
|
|
|
|
return;
|
2018-10-25 01:50:54 +02:00
|
|
|
|
2019-08-13 13:43:04 +02:00
|
|
|
if(opts.remote_joined_only)
|
2019-08-14 10:01:46 +02:00
|
|
|
if(local_only(room(room_id)))
|
2019-08-13 13:43:04 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(opts.local_only)
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!local_only(room(room_id)))
|
2019-08-13 13:43:04 +02:00
|
|
|
return;
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
if(opts.server && !opts.summary)
|
|
|
|
if(opts.server != room_id.host())
|
|
|
|
return;
|
2018-10-24 23:01:21 +02:00
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
if(opts.summary)
|
|
|
|
if(!summary::has(room_id))
|
|
|
|
return;
|
2018-10-24 23:01:21 +02:00
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
if(opts.server && opts.summary)
|
|
|
|
if(!room::aliases(room_id).count(opts.server))
|
|
|
|
return;
|
2018-10-24 23:01:21 +02:00
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
if(opts.join_rule)
|
2019-08-14 10:01:46 +02:00
|
|
|
if(!join_rule(room(room_id), opts.join_rule))
|
2019-08-13 07:59:27 +02:00
|
|
|
return;
|
2018-10-24 23:01:21 +02:00
|
|
|
|
2019-09-06 05:28:43 +02:00
|
|
|
if(startswith(opts.search_term, m::id::ROOM_ALIAS))
|
|
|
|
if(room::aliases(room_id).for_each(match_alias_prefix))
|
|
|
|
return; // no match
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
ret = closure(room_id);
|
|
|
|
}};
|
2018-10-24 23:01:21 +02:00
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
// branch for optimized public rooms searches.
|
|
|
|
if(opts.summary)
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
const room::id::buf public_room_id
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
"!public", my_host()
|
2018-10-24 23:01:21 +02:00
|
|
|
};
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
const room::state state{public_room_id};
|
|
|
|
return state.for_each("ircd.rooms", opts.server, [&proffer, &ret]
|
|
|
|
(const string_view &type, const string_view &state_key, const event::idx &event_idx)
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
room::id::buf buf;
|
|
|
|
proffer(room::id::unswap(state_key, buf));
|
|
|
|
return ret;
|
|
|
|
});
|
2018-10-24 23:01:21 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 03:44:58 +02:00
|
|
|
return events::type::for_each_in("m.room.create", [&proffer, &ret]
|
2019-08-13 07:59:27 +02:00
|
|
|
(const string_view &type, const event::idx &event_idx)
|
2018-10-24 23:01:21 +02:00
|
|
|
{
|
2019-08-13 07:59:27 +02:00
|
|
|
assert(type == "m.room.create");
|
|
|
|
m::get(std::nothrow, event_idx, "room_id", proffer);
|
|
|
|
return ret;
|
2018-10-24 23:01:21 +02:00
|
|
|
});
|
2018-10-25 00:18:07 +02:00
|
|
|
}
|