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.
|
|
|
|
|
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Room Account Data"
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
2019-03-06 02:10:22 +01:00
|
|
|
static bool room_account_data_polylog_tags(data &);
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool room_account_data_polylog_events_event(data &, const m::event &);
|
|
|
|
static bool room_account_data_polylog_events(data &);
|
|
|
|
static bool room_account_data_polylog(data &);
|
2019-03-06 02:10:22 +01:00
|
|
|
|
|
|
|
static bool room_account_data_linear_tags(data &, const m::event &);
|
|
|
|
static bool room_account_data_linear_events(data &, const m::event &);
|
2019-02-27 02:34:07 +01:00
|
|
|
static bool room_account_data_linear(data &);
|
2019-01-10 05:39:12 +01:00
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
extern item room_account_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::sync::room_account_data)
|
|
|
|
ircd::m::sync::room_account_data
|
|
|
|
{
|
2019-01-09 00:10:06 +01:00
|
|
|
"rooms.account_data",
|
2019-02-27 02:34:07 +01:00
|
|
|
room_account_data_polylog,
|
2019-04-16 09:49:28 +02:00
|
|
|
room_account_data_linear,
|
2019-08-17 12:43:01 +02:00
|
|
|
{
|
|
|
|
{ "phased", true }
|
|
|
|
}
|
2019-01-04 02:21:02 +01:00
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
bool
|
|
|
|
ircd::m::sync::room_account_data_linear(data &data)
|
|
|
|
{
|
2019-03-01 21:42:11 +01:00
|
|
|
if(!data.event || !data.event_idx)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const m::event &event{*data.event};
|
|
|
|
if(json::get<"room_id"_>(event) != data.user_room.room_id)
|
|
|
|
return false;
|
|
|
|
|
2019-03-30 21:46:54 +01:00
|
|
|
json::stack::checkpoint checkpoint
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
2019-03-06 02:10:22 +01:00
|
|
|
if(room_account_data_linear_events(data, event))
|
|
|
|
return true;
|
|
|
|
|
2019-03-30 21:46:54 +01:00
|
|
|
checkpoint.rollback();
|
2019-03-06 02:10:22 +01:00
|
|
|
if(room_account_data_linear_tags(data, event))
|
|
|
|
return true;
|
|
|
|
|
2019-03-30 21:46:54 +01:00
|
|
|
checkpoint.rollback();
|
2019-03-06 02:10:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::room_account_data_linear_events(data &data,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
if(!json::get<"state_key"_>(event))
|
|
|
|
return false;
|
|
|
|
|
2019-03-01 21:42:11 +01:00
|
|
|
const auto type
|
|
|
|
{
|
2019-03-06 02:10:22 +01:00
|
|
|
split(json::get<"type"_>(event), '!')
|
2019-03-01 21:42:11 +01:00
|
|
|
};
|
|
|
|
|
2019-03-06 02:10:22 +01:00
|
|
|
if(type.first != "ircd.account_data")
|
2019-03-01 21:42:11 +01:00
|
|
|
return false;
|
|
|
|
|
2019-04-02 18:53:02 +02:00
|
|
|
if(!type.second)
|
|
|
|
return false;
|
|
|
|
|
2019-03-06 02:10:22 +01:00
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
lstrip(json::get<"type"_>(event), type.first)
|
|
|
|
};
|
|
|
|
|
2019-07-25 23:02:25 +02:00
|
|
|
char membuf[room::MEMBERSHIP_MAX_SIZE];
|
2019-03-08 22:42:54 +01:00
|
|
|
const auto membership
|
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
m::membership(membuf, room, data.user)
|
2019-03-08 22:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!membership)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
json::stack::object rooms
|
|
|
|
{
|
|
|
|
*data.out, "rooms"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object membership_
|
|
|
|
{
|
|
|
|
*data.out, membership
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object room_
|
|
|
|
{
|
|
|
|
*data.out, room.room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object account_data
|
|
|
|
{
|
|
|
|
*data.out, "account_data"
|
|
|
|
};
|
|
|
|
|
2019-03-01 21:42:11 +01:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
const scope_restore room__{data.room, &room};
|
2019-03-30 21:48:01 +01:00
|
|
|
const scope_restore range_upper{data.range.second, data.event_idx + 1};
|
2019-03-01 21:42:11 +01:00
|
|
|
return room_account_data_polylog_events_event(data, event);
|
2019-02-27 02:34:07 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-03-06 02:10:22 +01:00
|
|
|
ircd::m::sync::room_account_data_linear_tags(data &data,
|
|
|
|
const m::event &event)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-03-06 02:10:22 +01:00
|
|
|
if(!json::get<"state_key"_>(event))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto type
|
|
|
|
{
|
|
|
|
split(json::get<"type"_>(event), '!')
|
|
|
|
};
|
|
|
|
|
|
|
|
if(type.first != "ircd.room_tag")
|
|
|
|
return false;
|
|
|
|
|
2019-04-02 18:53:02 +02:00
|
|
|
if(!type.second)
|
|
|
|
return false;
|
|
|
|
|
2019-03-06 02:10:22 +01:00
|
|
|
const m::room room
|
|
|
|
{
|
|
|
|
lstrip(json::get<"type"_>(event), type.first)
|
|
|
|
};
|
|
|
|
|
2019-07-25 23:02:25 +02:00
|
|
|
char membuf[room::MEMBERSHIP_MAX_SIZE];
|
2019-03-08 22:42:54 +01:00
|
|
|
const auto membership
|
|
|
|
{
|
2019-08-14 10:01:46 +02:00
|
|
|
m::membership(membuf, room, data.user)
|
2019-03-08 22:42:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!membership)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
json::stack::object rooms
|
|
|
|
{
|
|
|
|
*data.out, "rooms"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object membership_
|
|
|
|
{
|
|
|
|
*data.out, membership
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object room_
|
|
|
|
{
|
|
|
|
*data.out, room.room_id
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object account_data
|
|
|
|
{
|
|
|
|
*data.out, "account_data"
|
|
|
|
};
|
|
|
|
|
2019-03-06 02:10:22 +01:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Due to room tags being "all one event" we have to iterate all of the
|
|
|
|
// tags for this room for this user polylog style. This is because the
|
|
|
|
// merge algorithm for linear /sync isn't sophisticated enough to see
|
|
|
|
// past the events[] array and know to combine all of room tags into
|
|
|
|
// the required format. The event_idx is hacked to 0 here to trick the
|
|
|
|
// polylog apropos() into composing all tags unconditionally.
|
2019-03-30 21:48:01 +01:00
|
|
|
const scope_restore range_lower{data.range.first, 0UL};
|
2019-03-08 22:42:54 +01:00
|
|
|
const scope_restore room__{data.room, &room};
|
2019-03-06 02:10:22 +01:00
|
|
|
return room_account_data_polylog_tags(data);
|
2019-01-09 00:10:06 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-03-06 02:10:22 +01:00
|
|
|
ircd::m::sync::room_account_data_polylog(data &data)
|
2019-01-09 00:10:06 +01:00
|
|
|
{
|
|
|
|
json::stack::array array
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "events"
|
2019-01-09 00:10:06 +01:00
|
|
|
};
|
|
|
|
|
2019-03-06 02:10:22 +01:00
|
|
|
bool ret{false};
|
2019-08-16 12:55:07 +02:00
|
|
|
ret |= room_account_data_polylog_events(data);
|
2019-03-06 02:10:22 +01:00
|
|
|
ret |= room_account_data_polylog_tags(data);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::room_account_data_polylog_events(data &data)
|
|
|
|
{
|
2019-01-10 05:39:12 +01:00
|
|
|
assert(data.room);
|
2019-03-01 21:41:52 +01:00
|
|
|
char typebuf[m::user::room_account_data::typebuf_size];
|
2019-01-04 02:21:02 +01:00
|
|
|
const auto type
|
|
|
|
{
|
2019-02-21 20:53:59 +01:00
|
|
|
m::user::room_account_data::_type(typebuf, data.room->room_id)
|
|
|
|
};
|
|
|
|
|
2019-03-01 21:41:52 +01:00
|
|
|
bool ret{false};
|
|
|
|
data.user_state.for_each(type, [&data, &ret]
|
2019-03-08 22:42:24 +01:00
|
|
|
(const string_view &type, const string_view &state_key, const m::event::idx &event_idx)
|
2019-02-21 20:53:59 +01:00
|
|
|
{
|
2019-07-26 00:06:54 +02:00
|
|
|
// We have to ignore the since range here or Riot will forget or overwrite data.
|
|
|
|
//if(!apropos(data, event_idx))
|
|
|
|
// return true;
|
2019-02-21 20:53:59 +01:00
|
|
|
|
2019-03-01 21:41:52 +01:00
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
2019-03-08 22:42:24 +01:00
|
|
|
event::keys::include {"content"}
|
2019-03-01 21:41:52 +01:00
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-03-08 22:42:24 +01:00
|
|
|
m::event::fetch event
|
2019-03-01 21:41:52 +01:00
|
|
|
{
|
|
|
|
event_idx, std::nothrow, fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
2019-03-08 22:42:24 +01:00
|
|
|
return true;
|
2019-03-01 21:41:52 +01:00
|
|
|
|
2019-03-08 22:42:24 +01:00
|
|
|
json::get<"state_key"_>(event) = state_key;
|
2019-03-01 21:41:52 +01:00
|
|
|
ret |= room_account_data_polylog_events_event(data, event);
|
2019-03-08 22:42:24 +01:00
|
|
|
return true;
|
2019-01-10 05:39:12 +01:00
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return ret;
|
2019-01-10 05:39:12 +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_account_data_polylog_events_event(data &data,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
json::stack::object object
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-10 05:39:12 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "type", at<"state_key"_>(event)
|
|
|
|
};
|
2019-01-04 02:21:02 +01:00
|
|
|
|
2019-01-10 05:39:12 +01:00
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "content", at<"content"_>(event)
|
|
|
|
};
|
2019-02-24 20:59:53 +01:00
|
|
|
|
|
|
|
return true;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
2019-03-06 02:10:22 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::room_account_data_polylog_tags(data &data)
|
|
|
|
{
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "type", "m.tag"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object content
|
|
|
|
{
|
|
|
|
object, "content"
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object tags
|
|
|
|
{
|
|
|
|
content, "tags"
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(data.room);
|
|
|
|
char typebuf[m::user::room_tags::typebuf_size];
|
|
|
|
const auto type
|
|
|
|
{
|
|
|
|
m::user::room_tags::_type(typebuf, data.room->room_id)
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ret{false};
|
|
|
|
data.user_state.for_each(type, [&data, &tags, &ret]
|
2019-03-08 22:42:24 +01:00
|
|
|
(const string_view &type, const string_view &state_key, const m::event::idx &event_idx)
|
2019-03-06 02:10:22 +01:00
|
|
|
{
|
2019-06-06 00:18:46 +02:00
|
|
|
// We have to ignore the since range here or Riot will forget or overwrite tags.
|
|
|
|
//if(!apropos(data, event_idx))
|
|
|
|
// return true;
|
2019-03-06 02:10:22 +01:00
|
|
|
|
|
|
|
static const event::fetch::opts fopts
|
|
|
|
{
|
2019-03-08 22:42:24 +01:00
|
|
|
event::keys::include {"content"}
|
2019-03-06 02:10:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
|
|
|
event_idx, std::nothrow, fopts
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
2019-03-08 22:42:24 +01:00
|
|
|
return true;
|
2019-03-06 02:10:22 +01:00
|
|
|
|
2019-08-16 12:00:51 +02:00
|
|
|
json::stack::member
|
2019-03-06 02:10:22 +01:00
|
|
|
{
|
2019-03-08 22:42:24 +01:00
|
|
|
tags, state_key, json::get<"content"_>(event)
|
2019-03-06 02:10:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ret = true;
|
2019-03-08 22:42:24 +01:00
|
|
|
return true;
|
2019-03-06 02:10:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|