2019-01-04 14:47:01 -08: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-14 17:59:45 -07:00
|
|
|
static bool should_ignore(const data &);
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
static bool _rooms_polylog_room(data &, const m::room &);
|
2019-04-08 02:04:24 -07:00
|
|
|
static bool _rooms_polylog(data &, const string_view &membership, int64_t &phase);
|
2019-02-24 11:59:53 -08:00
|
|
|
static bool rooms_polylog(data &);
|
2019-01-09 20:39:12 -08:00
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
static bool _rooms_linear(data &, const string_view &membership);
|
|
|
|
static bool rooms_linear(data &);
|
2019-01-09 20:39:12 -08:00
|
|
|
|
2019-01-04 14:47:01 -08:00
|
|
|
extern item rooms;
|
|
|
|
}
|
|
|
|
|
2019-09-09 12:05:53 -07:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Rooms"
|
|
|
|
};
|
|
|
|
|
2019-01-04 14:47:01 -08:00
|
|
|
decltype(ircd::m::sync::rooms)
|
|
|
|
ircd::m::sync::rooms
|
|
|
|
{
|
2019-04-08 02:04:24 -07:00
|
|
|
"rooms", rooms_polylog, rooms_linear,
|
|
|
|
{
|
|
|
|
{ "phased", true }
|
|
|
|
}
|
2019-01-04 14:47:01 -08:00
|
|
|
};
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool
|
2019-01-04 14:47:01 -08:00
|
|
|
ircd::m::sync::rooms_linear(data &data)
|
|
|
|
{
|
2019-02-24 11:59:53 -08:00
|
|
|
assert(data.event);
|
|
|
|
const m::room room
|
|
|
|
{
|
2019-03-08 13:42:54 -08:00
|
|
|
json::get<"room_id"_>(*data.event)?
|
|
|
|
m::room::id{json::get<"room_id"_>(*data.event)}:
|
|
|
|
m::room::id{}
|
2019-02-24 11:59:53 -08:00
|
|
|
};
|
|
|
|
|
2019-02-26 17:34:07 -08:00
|
|
|
const scope_restore their_room
|
2019-02-24 11:59:53 -08:00
|
|
|
{
|
|
|
|
data.room, &room
|
|
|
|
};
|
|
|
|
|
2019-07-25 14:02:25 -07:00
|
|
|
char membuf[room::MEMBERSHIP_MAX_SIZE];
|
2019-02-26 17:34:07 -08:00
|
|
|
const string_view &membership
|
|
|
|
{
|
2019-03-08 13:42:54 -08:00
|
|
|
data.room?
|
2019-08-14 01:01:46 -07:00
|
|
|
m::membership(membuf, room, data.user):
|
2019-03-08 13:42:54 -08:00
|
|
|
string_view{}
|
2019-02-26 17:34:07 -08:00
|
|
|
};
|
2019-01-04 14:47:01 -08:00
|
|
|
|
2019-02-26 17:34:07 -08:00
|
|
|
const scope_restore their_membership
|
2019-01-09 20:39:12 -08:00
|
|
|
{
|
|
|
|
data.membership, membership
|
|
|
|
};
|
|
|
|
|
2019-03-14 17:59:45 -07:00
|
|
|
if(should_ignore(data))
|
|
|
|
return false;
|
|
|
|
|
2019-04-15 12:16:48 -07:00
|
|
|
const auto room_head
|
|
|
|
{
|
2019-04-15 14:46:14 -07:00
|
|
|
data.event_idx && room != data.user_room?
|
|
|
|
m::head_idx(std::nothrow, room):
|
|
|
|
0UL
|
2019-04-15 12:16:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore their_head
|
|
|
|
{
|
|
|
|
data.room_head, room_head
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto room_depth
|
|
|
|
{
|
2019-04-15 14:46:14 -07:00
|
|
|
data.event_idx && room != data.user_room?
|
|
|
|
m::depth(std::nothrow, room):
|
|
|
|
-1L
|
2019-04-15 12:16:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore their_depth
|
|
|
|
{
|
|
|
|
data.room_depth, room_depth
|
|
|
|
};
|
|
|
|
|
2019-03-27 19:32:54 -07:00
|
|
|
bool ret{false};
|
|
|
|
m::sync::for_each("rooms", [&data, &ret]
|
2019-02-24 11:59:53 -08:00
|
|
|
(item &item)
|
2019-01-09 20:39:12 -08:00
|
|
|
{
|
2019-02-26 17:34:07 -08:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
|
|
|
if(item.linear(data))
|
2019-03-27 19:32:54 -07:00
|
|
|
ret = true;
|
|
|
|
else
|
|
|
|
checkpoint.rollback();
|
2019-02-24 11:59:53 -08:00
|
|
|
|
2019-03-08 13:42:54 -08:00
|
|
|
return true;
|
|
|
|
});
|
2019-03-27 19:32:54 -07:00
|
|
|
|
|
|
|
return ret;
|
2019-01-09 20:39:12 -08:00
|
|
|
}
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool
|
2019-01-04 14:47:01 -08:00
|
|
|
ircd::m::sync::rooms_polylog(data &data)
|
|
|
|
{
|
2019-02-24 11:59:53 -08:00
|
|
|
bool ret{false};
|
2019-04-08 02:04:24 -07:00
|
|
|
int64_t phase(0);
|
|
|
|
|
|
|
|
ret |= _rooms_polylog(data, "join", phase);
|
|
|
|
if(data.phased && ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret |= _rooms_polylog(data, "invite", phase);
|
|
|
|
if(data.phased && ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret |= _rooms_polylog(data, "leave", phase);
|
|
|
|
if(data.phased && ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret |= _rooms_polylog(data, "ban", phase);
|
|
|
|
if(data.phased && ret)
|
|
|
|
return ret;
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
return ret;
|
2019-01-04 14:47:01 -08:00
|
|
|
}
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool
|
2019-01-04 14:47:01 -08:00
|
|
|
ircd::m::sync::_rooms_polylog(data &data,
|
2019-04-08 02:04:24 -07:00
|
|
|
const string_view &membership,
|
|
|
|
int64_t &phase)
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
2019-03-02 12:33:32 -08:00
|
|
|
const scope_restore theirs
|
2019-01-09 20:39:12 -08:00
|
|
|
{
|
|
|
|
data.membership, membership
|
|
|
|
};
|
|
|
|
|
2019-01-08 15:10:06 -08:00
|
|
|
json::stack::object object
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
2019-02-26 15:50:58 -08:00
|
|
|
*data.out, membership
|
2019-01-04 14:47:01 -08:00
|
|
|
};
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool ret{false};
|
2019-04-08 02:04:24 -07:00
|
|
|
const user::rooms::closure_bool closure{[&data, &ret, &phase]
|
2019-01-09 20:39:12 -08:00
|
|
|
(const m::room &room, const string_view &membership_)
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
2019-04-08 02:04:24 -07:00
|
|
|
if(data.phased)
|
|
|
|
{
|
|
|
|
if(phase < int64_t(data.range.first) && ret)
|
|
|
|
return false;
|
2019-04-08 04:43:23 -07:00
|
|
|
|
|
|
|
if(int64_t(data.range.first) < 0L)
|
|
|
|
--phase;
|
|
|
|
|
|
|
|
if(phase > int64_t(data.range.first))
|
|
|
|
return true;
|
2019-04-08 02:04:24 -07:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:17:21 -08:00
|
|
|
#if defined(RB_DEBUG)
|
2019-01-10 13:19:07 -08:00
|
|
|
sync::stats stats
|
|
|
|
{
|
2019-03-09 15:17:21 -08:00
|
|
|
data.stats && rooms.stats_debug?
|
2019-01-10 13:19:07 -08:00
|
|
|
*data.stats:
|
|
|
|
sync::stats{}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(data.stats)
|
|
|
|
stats.timer = timer{};
|
2019-01-04 14:47:01 -08:00
|
|
|
#endif
|
|
|
|
|
2019-08-16 03:55:07 -07:00
|
|
|
ret |= _rooms_polylog_room(data, room);
|
2019-04-08 02:04:24 -07:00
|
|
|
|
2019-03-09 15:17:21 -08:00
|
|
|
#if defined(RB_DEBUG)
|
2019-07-24 17:42:31 -07:00
|
|
|
thread_local char tmbuf[32];
|
2019-03-09 15:17:21 -08:00
|
|
|
if(data.stats && rooms.stats_debug) log::debug
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
2019-04-22 14:47:06 -07:00
|
|
|
log, "polylog %s %s %s in %s",
|
2019-01-08 15:10:06 -08:00
|
|
|
loghead(data),
|
2019-04-22 14:47:06 -07:00
|
|
|
membership_,
|
2019-01-04 14:47:01 -08:00
|
|
|
string_view{room.room_id},
|
2019-01-26 17:01:36 -08:00
|
|
|
ircd::pretty(tmbuf, stats.timer.at<milliseconds>(), true)
|
2019-01-04 14:47:01 -08:00
|
|
|
};
|
|
|
|
#endif
|
2019-04-08 02:04:24 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}};
|
|
|
|
|
|
|
|
const bool done
|
|
|
|
{
|
|
|
|
data.user_rooms.for_each(membership, closure)
|
|
|
|
};
|
2019-02-24 11:59:53 -08:00
|
|
|
|
|
|
|
return ret;
|
2019-01-04 14:47:01 -08:00
|
|
|
}
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool
|
2019-01-04 14:47:01 -08:00
|
|
|
ircd::m::sync::_rooms_polylog_room(data &data,
|
|
|
|
const m::room &room)
|
|
|
|
{
|
2019-03-02 12:33:32 -08:00
|
|
|
const scope_restore theirs
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
|
|
|
data.room, &room
|
|
|
|
};
|
|
|
|
|
2019-03-14 17:59:45 -07:00
|
|
|
if(should_ignore(data))
|
|
|
|
return false;
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
2019-02-26 15:50:58 -08:00
|
|
|
*data.out
|
2019-02-24 11:59:53 -08:00
|
|
|
};
|
|
|
|
|
2019-01-09 20:39:12 -08:00
|
|
|
json::stack::object object
|
|
|
|
{
|
2019-02-26 15:50:58 -08:00
|
|
|
*data.out, room.room_id
|
2019-01-09 20:39:12 -08:00
|
|
|
};
|
|
|
|
|
2020-04-03 15:09:32 -07:00
|
|
|
const auto &[top_event_id, top_depth, top_event_idx]
|
2019-02-22 09:25:00 -08:00
|
|
|
{
|
2019-04-15 12:16:48 -07:00
|
|
|
m::top(std::nothrow, room)
|
2019-02-22 09:25:00 -08:00
|
|
|
};
|
|
|
|
|
2019-03-02 12:33:32 -08:00
|
|
|
const scope_restore their_head
|
2019-02-22 09:25:00 -08:00
|
|
|
{
|
2020-04-03 15:09:32 -07:00
|
|
|
data.room_head, top_event_idx
|
2019-04-15 12:16:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore their_depth
|
|
|
|
{
|
2020-04-03 15:09:32 -07:00
|
|
|
data.room_depth, top_depth
|
2019-02-22 09:25:00 -08:00
|
|
|
};
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool ret{false};
|
|
|
|
m::sync::for_each("rooms", [&data, &ret]
|
2019-01-09 20:39:12 -08:00
|
|
|
(item &item)
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
2019-02-24 11:59:53 -08:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
2019-02-26 15:50:58 -08:00
|
|
|
*data.out
|
2019-02-24 11:59:53 -08:00
|
|
|
};
|
|
|
|
|
2019-02-22 09:25:00 -08:00
|
|
|
json::stack::object object
|
2019-01-04 14:47:01 -08:00
|
|
|
{
|
2019-02-26 15:50:58 -08:00
|
|
|
*data.out, item.member_name()
|
2019-01-04 14:47:01 -08:00
|
|
|
};
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
if(item.polylog(data))
|
2019-03-30 15:02:18 -07:00
|
|
|
{
|
2019-02-24 11:59:53 -08:00
|
|
|
ret = true;
|
2019-03-30 15:02:18 -07:00
|
|
|
data.out->invalidate_checkpoints();
|
|
|
|
}
|
2020-04-14 15:22:40 -07:00
|
|
|
else checkpoint.committing(false);
|
2019-02-24 11:59:53 -08:00
|
|
|
|
2019-01-08 15:10:06 -08:00
|
|
|
return true;
|
|
|
|
});
|
2019-02-24 11:59:53 -08:00
|
|
|
|
|
|
|
if(!ret)
|
2020-04-14 15:22:40 -07:00
|
|
|
checkpoint.committing(false);
|
2019-02-24 11:59:53 -08:00
|
|
|
|
|
|
|
return ret;
|
2019-01-04 14:47:01 -08:00
|
|
|
}
|
2019-03-14 17:59:45 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::should_ignore(const data &data)
|
|
|
|
{
|
|
|
|
if(data.membership != "invite")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!m::user::ignores::enforce("invites"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
assert(data.room);
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
*data.room
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::idx &event_idx
|
|
|
|
{
|
|
|
|
state.get(std::nothrow, "m.room.member", data.user.user_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::user::ignores ignores
|
|
|
|
{
|
|
|
|
data.user.user_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
m::get(std::nothrow, event_idx, "sender", [&ignores, &ret]
|
|
|
|
(const m::user::id &sender)
|
|
|
|
{
|
|
|
|
ret = ignores.has(sender);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|