2019-08-13 07:59:27 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 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::summary
|
|
|
|
{
|
|
|
|
static void chunk_remote(const room &, json::stack::object &o);
|
|
|
|
static void chunk_local(const room &, json::stack::object &o);
|
|
|
|
|
|
|
|
extern hookfn<vm::eval &> create_public_room;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
decltype(ircd::m::rooms::summary::create_public_room)
|
|
|
|
ircd::m::rooms::summary::create_public_room
|
|
|
|
{
|
|
|
|
{
|
|
|
|
{ "_site", "vm.effect" },
|
|
|
|
{ "room_id", "!ircd" },
|
|
|
|
{ "type", "m.room.create" },
|
|
|
|
},
|
2019-10-01 05:50:58 +02:00
|
|
|
[](const m::event &event, m::vm::eval &)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
auto &my
|
|
|
|
{
|
|
|
|
m::my(at<"origin"_>(event))
|
|
|
|
};
|
|
|
|
|
2019-10-04 21:48:02 +02:00
|
|
|
const m::room::id::buf public_room_id
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2019-10-04 21:48:02 +02:00
|
|
|
"public", origin(my)
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
m::create(public_room_id, me());
|
2019-08-13 07:59:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// rooms::summary::fetch
|
|
|
|
//
|
|
|
|
|
|
|
|
decltype(ircd::m::rooms::summary::fetch::timeout)
|
|
|
|
ircd::m::rooms::summary::fetch::timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.rooms.fetch.timeout" },
|
|
|
|
{ "default", 45L /* matrix.org :-/ */ },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::rooms::summary::fetch::limit)
|
|
|
|
ircd::m::rooms::summary::fetch::limit
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.rooms.fetch.limit" },
|
|
|
|
{ "default", 64L },
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// fetch::fetch
|
|
|
|
//
|
|
|
|
|
2019-09-14 03:10:49 +02:00
|
|
|
ircd::m::rooms::summary::fetch::fetch(const string_view &origin,
|
2019-08-13 07:59:27 +02:00
|
|
|
const string_view &since,
|
2020-05-29 10:39:25 +02:00
|
|
|
const size_t &limit,
|
|
|
|
const string_view &search_term)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::public_rooms::opts opts;
|
2019-08-13 07:59:27 +02:00
|
|
|
opts.limit = limit;
|
|
|
|
opts.since = since;
|
|
|
|
opts.include_all_networks = true;
|
2020-05-29 10:39:25 +02:00
|
|
|
opts.search_term = search_term;
|
2019-08-13 07:59:27 +02:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
// Buffer for headers and send content; received content is dynamic
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::public_rooms request
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2019-09-14 03:10:49 +02:00
|
|
|
origin, buf, std::move(opts)
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto code
|
|
|
|
{
|
|
|
|
request.get(seconds(timeout))
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object response
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &chunk
|
|
|
|
{
|
|
|
|
response.get("chunk")
|
|
|
|
};
|
|
|
|
|
|
|
|
for(const json::object &summary : chunk)
|
|
|
|
{
|
|
|
|
const json::string &room_id
|
|
|
|
{
|
|
|
|
summary.at("room_id")
|
|
|
|
};
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
summary::set(room_id, origin, summary);
|
2019-08-13 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this->total_room_count_estimate =
|
|
|
|
{
|
|
|
|
response.get("total_room_count_estimate", 0UL)
|
|
|
|
};
|
|
|
|
|
|
|
|
this->next_batch = json::string
|
|
|
|
{
|
|
|
|
response.get("next_batch", string_view{})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// rooms::summary
|
|
|
|
//
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
void
|
2019-08-13 07:59:27 +02:00
|
|
|
ircd::m::rooms::summary::del(const m::room &room)
|
2019-09-15 04:51:08 +02:00
|
|
|
{
|
|
|
|
for_each(room, [&room]
|
|
|
|
(const string_view &origin, const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
del(room, origin);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::rooms::summary::del(const m::room &room,
|
|
|
|
const string_view &origin)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const m::room::id::buf public_room_id
|
|
|
|
{
|
|
|
|
"public", my_host()
|
|
|
|
};
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
public_room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
char state_key_buf[m::event::STATE_KEY_MAX_SIZE];
|
|
|
|
const auto state_key
|
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
make_state_key(state_key_buf, room.room_id, origin)
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
state.get(std::nothrow, "ircd.rooms.summary", state_key)
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event_idx)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
m::event_id(event_idx)
|
|
|
|
};
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
return redact(public_room_id, me(), event_id, "delisted");
|
2019-08-13 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::rooms::summary::set(const m::room &room)
|
|
|
|
{
|
|
|
|
if(!exists(room))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Cannot set a summary for room '%s' which I have no state for",
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
48_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object summary
|
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
get(buf, room)
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
return set(room.room_id, my_host(), summary);
|
2019-08-13 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::rooms::summary::set(const m::room::id &room_id,
|
2019-09-15 04:51:08 +02:00
|
|
|
const string_view &origin,
|
2019-08-13 07:59:27 +02:00
|
|
|
const json::object &summary)
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const m::room::id::buf public_room_id
|
|
|
|
{
|
|
|
|
"public", my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
char state_key_buf[event::STATE_KEY_MAX_SIZE];
|
|
|
|
const auto state_key
|
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
make_state_key(state_key_buf, room_id, origin)
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
return send(public_room_id, me(), "ircd.rooms.summary", state_key, summary);
|
2019-08-13 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
2019-09-15 04:51:08 +02:00
|
|
|
ircd::m::rooms::summary::get(const mutable_buffer &buf,
|
|
|
|
const m::room &room)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
|
|
|
json::stack out{buf};
|
|
|
|
{
|
|
|
|
json::stack::object obj{out};
|
2019-09-15 04:51:08 +02:00
|
|
|
get(obj, room);
|
2019-08-13 07:59:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return json::object
|
|
|
|
{
|
|
|
|
out.completed()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-09-15 04:51:08 +02:00
|
|
|
ircd::m::rooms::summary::get(json::stack::object &obj,
|
|
|
|
const m::room &room)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
|
|
|
return exists(room)?
|
|
|
|
chunk_local(room, obj):
|
|
|
|
chunk_remote(room, obj);
|
|
|
|
}
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
bool
|
|
|
|
ircd::m::rooms::summary::has(const room::id &room_id,
|
|
|
|
const string_view &origin)
|
|
|
|
{
|
|
|
|
return !for_each(room_id, [&origin]
|
|
|
|
(const string_view &_origin, const json::object &summary)
|
|
|
|
{
|
|
|
|
if(!origin)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(origin && _origin == origin)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::rooms::summary::for_each(const room::id &room_id,
|
|
|
|
const closure &closure)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
return for_each(room_id, [&closure]
|
|
|
|
(const string_view &origin, const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
bool ret{true};
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&closure, &origin, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
ret = closure(origin, content);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::rooms::summary::for_each(const room::id &room_id,
|
|
|
|
const closure_idx &closure)
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const m::room::id::buf public_room_id
|
|
|
|
{
|
|
|
|
"public", my_host()
|
|
|
|
};
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
const m::room::state state
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
|
|
|
public_room_id
|
|
|
|
};
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
char state_key_buf[m::event::STATE_KEY_MAX_SIZE];
|
2019-08-13 07:59:27 +02:00
|
|
|
const auto state_key
|
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
make_state_key(state_key_buf, room_id, string_view{})
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
bool ret{true};
|
|
|
|
state.for_each("ircd.rooms.summary", state_key, [&room_id, &closure, &ret]
|
|
|
|
(const auto &type, const auto &state_key, const auto &event_idx)
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
const auto &[_room_id, origin]
|
2019-08-13 07:59:27 +02:00
|
|
|
{
|
2019-09-15 04:51:08 +02:00
|
|
|
unmake_state_key(state_key)
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 04:51:08 +02:00
|
|
|
if(_room_id != room_id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!closure(origin, event_idx))
|
|
|
|
ret = false;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<ircd::m::room::id, ircd::string_view>
|
|
|
|
ircd::m::rooms::summary::unmake_state_key(const string_view &key)
|
|
|
|
{
|
|
|
|
return rsplit(key, '!');
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::rooms::summary::make_state_key(const mutable_buffer &buf,
|
|
|
|
const m::room::id &room_id,
|
|
|
|
const string_view &origin)
|
|
|
|
{
|
|
|
|
return fmt::sprintf
|
|
|
|
{
|
|
|
|
buf, "%s!%s",
|
|
|
|
string_view{room_id},
|
|
|
|
origin,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// internal
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::rooms::summary::chunk_remote(const m::room &room,
|
|
|
|
json::stack::object &obj)
|
|
|
|
{
|
|
|
|
for_each(room, [&obj]
|
|
|
|
(const string_view &origin, const json::object &summary)
|
|
|
|
{
|
|
|
|
obj.append(summary);
|
|
|
|
return false;
|
2019-08-13 07:59:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::rooms::summary::chunk_local(const m::room &room,
|
|
|
|
json::stack::object &obj)
|
|
|
|
{
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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 auto &type, const auto &content_key, const auto &closure)
|
|
|
|
{
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, type, "")
|
|
|
|
};
|
|
|
|
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&content_key, &closure]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &value
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2020-03-07 21:16:33 +01:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-08-13 07:59:27 +02:00
|
|
|
const json::array aliases
|
|
|
|
{
|
2020-03-07 21:16:33 +01:00
|
|
|
content["aliases"]
|
2019-08-13 07:59:27 +02:00
|
|
|
};
|
|
|
|
|
2020-03-07 21:16:33 +01:00
|
|
|
for(const json::string &a : aliases)
|
|
|
|
array.append(a);
|
2019-08-13 07:59:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|