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
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
event::fetch::event_id(event_idx, std::nothrow, [&ret]
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
|
|
|
ircd::m::my(const room &room)
|
|
|
|
{
|
|
|
|
return my(room.room_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// room
|
|
|
|
//
|
|
|
|
|
2018-05-19 03:26:21 +02:00
|
|
|
bool
|
2018-05-21 05:15:50 +02:00
|
|
|
ircd::m::room::visible(const user::id &user_id,
|
|
|
|
const event *const &event_)
|
2018-05-19 03:26:21 +02:00
|
|
|
const
|
|
|
|
{
|
2018-05-21 05:15:50 +02:00
|
|
|
using prototype = bool (const room &, const user &, const event *const &);
|
|
|
|
|
|
|
|
static import<prototype> function
|
|
|
|
{
|
|
|
|
"m_room_history_visibility", "visible__user"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(*this, user_id, event_);
|
2018-05-19 03:26:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-05-21 05:15:50 +02:00
|
|
|
ircd::m::room::visible(const node::id &node_id,
|
|
|
|
const event *const &event_)
|
2018-05-19 03:26:21 +02:00
|
|
|
const
|
|
|
|
{
|
2018-05-21 05:15:50 +02:00
|
|
|
using prototype = bool (const room &, const node &, const event *const &);
|
|
|
|
|
|
|
|
static import<prototype> function
|
|
|
|
{
|
|
|
|
"m_room_history_visibility", "visible__node"
|
|
|
|
};
|
|
|
|
|
|
|
|
return function(*this, node_id, event_);
|
2018-05-19 03:26:21 +02:00
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::membership(const m::id::user &user_id,
|
|
|
|
const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
2018-03-01 06:05:09 +01:00
|
|
|
char buf[64];
|
|
|
|
return this->membership(buf, user_id) == membership;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::m::room::membership(const mutable_buffer &out,
|
|
|
|
const m::id::user &user_id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
string_view ret;
|
2018-02-13 23:23:56 +01:00
|
|
|
const state state{*this};
|
2018-03-01 06:05:09 +01:00
|
|
|
state.get(std::nothrow, "m.room.member"_sv, user_id, [&out, &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-03-01 06:05:09 +01:00
|
|
|
ret = { data(out), copy(out, unquote(at<"membership"_>(event))) };
|
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
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
fopts
|
|
|
|
};
|
|
|
|
|
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};
|
|
|
|
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
|
|
|
|
(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-02-13 23:25:01 +01:00
|
|
|
ircd::m::room::messages::messages(const m::room &room)
|
|
|
|
:room{room}
|
2018-05-20 13:35:17 +02:00
|
|
|
,_event{room.fopts}
|
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-05-20 13:35:17 +02:00
|
|
|
,_event{room.fopts}
|
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}
|
2018-05-20 13:35:17 +02:00
|
|
|
,_event{room.fopts}
|
2018-02-13 23:25:01 +01:00
|
|
|
{
|
|
|
|
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-04-18 04:14:39 +02:00
|
|
|
auto &column
|
2018-02-13 23:25:01 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
dbs::event_column.at(json::indexof<event, "depth"_>())
|
2018-02-13 23:25:01 +01:00
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
const event::idx event_idx
|
2018-02-11 04:50:53 +01:00
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
index(event_id)
|
2018-02-13 23:25:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t depth;
|
2018-04-18 04:14:39 +02:00
|
|
|
column(byte_view<string_view>(event_idx), [&depth]
|
|
|
|
(const string_view &value)
|
2018-02-13 23:25:01 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
depth = byte_view<uint64_t>(value);
|
2018-02-13 23:25:01 +01:00
|
|
|
});
|
|
|
|
|
2018-04-25 04:19:40 +02:00
|
|
|
char buf[dbs::ROOM_EVENTS_KEY_MAX_SIZE];
|
2018-02-13 23:25:01 +01:00
|
|
|
const auto seek_key
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
dbs::room_events_key(buf, room.room_id, depth, event_idx)
|
2018-02-13 23:25:01 +01:00
|
|
|
};
|
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-04-25 04:19:40 +02:00
|
|
|
char buf[dbs::ROOM_EVENTS_KEY_MAX_SIZE];
|
2018-02-13 23:25:01 +01:00
|
|
|
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-04-18 04:14:39 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::room::messages::event_id()
|
2018-03-09 04:01:38 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
event::id::buf ret;
|
|
|
|
event::fetch::event_id(this->event_idx(), [&ret]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
ret = event_id;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
2018-03-09 04:01:38 +01:00
|
|
|
}
|
|
|
|
|
2018-04-27 05:25:00 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::m::room::messages::state_root()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(bool(*this));
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
const ircd::m::event::idx &
|
|
|
|
ircd::m::room::messages::event_idx()
|
2018-02-13 23:25:01 +01:00
|
|
|
{
|
|
|
|
assert(bool(*this));
|
|
|
|
const auto &key{it->first};
|
|
|
|
const auto part{dbs::room_events_key(key)};
|
2018-04-18 04:14:39 +02:00
|
|
|
_event_idx = std::get<1>(part);
|
|
|
|
return _event_idx;
|
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()
|
|
|
|
{
|
2018-04-18 04:14:39 +02: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)
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(!m::seek(_event, event_idx(), std::nothrow))
|
2018-05-20 13:35:17 +02:00
|
|
|
_event = m::event::fetch{room.fopts};
|
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)
|
2018-05-21 05:14:44 +02:00
|
|
|
:state
|
|
|
|
{
|
|
|
|
room, room.fopts
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
,root_id
|
|
|
|
{
|
|
|
|
event_id?
|
|
|
|
dbs::state_root(root_id_buf, room_id, event_id):
|
|
|
|
m::state::id{}
|
|
|
|
}
|
2018-05-20 13:35:17 +02:00
|
|
|
,fopts
|
|
|
|
{
|
2018-05-21 05:14:44 +02:00
|
|
|
fopts
|
2018-05-20 13:35:17 +02:00
|
|
|
}
|
2018-02-11 02:34:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-04-05 02:06:58 +02:00
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::room::state::get(const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
event::id::buf ret;
|
|
|
|
get(type, state_key, event::id::closure{[&ret]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
ret = event_id;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::id::buf
|
|
|
|
ircd::m::room::state::get(std::nothrow_t,
|
|
|
|
const string_view &type,
|
|
|
|
const string_view &state_key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
event::id::buf ret;
|
|
|
|
get(std::nothrow, type, state_key, event::id::closure{[&ret]
|
|
|
|
(const event::id &event_id)
|
|
|
|
{
|
|
|
|
ret = event_id;
|
|
|
|
}});
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event_idx, fopts
|
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)
|
2018-04-03 07:05:36 +02:00
|
|
|
const try
|
2018-02-10 09:25:20 +01:00
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::get(root_id, type, state_key, [&closure]
|
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
get(type, state_key, event::closure_idx{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
event::fetch::event_id(idx, closure);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
catch(const db::not_found &e)
|
|
|
|
{
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"(%s,%s) in %s :%s",
|
|
|
|
type,
|
|
|
|
state_key,
|
|
|
|
string_view{room_id},
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2018-04-18 04:14:39 +02:00
|
|
|
m::state::get(root_id, type, state_key, [&closure]
|
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
closure(index(unquote(event_id)));
|
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
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event_idx, std::nothrow, fopts
|
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-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::get(std::nothrow, root_id, type, state_key, [&closure]
|
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return get(std::nothrow, type, state_key, event::closure_idx{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
event::fetch::event_id(idx, std::nothrow, closure);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2018-04-18 04:14:39 +02:00
|
|
|
return m::state::get(std::nothrow, root_id, type, state_key, [&closure]
|
|
|
|
(const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
return closure(index(unquote(event_id), std::nothrow));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::get(std::nothrow, root_id, type, state_key, []
|
|
|
|
(const string_view &event_id)
|
|
|
|
{});
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::count(root_id);
|
|
|
|
|
|
|
|
size_t ret{0};
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(room_id)}; 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
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::count(root_id, type);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
dbs::room_state_key(keybuf, room_id, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t ret{0};
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(key)}; bool(it); ++it)
|
|
|
|
if(std::get<0>(dbs::room_state_key(it->first)) == type)
|
|
|
|
++ret;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
return ret;
|
2018-02-25 04:52:02 +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::closure_bool &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
|
|
|
fopts
|
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return test(event::closure_idx_bool{[&event, &closure]
|
|
|
|
(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-02-10 09:25:20 +01:00
|
|
|
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-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::test(root_id, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
return closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return test(event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
|
|
|
|
(const event::id &id)
|
|
|
|
{
|
|
|
|
ret = closure(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::test(const event::closure_idx_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-18 04:14:39 +02:00
|
|
|
return m::state::test(root_id, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
return closure(index(unquote(event_id), std::nothrow));
|
2018-04-18 04:14:39 +02:00
|
|
|
});
|
|
|
|
|
2018-03-04 09:12:13 +01:00
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(room_id)}; bool(it); ++it)
|
2018-04-18 04:14:39 +02:00
|
|
|
if(closure(byte_view<event::idx>(it->second)))
|
2018-03-04 09:12:13 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
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
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const event::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
|
|
|
fopts
|
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return test(type, event::closure_idx_bool{[&event, &closure]
|
|
|
|
(const event::idx &event_idx)
|
2018-02-09 20:57:19 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(seek(event, event_idx, std::nothrow))
|
2018-02-10 09:25:20 +01:00
|
|
|
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-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::test(root_id, type, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
return closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return test(type, event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
|
|
|
|
(const event::id &id)
|
|
|
|
{
|
|
|
|
ret = closure(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-18 04:14:39 +02:00
|
|
|
return m::state::test(root_id, type, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
return closure(index(unquote(event_id), std::nothrow));
|
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-09 20:57:19 +01:00
|
|
|
{
|
2018-03-04 09:12:13 +01:00
|
|
|
dbs::room_state_key(keybuf, room_id, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(key)}; bool(it); ++it)
|
|
|
|
if(std::get<0>(dbs::room_state_key(it->first)) == type)
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(closure(byte_view<event::idx>(it->second)))
|
2018-03-04 09:12:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
|
|
|
|
return false;
|
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
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const keys_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-10 04:24:56 +02:00
|
|
|
return m::state::test(root_id, type, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
assert(size(key) >= 2);
|
|
|
|
return closure(unquote(key.at(1)));
|
|
|
|
});
|
|
|
|
|
2018-04-25 04:19:40 +02:00
|
|
|
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
|
2018-04-10 04:24:56 +02:00
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
dbs::room_state_key(keybuf, room_id, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(key)}; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_state_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(std::get<0>(part) == type)
|
|
|
|
{
|
|
|
|
if(closure(std::get<1>(part)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-24 04:38:56 +01:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const event::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
|
|
|
fopts
|
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return test(type, state_key_lb, event::closure_idx_bool{[&event, &closure]
|
|
|
|
(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-02-24 04:38:56 +01:00
|
|
|
if(closure(event))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const event::id::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::test(root_id, type, state_key_lb, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
return closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
return test(type, state_key_lb, event::closure_idx_bool{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
event::fetch::event_id(idx, std::nothrow, [&ret, &closure]
|
|
|
|
(const event::id &id)
|
|
|
|
{
|
|
|
|
ret = closure(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::state::test(const string_view &type,
|
|
|
|
const string_view &state_key_lb,
|
|
|
|
const event::closure_idx_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-18 04:14:39 +02:00
|
|
|
return m::state::test(root_id, type, state_key_lb, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
return closure(index(unquote(event_id), std::nothrow));
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(key)}; bool(it); ++it)
|
|
|
|
if(std::get<0>(dbs::room_state_key(it->first)) == type)
|
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(closure(byte_view<event::idx>(it->second)))
|
2018-03-04 09:12:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
|
|
|
|
return false;
|
2018-02-24 04:38:56 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 09:25:20 +01:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
|
|
|
fopts
|
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
for_each(event::closure_idx{[&event, &closure]
|
|
|
|
(const event::idx &event_idx)
|
2018-02-09 20:57:19 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(seek(event, event_idx, 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-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::for_each(root_id, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
for_each(event::closure_idx{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
event::fetch::event_id(idx, std::nothrow, closure);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const event::closure_idx &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-18 04:14:39 +02:00
|
|
|
return m::state::for_each(root_id, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
closure(index(unquote(event_id), std::nothrow));
|
2018-04-18 04:14:39 +02:00
|
|
|
});
|
|
|
|
|
2018-03-04 09:12:13 +01:00
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(room_id)}; bool(it); ++it)
|
2018-04-18 04:14:39 +02:00
|
|
|
closure(byte_view<event::idx>(it->second));
|
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-05-20 13:35:17 +02:00
|
|
|
event::fetch event
|
|
|
|
{
|
|
|
|
fopts
|
|
|
|
};
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
for_each(type, event::closure_idx{[&event, &closure]
|
|
|
|
(const event::idx &event_idx)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-04-18 04:14:39 +02:00
|
|
|
if(seek(event, event_idx, 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-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-03-04 09:12:13 +01:00
|
|
|
return m::state::for_each(root_id, type, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
|
|
|
closure(unquote(event_id));
|
|
|
|
});
|
|
|
|
|
2018-04-18 04:14:39 +02:00
|
|
|
for_each(type, event::closure_idx{[&closure]
|
|
|
|
(const event::idx &idx)
|
|
|
|
{
|
|
|
|
event::fetch::event_id(idx, std::nothrow, closure);
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const event::closure_idx &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-18 04:14:39 +02:00
|
|
|
return m::state::for_each(root_id, type, [&closure]
|
|
|
|
(const json::array &key, const string_view &event_id)
|
|
|
|
{
|
2018-05-10 01:38:11 +02:00
|
|
|
closure(index(unquote(event_id), std::nothrow));
|
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-09 21:10:34 +01:00
|
|
|
{
|
2018-03-04 09:12:13 +01:00
|
|
|
dbs::room_state_key(keybuf, room_id, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(key)}; bool(it); ++it)
|
|
|
|
if(std::get<0>(dbs::room_state_key(it->first)) == type)
|
2018-04-18 04:14:39 +02:00
|
|
|
closure(byte_view<event::idx>(it->second));
|
2018-03-04 09:12:13 +01:00
|
|
|
else
|
|
|
|
break;
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-04-10 04:24:56 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::state::for_each(const string_view &type,
|
|
|
|
const keys &closure)
|
|
|
|
const
|
|
|
|
{
|
2018-05-05 12:35:44 +02:00
|
|
|
if(!present())
|
2018-04-10 04:24:56 +02:00
|
|
|
return m::state::for_each(root_id, type, [&closure]
|
2018-04-18 04:14:39 +02:00
|
|
|
(const json::array &key, const string_view &)
|
2018-04-10 04:24:56 +02:00
|
|
|
{
|
|
|
|
assert(size(key) >= 2);
|
|
|
|
closure(unquote(key.at(1)));
|
|
|
|
});
|
|
|
|
|
2018-04-25 04:19:40 +02:00
|
|
|
char keybuf[dbs::ROOM_STATE_KEY_MAX_SIZE];
|
2018-04-10 04:24:56 +02:00
|
|
|
const auto &key
|
|
|
|
{
|
|
|
|
dbs::room_state_key(keybuf, room_id, type)
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column{dbs::room_state};
|
|
|
|
for(auto it{column.begin(key)}; bool(it); ++it)
|
|
|
|
{
|
|
|
|
const auto part
|
|
|
|
{
|
|
|
|
dbs::room_state_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(std::get<0>(part) == type)
|
|
|
|
closure(std::get<1>(part));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 12:35:44 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::state::present()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !event_id || !root_id || std::get<0>(top(room_id)) == event_id;
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:33:14 +01:00
|
|
|
//
|
|
|
|
// room::members
|
|
|
|
//
|
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
void
|
2018-04-12 23:10:03 +02:00
|
|
|
ircd::m::room::members::for_each(const closure &closure)
|
2018-02-10 22:03:13 +01:00
|
|
|
const
|
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::members::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::members::for_each(const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::members::test(const event::closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const room::state state{room};
|
|
|
|
return state.test("m.room.member", event::closure_bool{[&closure]
|
|
|
|
(const m::event &event)
|
2018-02-10 22:03:13 +01:00
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
return closure(event);
|
|
|
|
}});
|
2018-02-10 22:03:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::room::members::for_each(const string_view &membership,
|
2018-04-12 23:10:03 +02:00
|
|
|
const closure &closure)
|
2018-02-10 22:03:13 +01:00
|
|
|
const
|
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
for_each(membership, closure_bool{[&closure]
|
|
|
|
(const user::id &user_id)
|
2018-02-10 22:03:13 +01:00
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
closure(user_id);
|
|
|
|
return true;
|
|
|
|
}});
|
2018-02-10 22:03:13 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 05:16:31 +02:00
|
|
|
/// Iterate the mxid's of the users in the room, optionally with a specific
|
|
|
|
/// membership state. This query contains internal optimizations as the closure
|
|
|
|
/// only requires a user::id. The db::gopts set in the room.fopts pointer is
|
|
|
|
/// still used if provided.
|
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,
|
|
|
|
const closure_bool &closure)
|
2017-12-12 21:33:14 +01:00
|
|
|
const
|
|
|
|
{
|
2018-05-21 05:16:31 +02:00
|
|
|
// Setup the list of event fields to fetch for the closure
|
|
|
|
static const event::keys keys
|
|
|
|
{
|
|
|
|
event::keys::include
|
|
|
|
{
|
|
|
|
"event_id",
|
|
|
|
"membership",
|
|
|
|
"state_key",
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// In this case the fetch opts isn't static so it can maintain the
|
|
|
|
// previously given db::gopts, but it will use our keys list.
|
|
|
|
const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
keys, room.fopts? room.fopts->gopts : db::gopts{}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Stack-over the the current fetch opts with our new opts for this query,
|
|
|
|
// putting them back when we're finished. This requires a const_cast which
|
|
|
|
// should be okay here.
|
|
|
|
auto &room(const_cast<m::room &>(this->room));
|
|
|
|
const auto theirs{room.fopts};
|
|
|
|
room.fopts = &fopts;
|
|
|
|
const unwind reset{[&room, &theirs]
|
|
|
|
{
|
|
|
|
room.fopts = theirs;
|
|
|
|
}};
|
|
|
|
|
2018-04-12 23:10:03 +02:00
|
|
|
return !test(membership, event::closure_bool{[&closure]
|
|
|
|
(const event &event)
|
2018-02-09 08:18:42 +01:00
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
const user::id &user_id
|
|
|
|
{
|
|
|
|
at<"state_key"_>(event)
|
|
|
|
};
|
2018-02-09 08:18:42 +01:00
|
|
|
|
2018-04-12 23:10:03 +02:00
|
|
|
return !closure(user_id);
|
2018-02-10 09:25:20 +01:00
|
|
|
}});
|
2017-12-12 21:33:14 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 23:10:03 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::members::for_each(const string_view &membership,
|
|
|
|
const event::closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
test(membership, [&closure]
|
|
|
|
(const event &event)
|
|
|
|
{
|
|
|
|
closure(event);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
2018-04-12 23:10:03 +02:00
|
|
|
const event::closure_bool &closure)
|
2018-02-09 09:03:08 +01:00
|
|
|
const
|
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
if(empty(membership))
|
|
|
|
return test(closure);
|
|
|
|
|
2018-03-03 15:21:59 +01:00
|
|
|
// joined members optimization. Only possible when seeking
|
|
|
|
// membership="join" on the present state of the room.
|
|
|
|
if(!room.event_id && membership == "join")
|
|
|
|
{
|
|
|
|
const room::origins origins{room};
|
2018-04-12 23:10:03 +02:00
|
|
|
return origins._test_([&closure, this]
|
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
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
2018-04-12 23:10:03 +02:00
|
|
|
room.get(std::nothrow, "m.room.member", member, event::closure{[&closure, &ret]
|
|
|
|
(const event &event)
|
2018-03-03 15:21:59 +01:00
|
|
|
{
|
2018-04-12 23:10:03 +02:00
|
|
|
ret = closure(event);
|
|
|
|
}});
|
2018-03-03 15:21:59 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-12 23:10:03 +02:00
|
|
|
return test(event::closure_bool{[&membership, &closure]
|
|
|
|
(const event &event)
|
2018-02-09 09:03:08 +01:00
|
|
|
{
|
|
|
|
if(at<"membership"_>(event) == membership)
|
2018-04-12 23:10:03 +02:00
|
|
|
if(closure(event))
|
2018-02-10 22:03:13 +01:00
|
|
|
return true;
|
2018-02-09 09:03:08 +01:00
|
|
|
|
2018-02-10 22:03:13 +01:00
|
|
|
return false;
|
2018-04-12 23:10:03 +02:00
|
|
|
}});
|
2018-02-09 09:03:08 +01:00
|
|
|
}
|
|
|
|
|
2018-03-03 15:21:18 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::room::members::count(const string_view &membership)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
// joined members optimization. Only possible when seeking
|
|
|
|
// membership="join" on the present state of the room.
|
|
|
|
if(!room.event_id && membership == "join")
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
const room::origins origins{room};
|
|
|
|
origins._test_([&ret](const string_view &)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t ret{0};
|
2018-03-03 19:01:28 +01:00
|
|
|
state.for_each("m.room.member", event::closure{[&ret, &membership]
|
2018-03-03 15:21:18 +01:00
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
ret += m::membership(event) == membership;
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::members::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
const room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
2018-03-03 19:01:28 +01:00
|
|
|
return state.count("m.room.member");
|
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-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;
|
|
|
|
}
|
|
|
|
|
2018-04-03 21:49:34 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::origins::has(const string_view &origin)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
db::index &index
|
|
|
|
{
|
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
|
|
|
|
{
|
|
|
|
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
|
2018-03-03 15:06:15 +01:00
|
|
|
{
|
|
|
|
string_view last;
|
|
|
|
char lastbuf[256];
|
|
|
|
return _test_([&last, &lastbuf, &view]
|
|
|
|
(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 false;
|
|
|
|
|
|
|
|
if(view(origin))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
last = { lastbuf, copy(lastbuf, origin) };
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::origins::_test_(const closure_bool &view)
|
|
|
|
const
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-03-03 07:03:06 +01:00
|
|
|
db::index &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
|
|
|
|
{
|
|
|
|
index.begin(room.room_id)
|
|
|
|
};
|
|
|
|
|
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-03-03 15:06:15 +01:00
|
|
|
if(view(key))
|
2018-02-10 22:03:13 +01:00
|
|
|
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-05-05 05:25:44 +02:00
|
|
|
//
|
|
|
|
// room::head
|
|
|
|
//
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::room::head::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
for_each([&ret]
|
|
|
|
(const event::idx &event_idx, const event::id &event_id)
|
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-05 11:12:48 +02:00
|
|
|
bool
|
|
|
|
ircd::m::room::head::has(const event::id &event_id)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
bool ret{false};
|
|
|
|
for_each(closure_bool{[&ret, &event_id]
|
|
|
|
(const event::idx &event_idx, const event::id &event_id_)
|
|
|
|
{
|
|
|
|
ret = event_id_ == event_id;
|
|
|
|
return !ret; // for_each protocol: false to break
|
|
|
|
}});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-05 05:25:44 +02:00
|
|
|
void
|
|
|
|
ircd::m::room::head::for_each(const closure &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
for_each(closure_bool{[&closure]
|
|
|
|
(const event::idx &event_idx, const event::id &event_id)
|
|
|
|
{
|
|
|
|
closure(event_idx, event_id);
|
|
|
|
return true;
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::room::head::for_each(const closure_bool &closure)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
auto &index
|
|
|
|
{
|
|
|
|
dbs::room_head
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it
|
|
|
|
{
|
|
|
|
index.begin(room.room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
for(; it; ++it)
|
|
|
|
{
|
|
|
|
const event::id &event_id
|
|
|
|
{
|
|
|
|
dbs::room_head_key(it->first)
|
|
|
|
};
|
|
|
|
|
|
|
|
const event::idx &event_idx
|
|
|
|
{
|
|
|
|
byte_view<event::idx>{it->second}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!closure(event_idx, event_id))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-05-18 09:24:25 +02:00
|
|
|
const mutable_buffer &buf_)
|
2017-12-12 21:33:14 +01:00
|
|
|
{
|
2018-05-18 09:24:25 +02:00
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
room
|
|
|
|
};
|
|
|
|
|
|
|
|
window_buffer buf{buf_};
|
|
|
|
json::for_each(*this, [&state, &buf]
|
|
|
|
(const string_view &type, auto &event)
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, type, "", [&buf, &event]
|
|
|
|
(const event::idx &event_idx)
|
|
|
|
{
|
|
|
|
buf([&event, &event_idx]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
event = m::event
|
|
|
|
{
|
|
|
|
event_idx, buf
|
|
|
|
};
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2018-05-18 09:24:25 +02:00
|
|
|
return serialized(event);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
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
|
2018-05-18 09:32:42 +02:00
|
|
|
<< " :" << json::get<"depth"_>(event)
|
|
|
|
<< " " << at<"event_id"_>(event)
|
2017-12-12 21:33:14 +01:00
|
|
|
<< " " << json::get<"sender"_>(event)
|
2018-05-18 09:32:42 +02:00
|
|
|
<< " " << json::get<"content"_>(event)
|
2017-12-12 21:33:14 +01:00
|
|
|
<< 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;
|
|
|
|
}
|