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-03-05 23:43:00 +01:00
|
|
|
static void room_state_append(data &, json::stack::array &, const m::event &, const m::event::idx &);
|
|
|
|
|
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-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
|
|
|
{
|
|
|
|
if(data.membership == "invite")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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)
|
|
|
|
if(json::get<"depth"_>(*data.event) + int64_t(state_exposure_depth) >= data.room_depth)
|
|
|
|
return false;
|
|
|
|
|
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"?
|
|
|
|
"invite_state"_sv: // "invite_state"_sv:
|
|
|
|
"state"_sv
|
|
|
|
};
|
|
|
|
|
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-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-01-18 22:05:31 +01:00
|
|
|
const m::room &room{*data.room};
|
|
|
|
const m::room::state state{room};
|
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
|
|
|
};
|
|
|
|
|
|
|
|
ctx::mutex mutex;
|
2019-04-26 14:41:35 +02:00
|
|
|
std::array<event::idx, 64> md; //TODO: conf
|
|
|
|
std::vector<event::fetch> event(md.size() * 2);
|
|
|
|
for(auto &fetch : event)
|
|
|
|
fetch = event::fetch{_default_fopts};
|
|
|
|
|
|
|
|
size_t _i(0);
|
|
|
|
bool ret{false};
|
|
|
|
const event::closure_idx each_idx{[&data, &array, &mutex, &ret, &event, &_i]
|
2019-01-21 20:53:57 +01:00
|
|
|
(const m::event::idx event_idx)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-04-26 14:41:35 +02:00
|
|
|
const size_t i{_i++ % event.size()};
|
|
|
|
if(unlikely(!seek(event.at(i), event_idx, std::nothrow)))
|
2019-03-23 10:39:03 +01:00
|
|
|
{
|
|
|
|
assert(data.room);
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Failed to fetch event idx:%lu in room %s state.",
|
|
|
|
event_idx,
|
|
|
|
string_view{data.room->room_id}
|
|
|
|
};
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
return;
|
2019-03-23 10:39:03 +01:00
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-03-02 21:33:32 +01:00
|
|
|
const std::lock_guard lock{mutex};
|
2019-04-26 14:41:35 +02:00
|
|
|
room_state_append(data, array, event.at(i), event_idx);
|
2019-02-24 20:59:53 +01:00
|
|
|
ret = true;
|
2019-01-04 02:21:02 +01:00
|
|
|
}};
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
ctx::parallel<event::idx> parallel
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
m::sync::pool, md, each_idx
|
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
state.for_each([&data, ¶llel]
|
|
|
|
(const m::event::idx &event_idx)
|
|
|
|
{
|
|
|
|
if(apropos(data, event_idx))
|
|
|
|
parallel(event_idx);
|
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
2019-03-04 01:23:10 +01:00
|
|
|
parallel.wait_done();
|
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)
|
|
|
|
{
|
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
data.room->get(std::nothrow, "m.room.create", "", [&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
room_state_append(data, array, event, index(event));
|
|
|
|
ret = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
data.room->get(std::nothrow, "m.room.canonical_alias", "", [&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
room_state_append(data, array, event, index(event));
|
|
|
|
});
|
|
|
|
|
|
|
|
data.room->get(std::nothrow, "m.room.name", "", [&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
room_state_append(data, array, event, index(event));
|
|
|
|
});
|
|
|
|
|
|
|
|
data.room->get(std::nothrow, "m.room.avatar", "", [&]
|
|
|
|
(const m::event &event)
|
|
|
|
{
|
|
|
|
room_state_append(data, array, event, index(event));
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-05 23:43:00 +01:00
|
|
|
void
|
|
|
|
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-03-12 19:43:58 +01:00
|
|
|
m::append(events, event, opts);
|
2019-03-05 23:43:00 +01:00
|
|
|
}
|