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-09-08 11:32:49 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_M_ROOM_H
|
2017-10-25 18:47:03 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wsubobject-linkage"
|
2017-09-08 11:32:49 +02:00
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
IRCD_M_EXCEPTION(m::error, CONFLICT, http::CONFLICT);
|
|
|
|
IRCD_M_EXCEPTION(m::error, NOT_MODIFIED, http::NOT_MODIFIED);
|
|
|
|
IRCD_M_EXCEPTION(CONFLICT, ALREADY_MEMBER, http::CONFLICT);
|
|
|
|
|
|
|
|
struct room;
|
2017-10-25 18:47:03 +02:00
|
|
|
|
2018-02-13 23:23:56 +01:00
|
|
|
// Util
|
2017-12-12 21:33:14 +01:00
|
|
|
bool my(const room &);
|
2018-02-13 23:23:56 +01:00
|
|
|
|
|
|
|
// [GET] Util
|
2017-11-30 19:51:01 +01:00
|
|
|
bool exists(const id::room &);
|
2017-10-25 18:47:03 +02:00
|
|
|
|
2018-02-26 13:55:27 +01:00
|
|
|
id::room room_id(const mutable_buffer &, const id::room_alias &);
|
|
|
|
id::room::buf room_id(const id::room_alias &);
|
|
|
|
|
2018-02-09 05:59:48 +01:00
|
|
|
// [GET] Current Event suite (non-locking) (one)
|
2018-02-20 04:37:09 +01:00
|
|
|
id::event::buf head(std::nothrow_t, const id::room &, int64_t &);
|
|
|
|
id::event::buf head(const id::room &, uint64_t &);
|
|
|
|
id::event::buf head(std::nothrow_t, const id::room &);
|
2018-02-09 05:59:48 +01:00
|
|
|
id::event::buf head(const id::room &);
|
2018-02-20 04:37:09 +01:00
|
|
|
int64_t depth(std::nothrow_t, const id::room &);
|
|
|
|
uint64_t depth(const id::room &);
|
2018-02-09 05:59:48 +01:00
|
|
|
|
|
|
|
// [SET] Lowest-level
|
2018-01-26 21:23:28 +01:00
|
|
|
event::id::buf commit(const room &, json::iov &event, const json::iov &content);
|
|
|
|
|
2018-02-09 08:18:42 +01:00
|
|
|
// [SET] Send state to room
|
2018-01-26 21:23:28 +01:00
|
|
|
event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const string_view &state_key, const json::iov &content);
|
|
|
|
event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const string_view &state_key, const json::members &content);
|
|
|
|
event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const string_view &state_key, const json::object &content);
|
|
|
|
|
2018-02-09 08:18:42 +01:00
|
|
|
// [SET] Send non-state to room
|
2018-01-26 21:23:28 +01:00
|
|
|
event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const json::iov &content);
|
|
|
|
event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const json::members &content);
|
|
|
|
event::id::buf send(const room &, const m::id::user &sender, const string_view &type, const json::object &content);
|
|
|
|
|
2018-02-09 08:18:42 +01:00
|
|
|
// [SET] Convenience sends
|
2018-02-15 23:57:56 +01:00
|
|
|
event::id::buf redact(const room &, const m::id::user &sender, const m::id::event &, const string_view &reason);
|
2018-01-26 21:23:28 +01:00
|
|
|
event::id::buf message(const room &, const m::id::user &sender, const json::members &content);
|
|
|
|
event::id::buf message(const room &, const m::id::user &sender, const string_view &body, const string_view &msgtype = "m.text");
|
|
|
|
event::id::buf leave(const room &, const m::id::user &);
|
|
|
|
event::id::buf join(const room &, const m::id::user &);
|
|
|
|
|
2018-02-09 08:18:42 +01:00
|
|
|
// [SET] Create new room
|
2017-11-30 19:51:01 +01:00
|
|
|
room create(const id::room &, const id::user &creator, const id::room &parent, const string_view &type);
|
|
|
|
room create(const id::room &, const id::user &creator, const string_view &type = {});
|
2017-09-08 11:32:49 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
/// Interface to a room.
|
|
|
|
///
|
|
|
|
/// This is a lightweight object which uses a room_id and an optional event_id
|
|
|
|
/// to provide an interface to a matrix room. This object itself isn't the
|
|
|
|
/// actual room data since that takes the form of events in the database;
|
|
|
|
/// this is just a handle with aforementioned string_view's used by its member
|
|
|
|
/// functions.
|
|
|
|
///
|
|
|
|
/// This object allows the programmer to represent the room either at its
|
|
|
|
/// present state, or if an event_id is given, at the point of that event.
|
|
|
|
///
|
2018-02-13 23:23:56 +01:00
|
|
|
/// Many convenience functions are provided outside of this class.
|
|
|
|
/// Additionally, several sub-classes provide functionality even more specific
|
|
|
|
/// than this interface too. If a subclass is provided, for example:
|
|
|
|
/// `struct members`, such an interface may employ optimized tactics for its
|
|
|
|
/// specific task.
|
2017-12-12 21:33:14 +01:00
|
|
|
///
|
2017-09-08 11:32:49 +02:00
|
|
|
struct ircd::m::room
|
|
|
|
{
|
2018-02-11 04:50:53 +01:00
|
|
|
struct messages;
|
2017-10-25 18:47:03 +02:00
|
|
|
struct state;
|
2017-09-26 07:17:05 +02:00
|
|
|
struct members;
|
2018-02-10 09:25:20 +01:00
|
|
|
struct origins;
|
2017-09-08 11:32:49 +02:00
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
using id = m::id::room;
|
2017-12-12 21:33:14 +01:00
|
|
|
using alias = m::id::room_alias;
|
2017-09-08 11:32:49 +02:00
|
|
|
|
2017-09-25 03:05:42 +02:00
|
|
|
id room_id;
|
2017-12-12 21:33:14 +01:00
|
|
|
event::id event_id;
|
2017-09-08 11:32:49 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
operator const id &() const { return room_id; }
|
2017-10-25 18:47:03 +02:00
|
|
|
|
2018-02-11 02:57:22 +01:00
|
|
|
// Convenience passthru to room::state (logarithmic query)
|
2018-02-11 06:42:16 +01:00
|
|
|
bool has(const string_view &type, const string_view &state_key) const;
|
2018-02-11 02:57:22 +01:00
|
|
|
bool get(std::nothrow_t, const string_view &type, const string_view &state_key, const event::closure &) const;
|
|
|
|
void get(const string_view &type, const string_view &state_key, const event::closure &) const;
|
|
|
|
|
2018-02-16 04:17:09 +01:00
|
|
|
// Convenience passthru to room::messages (linear query)
|
|
|
|
bool get(std::nothrow_t, const string_view &type, const event::closure &) const;
|
|
|
|
void get(const string_view &type, const event::closure &) const;
|
|
|
|
bool has(const string_view &type) const;
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
// misc
|
2017-11-30 19:51:01 +01:00
|
|
|
bool membership(const m::id::user &, const string_view &membership = "join") const;
|
2017-10-03 13:12:54 +02:00
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
room(const id &room_id, const event::id &event_id = {})
|
2017-09-25 03:05:42 +02:00
|
|
|
:room_id{room_id}
|
2017-12-12 21:33:14 +01:00
|
|
|
,event_id{event_id}
|
|
|
|
{}
|
2018-02-15 21:11:28 +01:00
|
|
|
|
|
|
|
room() = default;
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
2018-02-11 04:50:53 +01:00
|
|
|
/// Interface to room messages
|
|
|
|
///
|
2018-02-13 23:25:01 +01:00
|
|
|
/// This interface has the form of an STL-style iterator over room messages
|
|
|
|
/// which are state and non-state events from all integrated timelines.
|
|
|
|
/// Moving the iterator is cheap, but the dereference operators fetch a
|
|
|
|
/// full event. One can iterate just event_id's by using event_id() instead
|
|
|
|
/// of the dereference operators.
|
2018-02-11 04:50:53 +01:00
|
|
|
///
|
|
|
|
struct ircd::m::room::messages
|
|
|
|
{
|
|
|
|
m::room room;
|
2018-02-13 23:25:01 +01:00
|
|
|
db::index::const_iterator it;
|
|
|
|
event::id _event_id;
|
|
|
|
event::fetch _event;
|
2018-02-11 04:50:53 +01:00
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
public:
|
|
|
|
operator bool() const { return bool(it); }
|
|
|
|
bool operator!() const { return !it; }
|
2018-02-11 04:50:53 +01:00
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
const event::id &event_id();
|
|
|
|
const m::event &fetch(std::nothrow_t);
|
|
|
|
const m::event &fetch();
|
2018-02-11 04:50:53 +01:00
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
bool seek(const uint64_t &depth);
|
|
|
|
bool seek(const event::id &);
|
|
|
|
bool seek();
|
|
|
|
|
|
|
|
// These are reversed on purpose
|
|
|
|
auto &operator++() { return --it; }
|
|
|
|
auto &operator--() { return ++it; }
|
|
|
|
|
|
|
|
const m::event &operator*();
|
|
|
|
const m::event *operator->() { return &operator*(); }
|
|
|
|
|
|
|
|
messages(const m::room &room, const uint64_t &depth);
|
|
|
|
messages(const m::room &room, const event::id &);
|
|
|
|
messages(const m::room &room);
|
2018-02-15 21:11:28 +01:00
|
|
|
messages() = default;
|
|
|
|
messages(const messages &) = delete;
|
|
|
|
messages &operator=(const messages &) = delete;
|
2018-02-11 04:50:53 +01:00
|
|
|
};
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
/// Interface to room state.
|
|
|
|
///
|
2018-02-11 04:50:53 +01:00
|
|
|
/// This interface focuses specifically on the details of room state. Most of
|
|
|
|
/// the queries to this interface respond in logarithmic time. Note that all
|
|
|
|
/// iterations are over the state tree. If an event with a state_key is present
|
|
|
|
/// in room::messages but it is not present in room::state (state tree) it was
|
|
|
|
/// accepted into the room but we will not apply it to our machine, though
|
|
|
|
/// other parties may (this is a state-conflict).
|
2018-02-10 09:25:20 +01:00
|
|
|
///
|
|
|
|
struct ircd::m::room::state
|
|
|
|
{
|
|
|
|
struct tuple;
|
|
|
|
|
2018-02-11 02:34:16 +01:00
|
|
|
room::id room_id;
|
|
|
|
event::id::buf event_id;
|
|
|
|
m::state::id_buffer root_id_buf;
|
|
|
|
m::state::id root_id;
|
2018-02-10 09:25:20 +01:00
|
|
|
|
|
|
|
// Iterate the state; for_each protocol
|
|
|
|
void for_each(const string_view &type, const event::id::closure &) const;
|
|
|
|
void for_each(const string_view &type, const event::closure &) const;
|
|
|
|
void for_each(const event::id::closure &) const;
|
|
|
|
void for_each(const event::closure &) const;
|
|
|
|
|
|
|
|
// Iterate the state; test protocol
|
2018-02-24 04:38:56 +01:00
|
|
|
bool test(const string_view &type, const string_view &lower_bound, const event::id::closure_bool &view) const;
|
|
|
|
bool test(const string_view &type, const string_view &lower_bound, const event::closure_bool &view) const;
|
2018-02-10 09:25:20 +01:00
|
|
|
bool test(const string_view &type, const event::id::closure_bool &view) const;
|
|
|
|
bool test(const string_view &type, const event::closure_bool &view) const;
|
|
|
|
bool test(const event::id::closure_bool &view) const;
|
|
|
|
bool test(const event::closure_bool &view) const;
|
|
|
|
|
2018-02-25 04:52:02 +01:00
|
|
|
// Counting / Statistics
|
|
|
|
size_t count(const string_view &type) const;
|
|
|
|
size_t count() const;
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
// Existential
|
|
|
|
bool has(const string_view &type, const string_view &state_key) const;
|
|
|
|
bool has(const string_view &type) const;
|
|
|
|
|
|
|
|
// Fetch a state event
|
|
|
|
bool get(std::nothrow_t, const string_view &type, const string_view &state_key, const event::id::closure &) const;
|
|
|
|
bool get(std::nothrow_t, const string_view &type, const string_view &state_key, const event::closure &) const;
|
|
|
|
void get(const string_view &type, const string_view &state_key, const event::id::closure &) const;
|
|
|
|
void get(const string_view &type, const string_view &state_key, const event::closure &) const;
|
|
|
|
|
2018-02-11 02:34:16 +01:00
|
|
|
// Refresh the cached root_id/root_id_buf members
|
|
|
|
const m::state::id &refresh();
|
|
|
|
|
|
|
|
state(const m::room &);
|
2018-02-15 21:11:28 +01:00
|
|
|
state() = default;
|
|
|
|
state(const state &) = delete;
|
|
|
|
state &operator=(const state &) = delete;
|
2018-02-10 09:25:20 +01:00
|
|
|
};
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
/// Interface to the members of a room.
|
|
|
|
///
|
|
|
|
/// This interface focuses specifically on room membership and its routines
|
|
|
|
/// are optimized for this area of room functionality.
|
|
|
|
///
|
|
|
|
struct ircd::m::room::members
|
|
|
|
{
|
|
|
|
m::room room;
|
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
bool test(const string_view &membership, const event::closure_bool &view) const;
|
|
|
|
bool test(const event::closure_bool &view) const;
|
|
|
|
|
|
|
|
void for_each(const string_view &membership, const event::closure &view) const;
|
|
|
|
void for_each(const event::closure &view) const;
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-02-15 21:11:28 +01:00
|
|
|
members(const m::room &room)
|
2017-12-12 21:33:14 +01:00
|
|
|
:room{room}
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
/// Interface to the origins (autonomous systems) of a room
|
2017-12-12 21:33:14 +01:00
|
|
|
///
|
2018-02-10 09:25:20 +01:00
|
|
|
/// This interface focuses specifically on the origins (from the field in the
|
|
|
|
/// event object) which are servers/networks/autonomous systems, or something.
|
|
|
|
/// Messages have to be sent to them, and an efficient iteration of the
|
|
|
|
/// origins as provided by this interface helps with that.
|
2017-12-12 21:33:14 +01:00
|
|
|
///
|
2018-02-10 09:25:20 +01:00
|
|
|
struct ircd::m::room::origins
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
using closure = std::function<void (const string_view &)>;
|
|
|
|
using closure_bool = std::function<bool (const string_view &)>;
|
|
|
|
|
|
|
|
m::room room;
|
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
bool test(const closure_bool &view) const;
|
|
|
|
void for_each(const closure &view) const;
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-02-15 21:11:28 +01:00
|
|
|
origins(const m::room &room)
|
2017-12-12 21:33:14 +01:00
|
|
|
:room{room}
|
2017-09-25 03:05:42 +02:00
|
|
|
{}
|
2017-09-08 11:32:49 +02:00
|
|
|
};
|
2017-10-25 18:47:03 +02:00
|
|
|
|
2017-11-16 02:48:25 +01:00
|
|
|
/// Tuple to represent fundamental room state singletons (state_key = "")
|
|
|
|
///
|
|
|
|
/// This is not a complete representation of room state. Missing from here
|
|
|
|
/// are any state events with a state_key which is not an empty string. When
|
|
|
|
/// the state_key is not "", multiple events of that type contribute to the
|
|
|
|
/// room's eigenvalue. Additionally, state events which are not related to
|
|
|
|
/// the matrix protocol `m.room.*` are not represented here.
|
|
|
|
///
|
|
|
|
/// NOTE that in C++-land state_key="" is represented as a valid but empty
|
|
|
|
/// character pointer. It is not a string containing "". Testing for a
|
|
|
|
/// "" state_key `ircd::defined(string_view) && ircd::empty(string_view)`
|
|
|
|
/// analogous to `data() && !*data()`. A default constructed string_view{}
|
|
|
|
/// is considered "JS undefined" because the character pointers are null.
|
|
|
|
/// A "JS null" is rarer and carried with a hack which will not be discussed
|
|
|
|
/// here.
|
|
|
|
///
|
2018-02-10 09:25:20 +01:00
|
|
|
struct ircd::m::room::state::tuple
|
2017-10-25 18:47:03 +02:00
|
|
|
:json::tuple
|
|
|
|
<
|
2018-01-26 21:22:06 +01:00
|
|
|
json::property<name::m_room_aliases, event>,
|
|
|
|
json::property<name::m_room_canonical_alias, event>,
|
|
|
|
json::property<name::m_room_create, event>,
|
|
|
|
json::property<name::m_room_join_rules, event>,
|
|
|
|
json::property<name::m_room_power_levels, event>,
|
|
|
|
json::property<name::m_room_message, event>,
|
|
|
|
json::property<name::m_room_name, event>,
|
|
|
|
json::property<name::m_room_topic, event>,
|
|
|
|
json::property<name::m_room_avatar, event>,
|
|
|
|
json::property<name::m_room_pinned_events, event>,
|
|
|
|
json::property<name::m_room_history_visibility, event>,
|
|
|
|
json::property<name::m_room_third_party_invite, event>,
|
|
|
|
json::property<name::m_room_guest_access, event>
|
2017-10-25 18:47:03 +02:00
|
|
|
>
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
using super_type::tuple;
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
tuple(const json::array &pdus);
|
|
|
|
tuple(const m::room &, const mutable_buffer &);
|
|
|
|
tuple() = default;
|
2017-11-16 02:48:25 +01:00
|
|
|
using super_type::operator=;
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
friend std::string pretty(const room::state::tuple &);
|
|
|
|
friend std::string pretty_oneline(const room::state::tuple &);
|
2017-11-16 02:48:25 +01:00
|
|
|
};
|
|
|
|
|
2017-10-25 18:47:03 +02:00
|
|
|
#pragma GCC diagnostic pop
|