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
|
|
|
|
|
|
|
#include <ircd/m/m.h>
|
|
|
|
|
|
|
|
const ircd::m::room::id::buf
|
|
|
|
init_room_id
|
|
|
|
{
|
|
|
|
"init", ircd::my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::create(const id::room &room_id,
|
|
|
|
const id::user &creator,
|
|
|
|
const string_view &type)
|
|
|
|
{
|
|
|
|
return create(room_id, creator, init_room_id, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room
|
|
|
|
ircd::m::create(const id::room &room_id,
|
|
|
|
const id::user &creator,
|
|
|
|
const id::room &parent,
|
|
|
|
const string_view &type)
|
|
|
|
{
|
|
|
|
json::iov event;
|
|
|
|
json::iov content;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "sender", creator }},
|
|
|
|
{ content, { "creator", creator }},
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add_if _parent
|
|
|
|
{
|
|
|
|
content, !parent.empty() && parent.local() != "init",
|
|
|
|
{
|
|
|
|
"parent", parent
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::iov::add_if _type
|
|
|
|
{
|
|
|
|
content, !type.empty() && type != "room",
|
|
|
|
{
|
|
|
|
"type", type
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov::set _set[]
|
|
|
|
{
|
2018-02-10 05:09:37 +01:00
|
|
|
{ event, { "depth", 0L }},
|
2017-12-12 21:33:14 +01:00
|
|
|
{ event, { "type", "m.room.create" }},
|
|
|
|
{ event, { "state_key", "" }},
|
|
|
|
};
|
|
|
|
|
|
|
|
room room
|
|
|
|
{
|
|
|
|
room_id
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:23:28 +01:00
|
|
|
commit(room, event, content);
|
2017-12-12 21:33:14 +01:00
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::join(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &user_id)
|
|
|
|
{
|
|
|
|
return membership(room, user_id, "join");
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::leave(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &user_id)
|
|
|
|
{
|
|
|
|
return membership(room, user_id, "leave");
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::membership(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &user_id,
|
|
|
|
const string_view &membership)
|
|
|
|
{
|
|
|
|
json::iov event;
|
|
|
|
json::iov content;
|
|
|
|
json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.room.member" }},
|
|
|
|
{ event, { "sender", user_id }},
|
|
|
|
{ event, { "state_key", user_id }},
|
|
|
|
{ event, { "membership", membership }},
|
|
|
|
{ content, { "membership", membership }},
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:23:28 +01:00
|
|
|
return commit(room, event, content);
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::message(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &body,
|
|
|
|
const string_view &msgtype)
|
|
|
|
{
|
|
|
|
return message(room, sender,
|
|
|
|
{
|
2018-02-13 04:52:10 +01:00
|
|
|
{ "body", { body, json::STRING } },
|
|
|
|
{ "msgtype", { msgtype, json::STRING } },
|
2017-12-12 21:33:14 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::message(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const json::members &contents)
|
|
|
|
{
|
|
|
|
return send(room, sender, "m.room.message", contents);
|
|
|
|
}
|
|
|
|
|
2018-02-15 23:57:56 +01:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::redact(const room &room,
|
|
|
|
const m::id::user &sender,
|
|
|
|
const m::id::event &event_id,
|
|
|
|
const string_view &reason)
|
|
|
|
{
|
|
|
|
json::iov event;
|
|
|
|
json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "type", "m.room.redaction" }},
|
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "redacts", event_id }},
|
|
|
|
};
|
|
|
|
|
|
|
|
json::iov content;
|
|
|
|
json::iov::set_if _reason
|
|
|
|
{
|
|
|
|
content, !empty(reason),
|
|
|
|
{
|
|
|
|
"reason", reason
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return commit(room, event, content);
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::send(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const json::members &contents)
|
|
|
|
{
|
2018-02-19 06:14:45 +01:00
|
|
|
json::iov _content;
|
2018-01-26 18:59:47 +01:00
|
|
|
json::iov::push content[contents.size()];
|
2018-02-19 06:14:45 +01:00
|
|
|
return send(room, sender, type, state_key, make_iov(_content, content, contents.size(), contents));
|
2018-01-26 18:59:47 +01:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-01-26 18:59:47 +01:00
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::send(const room &room,
|
2018-01-26 18:59:47 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const json::object &contents)
|
|
|
|
{
|
2018-02-19 06:14:45 +01:00
|
|
|
json::iov _content;
|
2018-01-26 18:59:47 +01:00
|
|
|
json::iov::push content[contents.size()];
|
2018-02-19 06:14:45 +01:00
|
|
|
return send(room, sender, type, state_key, make_iov(_content, content, contents.size(), contents));
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::send(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const json::iov &content)
|
|
|
|
{
|
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "type", type }},
|
|
|
|
{ event, { "state_key", state_key }},
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:23:28 +01:00
|
|
|
return commit(room, event, content);
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::send(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const json::members &contents)
|
|
|
|
{
|
2018-02-19 06:14:45 +01:00
|
|
|
json::iov _content;
|
2018-01-26 18:59:47 +01:00
|
|
|
json::iov::push content[contents.size()];
|
2018-02-19 06:14:45 +01:00
|
|
|
return send(room, sender, type, make_iov(_content, content, contents.size(), contents));
|
2018-01-26 18:59:47 +01:00
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-01-26 18:59:47 +01:00
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::send(const room &room,
|
2018-01-26 18:59:47 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const json::object &contents)
|
|
|
|
{
|
2018-02-19 06:14:45 +01:00
|
|
|
json::iov _content;
|
2018-01-26 18:59:47 +01:00
|
|
|
json::iov::push content[contents.count()];
|
2018-02-19 06:14:45 +01:00
|
|
|
return send(room, sender, type, make_iov(_content, content, contents.count(), contents));
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
2018-01-26 21:23:28 +01:00
|
|
|
ircd::m::send(const room &room,
|
2017-12-12 21:33:14 +01:00
|
|
|
const m::id::user &sender,
|
|
|
|
const string_view &type,
|
|
|
|
const json::iov &content)
|
|
|
|
{
|
|
|
|
json::iov event;
|
|
|
|
const json::iov::push push[]
|
|
|
|
{
|
|
|
|
{ event, { "sender", sender }},
|
|
|
|
{ event, { "type", type }},
|
|
|
|
};
|
|
|
|
|
2018-01-26 21:23:28 +01:00
|
|
|
return commit(room, event, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::commit(const room &room,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &contents)
|
|
|
|
{
|
2018-02-09 08:18:42 +01:00
|
|
|
const json::iov::push room_id
|
2018-01-26 21:23:28 +01:00
|
|
|
{
|
2018-02-09 08:18:42 +01:00
|
|
|
event, { "room_id", room.room_id }
|
|
|
|
};
|
|
|
|
|
|
|
|
int64_t depth;
|
2018-02-20 04:37:09 +01:00
|
|
|
const m::event::id::buf prev_event_id
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2018-02-20 04:37:09 +01:00
|
|
|
head(std::nothrow, room.room_id, depth)
|
2018-02-09 08:18:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: LCOCK
|
|
|
|
const json::iov::set_if depth_
|
|
|
|
{
|
|
|
|
event, !event.has("depth"),
|
|
|
|
{
|
|
|
|
"depth", depth + 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view auth_events{};
|
|
|
|
const string_view prev_state{};
|
|
|
|
|
|
|
|
json::value prev_event0[]
|
|
|
|
{
|
|
|
|
prev_event_id
|
|
|
|
};
|
|
|
|
|
|
|
|
json::value prev_events[]
|
|
|
|
{
|
|
|
|
{ prev_event0, 1 }
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: LOLCK
|
|
|
|
const json::iov::push prevs[]
|
|
|
|
{
|
|
|
|
{ event, { "auth_events", auth_events }},
|
|
|
|
{ event, { "prev_state", prev_state }},
|
|
|
|
{ event, { "prev_events", { prev_events, 1 } } },
|
2018-01-26 21:23:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return m::vm::commit(event, contents);
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 04:37:09 +01:00
|
|
|
uint64_t
|
2018-02-09 05:59:48 +01:00
|
|
|
ircd::m::depth(const id::room &room_id)
|
|
|
|
{
|
2018-02-20 04:37:09 +01:00
|
|
|
uint64_t depth;
|
2018-02-09 05:59:48 +01:00
|
|
|
head(room_id, depth);
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
2018-02-20 04:37:09 +01:00
|
|
|
int64_t
|
|
|
|
ircd::m::depth(std::nothrow_t,
|
|
|
|
const id::room &room_id)
|
|
|
|
{
|
|
|
|
int64_t depth;
|
|
|
|
head(std::nothrow, room_id, depth);
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:59:48 +01:00
|
|
|
ircd::m::id::event::buf
|
|
|
|
ircd::m::head(const id::room &room_id)
|
|
|
|
{
|
2018-02-20 04:37:09 +01:00
|
|
|
uint64_t depth;
|
2018-02-09 05:59:48 +01:00
|
|
|
return head(room_id, depth);
|
|
|
|
}
|
|
|
|
|
2018-02-20 04:37:09 +01:00
|
|
|
ircd::m::id::event::buf
|
|
|
|
ircd::m::head(std::nothrow_t,
|
|
|
|
const id::room &room_id)
|
|
|
|
{
|
|
|
|
int64_t depth;
|
|
|
|
return head(std::nothrow, room_id, depth);
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:59:48 +01:00
|
|
|
ircd::m::id::event::buf
|
|
|
|
ircd::m::head(const id::room &room_id,
|
2018-02-20 04:37:09 +01:00
|
|
|
uint64_t &depth)
|
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
head(std::nothrow, room_id, reinterpret_cast<int64_t &>(depth))
|
|
|
|
};
|
|
|
|
|
2018-02-21 00:00:22 +01:00
|
|
|
if(depth == uint64_t(-1))
|
2018-02-20 04:37:09 +01:00
|
|
|
throw m::NOT_FOUND{};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event::buf
|
|
|
|
ircd::m::head(std::nothrow_t,
|
|
|
|
const id::room &room_id,
|
2018-02-09 05:59:48 +01:00
|
|
|
int64_t &depth)
|
|
|
|
{
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
dbs::room_events.begin(room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!it)
|
|
|
|
{
|
|
|
|
depth = -1;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto &key{it->first};
|
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_events_key(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
depth = std::get<0>(part);
|
|
|
|
return std::get<1>(part);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::my(const room &room)
|
|
|
|
{
|
|
|
|
return my(room.room_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::m::room::room(const alias &alias,
|
|
|
|
const event::id &event_id)
|
|
|
|
:room_id{}
|
|
|
|
,event_id{event_id}
|
|
|
|
{
|
|
|
|
assert(0); //TODO: translate
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::membership(const m::id::user &user_id,
|
|
|
|
const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
2018-02-09 20:33:48 +01:00
|
|
|
bool ret{false};
|
2018-02-13 23:23:56 +01:00
|
|
|
const state state{*this};
|
|
|
|
state.get(std::nothrow, "m.room.member"_sv, user_id, [&membership, &ret]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const m::event &event)
|
2018-02-09 20:33:48 +01:00
|
|
|
{
|
|
|
|
assert(json::get<"type"_>(event) == "m.room.member");
|
2018-02-13 23:23:56 +01:00
|
|
|
ret = unquote(at<"membership"_>(event)) == membership;
|
2018-02-09 20:33:48 +01:00
|
|
|
});
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-02-09 20:33:48 +01:00
|
|
|
return ret;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-16 04:17:09 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::has(const string_view &type)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return get(std::nothrow, type, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
static constexpr auto idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "type"_>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_column.at(idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
messages it{*this};
|
2018-02-16 22:18:14 +01:00
|
|
|
for(; it; --it)
|
2018-02-16 04:17:09 +01:00
|
|
|
{
|
|
|
|
const auto &event_id{it.event_id()};
|
|
|
|
column(event_id, [&ret, &type](const string_view &value)
|
|
|
|
{
|
|
|
|
ret = value == type;
|
|
|
|
});
|
2018-02-16 22:18:14 +01:00
|
|
|
|
|
|
|
if(ret)
|
|
|
|
break;
|
2018-02-16 04:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ret && closure)
|
|
|
|
closure(*it);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-02-11 04:50:53 +01:00
|
|
|
//
|
|
|
|
// room::messages
|
|
|
|
//
|
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
ircd::m::room::messages::messages(const m::room &room)
|
|
|
|
:room{room}
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2018-02-13 23:25:01 +01:00
|
|
|
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,
|
|
|
|
const event::id &event_id)
|
|
|
|
:room{room}
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2018-02-13 23:25:01 +01:00
|
|
|
seek(event_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::room::messages::messages(const m::room &room,
|
|
|
|
const uint64_t &depth)
|
|
|
|
:room{room}
|
|
|
|
{
|
|
|
|
seek(depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::m::event &
|
|
|
|
ircd::m::room::messages::operator*()
|
|
|
|
{
|
|
|
|
return fetch(std::nothrow);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::messages::seek()
|
|
|
|
{
|
|
|
|
this->it = dbs::room_events.begin(room.room_id);
|
|
|
|
return bool(*this);
|
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-02-13 23:25:01 +01:00
|
|
|
static constexpr auto idx
|
|
|
|
{
|
|
|
|
json::indexof<event, "depth"_>()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2018-02-13 23:25:01 +01:00
|
|
|
dbs::event_column.at(idx)
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t depth;
|
|
|
|
column(event_id, [&](const string_view &binary)
|
|
|
|
{
|
|
|
|
assert(size(binary) == sizeof(depth));
|
|
|
|
depth = byte_view<uint64_t>(binary);
|
|
|
|
});
|
|
|
|
|
|
|
|
char buf[m::state::KEY_MAX_SZ];
|
|
|
|
const auto seek_key
|
|
|
|
{
|
|
|
|
dbs::room_events_key(buf, room.room_id, depth, event_id)
|
|
|
|
};
|
2018-02-11 04:50:53 +01:00
|
|
|
|
2018-02-13 23:25:01 +01:00
|
|
|
this->it = dbs::room_events.begin(seek_key);
|
|
|
|
return bool(*this);
|
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-02-13 23:25:01 +01:00
|
|
|
char buf[m::state::KEY_MAX_SZ];
|
|
|
|
const auto seek_key
|
2018-02-10 09:25:20 +01:00
|
|
|
{
|
2018-02-13 23:25:01 +01:00
|
|
|
dbs::room_events_key(buf, room.room_id, depth)
|
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-02-13 23:25:01 +01:00
|
|
|
const ircd::m::event::id &
|
|
|
|
ircd::m::room::messages::event_id()
|
|
|
|
{
|
|
|
|
assert(bool(*this));
|
|
|
|
const auto &key{it->first};
|
|
|
|
const auto part{dbs::room_events_key(key)};
|
|
|
|
_event_id = std::get<1>(part);
|
|
|
|
return _event_id;
|
|
|
|
}
|
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()
|
|
|
|
{
|
|
|
|
m::seek(_event, event_id());
|
|
|
|
return _event;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::m::event &
|
|
|
|
ircd::m::room::messages::fetch(std::nothrow_t)
|
|
|
|
{
|
|
|
|
if(!m::seek(_event, event_id(), std::nothrow))
|
|
|
|
_event = m::event::fetch{};
|
2018-02-11 02:57:22 +01:00
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
2018-02-11 02:34:16 +01:00
|
|
|
ircd::m::room::state::state(const m::room &room)
|
|
|
|
:room_id{room.room_id}
|
|
|
|
,event_id{room.event_id? room.event_id : head(room_id)}
|
|
|
|
{
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ircd::m::state::id &
|
|
|
|
ircd::m::room::state::refresh()
|
|
|
|
{
|
|
|
|
this->root_id = dbs::state_root(root_id_buf, room_id, event_id);
|
|
|
|
return this->root_id;
|
|
|
|
}
|
|
|
|
|
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-02-10 09:25:20 +01:00
|
|
|
get(type, state_key, event::id::closure{[&closure]
|
|
|
|
(const event::id &event_id)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
event::fetch event
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
event_id
|
2018-02-09 08:18:42 +01:00
|
|
|
};
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
if(event.valid(event_id))
|
|
|
|
closure(event);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::get(const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-02-11 02:34:16 +01:00
|
|
|
m::state::get(root_id, type, state_key, [&closure]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return get(std::nothrow, type, state_key, event::id::closure{[&closure]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
event::fetch event
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
event_id, std::nothrow
|
2018-02-09 08:18:42 +01:00
|
|
|
};
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
if(event.valid(event_id))
|
2018-02-09 20:22:46 +01: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-02-11 02:34:16 +01:00
|
|
|
return m::state::get(std::nothrow, root_id, type, state_key, [&closure]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
|
|
|
});
|
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-02-10 09:25:20 +01:00
|
|
|
return test(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-02-11 02:34:16 +01:00
|
|
|
return m::state::get(std::nothrow, root_id, type, state_key, []
|
2018-02-09 06:03:25 +01:00
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
2017-12-12 21:33:14 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::test(const event::closure_bool &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
event::fetch event;
|
|
|
|
return test(event::id::closure_bool{[&event, &closure]
|
|
|
|
(const event::id &event_id)
|
2018-02-09 21:10:34 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
if(seek(event, unquote(event_id), std::nothrow))
|
|
|
|
if(closure(event))
|
|
|
|
return true;
|
2018-02-09 21:10:34 +01:00
|
|
|
|
|
|
|
return false;
|
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::test(const event::id::closure_bool &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-02-11 02:34:16 +01:00
|
|
|
return m::state::test(root_id, [&closure]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const json::array &key, const string_view &event_id)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
return closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
}
|
2018-02-09 20:57:19 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const event::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
event::fetch event;
|
|
|
|
return test(type, event::id::closure_bool{[&event, &closure]
|
|
|
|
(const event::id &event_id)
|
2018-02-09 20:57:19 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
if(seek(event, unquote(event_id), std::nothrow))
|
|
|
|
if(closure(event))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}});
|
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-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const event::id::closure_bool &closure)
|
2018-02-09 22:08:41 +01:00
|
|
|
const
|
|
|
|
{
|
2018-02-11 02:34:16 +01:00
|
|
|
return m::state::test(root_id, type, [&closure]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const json::array &key, const string_view &event_id)
|
2018-02-09 20:57:19 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
return closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
}
|
2018-02-09 20:57:19 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-02-09 22:08:41 +01:00
|
|
|
event::fetch event;
|
2018-02-10 09:25:20 +01:00
|
|
|
for_each(event::id::closure{[&event, &closure]
|
|
|
|
(const event::id &event_id)
|
2018-02-09 20:57:19 +01:00
|
|
|
{
|
2018-02-13 23:23:56 +01:00
|
|
|
if(seek(event, event_id, std::nothrow))
|
2018-02-10 09:25:20 +01:00
|
|
|
closure(event);
|
|
|
|
}});
|
|
|
|
}
|
2018-02-09 22:08:41 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-02-11 02:34:16 +01:00
|
|
|
m::state::for_each(root_id, [&closure]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
2018-02-09 20:57:19 +01:00
|
|
|
});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::closure &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
event::fetch event;
|
|
|
|
for_each(type, event::id::closure{[&event, &closure]
|
|
|
|
(const event::id &event_id)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-02-13 23:23:56 +01:00
|
|
|
if(seek(event, event_id, std::nothrow))
|
2018-02-10 09:25:20 +01:00
|
|
|
closure(event);
|
|
|
|
}});
|
|
|
|
}
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::id::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-02-11 02:34:16 +01:00
|
|
|
m::state::for_each(root_id, type, [&closure]
|
2018-02-09 21:10:34 +01:00
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
closure(unquote(event_id));
|
2018-02-09 21:10:34 +01:00
|
|
|
});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room::members
|
|
|
|
//
|
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::members::for_each(const event::closure &view)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
test([&view](const m::event &event)
|
|
|
|
{
|
|
|
|
view(event);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::members::for_each(const string_view &membership,
|
|
|
|
const event::closure &view)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
test(membership, [&view](const m::event &event)
|
|
|
|
{
|
|
|
|
view(event);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
2018-02-10 22:03:13 +01:00
|
|
|
ircd::m::room::members::test(const event::closure_bool &view)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
const room::state state
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
room
|
2018-02-09 08:18:42 +01:00
|
|
|
};
|
|
|
|
|
2018-02-09 21:10:34 +01:00
|
|
|
static const string_view type{"m.room.member"};
|
2018-02-10 22:03:13 +01:00
|
|
|
return state.test(type, event::closure_bool{[&view]
|
2018-02-10 09:25:20 +01:00
|
|
|
(const m::event &event)
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2018-02-10 22:03:13 +01:00
|
|
|
return view(event);
|
2018-02-10 09:25:20 +01:00
|
|
|
}});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 09:03:08 +01:00
|
|
|
bool
|
2018-02-10 22:03:13 +01:00
|
|
|
ircd::m::room::members::test(const string_view &membership,
|
|
|
|
const event::closure_bool &view)
|
2018-02-09 09:03:08 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
//TODO: optimize with sequential column strat
|
2018-02-10 22:03:13 +01:00
|
|
|
return test([&membership, &view](const event &event)
|
2018-02-09 09:03:08 +01:00
|
|
|
{
|
|
|
|
if(at<"membership"_>(event) == membership)
|
2018-02-10 22:03:13 +01:00
|
|
|
if(view(event))
|
|
|
|
return true;
|
2018-02-09 09:03:08 +01:00
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
return false;
|
2018-02-09 09:03:08 +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-02-10 22:03:13 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::origins::for_each(const closure &view)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
test([&view](const string_view &origin)
|
|
|
|
{
|
|
|
|
view(origin);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
2018-02-10 22:03:13 +01:00
|
|
|
ircd::m::room::origins::test(const closure_bool &view)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
db::index index
|
|
|
|
{
|
2018-02-10 09:25:20 +01:00
|
|
|
*dbs::events, "origin_joined in room_id"
|
2017-12-12 21:33:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
auto it(index.begin(room.room_id));
|
|
|
|
for(; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const string_view &key(it->first);
|
|
|
|
const string_view &origin(split(key, ":::").second); //TODO: XXX
|
2018-02-10 22:03:13 +01:00
|
|
|
if(view(origin))
|
|
|
|
return true;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
return false;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-02-10 09:25:20 +01:00
|
|
|
// room::state::tuple
|
2017-12-12 21:33:14 +01:00
|
|
|
//
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::tuple::tuple(const m::room &room,
|
|
|
|
const mutable_buffer &buf)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::room::state::tuple::tuple(const json::array &pdus)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
for(const json::object &pdu : pdus)
|
|
|
|
{
|
|
|
|
const m::event &event{pdu};
|
|
|
|
json::set(*this, at<"type"_>(event), event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::pretty(const room::state::tuple &state)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 2048);
|
|
|
|
|
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, const auto &event)
|
|
|
|
{
|
|
|
|
if(!json::get<"event_id"_>(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s << std::setw(28) << std::right << key
|
|
|
|
<< " : " << at<"event_id"_>(event)
|
|
|
|
<< " " << json::get<"sender"_>(event)
|
|
|
|
<< " " << json::get<"depth"_>(event)
|
|
|
|
<< " " << pretty_oneline(event::prev{event})
|
|
|
|
<< std::endl;
|
|
|
|
}};
|
|
|
|
|
|
|
|
json::for_each(state, out);
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
2018-02-10 09:25:20 +01:00
|
|
|
ircd::m::pretty_oneline(const room::state::tuple &state)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
std::stringstream s;
|
|
|
|
pubsetbuf(s, ret, 1024);
|
|
|
|
|
|
|
|
const auto out{[&s]
|
|
|
|
(const string_view &key, const auto &event)
|
|
|
|
{
|
|
|
|
if(!json::get<"event_id"_>(event))
|
|
|
|
return;
|
|
|
|
|
|
|
|
s << key << " ";
|
|
|
|
}};
|
|
|
|
|
|
|
|
json::for_each(state, out);
|
|
|
|
resizebuf(s, ret);
|
|
|
|
return ret;
|
|
|
|
}
|