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