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
|
|
|
|
{
|
2020-03-18 22:55:24 +01:00
|
|
|
static bool push_rules(data &);
|
2019-03-01 21:45:48 +01:00
|
|
|
static bool account_data_(data &, const m::event &);
|
2019-02-24 20:59:53 +01:00
|
|
|
static bool account_data_polylog(data &);
|
|
|
|
static bool account_data_linear(data &);
|
2019-01-09 00:10:06 +01:00
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
extern item account_data;
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:05:53 +02:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Account Data"
|
|
|
|
};
|
|
|
|
|
2019-01-04 02:21:02 +01:00
|
|
|
decltype(ircd::m::sync::account_data)
|
|
|
|
ircd::m::sync::account_data
|
|
|
|
{
|
|
|
|
"account_data",
|
2019-01-10 05:39:12 +01:00
|
|
|
account_data_polylog,
|
2019-04-16 09:32:37 +02:00
|
|
|
account_data_linear,
|
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::account_data_linear(data &data)
|
|
|
|
{
|
2019-02-28 01:30:43 +01:00
|
|
|
if(!data.event_idx)
|
|
|
|
return false;
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
assert(data.event);
|
2019-03-01 21:45:48 +01:00
|
|
|
const m::event &event{*data.event};
|
2020-03-18 22:55:24 +01:00
|
|
|
if(json::get<"room_id"_>(event) != data.user_room.room_id)
|
2019-03-01 21:45:48 +01:00
|
|
|
return false;
|
|
|
|
|
2020-03-18 22:55:24 +01:00
|
|
|
const bool regard
|
|
|
|
{
|
|
|
|
json::get<"type"_>(event) == "ircd.account_data" ||
|
|
|
|
startswith(json::get<"type"_>(event), push::rule::type_prefix)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!regard)
|
2019-03-01 21:45:48 +01:00
|
|
|
return false;
|
|
|
|
|
2019-03-08 22:42:54 +01:00
|
|
|
json::stack::object account_data
|
|
|
|
{
|
|
|
|
*data.out, "account_data"
|
|
|
|
};
|
|
|
|
|
2019-02-27 02:34:07 +01:00
|
|
|
json::stack::array array
|
|
|
|
{
|
|
|
|
*data.out, "events"
|
|
|
|
};
|
|
|
|
|
2020-03-18 22:55:24 +01:00
|
|
|
if(startswith(json::get<"type"_>(event), push::rule::type_prefix))
|
|
|
|
return push_rules(data);
|
|
|
|
|
2019-03-01 21:45:48 +01:00
|
|
|
return account_data_(data, event);
|
2019-01-10 05:39:12 +01:00
|
|
|
}
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool
|
2019-01-04 02:21:02 +01:00
|
|
|
ircd::m::sync::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-02-21 20:53:59 +01:00
|
|
|
const m::room::state state
|
|
|
|
{
|
2019-03-01 21:45:48 +01:00
|
|
|
data.user_room
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
|
|
|
|
2019-02-24 20:59:53 +01:00
|
|
|
bool ret{false};
|
|
|
|
state.for_each("ircd.account_data", [&data, &array, &ret]
|
2019-03-01 21:45:48 +01:00
|
|
|
(const m::event::idx &event_idx)
|
2019-01-04 02:21:02 +01:00
|
|
|
{
|
2019-03-01 21:45:48 +01:00
|
|
|
if(!apropos(data, event_idx))
|
|
|
|
return;
|
|
|
|
|
|
|
|
static const m::event::fetch::opts fopts
|
|
|
|
{
|
|
|
|
m::event::keys::include {"state_key", "content"}
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::event::fetch event
|
|
|
|
{
|
2020-04-03 04:50:00 +02:00
|
|
|
std::nothrow, event_idx, fopts
|
2019-03-01 21:45:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!event.valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ret |= account_data_(data, event);
|
2019-01-04 02:21:02 +01:00
|
|
|
});
|
2019-02-24 20:59:53 +01:00
|
|
|
|
2020-03-18 22:55:24 +01:00
|
|
|
ret |= push_rules(data);
|
2019-02-24 20:59:53 +01:00
|
|
|
return ret;
|
2019-01-04 02:21:02 +01:00
|
|
|
}
|
2019-01-10 05:39:12 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::account_data_(data &data,
|
2019-03-01 21:45:48 +01:00
|
|
|
const m::event &event)
|
2019-01-10 05:39:12 +01:00
|
|
|
{
|
|
|
|
// Each account_data event is an object in the events array
|
|
|
|
json::stack::object object
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// type
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "type", at<"state_key"_>(event)
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// content
|
|
|
|
json::stack::member
|
|
|
|
{
|
2019-02-27 00:50:58 +01:00
|
|
|
*data.out, "content", at<"content"_>(event)
|
2019-01-10 05:39:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-03-18 22:55:24 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::sync::push_rules(data &data)
|
|
|
|
{
|
|
|
|
// Each account_data event is an object in the events array
|
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
*data.out
|
|
|
|
};
|
|
|
|
|
|
|
|
// type
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
object, "type", "m.push_rules"
|
|
|
|
};
|
|
|
|
|
|
|
|
// content
|
|
|
|
json::stack::object content
|
|
|
|
{
|
|
|
|
object, "content"
|
|
|
|
};
|
|
|
|
|
|
|
|
//TODO: XXX dynamic scopes / dynamic kinds please
|
|
|
|
//TODO: XXX please deduplicate and centralize a json::stack
|
|
|
|
//TODO: composition under m::push
|
|
|
|
const m::user::pushrules pushrules
|
|
|
|
{
|
|
|
|
data.user
|
|
|
|
};
|
|
|
|
|
|
|
|
json::stack::object _scope
|
|
|
|
{
|
|
|
|
content, "global"
|
|
|
|
};
|
|
|
|
|
2020-04-15 04:25:48 +02:00
|
|
|
bool ret{false};
|
|
|
|
const auto each_kind{[&_scope, &pushrules, &ret]
|
2020-03-18 22:55:24 +01:00
|
|
|
(const string_view &scope, const string_view &kind)
|
|
|
|
{
|
|
|
|
json::stack::array _kind
|
|
|
|
{
|
|
|
|
_scope, kind
|
|
|
|
};
|
|
|
|
|
2020-04-15 04:25:48 +02:00
|
|
|
pushrules.for_each(push::path{scope, kind, {}}, [&_kind, &ret]
|
2020-03-27 18:40:43 +01:00
|
|
|
(const auto &event_idx, const auto &path, const json::object &rule)
|
2020-03-18 22:55:24 +01:00
|
|
|
{
|
2020-03-27 18:40:43 +01:00
|
|
|
_kind.append(rule);
|
2020-04-15 04:25:48 +02:00
|
|
|
ret |= true;
|
2020-03-18 22:55:24 +01:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}};
|
|
|
|
|
|
|
|
each_kind("global", "content");
|
|
|
|
each_kind("global", "override");
|
|
|
|
each_kind("global", "room");
|
|
|
|
each_kind("global", "sender");
|
|
|
|
each_kind("global", "underride");
|
2020-04-15 04:25:48 +02:00
|
|
|
return ret;
|
2020-03-18 22:55:24 +01:00
|
|
|
}
|