2019-02-08 08:40:13 -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.
|
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
std::map<std::string, long>
|
|
|
|
count_one_time_keys(const m::user::id &, const m::device::id &);
|
|
|
|
}
|
|
|
|
|
2019-02-08 08:40:13 -08:00
|
|
|
namespace ircd::m::sync
|
|
|
|
{
|
2019-08-08 22:44:54 -07:00
|
|
|
static bool _device_one_time_keys_count(data &);
|
2019-02-24 11:59:53 -08:00
|
|
|
static bool device_one_time_keys_count_polylog(data &);
|
|
|
|
static bool device_one_time_keys_count_linear(data &);
|
2019-02-08 08:40:13 -08:00
|
|
|
|
|
|
|
extern item device_one_time_keys_count;
|
|
|
|
}
|
|
|
|
|
2019-09-09 12:05:53 -07:00
|
|
|
ircd::mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Client Sync :Device One Time Keys Count"
|
|
|
|
};
|
|
|
|
|
2019-02-08 08:40:13 -08:00
|
|
|
decltype(ircd::m::sync::device_one_time_keys_count)
|
|
|
|
ircd::m::sync::device_one_time_keys_count
|
|
|
|
{
|
|
|
|
"device_one_time_keys_count",
|
|
|
|
device_one_time_keys_count_polylog,
|
|
|
|
device_one_time_keys_count_linear
|
|
|
|
};
|
|
|
|
|
2019-02-24 11:59:53 -08:00
|
|
|
bool
|
2019-02-08 08:40:13 -08:00
|
|
|
ircd::m::sync::device_one_time_keys_count_linear(data &data)
|
|
|
|
{
|
2019-08-06 19:19:16 -07:00
|
|
|
if(!data.device_id)
|
|
|
|
return false;
|
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
if(!data.event || !data.event->event_id)
|
|
|
|
return false;
|
2019-02-08 08:40:13 -08:00
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
if(!startswith(json::get<"type"_>(*data.event), "ircd.device.one_time_key"))
|
2019-08-06 19:19:16 -07:00
|
|
|
return false;
|
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
if(json::get<"state_key"_>(*data.event) != data.device_id)
|
|
|
|
return false;
|
2019-08-06 19:19:16 -07:00
|
|
|
|
2019-08-10 23:01:25 -07:00
|
|
|
if(json::get<"room_id"_>(*data.event) != data.user_room)
|
2019-08-06 19:19:16 -07:00
|
|
|
return false;
|
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
const json::object &one_time_keys
|
2019-08-06 19:19:16 -07:00
|
|
|
{
|
2019-08-08 22:44:54 -07:00
|
|
|
json::get<"content"_>(*data.event)
|
|
|
|
};
|
2019-08-06 19:19:16 -07:00
|
|
|
|
2020-05-07 14:04:57 -07:00
|
|
|
json::stack::object object
|
|
|
|
{
|
|
|
|
*data.out, "device_one_time_keys_count"
|
|
|
|
};
|
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
return _device_one_time_keys_count(data);
|
|
|
|
}
|
2019-08-06 19:19:16 -07:00
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
bool
|
|
|
|
ircd::m::sync::device_one_time_keys_count_polylog(data &data)
|
|
|
|
{
|
|
|
|
if(!data.device_id)
|
|
|
|
return false;
|
2019-08-06 19:19:16 -07:00
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
return _device_one_time_keys_count(data);
|
|
|
|
}
|
2019-08-06 19:19:16 -07:00
|
|
|
|
2019-08-08 22:44:54 -07:00
|
|
|
bool
|
|
|
|
ircd::m::sync::_device_one_time_keys_count(data &data)
|
|
|
|
{
|
|
|
|
const auto counts
|
|
|
|
{
|
2020-04-01 18:30:02 -07:00
|
|
|
m::user::devices::count_one_time_keys(data.user, data.device_id)
|
2019-08-08 22:44:54 -07:00
|
|
|
};
|
|
|
|
|
2019-08-06 19:19:16 -07:00
|
|
|
for(const auto &[algorithm, count] : counts)
|
|
|
|
json::stack::member
|
|
|
|
{
|
2020-04-14 20:33:55 -07:00
|
|
|
*data.out, algorithm, json::value{count}
|
2019-08-06 19:19:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return true;
|
2019-02-08 08:40:13 -08:00
|
|
|
}
|