2019-01-04 23:47: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.
|
|
|
|
|
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Rooms"
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool _rooms_polylog_room(data &, const m::room &);
|
|
|
|
static bool _rooms_polylog(data &, const string_view &membership);
|
|
|
|
static bool rooms_polylog(data &);
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool _rooms_linear(data &, const string_view &membership);
|
|
|
|
static bool rooms_linear(data &);
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-01-04 23:47:01 +01:00
|
|
|
extern item rooms;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::rooms)
|
|
|
|
ircd::m::sync::rooms
|
|
|
|
{
|
|
|
|
"rooms",
|
|
|
|
rooms_polylog,
|
|
|
|
rooms_linear
|
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 23:47:01 +01:00
|
|
|
ircd::m::sync::rooms_linear(data &data)
|
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
assert(data.event);
|
|
|
|
const auto &event{*data.event};
|
|
|
|
if(!json::get<"room_id"_>(event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
json::get<"room_id"_>(event)
|
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
const scope_restore their_room
|
2019-02-24 20:59:53 +01:00
|
|
|
{
|
|
|
|
data.room, &room
|
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
char membuf[32];
|
|
|
|
const string_view &membership
|
|
|
|
{
|
|
|
|
data.room->membership(membuf, data.user)
|
|
|
|
};
|
2019-01-04 23:47:01 +01:00
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
if(!membership)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const scope_restore their_membership
|
2019-01-10 05:39:12 +01:00
|
|
|
{
|
|
|
|
data.membership, membership
|
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
const event::idx room_head
|
|
|
|
{
|
|
|
|
head_idx(std::nothrow, *data.room)
|
|
|
|
};
|
|
|
|
|
|
|
|
const scope_restore their_head
|
|
|
|
{
|
|
|
|
data.room_head, room_head
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object membership_
|
|
|
|
{
|
|
|
|
*data.out, membership
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object room_
|
|
|
|
{
|
|
|
|
*data.out, data.room->room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret(false);
|
2019-02-24 20:59:53 +01:00
|
|
|
m::sync::for_each("rooms", [&data, &ret]
|
|
|
|
(item &item)
|
2019-01-10 05:39:12 +01:00
|
|
|
{
|
2019-02-27 02:34:07 +01:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
json::stack::object object
|
2019-01-10 05:39:12 +01:00
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, item.member_name()
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
if(item.linear(data))
|
|
|
|
ret = true;
|
|
|
|
else
|
|
|
|
checkpoint.rollback();
|
|
|
|
|
|
|
|
return !ret;
|
2019-01-10 05:39:12 +01:00
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
if(!ret)
|
|
|
|
checkpoint.rollback();
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
return ret;
|
2019-01-10 05:39:12 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 23:47:01 +01:00
|
|
|
ircd::m::sync::rooms_polylog(data &data)
|
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
bool ret{false};
|
|
|
|
ret |= _rooms_polylog(data, "invite");
|
|
|
|
ret |= _rooms_polylog(data, "join");
|
|
|
|
ret |= _rooms_polylog(data, "leave");
|
|
|
|
ret |= _rooms_polylog(data, "ban");
|
|
|
|
return ret;
|
2019-01-04 23:47:01 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 23:47:01 +01:00
|
|
|
ircd::m::sync::_rooms_polylog(data &data,
|
|
|
|
const string_view &membership)
|
|
|
|
{
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore theirs
|
2019-01-10 05:39:12 +01:00
|
|
|
{
|
|
|
|
data.membership, membership
|
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out
|
2019-02-24 20:59:53 +01:00
|
|
|
};
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
json::stack::object object
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, membership
|
2019-01-04 23:47:01 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool ret{false};
|
|
|
|
data.user_rooms.for_each(membership, [&data, &ret]
|
2019-01-10 05:39:12 +01:00
|
|
|
(const m::room &room, const string_view &membership_)
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
2019-02-23 02:05:28 +01:00
|
|
|
#if defined(RB_DEBUG) && 0
|
2019-01-10 22:19:07 +01:00
|
|
|
sync::stats stats
|
|
|
|
{
|
|
|
|
data.stats?
|
|
|
|
*data.stats:
|
|
|
|
sync::stats{}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(data.stats)
|
|
|
|
stats.timer = timer{};
|
2019-01-04 23:47:01 +01:00
|
|
|
#endif
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
ret |= _rooms_polylog_room(data, room);
|
2019-01-04 23:47:01 +01:00
|
|
|
|
2019-02-23 02:05:28 +01:00
|
|
|
#if defined(RB_DEBUG) && 0
|
2019-01-09 00:10:06 +01:00
|
|
|
thread_local char tmbuf[32];
|
2019-02-22 20:13:55 +01:00
|
|
|
if(data.stats && bool(stats_debug)) log::debug
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
log, "polylog %s %s in %s",
|
|
|
|
loghead(data),
|
2019-01-04 23:47:01 +01:00
|
|
|
string_view{room.room_id},
|
2019-01-27 02:01:36 +01:00
|
|
|
ircd::pretty(tmbuf, stats.timer.at<milliseconds>(), true)
|
2019-01-04 23:47:01 +01:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
checkpoint.rollback();
|
|
|
|
|
|
|
|
return ret;
|
2019-01-04 23:47:01 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 23:47:01 +01:00
|
|
|
ircd::m::sync::_rooms_polylog_room(data &data,
|
|
|
|
const m::room &room)
|
|
|
|
{
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore theirs
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
|
|
|
data.room, &room
|
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out
|
2019-02-24 20:59:53 +01:00
|
|
|
};
|
|
|
|
|
2019-01-10 05:39:12 +01:00
|
|
|
json::stack::object object
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, room.room_id
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
|
|
|
|
2019-02-22 18:25:00 +01:00
|
|
|
const event::idx room_head
|
|
|
|
{
|
|
|
|
head_idx(std::nothrow, room)
|
|
|
|
};
|
|
|
|
|
2019-03-02 21:33:32 +01:00
|
|
|
const scope_restore their_head
|
2019-02-22 18:25:00 +01:00
|
|
|
{
|
|
|
|
data.room_head, room_head
|
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool ret{false};
|
|
|
|
m::sync::for_each("rooms", [&data, &ret]
|
2019-01-10 05:39:12 +01:00
|
|
|
(item &item)
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
2019-02-24 20:59:53 +01:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out
|
2019-02-24 20:59:53 +01:00
|
|
|
};
|
|
|
|
|
2019-02-22 18:25:00 +01:00
|
|
|
json::stack::object object
|
2019-01-04 23:47:01 +01:00
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, item.member_name()
|
2019-01-04 23:47:01 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
if(item.polylog(data))
|
|
|
|
ret = true;
|
|
|
|
else
|
|
|
|
checkpoint.rollback();
|
|
|
|
|
2019-01-09 00:10:06 +01:00
|
|
|
return true;
|
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
checkpoint.rollback();
|
|
|
|
|
|
|
|
return ret;
|
2019-01-04 23:47:01 +01:00
|
|
|
}
|