2019-01-04 02:21:02 +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.
|
|
|
|
|
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
2019-07-16 22:29:15 +02:00
|
|
|
static bool room_state_append(data &, json::stack::array &, const m::event &, const m::event::idx &);
|
2019-03-05 23:43:00 +01:00
|
|
|
|
2019-07-07 14:44:47 +02:00
|
|
|
static bool room_state_phased_member_events(data &, json::stack::array &);
|
2019-04-08 15:11:16 +02:00
|
|
|
static bool room_state_phased_events(data &);
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool room_state_polylog_events(data &);
|
2019-03-04 01:31:43 +01:00
|
|
|
static bool _room_state_polylog(data &);
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool room_state_polylog(data &);
|
2019-03-04 01:31:43 +01:00
|
|
|
static bool room_invite_state_polylog(data &);
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool room_state_linear_events(data &);
|
2019-04-23 04:40:21 +02:00
|
|
|
static bool room_invite_state_linear(data &);
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool room_state_linear(data &);
|
2019-01-09 00:10:06 +01:00
|
|
|
|
2019-06-08 10:05:50 +02:00
|
|
|
extern conf::item<int64_t> state_exposure_depth; //TODO: XXX
|
2019-01-09 00:10:06 +01:00
|
|
|
extern const event::keys::include _default_keys;
|
2019-01-18 22:05:31 +01:00
|
|
|
extern event::fetch::opts _default_fopts;
|
2019-01-17 01:46:01 +01:00
|
|
|
|
2019-03-04 01:31:43 +01:00
|
|
|
extern item room_invite_state;
|
2019-01-04 02:21:02 +01:00
|
|
|
extern item room_state;
|
|
|
|
}
|
|
|
|
|
2019-01-18 22:05:31 +01:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Room State", []
|
|
|
|
{
|
|
|
|
ircd::m::sync::_default_fopts.query_json_force = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
decltype(ircd::m::sync::room_state)
|
|
|
|
ircd::m::sync::room_state
|
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
"rooms.state",
|
2019-01-04 23:47:01 +01:00
|
|
|
room_state_polylog,
|
2019-04-08 13:43:23 +02:00
|
|
|
room_state_linear,
|
|
|
|
{
|
|
|
|
{ "phased", true },
|
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-03-04 01:31:43 +01:00
|
|
|
decltype(ircd::m::sync::room_invite_state)
|
|
|
|
ircd::m::sync::room_invite_state
|
|
|
|
{
|
|
|
|
"rooms.invite_state",
|
|
|
|
room_invite_state_polylog,
|
2019-04-23 04:40:21 +02:00
|
|
|
room_invite_state_linear,
|
2019-07-25 00:58:28 +02:00
|
|
|
{
|
|
|
|
{ "phased", true },
|
|
|
|
}
|
2019-03-04 01:31:43 +01:00
|
|
|
};
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
decltype(ircd::m::sync::_default_keys)
|
|
|
|
ircd::m::sync::_default_keys
|
|
|
|
{
|
|
|
|
"content",
|
|
|
|
"depth",
|
|
|
|
"event_id",
|
|
|
|
"origin_server_ts",
|
|
|
|
"redacts",
|
|
|
|
"room_id",
|
|
|
|
"sender",
|
|
|
|
"state_key",
|
|
|
|
"type",
|
|
|
|
};
|
|
|
|
|
2019-01-17 01:46:01 +01:00
|
|
|
decltype(ircd::m::sync::_default_fopts)
|
2019-01-17 03:39:40 +01:00
|
|
|
ircd::m::sync::_default_fopts
|
2019-01-17 01:46:01 +01:00
|
|
|
{
|
2019-01-17 03:39:40 +01:00
|
|
|
_default_keys
|
|
|
|
};
|
2019-01-17 01:46:01 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 23:47:01 +01:00
|
|
|
ircd::m::sync::room_state_linear(data &data)
|
2019-04-23 04:40:21 +02:00
|
|
|
{
|
2019-07-15 22:56:50 +02:00
|
|
|
if(data.membership == "invite")
|
|
|
|
return false;
|
|
|
|
|
2019-04-23 04:40:21 +02:00
|
|
|
return room_state_linear_events(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::room_invite_state_linear(data &data)
|
|
|
|
{
|
|
|
|
if(data.membership != "invite")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return room_state_linear_events(data);
|
|
|
|
}
|
|
|
|
|
2019-06-08 10:05:50 +02:00
|
|
|
//TODO: This has to be merged into the timeline conf items
|
|
|
|
decltype(ircd::m::sync::state_exposure_depth)
|
|
|
|
ircd::m::sync::state_exposure_depth
|
|
|
|
{
|
|
|
|
{ "name", "ircd.client.sync.rooms.state.exposure.depth" },
|
|
|
|
{ "default", 20L },
|
|
|
|
};
|
|
|
|
|
2019-04-23 04:40:21 +02:00
|
|
|
bool
|
|
|
|
ircd::m::sync::room_state_linear_events(data &data)
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
2019-02-28 03:24:12 +01:00
|
|
|
if(!data.event_idx)
|
|
|
|
return false;
|
|
|
|
|
2019-06-08 10:05:50 +02:00
|
|
|
if(!data.room)
|
2019-03-08 22:42:54 +01:00
|
|
|
return false;
|
|
|
|
|
2019-06-08 10:05:50 +02:00
|
|
|
if(!data.membership)
|
2019-03-08 22:42:54 +01:00
|
|
|
return false;
|
|
|
|
|
2019-01-04 23:47:01 +01:00
|
|
|
assert(data.event);
|
|
|
|
if(!json::get<"state_key"_>(*data.event))
|
2019-02-24 20:59:53 +01:00
|
|
|
return false;
|
2019-01-04 23:47:01 +01:00
|
|
|
|
2019-06-08 10:05:50 +02:00
|
|
|
// Figure out whether the event was included in the timeline or whether
|
|
|
|
// to include it here in the state, which comes before the timeline.
|
|
|
|
// Since linear-sync is already distinct from polylog-sync, the
|
|
|
|
// overwhelming majority of state events coming through linear-sync will
|
|
|
|
// use the timeline. We make an exception for past state events the server
|
|
|
|
// only recently obtained, to hide them from the timeline.
|
|
|
|
if(int64_t(state_exposure_depth) > -1)
|
2019-07-15 22:56:50 +02:00
|
|
|
if(data.membership != "invite")
|
|
|
|
if(json::get<"depth"_>(*data.event) + int64_t(state_exposure_depth) >= data.room_depth)
|
|
|
|
return false;
|
2019-06-08 10:05:50 +02:00
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
json::stack::object rooms
|
|
|
|
{
|
|
|
|
*data.out, "rooms"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object membership_
|
|
|
|
{
|
|
|
|
*data.out, data.membership
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object room_
|
|
|
|
{
|
|
|
|
*data.out, data.room->room_id
|
|
|
|
};
|
|
|
|
|
2019-04-23 04:40:21 +02:00
|
|
|
const auto &state_member_name
|
|
|
|
{
|
|
|
|
data.membership == "invite"?
|
2019-07-08 12:51:18 +02:00
|
|
|
"invite_state": // "invite_state"_sv:
|
|
|
|
"state"
|
2019-04-23 04:40:21 +02:00
|
|
|
};
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
json::stack::object state
|
|
|
|
{
|
2019-04-23 04:40:21 +02:00
|
|
|
*data.out, state_member_name
|
2019-03-08 22:42:54 +01:00
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
2019-01-04 23:47:01 +01:00
|
|
|
|
2019-07-15 22:56:50 +02:00
|
|
|
// Branch for supplying state to the client after its user's invite
|
|
|
|
// is processed. At this point the client has not received prior room
|
|
|
|
// state in /sync.
|
|
|
|
if(data.membership == "invite" &&
|
|
|
|
json::get<"type"_>(*data.event) == "m.room.member" &&
|
|
|
|
json::get<"state_key"_>(*data.event) == data.user.user_id)
|
|
|
|
{
|
|
|
|
const auto append{[&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
room_state_append(data, array, event, index(event));
|
|
|
|
}};
|
|
|
|
|
|
|
|
const m::room::state state{*data.room};
|
|
|
|
state.get(std::nothrow, "m.room.create", "", append);
|
|
|
|
state.get(std::nothrow, "m.room.join_rules", "", append);
|
|
|
|
state.get(std::nothrow, "m.room.history_visibility", "", append);
|
2019-07-25 00:11:19 +02:00
|
|
|
state.get(std::nothrow, "m.room.avatar", "", append);
|
2019-07-15 22:56:50 +02:00
|
|
|
|
|
|
|
const auto &sender(json::get<"sender"_>(*data.event));
|
|
|
|
state.get(std::nothrow, "m.room.member", sender, append);
|
|
|
|
}
|
|
|
|
|
2019-03-05 23:43:00 +01:00
|
|
|
room_state_append(data, array, *data.event, data.event_idx);
|
2019-02-27 02:34:07 +01:00
|
|
|
return true;
|
2019-01-04 23:47:01 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 02:21:02 +01:00
|
|
|
ircd::m::sync::room_state_polylog(data &data)
|
2019-03-04 01:31:43 +01:00
|
|
|
{
|
|
|
|
if(data.membership == "invite")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return _room_state_polylog(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::room_invite_state_polylog(data &data)
|
|
|
|
{
|
|
|
|
if(data.membership != "invite")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return _room_state_polylog(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::_room_state_polylog(data &data)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
if(!apropos(data, data.room_head))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return room_state_polylog_events(data);
|
2019-01-09 00:10:06 +01:00
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-10 05:39:12 +01:00
|
|
|
ircd::m::sync::room_state_polylog_events(data &data)
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
2019-04-08 15:11:16 +02:00
|
|
|
if(data.phased && data.range.first == 0)
|
|
|
|
return room_state_phased_events(data);
|
|
|
|
|
2019-07-15 21:46:19 +02:00
|
|
|
bool ret{false};
|
|
|
|
ctx::mutex mutex;
|
2019-01-04 02:21:02 +01:00
|
|
|
json::stack::array array
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "events"
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-07-15 21:46:19 +02:00
|
|
|
sync::pool.min(64); //TODO: XXX
|
|
|
|
ctx::concurrent<event::idx> concurrent
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-07-15 21:46:19 +02:00
|
|
|
sync::pool, [&](const event::idx &event_idx)
|
2019-03-23 10:39:03 +01:00
|
|
|
{
|
2019-07-15 21:46:19 +02:00
|
|
|
const m::event::fetch event
|
2019-03-23 10:39:03 +01:00
|
|
|
{
|
2019-07-15 21:46:19 +02:00
|
|
|
event_idx, std::nothrow, _default_fopts
|
2019-03-23 10:39:03 +01:00
|
|
|
};
|
|
|
|
|
2019-07-15 21:46:19 +02:00
|
|
|
if(unlikely(!event.valid))
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Failed to fetch event idx:%lu in room %s state.",
|
|
|
|
event_idx,
|
|
|
|
string_view{data.room->room_id},
|
|
|
|
};
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::lock_guard lock{mutex};
|
2019-07-16 22:29:15 +02:00
|
|
|
ret |= room_state_append(data, array, event, event_idx);
|
2019-03-23 10:39:03 +01:00
|
|
|
}
|
2019-01-09 00:10:06 +01:00
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-07-15 21:46:19 +02:00
|
|
|
const room::state state{*data.room};
|
|
|
|
state.for_each([&data, &concurrent]
|
|
|
|
(const event::idx &event_idx)
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
2019-07-13 00:18:26 +02:00
|
|
|
if(!apropos(data, event_idx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
concurrent(event_idx);
|
2019-01-09 00:10:06 +01:00
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
2019-07-15 21:46:19 +02:00
|
|
|
concurrent.wait();
|
2019-02-24 20:59:53 +01:00
|
|
|
return ret;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
2019-03-05 23:43:00 +01:00
|
|
|
|
2019-04-08 15:11:16 +02:00
|
|
|
bool
|
|
|
|
ircd::m::sync::room_state_phased_events(data &data)
|
|
|
|
{
|
2019-07-14 22:40:25 +02:00
|
|
|
bool ret{false};
|
|
|
|
ctx::mutex mutex;
|
2019-04-08 15:11:16 +02:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
|
|
|
|
2019-07-14 22:40:25 +02:00
|
|
|
static const std::pair<string_view, string_view> keys[]
|
2019-07-04 12:32:17 +02:00
|
|
|
{
|
2019-07-14 22:40:25 +02:00
|
|
|
{ "m.room.create", "" },
|
|
|
|
{ "m.room.canonical_alias", "" },
|
|
|
|
{ "m.room.name", "" },
|
|
|
|
{ "m.room.avatar", "" },
|
|
|
|
{ "m.room.aliases", data.user.user_id.host() },
|
|
|
|
{ "m.room.member", data.user.user_id },
|
|
|
|
};
|
2019-04-08 15:11:16 +02:00
|
|
|
|
2019-07-14 22:40:25 +02:00
|
|
|
const auto append
|
2019-04-08 15:11:16 +02:00
|
|
|
{
|
2019-07-14 22:40:25 +02:00
|
|
|
[&data, &array, &ret, &mutex](const m::event &event)
|
|
|
|
{
|
2019-07-15 00:13:49 +02:00
|
|
|
const auto event_idx(m::index(event));
|
2019-07-14 22:40:25 +02:00
|
|
|
const std::lock_guard lock{mutex};
|
2019-07-16 22:29:15 +02:00
|
|
|
ret |= room_state_append(data, array, event, event_idx);
|
2019-07-14 22:40:25 +02:00
|
|
|
}
|
|
|
|
};
|
2019-04-08 15:11:16 +02:00
|
|
|
|
2019-07-15 00:13:49 +02:00
|
|
|
sync::pool.min(6);
|
2019-07-14 22:40:25 +02:00
|
|
|
ctx::concurrent_for_each<const std::pair<string_view, string_view>>
|
2019-07-04 12:07:54 +02:00
|
|
|
{
|
2019-07-14 22:40:25 +02:00
|
|
|
sync::pool, keys, [&data, &append](const auto &key)
|
|
|
|
{
|
|
|
|
data.room->get(std::nothrow, key.first, key.second, append);
|
|
|
|
}
|
|
|
|
};
|
2019-07-04 12:07:54 +02:00
|
|
|
|
2019-07-07 14:44:47 +02:00
|
|
|
ret |= room_state_phased_member_events(data, array);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::room_state_phased_member_events(data &data,
|
|
|
|
json::stack::array &array)
|
|
|
|
{
|
|
|
|
static const auto count{10}, bufsz{48}, limit{10};
|
|
|
|
std::array<char[bufsz], count> buf;
|
|
|
|
std::array<string_view, count> last;
|
|
|
|
size_t i(0), ret(0);
|
|
|
|
m::room::messages it
|
|
|
|
{
|
|
|
|
*data.room
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto already{[&last, &ret]
|
|
|
|
(const string_view &sender) -> bool
|
|
|
|
{
|
|
|
|
return std::any_of(begin(last), begin(last)+ret, [&sender]
|
|
|
|
(const auto &last)
|
|
|
|
{
|
|
|
|
return startswith(last, sender);
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
|
|
|
for(; it && ret < count && i < limit; --it, ++i)
|
|
|
|
{
|
|
|
|
const auto &event_idx(it.event_idx());
|
|
|
|
m::get(std::nothrow, event_idx, "sender", [&]
|
|
|
|
(const auto &sender)
|
|
|
|
{
|
|
|
|
if(already(sender))
|
|
|
|
return;
|
|
|
|
|
2019-07-25 00:11:19 +02:00
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow
|
|
|
|
};
|
2019-07-07 14:44:47 +02:00
|
|
|
|
2019-07-25 00:11:19 +02:00
|
|
|
if(!event.valid)
|
2019-07-07 14:44:47 +02:00
|
|
|
return;
|
|
|
|
|
2019-07-25 00:11:19 +02:00
|
|
|
last.at(ret) = strlcpy(buf.at(ret), sender);
|
2019-07-07 14:44:47 +02:00
|
|
|
room_state_append(data, array, event, event_idx);
|
2019-07-25 00:11:19 +02:00
|
|
|
++ret;
|
2019-07-07 14:44:47 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-04-08 15:11:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-16 22:29:15 +02:00
|
|
|
bool
|
2019-03-05 23:43:00 +01:00
|
|
|
ircd::m::sync::room_state_append(data &data,
|
|
|
|
json::stack::array &events,
|
|
|
|
const m::event &event,
|
|
|
|
const m::event::idx &event_idx)
|
|
|
|
{
|
2019-03-12 00:02:48 +01:00
|
|
|
m::event_append_opts opts;
|
|
|
|
opts.event_idx = &event_idx;
|
|
|
|
opts.user_id = &data.user.user_id;
|
|
|
|
opts.user_room = &data.user_room;
|
2019-03-14 01:03:01 +01:00
|
|
|
opts.query_txnid = false;
|
2019-04-15 21:16:48 +02:00
|
|
|
opts.room_depth = &data.room_depth;
|
2019-07-16 22:29:15 +02:00
|
|
|
return m::append(events, event, opts);
|
2019-03-05 23:43:00 +01:00
|
|
|
}
|