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
|
|
|
}
|
|
|
|
|
|
|
|
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::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,
|
2019-10-06 00:50:18 +02:00
|
|
|
const user::id &target,
|
|
|
|
const user::id &sender,
|
2019-05-01 00:24:22 +02:00
|
|
|
json::iov &content)
|
|
|
|
{
|
2019-10-06 00:50:18 +02:00
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-10-06 00:50:18 +02:00
|
|
|
{ event, { "type", "m.room.member" }},
|
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "state_key", target }},
|
|
|
|
{ content, { "membership", "invite" }},
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-10-06 00:50:18 +02:00
|
|
|
return commit(room, event, content);
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::redact(const room &room,
|
|
|
|
const id::user &sender,
|
|
|
|
const id::event &event_id,
|
|
|
|
const string_view &reason)
|
|
|
|
{
|
2019-09-16 19:55:23 +02:00
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.room.redaction" }},
|
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "redacts", event_id }},
|
|
|
|
};
|
2019-05-01 00:24:22 +02:00
|
|
|
|
2019-09-16 19:55:23 +02:00
|
|
|
json::iov content;
|
|
|
|
const json::iov::set _reason
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-09-16 19:55:23 +02:00
|
|
|
content, !empty(reason),
|
|
|
|
{
|
|
|
|
"reason", [&reason]() -> json::value
|
|
|
|
{
|
|
|
|
return reason;
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 00:24:22 +02:00
|
|
|
};
|
|
|
|
|
2019-09-16 19:55:23 +02:00
|
|
|
return commit(room, event, content);
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::notice(const room &room,
|
|
|
|
const string_view &body)
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
return message(room, me(), body, "m.notice");
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12-11 20:40:54 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::annotate(const room &room,
|
|
|
|
const id::user &sender,
|
|
|
|
const id::event &target,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
json::iov relates;
|
|
|
|
const json::iov::push push
|
|
|
|
{
|
|
|
|
relates, { "key", key },
|
|
|
|
};
|
|
|
|
|
|
|
|
return react(room, sender, target, "m.annotation", relates);
|
|
|
|
}
|
|
|
|
|
2019-12-11 20:40:12 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::react(const room &room,
|
|
|
|
const id::user &sender,
|
|
|
|
const id::event &target,
|
|
|
|
const string_view &rel_type,
|
|
|
|
json::iov &relates)
|
|
|
|
{
|
|
|
|
json::iov content;
|
|
|
|
thread_local char buf[event::MAX_SIZE];
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ relates, { "event_id", target }},
|
|
|
|
{ relates, { "rel_type", rel_type }},
|
|
|
|
{ content, { "m.relates_to", stringify(mutable_buffer{buf}, relates) }},
|
|
|
|
};
|
|
|
|
|
|
|
|
return send(room, sender, "m.reaction", content);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-09-10 04:25:41 +02:00
|
|
|
ircd::m::room_id(const id::event &event_id)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
|
|
|
char buf[m::id::MAX_SIZE + 1];
|
|
|
|
static_assert(sizeof(buf) <= 256);
|
2019-09-10 04:25:41 +02:00
|
|
|
return room_id(buf, event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::room_id(const string_view &mxid)
|
|
|
|
{
|
|
|
|
char buf[m::id::MAX_SIZE + 1];
|
|
|
|
static_assert(sizeof(buf) <= 256);
|
|
|
|
return room_id(buf, mxid);
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-24 04:31:21 +01:00
|
|
|
ircd::m::id::room::buf
|
|
|
|
ircd::m::room_id(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
char buf[m::id::MAX_SIZE + 1];
|
|
|
|
static_assert(sizeof(buf) <= 256);
|
|
|
|
return room_id(buf, event_idx);
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::room_id(const mutable_buffer &out,
|
2019-09-10 04:25:41 +02:00
|
|
|
const string_view &mxid)
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
2019-09-10 04:25:41 +02:00
|
|
|
switch(m::sigil(mxid))
|
2019-05-01 00:24:22 +02:00
|
|
|
{
|
|
|
|
case id::ROOM:
|
2019-09-10 04:25:41 +02:00
|
|
|
return id::room{out, mxid};
|
2019-05-01 00:24:22 +02:00
|
|
|
|
|
|
|
case id::USER:
|
|
|
|
{
|
2019-09-10 04:25:41 +02:00
|
|
|
const m::user::room user_room(mxid);
|
2019-05-01 00:24:22 +02:00
|
|
|
return string_view{data(out), copy(out, user_room.room_id)};
|
|
|
|
}
|
|
|
|
|
2019-08-12 07:51:26 +02:00
|
|
|
case id::NODE:
|
|
|
|
{
|
2019-09-10 04:25:41 +02:00
|
|
|
const m::node node(lstrip(mxid, ':'));
|
2019-08-12 07:51:26 +02:00
|
|
|
return node.room_id(out);
|
|
|
|
}
|
|
|
|
|
2019-09-10 04:25:41 +02:00
|
|
|
case id::EVENT:
|
|
|
|
return room_id(out, id::event{mxid});
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
default:
|
2019-09-10 04:25:41 +02:00
|
|
|
return room_id(out, id::room_alias{mxid});
|
2019-05-01 00:24:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-24 04:31:21 +01:00
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::room_id(const mutable_buffer &out,
|
|
|
|
const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
room::id ret;
|
|
|
|
m::get(event_idx, "room_id", [&out, &ret]
|
|
|
|
(const room::id &room_id)
|
|
|
|
{
|
|
|
|
ret = string_view { data(out), copy(out, room_id) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-10 04:25:41 +02:00
|
|
|
ircd::m::id::room
|
|
|
|
ircd::m::room_id(const mutable_buffer &out,
|
|
|
|
const id::event &event_id)
|
|
|
|
{
|
|
|
|
room::id ret;
|
|
|
|
m::get(event_id, "room_id", [&out, &ret]
|
|
|
|
(const room::id &room_id)
|
|
|
|
{
|
|
|
|
ret = string_view { data(out), copy(out, room_id) };
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-01 00:24:22 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
//
|
|
|
|
|
2019-09-08 01:06:45 +02:00
|
|
|
/// The only members are from our origin, in any membership state. This
|
|
|
|
/// indicates we won't have any other federation servers that could possibly
|
|
|
|
/// be party to anything about this room.
|
2019-08-14 10:01:46 +02:00
|
|
|
bool
|
|
|
|
ircd::m::local_only(const room &room)
|
2019-09-08 01:06:45 +02:00
|
|
|
{
|
2019-09-08 01:55:44 +02:00
|
|
|
// Branch to test if any remote users are joined to the room, meaning
|
|
|
|
// this result must be false; this is a fast query.
|
|
|
|
if(remote_joined(room))
|
|
|
|
return false;
|
|
|
|
|
2019-09-08 01:06:45 +02:00
|
|
|
const room::members members
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-09-08 01:55:44 +02:00
|
|
|
return members.for_each([]
|
|
|
|
(const id::user &user_id)
|
2019-09-08 01:06:45 +02:00
|
|
|
{
|
|
|
|
return my(user_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Member(s) from our server are presently joined to the room. Returns false
|
|
|
|
/// if there's a room on the server where all of our users have left. Note that
|
|
|
|
/// some internal rooms have no memberships at all and this will also be false.
|
|
|
|
/// This can return true if other servers have memberships in the room too, as
|
|
|
|
/// long as one of our users is joined.
|
|
|
|
bool
|
|
|
|
ircd::m::local_joined(const room &room)
|
|
|
|
{
|
2019-09-08 01:55:44 +02:00
|
|
|
const room::members members
|
2019-09-08 01:06:45 +02:00
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
return !for_each([&members]
|
|
|
|
(const homeserver &homeserver)
|
|
|
|
{
|
|
|
|
// return false to break and return false; true to continue
|
|
|
|
return members.empty("join", origin(homeserver));
|
|
|
|
});
|
2019-09-08 01:06:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Member(s) from another server are presently joined to the room. For example
|
|
|
|
/// if another user leaves a PM with our user who is still joined, this returns
|
|
|
|
/// false. This can return true even if the room has no memberships in any
|
|
|
|
/// state from our server, as long as there's a joined member from a remote.
|
|
|
|
bool
|
|
|
|
ircd::m::remote_joined(const room &room)
|
2019-08-14 10:01:46 +02:00
|
|
|
{
|
2019-09-08 01:55:44 +02:00
|
|
|
const room::members members
|
2019-08-14 10:01:46 +02:00
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2019-09-08 01:55:44 +02:00
|
|
|
return !members.for_each("join", []
|
|
|
|
(const id::user &user_id)
|
|
|
|
{
|
|
|
|
return my(user_id)? true : false; // false to break.
|
|
|
|
});
|
2019-08-14 10:01:46 +02:00
|
|
|
}
|
|
|
|
|
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::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-10-01 05:50:58 +02:00
|
|
|
if(!creator(room, me()))
|
2019-07-19 23:27:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const id::room &room_id)
|
|
|
|
{
|
2019-09-16 22:43:46 +02:00
|
|
|
const m::room::events it
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2019-09-16 22:43:46 +02:00
|
|
|
room_id, 0UL
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2019-09-16 03:06:55 +02:00
|
|
|
if(!it)
|
|
|
|
return false;
|
|
|
|
|
2019-09-16 22:43:46 +02:00
|
|
|
if(likely(it.depth() < 2UL))
|
2019-09-16 03:06:55 +02:00
|
|
|
return true;
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
if(my_host(room_id.host()) && creator(room_id, me()))
|
2019-09-16 03:06:55 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
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};
|
2019-08-30 23:26:07 +02:00
|
|
|
room::events it
|
2019-02-05 03:11:05 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
2019-08-30 23:26:07 +02:00
|
|
|
events it{*this};
|
2018-04-12 11:14:10 +02:00
|
|
|
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
|
|
|
}
|