2018-02-04 03:22:01 +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.
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::purge(const room &room)
|
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
size_t ret(0);
|
|
|
|
db::txn txn
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
*m::dbs::events
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
room.for_each([&txn, &ret]
|
|
|
|
(const m::event::idx &idx)
|
|
|
|
{
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
idx
|
|
|
|
};
|
|
|
|
|
|
|
|
m::dbs::write_opts opts;
|
|
|
|
opts.op = db::op::DELETE;
|
|
|
|
opts.event_idx = idx;
|
|
|
|
m::dbs::write(txn, event, opts);
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
txn();
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
ircd::m::room::state::rebuild::rebuild(const room::id &room_id)
|
|
|
|
{
|
|
|
|
const m::event::id::buf event_id
|
|
|
|
{
|
|
|
|
m::head(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state::history history
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state present_state
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
m::dbs::write_opts opts;
|
|
|
|
opts.appendix.reset();
|
|
|
|
opts.appendix.set(dbs::appendix::ROOM_STATE);
|
|
|
|
opts.appendix.set(dbs::appendix::ROOM_JOINED);
|
|
|
|
db::txn txn
|
|
|
|
{
|
|
|
|
*m::dbs::events
|
|
|
|
};
|
|
|
|
|
|
|
|
ssize_t deleted(0);
|
|
|
|
present_state.for_each([&opts, &txn, &deleted]
|
|
|
|
(const auto &type, const auto &state_key, const auto &event_idx)
|
|
|
|
{
|
|
|
|
const m::event::fetch &event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto _opts(opts);
|
|
|
|
_opts.op = db::op::DELETE;
|
|
|
|
_opts.event_idx = event_idx;
|
|
|
|
dbs::write(txn, event, _opts);
|
|
|
|
++deleted;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
ssize_t added(0);
|
2019-08-22 14:13:59 +02:00
|
|
|
history.for_each([&opts, &txn, &added, &room_id]
|
2019-08-22 13:32:15 +02:00
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
const m::event::fetch &event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
|
|
|
return true;
|
|
|
|
|
2019-08-22 14:13:59 +02:00
|
|
|
const auto &[pass, fail]
|
|
|
|
{
|
|
|
|
auth::check_present(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!pass)
|
|
|
|
{
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "%s fails for present state in %s :%s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
what(fail),
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
auto _opts(opts);
|
|
|
|
_opts.op = db::op::SET;
|
|
|
|
_opts.event_idx = event_idx;
|
|
|
|
dbs::write(txn, event, _opts);
|
|
|
|
++added;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
log, "Present state of %s @ %s rebuild complete with %zu size:%s del:%zd add:%zd (%zd)",
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{event_id},
|
|
|
|
txn.size(),
|
|
|
|
pretty(iec(txn.bytes())),
|
|
|
|
deleted,
|
|
|
|
added,
|
|
|
|
(added - deleted),
|
|
|
|
};
|
|
|
|
|
|
|
|
txn();
|
|
|
|
}
|
|
|
|
|
2019-05-07 05:47:12 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::is(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
m::get(event_idx, "state_key", [&ret]
|
|
|
|
(const string_view &state_key)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::is(std::nothrow_t,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
m::get(std::nothrow, event_idx, "state_key", [&ret]
|
|
|
|
(const string_view &state_key)
|
|
|
|
{
|
|
|
|
ret = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
size_t
|
2019-08-16 11:16:00 +02:00
|
|
|
ircd::m::room::state::purge_replaced(const room::id &room_id)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
db::txn txn
|
|
|
|
{
|
|
|
|
*m::dbs::events
|
|
|
|
};
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
size_t ret(0);
|
|
|
|
m::room::messages it
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-08-16 11:16:00 +02:00
|
|
|
room_id, uint64_t(0)
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
if(!it)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for(; it; ++it)
|
|
|
|
{
|
2019-08-16 12:00:51 +02:00
|
|
|
const m::event::idx &event_idx(it.event_idx());
|
2019-05-06 03:04:56 +02:00
|
|
|
if(!m::get(std::nothrow, event_idx, "state_key", [](const auto &) {}))
|
|
|
|
continue;
|
|
|
|
|
2019-05-10 07:43:28 +02:00
|
|
|
if(!m::event::refs(event_idx).count(m::dbs::ref::NEXT_STATE))
|
2019-05-06 03:04:56 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// TODO: erase event
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 06:36:00 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::present(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
event::keys::include { "room_id", "type", "state_key" },
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
at<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto state_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, at<"type"_>(event), at<"state_key"_>(event))
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(event_idx);
|
|
|
|
return state_idx == event_idx;
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::state::prefetch(const state &state,
|
|
|
|
const string_view &type,
|
|
|
|
const event::idx_range &range)
|
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
const m::event::fetch::opts &fopts
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
state.fopts?
|
|
|
|
*state.fopts:
|
|
|
|
m::event::fetch::default_opts
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
size_t ret{0};
|
|
|
|
state.for_each(type, m::event::closure_idx{[&ret, &fopts, &range]
|
|
|
|
(const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
if(event_idx < range.first)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(range.second && event_idx > range.second)
|
|
|
|
return;
|
|
|
|
|
2019-08-20 06:17:42 +02:00
|
|
|
ret += m::prefetch(event_idx, fopts);
|
2019-05-06 03:04:56 +02:00
|
|
|
}});
|
|
|
|
|
2019-08-25 04:45:26 +02:00
|
|
|
ctx::yield();
|
2019-05-06 03:04:56 +02:00
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::room::state::prev(const event::idx &event_idx)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
event::idx ret{0};
|
|
|
|
prev(event_idx, [&ret]
|
|
|
|
(const event::idx &event_idx)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
if(event_idx > ret)
|
|
|
|
ret = event_idx;
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::room::state::next(const event::idx &event_idx)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
event::idx ret{0};
|
|
|
|
next(event_idx, [&ret]
|
|
|
|
(const event::idx &event_idx)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
if(event_idx > ret)
|
|
|
|
ret = event_idx;
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::next(const event::idx &event_idx,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
const m::event::refs refs
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
event_idx
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-10 07:43:28 +02:00
|
|
|
return refs.for_each(dbs::ref::NEXT_STATE, [&closure]
|
2019-05-06 03:04:56 +02:00
|
|
|
(const event::idx &event_idx, const dbs::ref &ref)
|
|
|
|
{
|
2019-05-10 07:43:28 +02:00
|
|
|
assert(ref == dbs::ref::NEXT_STATE);
|
2019-05-06 03:04:56 +02:00
|
|
|
return closure(event_idx);
|
|
|
|
});
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::prev(const event::idx &event_idx,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
const m::event::refs refs
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
event_idx
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
return refs.for_each(dbs::ref::PREV_STATE, [&closure]
|
|
|
|
(const event::idx &event_idx, const dbs::ref &ref)
|
|
|
|
{
|
|
|
|
assert(ref == dbs::ref::PREV_STATE);
|
|
|
|
return closure(event_idx);
|
|
|
|
});
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::create(const id::room &room_id,
|
|
|
|
const id::user &creator,
|
|
|
|
const string_view &preset)
|
|
|
|
{
|
|
|
|
return create(createroom
|
|
|
|
{
|
|
|
|
{ "room_id", room_id },
|
|
|
|
{ "creator", creator },
|
|
|
|
{ "preset", preset },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::create(const createroom &c,
|
|
|
|
json::stack::array *const &errors)
|
|
|
|
{
|
|
|
|
using prototype = room (const createroom &, json::stack::array *const &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
2019-08-26 04:11:29 +02:00
|
|
|
"m_room_create", "ircd::m::create"
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return call(c, errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::join(const id::room_alias &room_alias,
|
|
|
|
const id::user &user_id)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const id::room_alias &, const id::user &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-08-26 03:17:00 +02:00
|
|
|
"m_room_join", "ircd::m::join"
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room_alias, user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::join(const room &room,
|
|
|
|
const id::user &user_id)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::room &, const id::user &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-08-26 03:17:00 +02:00
|
|
|
"m_room_join", "ircd::m::join"
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room, user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::leave(const room &room,
|
|
|
|
const id::user &user_id)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::room &, const id::user &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
2019-08-26 03:39:01 +02:00
|
|
|
"m_room_leave", "ircd::m::leave"
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return function(room, user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::invite(const room &room,
|
|
|
|
const id::user &target,
|
|
|
|
const id::user &sender)
|
|
|
|
{
|
|
|
|
json::iov content;
|
|
|
|
return invite(room, target, sender, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::invite(const room &room,
|
|
|
|
const id::user &target,
|
|
|
|
const id::user &sender,
|
|
|
|
json::iov &content)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::room &, const id::user &, const id::user &, json::iov &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"client_rooms", "ircd::m::invite"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(room, target, sender, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::redact(const room &room,
|
|
|
|
const id::user &sender,
|
|
|
|
const id::event &event_id,
|
|
|
|
const string_view &reason)
|
|
|
|
{
|
|
|
|
using prototype = event::id::buf (const m::room &, const id::user &, const id::event &, const string_view &);
|
|
|
|
|
|
|
|
static mods::import<prototype> function
|
|
|
|
{
|
|
|
|
"client_rooms", "redact__"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(room, sender, event_id, reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::notice(const room &room,
|
|
|
|
const string_view &body)
|
|
|
|
{
|
|
|
|
return message(room, me.user_id, body, "m.notice");
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::notice(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &body)
|
|
|
|
{
|
|
|
|
return message(room, sender, body, "m.notice");
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::msghtml(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &html,
|
|
|
|
const string_view &alt,
|
|
|
|
const string_view &msgtype)
|
|
|
|
{
|
|
|
|
return message(room, sender,
|
|
|
|
{
|
|
|
|
{ "msgtype", msgtype },
|
|
|
|
{ "format", "org.matrix.custom.html" },
|
|
|
|
{ "body", { alt?: html, json::STRING } },
|
|
|
|
{ "formatted_body", { html, json::STRING } },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::message(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &body,
|
|
|
|
const string_view &msgtype)
|
|
|
|
{
|
|
|
|
return message(room, sender,
|
|
|
|
{
|
|
|
|
{ "body", { body, json::STRING } },
|
|
|
|
{ "msgtype", { msgtype, json::STRING } },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::message(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const json::members &contents)
|
|
|
|
{
|
|
|
|
return send(room, sender, "m.room.message", contents);
|
|
|
|
}
|
|
|
|
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2019-05-01 00:24:22 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
__attribute__((stack_protect))
|
|
|
|
ircd::m::send(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const json::members &contents)
|
|
|
|
{
|
|
|
|
const size_t contents_count
|
|
|
|
{
|
|
|
|
std::min(contents.size(), json::object::max_sorted_members)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov _content;
|
|
|
|
json::iov::push content[contents_count]; // 48B each
|
|
|
|
return send(room, sender, type, state_key, make_iov(_content, content, contents_count, contents));
|
|
|
|
}
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2019-05-01 00:24:22 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
__attribute__((stack_protect))
|
|
|
|
ircd::m::send(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const json::object &contents)
|
|
|
|
{
|
|
|
|
const size_t contents_count
|
|
|
|
{
|
|
|
|
std::min(contents.size(), json::object::max_sorted_members)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov _content;
|
|
|
|
json::iov::push content[contents_count]; // 48B each
|
|
|
|
return send(room, sender, type, state_key, make_iov(_content, content, contents_count, contents));
|
|
|
|
}
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2019-05-01 00:24:22 +02:00
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::send(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const json::iov &content)
|
|
|
|
{
|
2019-08-26 04:28:06 +02:00
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-08-26 04:28:06 +02:00
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "type", type }},
|
|
|
|
{ event, { "state_key", state_key }},
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-08-26 04:28:06 +02:00
|
|
|
return commit(room, event, content);
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2019-05-01 00:24:22 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
__attribute__((stack_protect))
|
|
|
|
ircd::m::send(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const json::members &contents)
|
|
|
|
{
|
|
|
|
const size_t contents_count
|
|
|
|
{
|
|
|
|
std::min(contents.size(), json::object::max_sorted_members)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov _content;
|
|
|
|
json::iov::push content[contents_count]; // 48B each
|
|
|
|
return send(room, sender, type, make_iov(_content, content, contents_count, contents));
|
|
|
|
}
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstack-usage="
|
2019-05-01 00:24:22 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
__attribute__((stack_protect))
|
|
|
|
ircd::m::send(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const json::object &contents)
|
|
|
|
{
|
|
|
|
const size_t contents_count
|
|
|
|
{
|
|
|
|
std::min(contents.size(), json::object::max_sorted_members)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov _content;
|
|
|
|
json::iov::push content[contents_count]; // 48B each
|
|
|
|
return send(room, sender, type, make_iov(_content, content, contents_count, contents));
|
|
|
|
}
|
2019-06-23 01:27:22 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2019-05-01 00:24:22 +02:00
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::send(const room &room,
|
|
|
|
const id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const json::iov &content)
|
|
|
|
{
|
2019-08-26 04:28:06 +02:00
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-08-26 04:28:06 +02:00
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "type", type }},
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-08-26 04:28:06 +02:00
|
|
|
return commit(room, event, content);
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::commit(const room &room,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &contents)
|
|
|
|
{
|
2019-07-17 21:15:17 +02:00
|
|
|
// Set the room_id on the iov
|
|
|
|
json::iov::push room_id
|
|
|
|
{
|
|
|
|
event, { "room_id", room.room_id }
|
|
|
|
};
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
vm::copts opts
|
|
|
|
{
|
|
|
|
room.copts?
|
|
|
|
*room.copts:
|
|
|
|
vm::default_copts
|
|
|
|
};
|
|
|
|
|
|
|
|
// Some functionality on this server may create an event on behalf
|
|
|
|
// of remote users. It's safe for us to mask this here, but eval'ing
|
|
|
|
// this event in any replay later will require special casing.
|
|
|
|
opts.non_conform |= event::conforms::MISMATCH_ORIGIN_SENDER;
|
|
|
|
|
|
|
|
// Don't need this here
|
|
|
|
opts.verify = false;
|
|
|
|
|
2019-07-17 21:15:17 +02:00
|
|
|
return vm::eval
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-07-17 21:15:17 +02:00
|
|
|
event, contents, opts
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int64_t, ircd::m::event::idx>
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::twain(const room &room)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
std::pair<int64_t, m::event::idx> ret
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
-1, 0
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
rfor_each_depth_gap(room, [&ret]
|
|
|
|
(const auto &range, const auto &event_idx)
|
|
|
|
{
|
|
|
|
ret.first = range.first - 1;
|
|
|
|
ret.second = event_idx;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int64_t, ircd::m::event::idx>
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::sounding(const room &room)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
std::pair<int64_t, m::event::idx> ret
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
-1, 0
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
rfor_each_depth_gap(room, [&ret]
|
|
|
|
(const auto &range, const auto &event_idx)
|
|
|
|
{
|
|
|
|
ret.first = range.second;
|
|
|
|
ret.second = event_idx;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int64_t, ircd::m::event::idx>
|
2019-05-10 09:20:02 +02:00
|
|
|
ircd::m::hazard(const room &room)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
std::pair<int64_t, m::event::idx> ret {0, 0};
|
|
|
|
for_each_depth_gap(room, [&ret]
|
|
|
|
(const auto &range, const auto &event_idx)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
ret.first = range.first;
|
|
|
|
ret.second = event_idx;
|
|
|
|
return false;
|
|
|
|
});
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-05-10 08:43:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sounding(const room &room,
|
|
|
|
const depth_range_closure &closure)
|
|
|
|
{
|
|
|
|
bool ret(true);
|
|
|
|
int64_t depth(-1);
|
|
|
|
rfor_each_depth_gap(room, [&depth, &ret, &closure]
|
|
|
|
(const auto &range, const auto &event_idx)
|
|
|
|
{
|
|
|
|
if(depth != -1 && depth != range.second)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
depth = range.second;
|
|
|
|
if(!closure(range, event_idx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::rfor_each_depth_gap(const room &room,
|
|
|
|
const depth_range_closure &closure)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
room::messages it
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
room
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
if(!it)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
event::idx idx{0};
|
|
|
|
for(depth_range range{0L, it.depth()}; it; --it)
|
|
|
|
{
|
|
|
|
range.first = it.depth();
|
|
|
|
if(range.first == range.second)
|
|
|
|
{
|
|
|
|
idx = it.event_idx();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
--range.second;
|
|
|
|
if(range.first == range.second)
|
|
|
|
{
|
|
|
|
idx = it.event_idx();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!closure({range.first+1, range.second+1}, idx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
range.second = range.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::for_each_depth_gap(const room &room,
|
|
|
|
const depth_range_closure &closure)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
room::messages it
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
room, int64_t(0L)
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
for(depth_range range{0L, 0L}; it; ++it)
|
|
|
|
{
|
|
|
|
range.second = it.depth();
|
|
|
|
if(range.first == range.second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
++range.first;
|
|
|
|
if(range.first == range.second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(!closure(range, it.event_idx()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
range.first = range.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::count_since(const m::event::id &a,
|
|
|
|
const m::event::id &b)
|
|
|
|
{
|
|
|
|
return count_since(index(a), index(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::count_since(const m::event::idx &a,
|
|
|
|
const m::event::idx &b)
|
|
|
|
{
|
|
|
|
// Get the room_id from b here; a might not be in the same room but downstream
|
|
|
|
// the counter seeks to a in the given room and will properly fail there.
|
|
|
|
room::id::buf room_id
|
|
|
|
{
|
|
|
|
m::get(std::max(a, b), "room_id", room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
return count_since(room_id, a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::count_since(const room &room,
|
|
|
|
const m::event::id &a,
|
|
|
|
const m::event::id &b)
|
|
|
|
{
|
|
|
|
return count_since(room, index(a), index(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::count_since(const m::room &room,
|
|
|
|
const m::event::idx &a,
|
|
|
|
const m::event::idx &b)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
m::room::messages it
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
room
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
assert(a <= b);
|
|
|
|
it.seek_idx(a);
|
|
|
|
|
|
|
|
if(!it && !exists(room))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Cannot find room '%s' to count events in",
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
else if(!it)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Event @ idx:%lu or idx:%lu not found in room '%s' or at all",
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t ret{0};
|
|
|
|
// Hit the iterator once first otherwise the count will always increment
|
|
|
|
// to `1` erroneously when it ought to show `0`.
|
|
|
|
for(++it; it && it.event_idx() < b; ++it, ++ret);
|
|
|
|
return ret;
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::room_id(const id::room_alias &room_alias)
|
|
|
|
{
|
|
|
|
char buf[m::id::MAX_SIZE + 1];
|
|
|
|
static_assert(sizeof(buf) <= 256);
|
|
|
|
return room_id(buf, room_alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::room_id(const string_view &room_id_or_alias)
|
|
|
|
{
|
|
|
|
char buf[m::id::MAX_SIZE + 1];
|
|
|
|
static_assert(sizeof(buf) <= 256);
|
|
|
|
return room_id(buf, room_id_or_alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::room_id(const mutable_buffer &out,
|
|
|
|
const string_view &room_id_or_alias)
|
|
|
|
{
|
|
|
|
switch(m::sigil(room_id_or_alias))
|
|
|
|
{
|
|
|
|
case id::ROOM:
|
|
|
|
return id::room{out, room_id_or_alias};
|
|
|
|
|
|
|
|
case id::USER:
|
|
|
|
{
|
|
|
|
const m::user::room user_room(room_id_or_alias);
|
|
|
|
return string_view{data(out), copy(out, user_room.room_id)};
|
|
|
|
}
|
|
|
|
|
2019-08-12 07:51:26 +02:00
|
|
|
case id::NODE:
|
|
|
|
{
|
|
|
|
const m::node node(lstrip(room_id_or_alias, ':'));
|
|
|
|
return node.room_id(out);
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
default:
|
|
|
|
return room_id(out, id::room_alias{room_id_or_alias});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::room_id(const mutable_buffer &out,
|
|
|
|
const id::room_alias &room_alias)
|
|
|
|
{
|
|
|
|
room::id ret;
|
|
|
|
room::aliases::cache::get(room_alias, [&out, &ret]
|
|
|
|
(const room::id &room_id)
|
|
|
|
{
|
|
|
|
ret = string_view { data(out), copy(out, room_id) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::exists(const id::room_alias &room_alias,
|
|
|
|
const bool &remote_query)
|
|
|
|
{
|
|
|
|
if(room::aliases::cache::has(room_alias))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!remote_query)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return room::aliases::cache::get(std::nothrow, room_alias, [](const room::id &room_id) {});
|
|
|
|
}
|
|
|
|
|
2018-04-06 06:57:17 +02:00
|
|
|
int64_t
|
2018-02-09 05:59:48 +01:00
|
|
|
ircd::m::depth(const id::room &room_id)
|
|
|
|
{
|
2018-04-18 23:55:38 +02:00
|
|
|
return std::get<int64_t>(top(room_id));
|
2018-02-09 05:59:48 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 04:37:09 +01:00
|
|
|
int64_t
|
|
|
|
ircd::m::depth(std::nothrow_t,
|
|
|
|
const id::room &room_id)
|
|
|
|
{
|
2018-04-19 00:13:17 +02:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
dbs::room_events.begin(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_events_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::get<0>(part);
|
2018-02-20 04:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:09:13 +02:00
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::head_idx(const id::room &room_id)
|
|
|
|
{
|
|
|
|
return std::get<event::idx>(top(room_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::head_idx(std::nothrow_t,
|
|
|
|
const id::room &room_id)
|
|
|
|
{
|
2018-04-19 00:13:17 +02:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
dbs::room_events.begin(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_events_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::get<1>(part);
|
2018-04-19 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:04:46 +02:00
|
|
|
ircd::m::event::id::buf
|
2018-02-09 05:59:48 +01:00
|
|
|
ircd::m::head(const id::room &room_id)
|
|
|
|
{
|
2018-04-18 23:55:38 +02:00
|
|
|
return std::get<event::id::buf>(top(room_id));
|
2018-02-09 05:59:48 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:04:46 +02:00
|
|
|
ircd::m::event::id::buf
|
2018-02-20 04:37:09 +01:00
|
|
|
ircd::m::head(std::nothrow_t,
|
|
|
|
const id::room &room_id)
|
|
|
|
{
|
2018-04-18 23:55:38 +02:00
|
|
|
return std::get<event::id::buf>(top(std::nothrow, room_id));
|
2018-02-20 04:37:09 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 00:04:46 +02:00
|
|
|
std::tuple<ircd::m::event::id::buf, int64_t, ircd::m::event::idx>
|
2018-04-06 06:57:17 +02:00
|
|
|
ircd::m::top(const id::room &room_id)
|
2018-02-20 04:37:09 +01:00
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
2018-04-06 06:57:17 +02:00
|
|
|
top(std::nothrow, room_id)
|
2018-02-20 04:37:09 +01:00
|
|
|
};
|
|
|
|
|
2018-04-18 23:55:38 +02:00
|
|
|
if(std::get<int64_t>(ret) == -1)
|
2018-04-18 04:14:39 +02:00
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"No head for room %s", string_view{room_id}
|
|
|
|
};
|
2018-02-20 04:37:09 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-19 00:04:46 +02:00
|
|
|
std::tuple<ircd::m::event::id::buf, int64_t, ircd::m::event::idx>
|
2018-04-06 06:57:17 +02:00
|
|
|
ircd::m::top(std::nothrow_t,
|
|
|
|
const id::room &room_id)
|
2018-02-09 05:59:48 +01:00
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
dbs::room_events.begin(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
2018-04-06 06:57:17 +02:00
|
|
|
return
|
|
|
|
{
|
2018-04-19 00:04:46 +02:00
|
|
|
event::id::buf{}, -1, 0
|
2018-04-06 06:57:17 +02:00
|
|
|
};
|
2018-02-09 05:59:48 +01:00
|
|
|
|
|
|
|
const auto part
|
|
|
|
{
|
2018-04-18 23:55:38 +02:00
|
|
|
dbs::room_events_key(it->first)
|
2018-02-09 05:59:48 +01:00
|
|
|
};
|
|
|
|
|
2018-04-19 00:04:46 +02:00
|
|
|
const int64_t &depth
|
2018-04-06 06:57:17 +02:00
|
|
|
{
|
2018-04-19 00:04:46 +02:00
|
|
|
int64_t(std::get<0>(part))
|
2018-04-18 23:55:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx &event_idx
|
|
|
|
{
|
|
|
|
std::get<1>(part)
|
2018-04-06 06:57:17 +02:00
|
|
|
};
|
2018-04-18 04:14:39 +02:00
|
|
|
|
2018-04-19 00:04:46 +02:00
|
|
|
std::tuple<event::id::buf, int64_t, event::idx> ret
|
|
|
|
{
|
|
|
|
event::id::buf{}, depth, event_idx
|
|
|
|
};
|
|
|
|
|
2019-07-06 07:01:34 +02:00
|
|
|
m::event_id(event_idx, std::nothrow, [&ret]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
2018-04-18 23:55:38 +02:00
|
|
|
std::get<event::id::buf>(ret) = event_id;
|
2018-04-18 04:14:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2018-02-09 05:59:48 +01:00
|
|
|
}
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
ircd::m::id::user::buf
|
|
|
|
ircd::m::any_user(const room &room,
|
|
|
|
const string_view &host,
|
|
|
|
const string_view &membership)
|
|
|
|
{
|
|
|
|
user::id::buf ret;
|
|
|
|
const room::members members{room};
|
|
|
|
members.for_each(membership, [&host, &ret]
|
|
|
|
(const auto &user_id, const auto &event_idx)
|
|
|
|
{
|
|
|
|
if(host && user_id.host() != host)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret = user_id;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::membership(const mutable_buffer &out,
|
|
|
|
const room &room,
|
|
|
|
const user::id &user_id)
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, "m.room.member", user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
return room::members::membership(out, event_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive the join_rule of the room into buffer of sufficient size.
|
|
|
|
/// The protocol does not specify a join_rule string longer than 7
|
|
|
|
/// characters but do be considerate of the future. This function
|
|
|
|
/// properly defaults the string as per the protocol spec.
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::join_rule(const mutable_buffer &out,
|
|
|
|
const room &room)
|
|
|
|
{
|
|
|
|
static const string_view default_join_rule
|
|
|
|
{
|
|
|
|
"invite"
|
|
|
|
};
|
|
|
|
|
|
|
|
string_view ret
|
|
|
|
{
|
|
|
|
default_join_rule
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::keys::include keys
|
|
|
|
{
|
|
|
|
"content"
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
keys, room.fopts? room.fopts->gopts : db::gopts{}
|
|
|
|
};
|
|
|
|
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
state.get(std::nothrow, "m.room.join_rules", "", [&ret, &out]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
const auto &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string &rule
|
|
|
|
{
|
|
|
|
content.get("join_rule", default_join_rule)
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = string_view
|
|
|
|
{
|
|
|
|
data(out), copy(out, rule)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:03:06 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::version(const mutable_buffer &buf,
|
|
|
|
const room &room)
|
2018-09-13 08:21:40 +02:00
|
|
|
{
|
2019-03-06 20:03:06 +01:00
|
|
|
const auto ret
|
2018-09-13 08:21:40 +02:00
|
|
|
{
|
2019-03-06 20:03:06 +01:00
|
|
|
version(buf, room, std::nothrow)
|
2018-09-13 08:21:40 +02:00
|
|
|
};
|
|
|
|
|
2019-03-06 20:03:06 +01:00
|
|
|
if(!ret)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Failed to find room %s to query its version",
|
|
|
|
string_view{room.room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::version(const mutable_buffer &buf,
|
|
|
|
const room &room,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
|
|
|
const auto event_idx
|
2018-09-13 08:21:40 +02:00
|
|
|
{
|
2019-03-06 20:03:06 +01:00
|
|
|
room.get(std::nothrow, "m.room.create", "")
|
2018-09-13 08:21:40 +02:00
|
|
|
};
|
|
|
|
|
2019-07-10 10:30:25 +02:00
|
|
|
string_view ret
|
2019-03-06 20:03:06 +01:00
|
|
|
{
|
2019-07-10 10:30:25 +02:00
|
|
|
strlcpy{buf, "1"_sv}
|
|
|
|
};
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
m::get(std::nothrow, event_idx, "content", [&buf, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &version
|
2019-07-10 10:30:25 +02:00
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
content.get("room_version", "1")
|
|
|
|
};
|
2019-07-10 10:30:25 +02:00
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
ret = strlcpy
|
|
|
|
{
|
|
|
|
buf, version
|
|
|
|
};
|
|
|
|
});
|
2018-09-13 08:21:40 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-19 22:59:09 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::type(const mutable_buffer &buf,
|
|
|
|
const room &room)
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
room.get(std::nothrow, "m.room.create", "")
|
|
|
|
};
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
m::get(std::nothrow, event_idx, "content", [&buf, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &type
|
2019-07-19 22:59:09 +02:00
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
content.get("type")
|
|
|
|
};
|
2019-07-19 22:59:09 +02:00
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
ret = strlcpy
|
|
|
|
{
|
|
|
|
buf, type
|
|
|
|
};
|
|
|
|
});
|
2019-07-19 22:59:09 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-26 01:25:18 +02:00
|
|
|
ircd::m::id::user::buf
|
|
|
|
ircd::m::creator(const id::room &room_id)
|
|
|
|
{
|
|
|
|
// Query the sender field of the event to get the creator. This is for
|
|
|
|
// future compatibility if the content.creator field gets eliminated.
|
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
event::keys::include {"sender"}
|
2018-10-26 01:25:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room_id, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
id::user::buf ret;
|
|
|
|
state.get("m.room.create", "", [&ret]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
ret = user::id
|
|
|
|
{
|
|
|
|
json::get<"sender"_>(event)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
//
|
|
|
|
// boolean suite
|
|
|
|
//
|
|
|
|
|
|
|
|
/// The only joined members are from our origin (local only). This indicates
|
|
|
|
/// we won't have any other federation servers to query for room data, nor do
|
|
|
|
/// we need to broadcast events to the federation. This is not an authority
|
|
|
|
/// about a room's type or ability to federate. Returned value changes to false
|
|
|
|
/// when another origin joins.
|
|
|
|
bool
|
|
|
|
ircd::m::local_only(const room &room)
|
|
|
|
{
|
|
|
|
const room::origins origins
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
return origins.empty() || origins.only(my_host());
|
|
|
|
}
|
|
|
|
|
2018-09-13 08:26:28 +02:00
|
|
|
bool
|
2019-08-14 10:01:46 +02:00
|
|
|
ircd::m::visible(const room &room,
|
|
|
|
const string_view &mxid,
|
|
|
|
const event *const &event)
|
2018-09-13 08:26:28 +02:00
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
if(event)
|
|
|
|
return m::visible(*event, mxid);
|
|
|
|
|
|
|
|
const m::event event_
|
2018-09-13 08:26:28 +02:00
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
json::members
|
2018-09-13 08:26:28 +02:00
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
{ "event_id", room.event_id },
|
|
|
|
{ "room_id", room.room_id },
|
2018-09-13 08:26:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
return m::visible(event_, mxid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test of the join_rule of the room is the argument.
|
|
|
|
bool
|
|
|
|
ircd::m::join_rule(const room &room,
|
|
|
|
const string_view &rule)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
return join_rule(buf, room) == rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::membership(const room &room,
|
|
|
|
const user::id &user_id,
|
|
|
|
const string_view &membership)
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, "m.room.member", user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
return room::members::membership(event_idx, membership);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::creator(const room::id &room_id,
|
|
|
|
const user::id &user_id)
|
|
|
|
{
|
|
|
|
const auto creator_user_id
|
|
|
|
{
|
|
|
|
creator(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
return creator_user_id == user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::federated(const id::room &room_id)
|
|
|
|
{
|
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
event::keys::include { "content" },
|
|
|
|
};
|
|
|
|
|
2018-09-13 08:26:28 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room_id, &fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret;
|
|
|
|
state.get("m.room.create", "", [&ret]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
ret = json::get<"content"_>(event).get("m.federate", true);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-19 23:27:34 +02:00
|
|
|
/// Determine if this is an internal room. The following must be satisfied:
|
|
|
|
///
|
|
|
|
/// - The room was created by this origin.
|
|
|
|
/// - The creator was the server itself, not any other user.
|
|
|
|
bool
|
|
|
|
ircd::m::internal(const id::room &room_id)
|
|
|
|
{
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!my(room))
|
|
|
|
return false;
|
|
|
|
|
2019-08-17 17:29:47 +02:00
|
|
|
if(!exists(room))
|
|
|
|
return false;
|
|
|
|
|
2019-07-19 23:27:34 +02:00
|
|
|
if(!creator(room, m::me))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const id::room &room_id)
|
|
|
|
{
|
2018-02-09 06:01:26 +01:00
|
|
|
const auto it
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-09 06:01:26 +01:00
|
|
|
dbs::room_events.begin(room_id)
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2018-02-09 06:01:26 +01:00
|
|
|
return bool(it);
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-08 20:38:29 +01:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const room &room)
|
|
|
|
{
|
|
|
|
return exists(room.room_id);
|
|
|
|
}
|
|
|
|
|
2019-08-14 10:01:46 +02:00
|
|
|
//
|
|
|
|
// util
|
|
|
|
//
|
|
|
|
|
2019-04-07 23:50:27 +02:00
|
|
|
bool
|
|
|
|
ircd::m::operator==(const room &a, const room &b)
|
|
|
|
{
|
|
|
|
return !(a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-06-24 07:36:08 +02:00
|
|
|
ircd::m::operator!=(const room &a_, const room &b_)
|
2019-04-07 23:50:27 +02:00
|
|
|
{
|
2019-06-24 07:36:08 +02:00
|
|
|
const string_view &a{a_.room_id}, &b{b_.room_id};
|
|
|
|
return a != b;
|
2019-04-07 23:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::operator!(const room &a)
|
|
|
|
{
|
|
|
|
return !a.room_id;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
|
|
|
ircd::m::my(const room &room)
|
|
|
|
{
|
|
|
|
return my(room.room_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room
|
|
|
|
//
|
|
|
|
|
2019-02-05 03:11:05 +01:00
|
|
|
/// A room index is just the event::idx of its create event.
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::index(const room::id &room_id)
|
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
index(room_id, std::nothrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"No index for room %s", string_view{room_id}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::index(const room::id &room_id,
|
|
|
|
std::nothrow_t)
|
|
|
|
{
|
|
|
|
uint64_t depth{0};
|
|
|
|
room::messages it
|
|
|
|
{
|
|
|
|
room_id, depth
|
|
|
|
};
|
|
|
|
|
|
|
|
return it? it.event_idx() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room::room
|
|
|
|
//
|
|
|
|
|
2019-08-18 17:07:38 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(event::closure_idx_bool{[&ret]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::count(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(type, event::closure_idx_bool{[&ret]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::count(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(type, event::closure_idx_bool{[&state_key, &ret]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
ret += query(std::nothrow, event_idx, "state_key", [&state_key]
|
|
|
|
(const string_view &_state_key) -> bool
|
|
|
|
{
|
|
|
|
return state_key == _state_key;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-16 04:17:09 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::has(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
2019-02-13 23:40:02 +01:00
|
|
|
return get(std::nothrow, type, event::closure{});
|
2018-02-16 04:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::get(const string_view &type,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, type, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"No events of type '%s' found in '%s'",
|
|
|
|
type,
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{false};
|
2018-04-12 11:14:10 +02:00
|
|
|
for_each(type, event::closure_bool{[&ret, &closure]
|
|
|
|
(const event &event)
|
2018-02-16 04:17:09 +01:00
|
|
|
{
|
2018-04-12 11:14:10 +02:00
|
|
|
if(closure)
|
|
|
|
closure(event);
|
2018-02-16 04:17:09 +01:00
|
|
|
|
2018-04-12 11:14:10 +02:00
|
|
|
ret = true;
|
|
|
|
return false;
|
|
|
|
}});
|
2018-02-16 04:17:09 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-12 03:03:39 +02:00
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::get(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const event::idx ret
|
|
|
|
{
|
|
|
|
get(std::nothrow, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"No events of type '%s' found in '%s'",
|
|
|
|
type,
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::get(std::nothrow_t,
|
|
|
|
const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
event::idx ret{0};
|
|
|
|
for_each(type, event::closure_idx_bool{[&ret]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
ret = event_idx;
|
|
|
|
return false;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:40:02 +01:00
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::get(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return state(*this).get(type, state_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return state(*this).get(std::nothrow, type, state_key);
|
|
|
|
}
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
void
|
2018-02-11 04:50:53 +01:00
|
|
|
ircd::m::room::get(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const state state{*this};
|
|
|
|
state.get(type, state_key, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const state state{*this};
|
|
|
|
return state.get(std::nothrow, type, state_key, closure);
|
|
|
|
}
|
|
|
|
|
2018-02-11 06:42:16 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::has(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const state state{*this};
|
|
|
|
return state.has(type, state_key);
|
|
|
|
}
|
|
|
|
|
2018-04-12 11:14:10 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::for_each(const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
for_each(string_view{}, closure);
|
2018-04-12 11:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool
|
2018-04-12 11:14:10 +02:00
|
|
|
ircd::m::room::for_each(const event::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
return for_each(string_view{}, closure);
|
2018-04-12 11:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::for_each(const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
for_each(string_view{}, closure);
|
2018-04-12 11:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool
|
2018-04-12 11:14:10 +02:00
|
|
|
ircd::m::room::for_each(const event::id::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
return for_each(string_view{}, closure);
|
|
|
|
}
|
2018-04-12 11:14:10 +02:00
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::for_each(const event::closure_idx &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(string_view{}, closure);
|
|
|
|
}
|
2018-04-12 11:14:10 +02:00
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::for_each(const event::closure_idx_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, closure);
|
2018-04-12 11:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::for_each(const string_view &type,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(type, event::closure_bool{[&closure]
|
|
|
|
(const event &event)
|
|
|
|
{
|
|
|
|
closure(event);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool
|
2018-04-12 11:14:10 +02:00
|
|
|
ircd::m::room::for_each(const string_view &type,
|
|
|
|
const event::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
2019-01-16 21:12:59 +01:00
|
|
|
fopts? *fopts : event::fetch::default_opts
|
2018-05-20 13:35:17 +02:00
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return for_each(type, event::closure_idx_bool{[&closure, &event]
|
|
|
|
(const event::idx &event_idx)
|
2018-04-12 11:14:10 +02:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(!seek(event, event_idx, std::nothrow))
|
2018-04-12 11:14:10 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return closure(event);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::for_each(const string_view &type,
|
|
|
|
const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(type, event::id::closure_bool{[&closure]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
closure(event_id);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool
|
2018-04-12 11:14:10 +02:00
|
|
|
ircd::m::room::for_each(const string_view &type,
|
|
|
|
const event::id::closure_bool &closure)
|
|
|
|
const
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
|
|
|
return for_each(type, event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
bool ret{true};
|
2019-07-06 07:01:34 +02:00
|
|
|
m::event_id(idx, std::nothrow, [&ret, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
ret = closure(event_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::for_each(const string_view &type,
|
|
|
|
const event::closure_idx &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(type, event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
closure(idx);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::for_each(const string_view &type,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
const
|
2018-04-12 11:14:10 +02:00
|
|
|
{
|
|
|
|
static constexpr auto idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "type"_>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_column.at(idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
messages it{*this};
|
|
|
|
for(; it; --it)
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
const auto &event_idx
|
2018-04-12 11:14:10 +02:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
it.event_idx()
|
2018-04-12 11:14:10 +02:00
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool match
|
2018-04-12 11:14:10 +02:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
empty(type) // allow empty type to always match and bypass query
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!match)
|
|
|
|
column(byte_view<string_view>(event_idx), std::nothrow, [&match, &type]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
match = value == type;
|
|
|
|
});
|
2018-04-12 11:14:10 +02:00
|
|
|
|
|
|
|
if(match)
|
2018-04-18 04:14:39 +02:00
|
|
|
if(!closure(event_idx))
|
|
|
|
return false;
|
2018-04-12 11:14:10 +02:00
|
|
|
}
|
2018-04-18 04:14:39 +02:00
|
|
|
|
|
|
|
return true;
|
2018-04-12 11:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-02-11 04:50:53 +01:00
|
|
|
//
|
|
|
|
// room::messages
|
|
|
|
//
|
|
|
|
|
2018-05-22 02:36:16 +02:00
|
|
|
ircd::m::room::messages::messages(const m::room &room,
|
|
|
|
const event::fetch::opts *const &fopts)
|
2018-02-13 23:25:01 +01:00
|
|
|
:room{room}
|
2018-05-22 02:36:16 +02:00
|
|
|
,_event
|
|
|
|
{
|
|
|
|
fopts?
|
2019-01-16 21:12:59 +01:00
|
|
|
*fopts:
|
|
|
|
room.fopts?
|
|
|
|
*room.fopts:
|
|
|
|
event::fetch::default_opts
|
2018-05-22 02:36:16 +02:00
|
|
|
}
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2019-08-16 11:05:34 +02:00
|
|
|
assert(room.room_id);
|
|
|
|
|
2018-06-03 23:50:42 +02:00
|
|
|
if(room.event_id)
|
|
|
|
seek(room.event_id);
|
|
|
|
else
|
|
|
|
seek();
|
2018-02-11 04:50:53 +01:00
|
|
|
}
|
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
ircd::m::room::messages::messages(const m::room &room,
|
2018-05-22 02:36:16 +02:00
|
|
|
const event::id &event_id,
|
|
|
|
const event::fetch::opts *const &fopts)
|
2018-02-13 23:25:01 +01:00
|
|
|
:room{room}
|
2018-05-22 02:36:16 +02:00
|
|
|
,_event
|
|
|
|
{
|
|
|
|
fopts?
|
2019-01-16 21:12:59 +01:00
|
|
|
*fopts:
|
|
|
|
room.fopts?
|
|
|
|
*room.fopts:
|
|
|
|
event::fetch::default_opts
|
2018-05-22 02:36:16 +02:00
|
|
|
}
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2019-08-16 11:05:34 +02:00
|
|
|
assert(room.room_id);
|
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
seek(event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::messages::messages(const m::room &room,
|
2018-05-22 02:36:16 +02:00
|
|
|
const uint64_t &depth,
|
|
|
|
const event::fetch::opts *const &fopts)
|
2018-02-13 23:25:01 +01:00
|
|
|
:room{room}
|
2018-05-22 02:36:16 +02:00
|
|
|
,_event
|
|
|
|
{
|
2019-01-16 21:12:59 +01:00
|
|
|
fopts?
|
|
|
|
*fopts:
|
|
|
|
room.fopts?
|
|
|
|
*room.fopts:
|
|
|
|
event::fetch::default_opts
|
2018-05-22 02:36:16 +02:00
|
|
|
}
|
2018-02-13 23:25:01 +01:00
|
|
|
{
|
2019-08-16 11:05:34 +02:00
|
|
|
assert(room.room_id);
|
|
|
|
|
2019-02-05 05:01:31 +01:00
|
|
|
// As a special convenience for the ctor only, if the depth=0 and
|
|
|
|
// nothing is found another attempt is made for depth=1 for synapse
|
|
|
|
// rooms which start at depth=1.
|
|
|
|
if(!seek(depth) && depth == 0)
|
|
|
|
seek(1);
|
2018-02-13 23:25:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::m::event &
|
|
|
|
ircd::m::room::messages::operator*()
|
|
|
|
{
|
|
|
|
return fetch(std::nothrow);
|
|
|
|
};
|
|
|
|
|
2018-02-11 04:50:53 +01:00
|
|
|
bool
|
2018-02-13 23:25:01 +01:00
|
|
|
ircd::m::room::messages::seek(const event::id &event_id)
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2018-09-07 15:19:21 +02:00
|
|
|
const event::idx &event_idx
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2019-02-05 11:20:14 +01:00
|
|
|
m::index(event_id, std::nothrow)
|
2018-02-13 23:25:01 +01:00
|
|
|
};
|
|
|
|
|
2019-02-05 03:10:23 +01:00
|
|
|
return event_idx?
|
|
|
|
seek_idx(event_idx):
|
|
|
|
false;
|
2018-06-03 16:44:37 +02:00
|
|
|
}
|
2018-02-11 04:50:53 +01:00
|
|
|
|
|
|
|
bool
|
2018-02-13 23:25:01 +01:00
|
|
|
ircd::m::room::messages::seek(const uint64_t &depth)
|
2018-02-10 09:25:20 +01:00
|
|
|
{
|
2018-04-25 04:19:40 +02:00
|
|
|
char buf[dbs::ROOM_EVENTS_KEY_MAX_SIZE];
|
2019-02-05 03:10:23 +01:00
|
|
|
const string_view seek_key
|
2018-02-10 09:25:20 +01:00
|
|
|
{
|
2019-02-05 03:10:23 +01:00
|
|
|
depth != uint64_t(-1)?
|
|
|
|
dbs::room_events_key(buf, room.room_id, depth):
|
|
|
|
room.room_id
|
2018-02-10 09:25:20 +01:00
|
|
|
};
|
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
this->it = dbs::room_events.begin(seek_key);
|
|
|
|
return bool(*this);
|
|
|
|
}
|
2018-02-10 09:25:20 +01:00
|
|
|
|
2018-09-07 15:19:21 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::messages::seek_idx(const event::idx &event_idx)
|
|
|
|
try
|
|
|
|
{
|
2019-01-10 22:17:01 +01:00
|
|
|
uint64_t depth(0);
|
|
|
|
if(event_idx)
|
|
|
|
m::get(event_idx, "depth", mutable_buffer
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(&depth), sizeof(depth)
|
|
|
|
});
|
2018-09-07 15:19:21 +02:00
|
|
|
|
|
|
|
char buf[dbs::ROOM_EVENTS_KEY_MAX_SIZE];
|
|
|
|
const auto &seek_key
|
|
|
|
{
|
|
|
|
dbs::room_events_key(buf, room.room_id, depth, event_idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
this->it = dbs::room_events.begin(seek_key);
|
|
|
|
if(!bool(*this))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if this event_idx is actually in this room
|
2019-01-10 22:17:01 +01:00
|
|
|
if(event_idx && event_idx != this->event_idx())
|
2018-09-07 15:19:21 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const db::not_found &e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-06 23:36:49 +02:00
|
|
|
ircd::m::room::messages::operator
|
|
|
|
ircd::m::event::idx()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return event_idx();
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::room::messages::event_id()
|
2018-11-14 02:04:57 +01:00
|
|
|
const
|
2018-03-09 04:01:38 +01:00
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
return m::event_id(this->event_idx(), std::nothrow);
|
2018-03-09 04:01:38 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 02:04:57 +01:00
|
|
|
uint64_t
|
|
|
|
ircd::m::room::messages::depth()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(bool(*this));
|
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_events_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::get<0>(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
2018-04-18 04:14:39 +02:00
|
|
|
ircd::m::room::messages::event_idx()
|
2018-11-14 02:04:57 +01:00
|
|
|
const
|
2018-02-13 23:25:01 +01:00
|
|
|
{
|
|
|
|
assert(bool(*this));
|
2018-11-14 02:04:57 +01:00
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_events_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
return std::get<1>(part);
|
2018-02-13 23:25:01 +01:00
|
|
|
}
|
2018-02-10 09:25:20 +01:00
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
const ircd::m::event &
|
|
|
|
ircd::m::room::messages::fetch()
|
|
|
|
{
|
2019-01-26 22:49:34 +01:00
|
|
|
m::seek(_event, event_idx());
|
2018-02-13 23:25:01 +01:00
|
|
|
return _event;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::m::event &
|
|
|
|
ircd::m::room::messages::fetch(std::nothrow_t)
|
|
|
|
{
|
2019-01-26 22:49:34 +01:00
|
|
|
m::seek(_event, event_idx(), std::nothrow);
|
2018-02-13 23:25:01 +01:00
|
|
|
return _event;
|
2018-02-09 20:22:46 +01:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
//
|
|
|
|
// room::state
|
|
|
|
//
|
|
|
|
|
2019-06-16 11:18:04 +02:00
|
|
|
decltype(ircd::m::room::state::enable_history)
|
|
|
|
ircd::m::room::state::enable_history
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.room.state.enable_history" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2018-11-03 04:05:11 +01:00
|
|
|
decltype(ircd::m::room::state::readahead_size)
|
|
|
|
ircd::m::room::state::readahead_size
|
|
|
|
{
|
|
|
|
{ "name", "ircd.m.room.state.readahead_size" },
|
|
|
|
{ "default", 0L },
|
|
|
|
};
|
|
|
|
|
2018-11-03 04:04:17 +01:00
|
|
|
//
|
|
|
|
// room::state::state
|
|
|
|
//
|
|
|
|
|
2018-05-21 05:14:44 +02:00
|
|
|
ircd::m::room::state::state(const m::room &room,
|
|
|
|
const event::fetch::opts *const &fopts)
|
2018-03-04 09:12:13 +01:00
|
|
|
:room_id
|
|
|
|
{
|
|
|
|
room.room_id
|
|
|
|
}
|
|
|
|
,event_id
|
|
|
|
{
|
|
|
|
room.event_id?
|
|
|
|
event::id::buf{room.event_id}:
|
2018-05-20 14:11:17 +02:00
|
|
|
event::id::buf{}
|
2018-03-04 09:12:13 +01:00
|
|
|
}
|
2018-05-20 13:35:17 +02:00
|
|
|
,fopts
|
|
|
|
{
|
2018-05-22 02:36:16 +02:00
|
|
|
fopts?
|
|
|
|
fopts:
|
|
|
|
room.fopts
|
2018-05-20 13:35:17 +02:00
|
|
|
}
|
2018-02-11 02:34:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:46:37 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::room::state::prefetch(const event::idx &start,
|
|
|
|
const event::idx &stop)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return prefetch(string_view{}, start, stop);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::state::prefetch(const string_view &type,
|
|
|
|
const event::idx &start,
|
|
|
|
const event::idx &stop)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return prefetch(*this, type, event::idx_range{start, stop});
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:27:51 +01:00
|
|
|
ircd::m::event::idx
|
2018-04-05 02:06:58 +02:00
|
|
|
ircd::m::room::state::get(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
2019-02-13 23:27:51 +01:00
|
|
|
event::idx ret;
|
|
|
|
get(type, state_key, event::closure_idx{[&ret]
|
|
|
|
(const event::idx &event_idx)
|
2018-04-05 02:06:58 +02:00
|
|
|
{
|
2019-02-13 23:27:51 +01:00
|
|
|
ret = event_idx;
|
2018-04-05 02:06:58 +02:00
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:27:51 +01:00
|
|
|
ircd::m::event::idx
|
2018-04-05 02:06:58 +02:00
|
|
|
ircd::m::room::state::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
2019-02-13 23:27:51 +01:00
|
|
|
event::idx ret{0};
|
|
|
|
get(std::nothrow, type, state_key, event::closure_idx{[&ret]
|
|
|
|
(const event::idx &event_idx)
|
2018-04-05 02:06:58 +02:00
|
|
|
{
|
2019-02-13 23:27:51 +01:00
|
|
|
ret = event_idx;
|
2018-04-05 02:06:58 +02:00
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::state::get(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
get(type, state_key, event::closure_idx{[this, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &event_idx)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-04-12 03:20:13 +02:00
|
|
|
const event::fetch event
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2019-01-16 21:12:59 +01:00
|
|
|
event_idx, fopts? *fopts : event::fetch::default_opts
|
2018-02-09 08:18:42 +01:00
|
|
|
};
|
|
|
|
|
2018-04-12 23:10:45 +02:00
|
|
|
closure(event);
|
2018-02-10 09:25:20 +01:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::get(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::id::closure &closure)
|
2019-06-15 00:58:49 +02:00
|
|
|
const
|
2018-02-10 09:25:20 +01:00
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
get(type, state_key, event::closure_idx{[&]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
if(!m::event_id(idx, std::nothrow, closure))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"(%s,%s) in %s idx:%lu event_id :not found",
|
|
|
|
type,
|
|
|
|
state_key,
|
|
|
|
string_view{room_id},
|
|
|
|
idx,
|
|
|
|
};
|
2018-04-18 04:14:39 +02:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::get(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure_idx &closure)
|
|
|
|
const try
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2019-06-15 00:58:49 +02:00
|
|
|
{
|
2019-06-16 11:18:04 +02:00
|
|
|
const history history
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(history.get(type, state_key));
|
2019-06-15 00:58:49 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-04-18 04:14:39 +02:00
|
|
|
|
2018-03-04 09:12:13 +01:00
|
|
|
auto &column{dbs::room_state};
|
2018-04-25 04:19:40 +02:00
|
|
|
char key[dbs::ROOM_STATE_KEY_MAX_SIZE];
|
2018-04-18 04:14:39 +02:00
|
|
|
column(dbs::room_state_key(key, room_id, type, state_key), [&closure]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
closure(byte_view<event::idx>(value));
|
|
|
|
});
|
2018-02-10 09:25:20 +01:00
|
|
|
}
|
2018-04-03 07:05:36 +02:00
|
|
|
catch(const db::not_found &e)
|
|
|
|
{
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2018-04-03 11:11:49 +02:00
|
|
|
"(%s,%s) in %s :%s",
|
|
|
|
type,
|
|
|
|
state_key,
|
|
|
|
string_view{room_id},
|
|
|
|
e.what()
|
2018-04-03 07:05:36 +02:00
|
|
|
};
|
|
|
|
}
|
2018-02-10 09:25:20 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
return get(std::nothrow, type, state_key, event::closure_idx{[this, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &event_idx)
|
2018-02-10 09:25:20 +01:00
|
|
|
{
|
2018-04-12 03:20:13 +02:00
|
|
|
const event::fetch event
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2019-01-16 21:12:59 +01:00
|
|
|
event_idx, std::nothrow, fopts? *fopts : event::fetch::default_opts
|
2018-02-09 08:18:42 +01:00
|
|
|
};
|
|
|
|
|
2018-04-12 23:10:45 +02:00
|
|
|
closure(event);
|
2018-02-10 09:25:20 +01:00
|
|
|
}});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::id::closure &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
return get(std::nothrow, type, state_key, event::closure_idx{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
2019-07-06 07:01:34 +02:00
|
|
|
m::event_id(idx, std::nothrow, closure);
|
2018-04-18 04:14:39 +02:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure_idx &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2019-06-15 00:58:49 +02:00
|
|
|
{
|
2019-06-16 11:18:04 +02:00
|
|
|
const history history
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
history.get(std::nothrow, type, state_key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(event_idx)
|
|
|
|
{
|
|
|
|
closure(event_idx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
2019-06-15 00:58:49 +02:00
|
|
|
}
|
2018-04-18 04:14:39 +02:00
|
|
|
|
2018-03-04 09:12:13 +01:00
|
|
|
auto &column{dbs::room_state};
|
2018-04-25 04:19:40 +02:00
|
|
|
char key[dbs::ROOM_STATE_KEY_MAX_SIZE];
|
2018-04-18 04:14:39 +02:00
|
|
|
return column(dbs::room_state_key(key, room_id, type, state_key), std::nothrow, [&closure]
|
|
|
|
(const string_view &value)
|
|
|
|
{
|
|
|
|
closure(byte_view<event::idx>(value));
|
|
|
|
});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2019-08-21 06:36:00 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::has(const event::idx &event_idx)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
event::keys::include { "type", "state_key" },
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow, fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto state_idx
|
|
|
|
{
|
|
|
|
get(std::nothrow, at<"type"_>(event), at<"state_key"_>(event))
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(event_idx);
|
|
|
|
return event_idx == state_idx;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::has(const string_view &type)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
return for_each(type, event::id::closure_bool{[](const m::event::id &)
|
2018-02-09 06:03:25 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
2018-02-09 06:03:25 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::has(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2019-06-15 00:58:49 +02:00
|
|
|
{
|
2019-06-16 11:18:04 +02:00
|
|
|
const history history
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
return history.has(type, state_key);
|
2019-06-15 00:58:49 +02:00
|
|
|
}
|
2018-03-04 09:12:13 +01:00
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
2018-04-25 04:19:40 +02:00
|
|
|
char key[dbs::ROOM_STATE_KEY_MAX_SIZE];
|
2018-03-04 09:12:13 +01:00
|
|
|
return db::has(column, dbs::room_state_key(key, room_id, type, state_key));
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-25 04:52:02 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::room::state::count()
|
|
|
|
const
|
|
|
|
{
|
2019-07-04 10:50:23 +02:00
|
|
|
if(!present())
|
|
|
|
return count(string_view{});
|
|
|
|
|
|
|
|
const db::gopts &opts
|
|
|
|
{
|
|
|
|
this->fopts? this->fopts->gopts : db::gopts{}
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t ret(0);
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(room_id, opts)}; bool(it); ++it)
|
|
|
|
++ret;
|
|
|
|
|
|
|
|
return ret;
|
2018-02-25 04:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::state::count(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
2019-07-04 10:53:30 +02:00
|
|
|
if(!present())
|
|
|
|
return count(type);
|
|
|
|
|
|
|
|
const db::gopts &opts
|
2018-10-19 01:18:02 +02:00
|
|
|
{
|
2019-07-04 10:53:30 +02:00
|
|
|
this->fopts? this->fopts->gopts : db::gopts{}
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t ret(0);
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(room_id, opts)}; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const auto key(dbs::room_state_key(it->first));
|
|
|
|
ret += std::get<0>(key) == type;
|
|
|
|
}
|
2018-03-04 09:12:13 +01:00
|
|
|
|
|
|
|
return ret;
|
2018-02-25 04:52:02 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(event::closure_bool{[&closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
closure(event);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const event::closure_bool &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
2019-01-16 21:12:59 +01:00
|
|
|
fopts? *fopts : event::fetch::default_opts
|
2018-05-20 13:35:17 +02:00
|
|
|
};
|
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
return for_each(event::closure_idx_bool{[&event, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &event_idx)
|
2018-02-09 21:10:34 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(seek(event, event_idx, std::nothrow))
|
2018-09-26 01:02:19 +02:00
|
|
|
if(!closure(event))
|
|
|
|
return false;
|
2018-02-09 21:10:34 +01:00
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(event::id::closure_bool{[&closure]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
closure(event_id);
|
|
|
|
return true;
|
2018-02-10 09:25:20 +01:00
|
|
|
}});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const event::id::closure_bool &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
return for_each(event::closure_idx_bool{[&closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
bool ret{true};
|
2019-07-06 07:01:34 +02:00
|
|
|
m::event_id(idx, std::nothrow, [&ret, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::id &id)
|
|
|
|
{
|
|
|
|
ret = closure(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::closure_idx &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
closure(event_idx);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const event::closure_idx_bool &closure)
|
2018-04-18 04:14:39 +02:00
|
|
|
const
|
|
|
|
{
|
2019-04-01 00:12:38 +02:00
|
|
|
return for_each(closure_bool{[&closure]
|
|
|
|
(const string_view &type, const string_view &state_key, const event::idx &event_idx)
|
2018-08-21 17:48:42 +02:00
|
|
|
{
|
2019-04-01 00:12:38 +02:00
|
|
|
return closure(event_idx);
|
|
|
|
}});
|
2018-09-26 01:02:19 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:21:56 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!present())
|
2019-06-15 00:58:49 +02:00
|
|
|
{
|
2019-06-16 11:18:04 +02:00
|
|
|
const history history
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
return history.for_each([&closure]
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
return closure(type, state_key, event_idx);
|
|
|
|
});
|
2019-06-15 00:58:49 +02:00
|
|
|
}
|
2019-03-08 20:21:56 +01:00
|
|
|
|
|
|
|
db::gopts opts
|
|
|
|
{
|
|
|
|
this->fopts? this->fopts->gopts : db::gopts{}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!opts.readahead)
|
|
|
|
opts.readahead = size_t(readahead_size);
|
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(room_id, opts)}; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const byte_view<event::idx> idx(it->second);
|
|
|
|
const auto key(dbs::room_state_key(it->first));
|
|
|
|
if(!closure(std::get<0>(key), std::get<1>(key), idx))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-16 10:19:00 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::for_each(const type_prefix &prefix,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret(true), cont(true);
|
|
|
|
for_each(closure_bool{[&prefix, &closure, &ret, &cont]
|
|
|
|
(const string_view &type, const string_view &state_key, const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
if(!startswith(type, string_view(prefix)))
|
|
|
|
return cont;
|
|
|
|
|
|
|
|
cont = false;
|
|
|
|
ret = closure(type, state_key, event_idx);
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(type, event::closure_bool{[&closure]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
closure(event);
|
|
|
|
return true;
|
|
|
|
}});
|
2018-02-10 09:25:20 +01:00
|
|
|
}
|
2018-02-09 20:57:19 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::closure_bool &closure)
|
2018-02-10 09:25:20 +01:00
|
|
|
const
|
|
|
|
{
|
2018-10-24 03:11:56 +02:00
|
|
|
return type?
|
|
|
|
for_each(type, string_view{}, closure):
|
|
|
|
for_each(closure);
|
2018-09-26 01:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(type, event::id::closure_bool{[&closure]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
closure(event_id);
|
|
|
|
return true;
|
2018-02-10 09:25:20 +01:00
|
|
|
}});
|
2018-02-09 22:08:41 +01:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-02-09 22:08:41 +01:00
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::id::closure_bool &closure)
|
2018-02-09 22:08:41 +01:00
|
|
|
const
|
|
|
|
{
|
2018-10-24 03:11:56 +02:00
|
|
|
return type?
|
|
|
|
for_each(type, string_view{}, closure):
|
|
|
|
for_each(closure);
|
2018-04-18 04:14:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::closure_idx &closure)
|
2018-04-18 04:14:39 +02:00
|
|
|
const
|
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
for_each(type, event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &event_idx)
|
2018-08-21 17:48:42 +02:00
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
closure(event_idx);
|
|
|
|
return true;
|
|
|
|
}});
|
2018-02-10 09:25:20 +01:00
|
|
|
}
|
2018-02-09 20:57:19 +01:00
|
|
|
|
2018-04-10 04:24:56 +02:00
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::closure_idx_bool &closure)
|
2018-04-10 04:24:56 +02:00
|
|
|
const
|
|
|
|
{
|
2018-10-24 03:11:56 +02:00
|
|
|
return type?
|
|
|
|
for_each(type, string_view{}, closure):
|
|
|
|
for_each(closure);
|
2018-04-10 04:24:56 +02:00
|
|
|
}
|
|
|
|
|
2019-03-08 20:21:56 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return type?
|
|
|
|
for_each(type, string_view{}, closure):
|
|
|
|
for_each(closure);
|
|
|
|
}
|
|
|
|
|
2018-02-24 04:38:56 +01:00
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const event::closure_bool &closure)
|
2018-02-24 04:38:56 +01:00
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
2019-01-16 21:12:59 +01:00
|
|
|
fopts? *fopts : event::fetch::default_opts
|
2018-05-20 13:35:17 +02:00
|
|
|
};
|
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
return for_each(type, state_key_lb, event::closure_idx_bool{[&event, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &event_idx)
|
2018-02-24 04:38:56 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(seek(event, event_idx, std::nothrow))
|
2018-09-26 01:02:19 +02:00
|
|
|
if(!closure(event))
|
|
|
|
return false;
|
2018-02-24 04:38:56 +01:00
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
return true;
|
2018-02-24 04:38:56 +01:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const event::id::closure_bool &closure)
|
2018-02-24 04:38:56 +01:00
|
|
|
const
|
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
return for_each(type, state_key_lb, event::closure_idx_bool{[&closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
2018-09-26 01:02:19 +02:00
|
|
|
bool ret{true};
|
2019-07-06 07:01:34 +02:00
|
|
|
m::event_id(idx, std::nothrow, [&ret, &closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const event::id &id)
|
|
|
|
{
|
|
|
|
ret = closure(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-09-26 01:02:19 +02:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const event::closure_idx_bool &closure)
|
2018-04-18 04:14:39 +02:00
|
|
|
const
|
2019-03-08 20:21:56 +01:00
|
|
|
{
|
|
|
|
return for_each(type, state_key_lb, closure_bool{[&closure]
|
|
|
|
(const string_view &type, const string_view &state_key, const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
return closure(event_idx);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2019-06-15 00:58:49 +02:00
|
|
|
{
|
2019-06-16 11:18:04 +02:00
|
|
|
const history history
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
return history.for_each(type, state_key_lb, [&closure]
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
return closure(type, state_key, event_idx);
|
|
|
|
});
|
2019-06-15 00:58:49 +02:00
|
|
|
}
|
2018-04-18 04:14:39 +02:00
|
|
|
|
2018-04-25 04:19:40 +02:00
|
|
|
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
|
2018-03-04 09:12:13 +01:00
|
|
|
const auto &key
|
2018-02-24 04:38:56 +01:00
|
|
|
{
|
2018-03-04 09:12:13 +01:00
|
|
|
dbs::room_state_key(keybuf, room_id, type, state_key_lb)
|
|
|
|
};
|
|
|
|
|
2018-08-21 17:48:42 +02:00
|
|
|
db::gopts opts
|
|
|
|
{
|
|
|
|
this->fopts? this->fopts->gopts : db::gopts{}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!opts.readahead)
|
2018-11-03 04:05:11 +01:00
|
|
|
opts.readahead = size_t(readahead_size);
|
2018-08-21 17:48:42 +02:00
|
|
|
|
2018-03-04 09:12:13 +01:00
|
|
|
auto &column{dbs::room_state};
|
2018-08-21 17:48:42 +02:00
|
|
|
for(auto it{column.begin(key, opts)}; bool(it); ++it)
|
2019-03-08 20:21:56 +01:00
|
|
|
{
|
|
|
|
const auto key
|
2018-03-04 09:12:13 +01:00
|
|
|
{
|
2019-03-08 20:21:56 +01:00
|
|
|
dbs::room_state_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(std::get<0>(key) != type)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const byte_view<event::idx> idx(it->second);
|
|
|
|
if(!closure(std::get<0>(key), std::get<1>(key), idx))
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-04 09:12:13 +01:00
|
|
|
|
2018-09-26 01:02:19 +02:00
|
|
|
return true;
|
2018-02-10 09:25:20 +01:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-10-26 00:37:35 +02:00
|
|
|
/// Figure out if this instance of room::state is presenting the current
|
|
|
|
/// "present" state of the room or the state of the room at some previous
|
|
|
|
/// event. This is an important distinction because the present state of
|
|
|
|
/// the room should provide optimal performance for the functions of this
|
|
|
|
/// interface by using the present state table. Prior states will use the
|
|
|
|
/// state btree.
|
2018-05-05 12:35:44 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::present()
|
|
|
|
const
|
|
|
|
{
|
2018-10-26 00:37:35 +02:00
|
|
|
// When no event_id is passed to the state constructor that immediately
|
|
|
|
// indicates the present state of the room is sought.
|
|
|
|
if(!event_id)
|
|
|
|
return true;
|
|
|
|
|
2019-06-16 11:18:04 +02:00
|
|
|
// When the global configuration disables history, always consider the
|
|
|
|
// present state. (disabling may yield unexpected incorrect results by
|
|
|
|
// returning the present state without error).
|
|
|
|
if(!enable_history)
|
|
|
|
return true;
|
|
|
|
|
2018-10-26 00:37:35 +02:00
|
|
|
// Check the cached value from a previous false result of this function
|
|
|
|
// before doing any real work/IO below. If this function ever returned
|
|
|
|
// false it will never return true after.
|
|
|
|
if(_not_present)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto head_id
|
|
|
|
{
|
|
|
|
m::head(std::nothrow, room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the event_id passed is exactly the latest event we can obviously
|
|
|
|
// consider this the present state.
|
|
|
|
if(!head_id || head_id == event_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// This result is cacheable because once it's no longer the present
|
|
|
|
// it will never be again. Panta chorei kai ouden menei. Note that this
|
|
|
|
// is a const member function; the cache variable is an appropriate case
|
|
|
|
// for the 'mutable' keyword.
|
|
|
|
_not_present = true;
|
|
|
|
return false;
|
2018-05-05 12:35:44 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 09:14:15 +02:00
|
|
|
//
|
|
|
|
// room::state::history
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room::state::history::history(const m::room &room)
|
|
|
|
:history
|
|
|
|
{
|
|
|
|
room, -1
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::state::history::history(const m::room::id &room_id,
|
|
|
|
const m::event::id &event_id)
|
|
|
|
:history
|
|
|
|
{
|
|
|
|
m::room
|
|
|
|
{
|
|
|
|
room_id, event_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::state::history::history(const m::room &room,
|
|
|
|
const int64_t &bound)
|
|
|
|
:space
|
|
|
|
{
|
|
|
|
room
|
|
|
|
}
|
|
|
|
,bound
|
|
|
|
{
|
|
|
|
bound < 0 && room.event_id?
|
|
|
|
m::get<int64_t>(m::index(room.event_id), "depth"):
|
|
|
|
bound
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::state::history::get(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
get(std::nothrow, type, state_key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(unlikely(!ret))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"(%s,%s) in %s @%ld$%s",
|
|
|
|
type,
|
|
|
|
state_key,
|
|
|
|
string_view{space.room.room_id},
|
|
|
|
bound,
|
|
|
|
string_view{space.room.event_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::idx
|
|
|
|
ircd::m::room::state::history::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
event::idx ret{0};
|
|
|
|
assert(type && defined(state_key));
|
|
|
|
for_each(type, state_key, [&ret]
|
|
|
|
(const auto &, const auto &, const auto &, const auto &event_idx)
|
|
|
|
{
|
|
|
|
ret = event_idx;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::history::has(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return has(type, string_view{});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::history::has(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !for_each(type, state_key, []
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::state::history::count(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count(type, string_view{});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::state::history::count(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(type, state_key, [&ret]
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::history::for_each(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::history::for_each(const string_view &type,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(type, string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::history::for_each(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
char type_buf[m::event::TYPE_MAX_SIZE];
|
|
|
|
char state_key_buf[m::event::STATE_KEY_MAX_SIZE];
|
|
|
|
|
|
|
|
string_view last_type;
|
|
|
|
string_view last_state_key;
|
|
|
|
|
|
|
|
return space.for_each(type, state_key, [&]
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
2019-08-21 11:28:21 +02:00
|
|
|
if(bound > -1 && depth >= bound)
|
2019-06-16 09:14:15 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if(type == last_type && state_key == last_state_key)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(!closure(type, state_key, depth, event_idx))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(type != last_type)
|
|
|
|
last_type = { type_buf, copy(type_buf, type) };
|
|
|
|
|
|
|
|
if(state_key != last_state_key)
|
|
|
|
last_state_key = { state_key_buf, copy(state_key_buf, state_key) };
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-13 19:20:06 +02:00
|
|
|
//
|
|
|
|
// room::state::space
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room::state::space::space(const m::room &room)
|
|
|
|
:room
|
|
|
|
{
|
|
|
|
room
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::has(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return has(type, string_view{});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::has(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return has(type, state_key, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::has(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const int64_t &depth)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !for_each(type, state_key, depth, []
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-18 17:31:57 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::state::space::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count(string_view{});
|
|
|
|
}
|
|
|
|
|
2019-05-13 19:20:06 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::state::space::count(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count(type, string_view{});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::state::space::count(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count(type, state_key, -1L);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::state::space::count(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const int64_t &depth)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(type, state_key, depth, [&ret]
|
|
|
|
(const auto &type, const auto &state_key, const auto &depth, const auto &event_idx)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::for_each(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2019-06-16 09:12:06 +02:00
|
|
|
return for_each(string_view{}, string_view{}, -1L, closure);
|
2019-05-13 19:20:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::for_each(const string_view &type,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(type, string_view{}, -1L, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::for_each(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(type, state_key, -1L, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::space::for_each(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const int64_t &depth,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2019-08-18 13:10:57 +02:00
|
|
|
const int64_t &_depth
|
|
|
|
{
|
|
|
|
type? depth : 0L
|
|
|
|
};
|
|
|
|
|
2019-05-13 19:29:20 +02:00
|
|
|
char buf[dbs::ROOM_STATE_SPACE_KEY_MAX_SIZE];
|
2019-05-13 19:20:06 +02:00
|
|
|
const string_view &key
|
|
|
|
{
|
2019-08-18 13:10:57 +02:00
|
|
|
dbs::room_state_space_key(buf, room.room_id, type, state_key, _depth, 0UL)
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
2019-05-13 19:29:20 +02:00
|
|
|
dbs::room_state_space.begin(key)
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for(; it; ++it)
|
|
|
|
{
|
2019-08-18 13:11:35 +02:00
|
|
|
const auto &[_type, _state_key, _depth, _event_idx]
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-05-13 19:29:20 +02:00
|
|
|
dbs::room_state_space_key(it->first)
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
|
|
|
|
2019-08-18 13:11:35 +02:00
|
|
|
if(type && type != _type)
|
2019-05-13 19:20:06 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-18 13:11:35 +02:00
|
|
|
if(state_key && state_key != _state_key)
|
2019-05-13 19:20:06 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-18 13:11:35 +02:00
|
|
|
if(depth >= 0 && depth != _depth)
|
2019-05-13 19:20:06 +02:00
|
|
|
break;
|
|
|
|
|
2019-08-18 13:11:35 +02:00
|
|
|
if(!closure(_type, _state_key, _depth, _event_idx))
|
2019-05-13 19:20:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room::state::space::rebuild
|
|
|
|
//
|
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
ircd::m::room::state::space::rebuild::rebuild(const room::id &room_id)
|
|
|
|
{
|
|
|
|
db::txn txn
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-08-22 13:32:15 +02:00
|
|
|
*m::dbs::events
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
m::room::messages it
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-08-22 13:32:15 +02:00
|
|
|
room_id, uint64_t(0)
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
if(!it)
|
|
|
|
return;
|
2019-05-13 19:20:06 +02:00
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
size_t state_count(0), messages_count(0), state_deleted(0);
|
|
|
|
for(; it; ++it, ++messages_count) try
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-08-22 13:32:15 +02:00
|
|
|
const m::event::idx &event_idx
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-08-22 13:32:15 +02:00
|
|
|
it.event_idx()
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
if(!state::is(std::nothrow, event_idx))
|
|
|
|
continue;
|
2019-05-13 19:20:06 +02:00
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
++state_count;
|
|
|
|
const m::event &event{*it};
|
2019-08-22 14:13:59 +02:00
|
|
|
const auto &[pass_static, reason_static]
|
2019-05-13 19:35:51 +02:00
|
|
|
{
|
2019-08-22 14:13:59 +02:00
|
|
|
room::auth::check_static(event)
|
2019-08-22 13:32:15 +02:00
|
|
|
};
|
|
|
|
|
2019-08-22 14:13:59 +02:00
|
|
|
if(!pass_static)
|
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "%s in %s erased from state space (static) :%s",
|
|
|
|
string_view{event.event_id},
|
|
|
|
string_view{room_id},
|
|
|
|
what(reason_static),
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &[pass_relative, reason_relative]
|
|
|
|
{
|
|
|
|
pass_static?
|
|
|
|
room::auth::check_relative(event):
|
|
|
|
room::auth::passfail{false, {}},
|
|
|
|
};
|
|
|
|
|
|
|
|
if(pass_static && !pass_relative)
|
2019-08-22 13:32:15 +02:00
|
|
|
log::dwarning
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-08-22 14:13:59 +02:00
|
|
|
log, "%s in %s erased from state space (relative) :%s",
|
2019-08-22 13:32:15 +02:00
|
|
|
string_view{event.event_id},
|
|
|
|
string_view{room_id},
|
2019-08-22 14:13:59 +02:00
|
|
|
what(reason_relative),
|
2019-05-13 19:20:06 +02:00
|
|
|
};
|
2019-05-13 19:35:51 +02:00
|
|
|
|
2019-08-22 13:32:15 +02:00
|
|
|
dbs::write_opts opts;
|
|
|
|
opts.event_idx = event_idx;
|
|
|
|
|
|
|
|
opts.appendix.reset();
|
|
|
|
opts.appendix.set(dbs::appendix::ROOM_STATE_SPACE);
|
|
|
|
|
2019-08-22 14:13:59 +02:00
|
|
|
opts.op = pass_static && pass_relative? db::op::SET : db::op::DELETE;
|
|
|
|
state_deleted += opts.op == db::op::DELETE;
|
2019-08-22 13:32:15 +02:00
|
|
|
|
|
|
|
dbs::write(txn, event, opts);
|
2019-05-13 19:20:06 +02:00
|
|
|
}
|
2019-08-12 04:04:40 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
2019-05-13 19:20:06 +02:00
|
|
|
{
|
2019-08-12 04:04:40 +02:00
|
|
|
log::dwarning
|
|
|
|
{
|
|
|
|
log, "room::state::space::rebuild :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
2019-08-22 15:05:14 +02:00
|
|
|
throw;
|
2019-05-13 19:20:06 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "room::state::space::rebuild :%s",
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
2019-08-22 13:32:15 +02:00
|
|
|
log, "room::state::space::rebuild %s complete msgs:%zu state:%zu del:%zu transaction elems:%zu size:%s",
|
|
|
|
string_view{room_id},
|
|
|
|
messages_count,
|
|
|
|
state_count,
|
|
|
|
state_deleted,
|
2019-05-13 19:20:06 +02:00
|
|
|
txn.size(),
|
|
|
|
pretty(iec(txn.bytes()))
|
|
|
|
};
|
|
|
|
|
|
|
|
txn();
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
//
|
|
|
|
// room::members
|
|
|
|
//
|
|
|
|
|
2019-07-19 22:56:08 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::members::empty()
|
|
|
|
const
|
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return empty(string_view{});
|
2019-07-19 22:56:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::members::empty(const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return for_each(membership, closure{[](const auto &)
|
2019-07-19 22:56:08 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return false;
|
2019-07-19 22:56:08 +02:00
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:16:47 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::members::count()
|
|
|
|
const
|
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return count(string_view{});
|
2018-09-26 01:16:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::members::count(const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
2019-07-24 04:31:40 +02:00
|
|
|
this->for_each(membership, room::members::closure{[&ret]
|
|
|
|
(const id::user &)
|
2018-09-26 01:16:47 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
++ret;
|
|
|
|
return true;
|
2018-09-26 01:16:47 +02:00
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-04-12 23:10:03 +02:00
|
|
|
bool
|
2019-07-24 04:31:40 +02:00
|
|
|
ircd::m::room::members::for_each(const closure &closure)
|
2018-04-12 23:10:03 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-07-24 04:31:40 +02:00
|
|
|
ircd::m::room::members::for_each(const closure_idx &closure)
|
2018-02-10 22:03:13 +01:00
|
|
|
const
|
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return for_each(string_view{}, closure);
|
2018-02-10 22:03:13 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
2018-04-12 23:10:03 +02:00
|
|
|
ircd::m::room::members::for_each(const string_view &membership,
|
2019-07-24 04:31:40 +02:00
|
|
|
const closure_idx &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
const m::room::state state
|
2018-05-21 05:16:31 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
room
|
2018-05-21 05:16:31 +02:00
|
|
|
};
|
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
const bool present
|
2018-05-21 05:16:31 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
state.present()
|
2019-07-07 12:58:35 +02:00
|
|
|
};
|
2018-05-21 05:16:31 +02:00
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
// joined members optimization. Only possible when seeking
|
|
|
|
// membership="join" on the present state of the room.
|
|
|
|
if(membership == "join" && present)
|
2019-07-27 02:31:56 +02:00
|
|
|
return this->for_each(membership, [this, &closure, &state]
|
2019-07-24 04:31:40 +02:00
|
|
|
(const id::user &member)
|
2018-04-12 23:10:03 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
const auto event_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, "m.room.member", member)
|
|
|
|
};
|
2018-02-09 08:18:42 +01:00
|
|
|
|
2019-07-27 02:31:56 +02:00
|
|
|
if(likely(event_idx))
|
|
|
|
return closure(member, event_idx);
|
|
|
|
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Failed to find member event_idx:%zu for %s in present state of %s",
|
|
|
|
event_idx,
|
|
|
|
string_view{member},
|
|
|
|
string_view{room.room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
2019-07-24 04:31:40 +02:00
|
|
|
});
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
return state.for_each("m.room.member", [this, &membership, &closure]
|
|
|
|
(const string_view &type, const string_view &state_key, const event::idx &event_idx)
|
2018-04-12 23:10:03 +02:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return !membership || this->membership(event_idx, membership)?
|
|
|
|
closure(state_key, event_idx):
|
|
|
|
true;
|
|
|
|
});
|
2018-04-12 23:10:03 +02:00
|
|
|
}
|
|
|
|
|
2018-02-09 09:03:08 +01:00
|
|
|
bool
|
2018-09-26 01:16:47 +02:00
|
|
|
ircd::m::room::members::for_each(const string_view &membership,
|
2019-07-24 04:31:40 +02:00
|
|
|
const closure &closure)
|
2018-02-09 09:03:08 +01:00
|
|
|
const
|
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool present
|
|
|
|
{
|
|
|
|
state.present()
|
|
|
|
};
|
2018-04-12 23:10:03 +02:00
|
|
|
|
2018-03-03 15:21:59 +01:00
|
|
|
// joined members optimization. Only possible when seeking
|
|
|
|
// membership="join" on the present state of the room.
|
2019-07-24 04:31:40 +02:00
|
|
|
if(membership == "join" && present)
|
|
|
|
return room::origins::_for_each(room, [this, &closure]
|
2018-03-03 15:21:59 +01:00
|
|
|
(const string_view &key)
|
|
|
|
{
|
|
|
|
const string_view &member
|
|
|
|
{
|
2018-04-15 23:40:10 +02:00
|
|
|
std::get<1>(dbs::room_joined_key(key))
|
2018-03-03 15:21:59 +01:00
|
|
|
};
|
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
return closure(member);
|
2018-03-03 15:21:59 +01:00
|
|
|
});
|
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
return this->for_each(membership, [&closure]
|
|
|
|
(const auto &user_id, const auto &event_idx)
|
2018-02-09 09:03:08 +01:00
|
|
|
{
|
2019-07-24 04:31:40 +02:00
|
|
|
return closure(user_id);
|
|
|
|
});
|
|
|
|
}
|
2018-03-03 15:21:18 +01:00
|
|
|
|
2019-07-24 04:31:40 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::members::membership(const event::idx &event_idx,
|
|
|
|
const string_view &membership)
|
|
|
|
{
|
|
|
|
return m::query(std::nothrow, event_idx, "content", [&membership]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::string &content_membership
|
|
|
|
{
|
|
|
|
content["membership"]
|
|
|
|
};
|
|
|
|
|
|
|
|
return content_membership && content_membership == membership;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::room::members::membership(const mutable_buffer &out,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
return m::query(std::nothrow, event_idx, "content", [&out]
|
|
|
|
(const json::object &content) -> string_view
|
|
|
|
{
|
|
|
|
const json::string &content_membership
|
|
|
|
{
|
|
|
|
content["membership"]
|
|
|
|
};
|
|
|
|
|
|
|
|
return strlcpy
|
|
|
|
{
|
|
|
|
out, content_membership
|
|
|
|
};
|
|
|
|
});
|
2018-03-03 15:21:18 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
//
|
2018-02-10 09:25:20 +01:00
|
|
|
// room::origins
|
2017-12-12 21:33:14 +01:00
|
|
|
//
|
|
|
|
|
2018-08-24 13:14:24 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::room::origins::random(const mutable_buffer &buf,
|
|
|
|
const closure_bool &proffer)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
string_view ret;
|
|
|
|
const auto closure{[&buf, &proffer, &ret]
|
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
ret = { data(buf), copy(buf, origin) };
|
|
|
|
}};
|
|
|
|
|
|
|
|
random(closure, proffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::origins::random(const closure &view,
|
|
|
|
const closure_bool &proffer)
|
|
|
|
const
|
|
|
|
{
|
2019-02-28 23:07:04 +01:00
|
|
|
return random(*this, view, proffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::room::origins::random(const origins &origins,
|
2019-02-28 23:07:04 +01:00
|
|
|
const closure &view,
|
|
|
|
const closure_bool &proffer)
|
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
bool ret{false};
|
|
|
|
const size_t max
|
|
|
|
{
|
|
|
|
origins.count()
|
|
|
|
};
|
2018-08-24 13:14:24 +02:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
if(unlikely(!max))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
auto select
|
2018-08-24 13:14:24 +02:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
ssize_t(rand::integer(0, max - 1))
|
2018-08-24 13:14:24 +02:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
const closure_bool closure{[&proffer, &view, &select]
|
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
if(select-- > 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Test if this random selection is "ok" e.g. the callback allows the
|
|
|
|
// user to test a blacklist for this origin. Skip to next if not.
|
|
|
|
if(proffer && !proffer(origin))
|
|
|
|
{
|
|
|
|
++select;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
view(origin);
|
|
|
|
return false;
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto iteration{[&origins, &closure, &ret]
|
|
|
|
{
|
|
|
|
ret = !origins.for_each(closure);
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Attempt select on first iteration
|
|
|
|
iteration();
|
|
|
|
|
|
|
|
// If nothing was OK between the random int and the end of the iteration
|
|
|
|
// then start again and pick the first OK.
|
|
|
|
if(!ret && select >= 0)
|
|
|
|
iteration();
|
|
|
|
|
|
|
|
return ret;
|
2018-08-24 13:14:24 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 14:29:48 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::origins::empty()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(closure_bool{[]
|
|
|
|
(const string_view &)
|
|
|
|
{
|
|
|
|
// return false to break and return false.
|
|
|
|
return false;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
2018-03-03 14:56:42 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::room::origins::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each([&ret](const string_view &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-22 04:06:43 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::room::origins::count_error()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each([&ret](const string_view &server)
|
|
|
|
{
|
2019-08-13 14:29:48 +02:00
|
|
|
ret += !ircd::empty(server::errmsg(server));
|
2019-07-22 04:06:43 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::origins::count_online()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
ssize_t ret
|
|
|
|
{
|
|
|
|
0 - ssize_t(count_error())
|
|
|
|
};
|
|
|
|
|
|
|
|
for_each([&ret](const string_view &hostport)
|
|
|
|
{
|
|
|
|
ret += bool(server::exists(hostport));
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(ret >= 0L);
|
|
|
|
return std::max(ret, 0L);
|
|
|
|
}
|
|
|
|
|
2018-07-01 06:55:01 +02:00
|
|
|
/// Tests if argument is the only origin in the room.
|
|
|
|
/// If a zero or more than one origins exist, returns false. If the only origin
|
|
|
|
/// in the room is the argument origin, returns true.
|
|
|
|
bool
|
|
|
|
ircd::m::room::origins::only(const string_view &origin)
|
|
|
|
const
|
|
|
|
{
|
2018-09-26 01:16:47 +02:00
|
|
|
ushort ret{2};
|
|
|
|
for_each(closure_bool{[&ret, &origin]
|
|
|
|
(const string_view &origin_) -> bool
|
2018-07-01 06:55:01 +02:00
|
|
|
{
|
|
|
|
if(origin == origin_)
|
2018-09-26 01:16:47 +02:00
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = 0;
|
2018-07-01 06:55:01 +02:00
|
|
|
|
|
|
|
return ret;
|
2018-09-26 01:16:47 +02:00
|
|
|
}});
|
2018-07-01 06:55:01 +02:00
|
|
|
|
2018-09-26 01:16:47 +02:00
|
|
|
return ret == 1;
|
2018-07-01 06:55:01 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 21:49:34 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::origins::has(const string_view &origin)
|
|
|
|
const
|
|
|
|
{
|
2019-06-11 21:55:14 +02:00
|
|
|
db::domain &index
|
2018-04-03 21:49:34 +02:00
|
|
|
{
|
2018-04-15 23:40:10 +02:00
|
|
|
dbs::room_joined
|
2018-04-03 21:49:34 +02:00
|
|
|
};
|
|
|
|
|
2018-04-25 04:19:40 +02:00
|
|
|
char querybuf[dbs::ROOM_JOINED_KEY_MAX_SIZE];
|
2018-04-03 21:49:34 +02:00
|
|
|
const auto query
|
|
|
|
{
|
2018-04-15 23:40:10 +02:00
|
|
|
dbs::room_joined_key(querybuf, room.room_id, origin)
|
2018-04-03 21:49:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
index.begin(query)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const string_view &key
|
|
|
|
{
|
|
|
|
lstrip(it->first, "\0"_sv)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &key_origin
|
|
|
|
{
|
2018-04-15 23:40:10 +02:00
|
|
|
std::get<0>(dbs::room_joined_key(key))
|
2018-04-03 21:49:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return key_origin == origin;
|
|
|
|
}
|
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::origins::for_each(const closure &view)
|
|
|
|
const
|
|
|
|
{
|
2018-09-26 01:16:47 +02:00
|
|
|
for_each(closure_bool{[&view]
|
|
|
|
(const string_view &origin)
|
2018-02-10 22:03:13 +01:00
|
|
|
{
|
|
|
|
view(origin);
|
2018-09-26 01:16:47 +02:00
|
|
|
return true;
|
|
|
|
}});
|
2018-02-10 22:03:13 +01:00
|
|
|
}
|
|
|
|
|
2018-08-23 02:42:35 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::origins::for_each(const closure_bool &view)
|
|
|
|
const
|
2018-03-03 15:06:15 +01:00
|
|
|
{
|
|
|
|
string_view last;
|
2019-05-27 01:53:08 +02:00
|
|
|
char lastbuf[rfc1035::NAME_BUFSIZE];
|
2019-04-28 01:22:47 +02:00
|
|
|
return _for_each(*this, [&last, &lastbuf, &view]
|
2018-03-03 15:06:15 +01:00
|
|
|
(const string_view &key)
|
|
|
|
{
|
2018-03-04 09:08:30 +01:00
|
|
|
const string_view &origin
|
2018-03-03 15:06:15 +01:00
|
|
|
{
|
2018-04-15 23:40:10 +02:00
|
|
|
std::get<0>(dbs::room_joined_key(key))
|
2018-03-03 15:06:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(origin == last)
|
|
|
|
return true;
|
|
|
|
|
2018-09-26 01:16:47 +02:00
|
|
|
if(!view(origin))
|
|
|
|
return false;
|
|
|
|
|
2018-03-03 15:06:15 +01:00
|
|
|
last = { lastbuf, copy(lastbuf, origin) };
|
2018-09-26 01:16:47 +02:00
|
|
|
return true;
|
2018-03-03 15:06:15 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-04-28 01:22:47 +02:00
|
|
|
ircd::m::room::origins::_for_each(const origins &origins,
|
|
|
|
const closure_bool &view)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2019-06-11 21:55:14 +02:00
|
|
|
db::domain &index
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-04-15 23:40:10 +02:00
|
|
|
dbs::room_joined
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2018-03-03 14:40:53 +01:00
|
|
|
auto it
|
|
|
|
{
|
2019-04-28 01:22:47 +02:00
|
|
|
index.begin(origins.room.room_id)
|
2018-03-03 14:40:53 +01:00
|
|
|
};
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
for(; bool(it); ++it)
|
|
|
|
{
|
2018-03-04 09:25:27 +01:00
|
|
|
const string_view &key
|
2018-03-03 14:40:53 +01:00
|
|
|
{
|
2018-03-04 09:25:27 +01:00
|
|
|
lstrip(it->first, "\0"_sv)
|
2018-03-03 14:40:53 +01:00
|
|
|
};
|
|
|
|
|
2018-09-26 01:16:47 +02:00
|
|
|
if(!view(key))
|
|
|
|
return false;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-09-26 01:16:47 +02:00
|
|
|
return true;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2019-03-30 23:53:40 +01:00
|
|
|
//
|
|
|
|
// room::aliases
|
|
|
|
//
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::aliases::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return count(string_view{});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::aliases::count(const string_view &server)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each(server, [&ret](const auto &a)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::has(const alias &alias)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !for_each(alias.host(), [&alias]
|
|
|
|
(const id::room_alias &a)
|
|
|
|
{
|
|
|
|
assert(a.host() == alias.host());
|
|
|
|
return a == alias? false : true; // false to break on found
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-08-16 10:19:00 +02:00
|
|
|
return state.for_each("m.room.aliases", [this, &closure]
|
|
|
|
(const string_view &type, const string_view &state_key, const event::idx &)
|
2019-03-30 23:53:40 +01:00
|
|
|
{
|
|
|
|
return for_each(state_key, closure);
|
2019-08-16 10:19:00 +02:00
|
|
|
});
|
2019-03-30 23:53:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::for_each(const string_view &server,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
if(!server)
|
|
|
|
return for_each(closure);
|
|
|
|
|
2019-03-31 00:02:40 +01:00
|
|
|
return for_each(room, server, closure);
|
|
|
|
}
|
2019-03-30 23:53:40 +01:00
|
|
|
|
2019-03-31 00:02:40 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::for_each(const m::room &room,
|
|
|
|
const string_view &server,
|
|
|
|
const closure_bool &closure)
|
|
|
|
{
|
|
|
|
using prototype = bool (const m::room &, const string_view &, const closure_bool &);
|
2019-03-30 23:53:40 +01:00
|
|
|
|
2019-03-31 00:02:40 +01:00
|
|
|
static mods::import<prototype> call
|
2019-03-30 23:53:40 +01:00
|
|
|
{
|
2019-03-31 00:02:40 +01:00
|
|
|
"m_room_aliases", "ircd::m::room::aliases::for_each"
|
|
|
|
};
|
2019-03-30 23:53:40 +01:00
|
|
|
|
2019-03-31 00:02:40 +01:00
|
|
|
return call(room, server, closure);
|
2019-03-30 23:53:40 +01:00
|
|
|
}
|
|
|
|
|
2019-03-31 01:04:35 +01:00
|
|
|
//
|
|
|
|
// room::aliases::cache
|
|
|
|
//
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::del(const alias &a)
|
|
|
|
{
|
|
|
|
using prototype = bool (const alias &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_room_aliases", "ircd::m::room::aliases::cache::del"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::set(const alias &a,
|
|
|
|
const id &i)
|
|
|
|
{
|
|
|
|
using prototype = bool (const alias &, const id &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_room_aliases", "ircd::m::room::aliases::cache::set"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(a, i);
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:52:28 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::fetch(std::nothrow_t,
|
|
|
|
const alias &a,
|
|
|
|
const net::hostport &hp)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fetch(a, hp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
thread_local char buf[384];
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Failed to fetch room_id for %s from %s :%s",
|
|
|
|
string_view{a},
|
|
|
|
string(buf, hp),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::aliases::cache::fetch(const alias &a,
|
|
|
|
const net::hostport &hp)
|
|
|
|
{
|
|
|
|
using prototype = void (const alias &, const net::hostport &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_room_aliases", "ircd::m::room::aliases::cache::fetch"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(a, hp);
|
|
|
|
}
|
|
|
|
|
2019-03-31 01:04:35 +01:00
|
|
|
ircd::m::room::id::buf
|
|
|
|
ircd::m::room::aliases::cache::get(const alias &a)
|
|
|
|
{
|
|
|
|
id::buf ret;
|
2019-03-31 01:52:28 +01:00
|
|
|
get(a, [&ret]
|
|
|
|
(const id &room_id)
|
|
|
|
{
|
|
|
|
ret = room_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::id::buf
|
|
|
|
ircd::m::room::aliases::cache::get(std::nothrow_t,
|
|
|
|
const alias &a)
|
|
|
|
{
|
|
|
|
id::buf ret;
|
|
|
|
get(std::nothrow, a, [&ret]
|
|
|
|
(const id &room_id)
|
2019-03-31 01:04:35 +01:00
|
|
|
{
|
|
|
|
ret = room_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::aliases::cache::get(const alias &a,
|
|
|
|
const id::closure &c)
|
|
|
|
{
|
|
|
|
if(!get(std::nothrow, a, c))
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"Cannot find room_id for %s",
|
|
|
|
string_view{a}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::get(std::nothrow_t,
|
|
|
|
const alias &a,
|
|
|
|
const id::closure &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (std::nothrow_t, const alias &, const id::closure &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_room_aliases", "ircd::m::room::aliases::cache::get"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(std::nothrow, a, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::has(const alias &a)
|
|
|
|
{
|
|
|
|
using prototype = bool (const alias &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_room_aliases", "ircd::m::room::aliases::cache::has"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::for_each(const closure_bool &c)
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::aliases::cache::for_each(const string_view &s,
|
|
|
|
const closure_bool &c)
|
|
|
|
{
|
|
|
|
using prototype = bool (const string_view &, const closure_bool &);
|
|
|
|
|
|
|
|
static mods::import<prototype> call
|
|
|
|
{
|
|
|
|
"m_room_aliases", "ircd::m::room::aliases::cache::for_each"
|
|
|
|
};
|
|
|
|
|
|
|
|
return call(s, c);
|
|
|
|
}
|
|
|
|
|
2018-06-04 23:23:39 +02:00
|
|
|
//
|
|
|
|
// room::power
|
|
|
|
//
|
|
|
|
|
2018-10-26 01:28:30 +02:00
|
|
|
decltype(ircd::m::room::power::default_creator_level)
|
|
|
|
ircd::m::room::power::default_creator_level
|
|
|
|
{
|
|
|
|
100
|
|
|
|
};
|
|
|
|
|
2018-06-04 23:23:39 +02:00
|
|
|
decltype(ircd::m::room::power::default_power_level)
|
|
|
|
ircd::m::room::power::default_power_level
|
|
|
|
{
|
|
|
|
50
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::room::power::default_event_level)
|
|
|
|
ircd::m::room::power::default_event_level
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::room::power::default_user_level)
|
|
|
|
ircd::m::room::power::default_user_level
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-03-05 22:32:52 +01:00
|
|
|
ircd::json::object
|
2019-05-06 03:04:56 +02:00
|
|
|
ircd::m::room::power::default_content(const mutable_buffer &buf,
|
|
|
|
const m::user::id &creator)
|
2019-08-18 15:35:23 +02:00
|
|
|
{
|
|
|
|
return compose_content(buf, [&creator]
|
|
|
|
(const string_view &key, json::stack::object &object)
|
|
|
|
{
|
|
|
|
if(key != "users")
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(default_creator_level == 100);
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, creator, json::value(default_creator_level)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::json::object
|
|
|
|
ircd::m::room::power::compose_content(const mutable_buffer &buf,
|
|
|
|
const compose_closure &closure)
|
2019-03-05 22:32:52 +01:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
json::stack out{buf};
|
|
|
|
json::stack::object content{out};
|
2019-03-05 22:32:52 +01:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
assert(default_power_level == 50);
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
content, "ban", json::value(default_power_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2019-08-18 15:35:23 +02:00
|
|
|
json::stack::object events
|
|
|
|
{
|
|
|
|
content, "events"
|
|
|
|
};
|
|
|
|
|
|
|
|
closure("events", events);
|
|
|
|
}
|
2019-05-06 03:04:56 +02:00
|
|
|
|
|
|
|
assert(default_event_level == 0);
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
content, "events_default", json::value(default_event_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
content, "invite", json::value(default_power_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
2019-03-05 22:32:52 +01:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
content, "kick", json::value(default_power_level)
|
2019-03-05 22:32:52 +01:00
|
|
|
};
|
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
{
|
|
|
|
json::stack::object notifications
|
|
|
|
{
|
|
|
|
content, "notifications"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
notifications, "room", json::value(default_power_level)
|
|
|
|
};
|
2019-08-18 15:35:23 +02:00
|
|
|
|
|
|
|
closure("notifications", notifications);
|
2019-05-06 03:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
content, "redact", json::value(default_power_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
content, "state_default", json::value(default_power_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
json::stack::object users
|
|
|
|
{
|
|
|
|
content, "users"
|
|
|
|
};
|
|
|
|
|
2019-08-18 15:35:23 +02:00
|
|
|
closure("users", users);
|
2019-05-06 03:04:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(default_user_level == 0);
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
content, "users_default", json::value(default_user_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
content.~object();
|
|
|
|
return json::object{out.completed()};
|
2019-03-05 22:32:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room::power::power
|
|
|
|
//
|
|
|
|
|
2019-02-14 00:59:42 +01:00
|
|
|
ircd::m::room::power::power(const m::room &room)
|
|
|
|
:power
|
|
|
|
{
|
|
|
|
room, room.get(std::nothrow, "m.room.power_levels", "")
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::power::power(const m::room &room,
|
|
|
|
const event::idx &power_event_idx)
|
|
|
|
:room
|
|
|
|
{
|
|
|
|
room
|
|
|
|
}
|
|
|
|
,power_event_idx
|
|
|
|
{
|
|
|
|
power_event_idx
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::power::power(const m::event &power_event,
|
|
|
|
const m::event &create_event)
|
|
|
|
:power
|
|
|
|
{
|
|
|
|
power_event, m::user::id(unquote(json::get<"content"_>(create_event).get("creator")))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::power::power(const m::event &power_event,
|
|
|
|
const m::user::id &room_creator_id)
|
|
|
|
:power
|
|
|
|
{
|
|
|
|
json::get<"content"_>(power_event), room_creator_id
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::power::power(const json::object &power_event_content,
|
|
|
|
const m::user::id &room_creator_id)
|
|
|
|
:power_event_content
|
|
|
|
{
|
|
|
|
power_event_content
|
|
|
|
}
|
|
|
|
,room_creator_id
|
|
|
|
{
|
|
|
|
room_creator_id
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-26 00:22:22 +02:00
|
|
|
/// "all who attain great power and riches make use of either force or fraud"
|
|
|
|
///
|
|
|
|
/// Returns bool for "allow" or "deny"
|
|
|
|
///
|
|
|
|
/// Provide the user invoking the power. The return value indicates whether
|
|
|
|
/// they have the power.
|
|
|
|
///
|
|
|
|
/// Provide the property/event_type. There are two usages here: 1. This is a
|
|
|
|
/// string corresponding to one of the spec top-level properties like "ban"
|
|
|
|
/// and "redact". In this case, the type and state_key parameters to this
|
|
|
|
/// function are not used. 2. This string is empty or "events" in which case
|
|
|
|
/// the type parameter is used to fetch the power threshold for that type.
|
|
|
|
/// For state events of a type, the state_key must be provided for inspection
|
|
|
|
/// here as well.
|
2018-06-04 23:23:39 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::power::operator()(const m::user::id &user_id,
|
|
|
|
const string_view &prop,
|
2018-10-26 02:49:11 +02:00
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
2018-06-04 23:23:39 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &user_level
|
|
|
|
{
|
|
|
|
level_user(user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &required_level
|
|
|
|
{
|
2019-03-14 21:21:55 +01:00
|
|
|
empty(prop) || prop == "events"?
|
2018-10-27 23:27:53 +02:00
|
|
|
level_event(type, state_key):
|
2018-06-04 23:23:39 +02:00
|
|
|
level(prop)
|
|
|
|
};
|
|
|
|
|
|
|
|
return user_level >= required_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
ircd::m::room::power::level_user(const m::user::id &user_id)
|
|
|
|
const try
|
|
|
|
{
|
|
|
|
int64_t ret
|
|
|
|
{
|
|
|
|
default_user_level
|
|
|
|
};
|
|
|
|
|
2018-10-26 01:28:30 +02:00
|
|
|
const auto closure{[&user_id, &ret]
|
2018-06-04 23:23:39 +02:00
|
|
|
(const json::object &content)
|
|
|
|
{
|
2018-10-26 02:49:11 +02:00
|
|
|
const auto users_default
|
|
|
|
{
|
|
|
|
content.get<int64_t>("users_default", default_user_level)
|
|
|
|
};
|
|
|
|
|
2018-06-04 23:23:39 +02:00
|
|
|
const json::object &users
|
|
|
|
{
|
2019-02-28 05:31:39 +01:00
|
|
|
content.get("users")
|
2018-06-04 23:23:39 +02:00
|
|
|
};
|
|
|
|
|
2018-10-26 02:49:11 +02:00
|
|
|
ret = users.get<int64_t>(user_id, users_default);
|
2018-10-26 01:28:30 +02:00
|
|
|
}};
|
|
|
|
|
|
|
|
const bool has_power_levels_event
|
|
|
|
{
|
|
|
|
view(closure)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!has_power_levels_event)
|
2019-02-14 00:59:42 +01:00
|
|
|
{
|
|
|
|
if(room_creator_id && user_id == room_creator_id)
|
2018-10-26 01:28:30 +02:00
|
|
|
ret = default_creator_level;
|
2018-06-04 23:23:39 +02:00
|
|
|
|
2019-02-14 00:59:42 +01:00
|
|
|
if(room.room_id && creator(room, user_id))
|
|
|
|
ret = default_creator_level;
|
|
|
|
}
|
|
|
|
|
2018-06-04 23:23:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const json::error &e)
|
|
|
|
{
|
|
|
|
return default_user_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
ircd::m::room::power::level_event(const string_view &type)
|
|
|
|
const try
|
|
|
|
{
|
|
|
|
int64_t ret
|
|
|
|
{
|
|
|
|
default_event_level
|
|
|
|
};
|
|
|
|
|
2019-02-28 05:31:39 +01:00
|
|
|
const auto closure{[&type, &ret]
|
2018-06-04 23:23:39 +02:00
|
|
|
(const json::object &content)
|
|
|
|
{
|
2018-10-26 02:49:11 +02:00
|
|
|
const auto &events_default
|
|
|
|
{
|
|
|
|
content.get<int64_t>("events_default", default_event_level)
|
|
|
|
};
|
|
|
|
|
2018-06-04 23:23:39 +02:00
|
|
|
const json::object &events
|
|
|
|
{
|
2018-10-26 02:49:11 +02:00
|
|
|
content.get("events")
|
2018-06-04 23:23:39 +02:00
|
|
|
};
|
|
|
|
|
2018-10-26 02:49:11 +02:00
|
|
|
ret = events.get<int64_t>(type, events_default);
|
2019-02-28 05:31:39 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
const bool has_power_levels_event
|
|
|
|
{
|
|
|
|
view(closure)
|
|
|
|
};
|
2018-06-04 23:23:39 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const json::error &e)
|
|
|
|
{
|
|
|
|
return default_event_level;
|
|
|
|
}
|
|
|
|
|
2018-10-26 02:49:11 +02:00
|
|
|
int64_t
|
2018-10-27 23:27:53 +02:00
|
|
|
ircd::m::room::power::level_event(const string_view &type,
|
2018-10-26 02:49:11 +02:00
|
|
|
const string_view &state_key)
|
|
|
|
const try
|
|
|
|
{
|
2018-10-27 23:27:53 +02:00
|
|
|
if(!defined(state_key))
|
|
|
|
return level_event(type);
|
|
|
|
|
2018-10-26 02:49:11 +02:00
|
|
|
int64_t ret
|
|
|
|
{
|
|
|
|
default_power_level
|
|
|
|
};
|
|
|
|
|
2019-02-28 05:31:39 +01:00
|
|
|
const auto closure{[&type, &ret]
|
2018-10-26 02:49:11 +02:00
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const auto &state_default
|
|
|
|
{
|
|
|
|
content.get<int64_t>("state_default", default_power_level)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &events
|
|
|
|
{
|
|
|
|
content.get("events")
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = events.get<int64_t>(type, state_default);
|
2019-02-28 05:31:39 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
const bool has_power_levels_event
|
|
|
|
{
|
|
|
|
view(closure)
|
|
|
|
};
|
2018-10-26 02:49:11 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const json::error &e)
|
|
|
|
{
|
|
|
|
return default_power_level;
|
|
|
|
}
|
|
|
|
|
2018-06-04 23:23:39 +02:00
|
|
|
int64_t
|
|
|
|
ircd::m::room::power::level(const string_view &prop)
|
|
|
|
const try
|
|
|
|
{
|
|
|
|
int64_t ret
|
|
|
|
{
|
|
|
|
default_power_level
|
|
|
|
};
|
|
|
|
|
|
|
|
view([&prop, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
ret = content.at<int64_t>(prop);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const json::error &e)
|
|
|
|
{
|
|
|
|
return default_power_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::power::count_levels()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each([&ret]
|
|
|
|
(const string_view &, const int64_t &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::power::count_collections()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
view([&ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
for(const auto &member : content)
|
|
|
|
ret += json::type(member.second) == json::OBJECT;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::power::count(const string_view &prop)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for_each(prop, [&ret]
|
|
|
|
(const string_view &, const int64_t &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::has_event(const string_view &type)
|
|
|
|
const try
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
view([&type, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::object &events
|
|
|
|
{
|
|
|
|
content.at("events")
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &level
|
|
|
|
{
|
2019-02-14 21:51:33 +01:00
|
|
|
unquote(events.at(type))
|
2018-06-04 23:23:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = json::type(level) == json::NUMBER;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const json::error &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::has_user(const m::user::id &user_id)
|
|
|
|
const try
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
view([&user_id, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::object &users
|
|
|
|
{
|
|
|
|
content.at("users")
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &level
|
|
|
|
{
|
2019-02-14 21:51:33 +01:00
|
|
|
unquote(users.at(user_id))
|
2018-06-04 23:23:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = json::type(level) == json::NUMBER;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const json::error &)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::has_collection(const string_view &prop)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
view([&prop, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const auto &value{content.get(prop)};
|
|
|
|
if(value && json::type(value) == json::OBJECT)
|
|
|
|
ret = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::has_level(const string_view &prop)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
view([&prop, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
2019-02-14 21:51:33 +01:00
|
|
|
const auto &value(unquote(content.get(prop)));
|
2018-06-04 23:23:39 +02:00
|
|
|
if(value && json::type(value) == json::NUMBER)
|
|
|
|
ret = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::power::for_each(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::power::for_each(const string_view &prop,
|
|
|
|
const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(prop, closure_bool{[&closure]
|
|
|
|
(const string_view &key, const int64_t &level)
|
|
|
|
{
|
|
|
|
closure(key, level);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::for_each(const string_view &prop,
|
|
|
|
const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{true};
|
|
|
|
view([&prop, &closure, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::object &collection
|
|
|
|
{
|
|
|
|
// This little cmov gimmick sets collection to be the outer object
|
|
|
|
// itself if no property was given, allowing us to reuse this func
|
|
|
|
// for all iterations of key -> level mappings.
|
|
|
|
prop? json::object{content.get(prop)} : content
|
|
|
|
};
|
|
|
|
|
2019-08-07 11:44:47 +02:00
|
|
|
const string_view _collection{collection};
|
|
|
|
if(prop && (!_collection || json::type(_collection) != json::OBJECT))
|
2018-06-04 23:23:39 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
for(auto it(begin(collection)); it != end(collection) && ret; ++it)
|
|
|
|
{
|
|
|
|
const auto &member(*it);
|
2019-02-14 21:51:33 +01:00
|
|
|
if(json::type(unquote(member.second)) != json::NUMBER)
|
2018-06-04 23:23:39 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
unquote(member.first)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &val
|
|
|
|
{
|
|
|
|
lex_cast<int64_t>(member.second)
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = closure(key, val);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::power::view(const std::function<void (const json::object &)> &closure)
|
|
|
|
const
|
|
|
|
{
|
2019-02-14 00:59:42 +01:00
|
|
|
if(power_event_idx)
|
|
|
|
if(m::get(std::nothrow, power_event_idx, "content", closure))
|
|
|
|
return true;
|
2018-06-04 23:23:39 +02:00
|
|
|
|
2019-02-14 00:59:42 +01:00
|
|
|
closure(power_event_content);
|
|
|
|
return !empty(power_event_content);
|
2018-06-04 23:23:39 +02:00
|
|
|
}
|
2019-02-26 01:28:53 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// room::stats
|
|
|
|
//
|
|
|
|
|
|
|
|
size_t
|
2019-05-06 03:04:56 +02:00
|
|
|
__attribute__((noreturn))
|
|
|
|
ircd::m::room::stats::bytes_total(const m::room &room)
|
2019-02-26 01:28:53 +01:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
throw m::UNSUPPORTED
|
2019-02-26 01:28:53 +01:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
"Not yet implemented."
|
2019-02-26 01:28:53 +01:00
|
|
|
};
|
2019-05-06 03:04:56 +02:00
|
|
|
}
|
2019-02-26 01:28:53 +01:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
size_t
|
|
|
|
__attribute__((noreturn))
|
|
|
|
ircd::m::room::stats::bytes_total_compressed(const m::room &room)
|
|
|
|
{
|
|
|
|
throw m::UNSUPPORTED
|
|
|
|
{
|
|
|
|
"Not yet implemented."
|
|
|
|
};
|
2019-02-26 01:28:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::stats::bytes_json(const m::room &room)
|
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
size_t ret(0);
|
|
|
|
for(m::room::messages it(room); it; --it)
|
|
|
|
{
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
|
|
|
it.event_idx()
|
|
|
|
};
|
2019-02-26 01:28:53 +01:00
|
|
|
|
2019-05-06 03:04:56 +02:00
|
|
|
const byte_view<string_view> key
|
|
|
|
{
|
|
|
|
event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
static const db::gopts gopts
|
|
|
|
{
|
|
|
|
db::get::NO_CACHE
|
|
|
|
};
|
|
|
|
|
|
|
|
ret += db::bytes_value(m::dbs::event_json, key, gopts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
__attribute__((noreturn))
|
|
|
|
ircd::m::room::stats::bytes_json_compressed(const m::room &room)
|
|
|
|
{
|
|
|
|
throw m::UNSUPPORTED
|
2019-02-26 01:28:53 +01:00
|
|
|
{
|
2019-05-06 03:04:56 +02:00
|
|
|
"Not yet implemented."
|
2019-02-26 01:28:53 +01:00
|
|
|
};
|
2019-05-06 03:04:56 +02:00
|
|
|
}
|