libkazv
device-list-tracker.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 #include <string>
10 
11 #include <immer/map.hpp>
12 #include <immer/flex_vector.hpp>
13 
14 #include <boost/serialization/string.hpp>
16 
18 
19 #include <crypto.hpp>
20 #include <csapi/keys.hpp>
21 
22 #include "cursorutil.hpp"
23 
24 namespace Kazv
25 {
27  {
32  };
33 
35  {
36  std::string deviceId;
37  std::string ed25519Key;
38  std::string curve25519Key;
39  std::optional<std::string> displayName;
41  friend bool operator==(const DeviceKeyInfo &a, const DeviceKeyInfo &b) = default;
42  friend bool operator!=(const DeviceKeyInfo &a, const DeviceKeyInfo &b) = default;
43  };
44 
45  template<class Archive>
46  void serialize(Archive &ar, DeviceKeyInfo &i, std::uint32_t const /*version*/)
47  {
48  ar
49  & i.deviceId
50  & i.ed25519Key
51  & i.curve25519Key
52  & i.displayName
53  & i.trustLevel
54  ;
55  }
56 
58  {
59  using DeviceMapT = immer::map<std::string /* deviceId */, DeviceKeyInfo>;
60  immer::map<std::string /* userId */, bool /* outdated */> usersToTrackDeviceLists;
61  immer::map<std::string /* userId */, DeviceMapT> deviceLists;
62 
63  template<class RangeT>
64  void track(RangeT &&userIds) {
65  for (auto userId : std::forward<RangeT>(userIds)) {
67  .set(userId, true);
68  }
69  }
70 
71  template<class RangeT>
72  void untrack(RangeT &&userIds) {
73  for (auto userId : std::forward<RangeT>(userIds)) {
74  usersToTrackDeviceLists = std::move(usersToTrackDeviceLists).erase(userId);
75  }
76  }
77 
78  immer::flex_vector<std::string> outdatedUsers() const;
79 
80  bool addDevice(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo, Crypto &crypto);
81 
82  void markUpToDate(std::string userId);
83 
84  DeviceMapT devicesFor(std::string userId) const;
85 
86  std::optional<DeviceKeyInfo> get(std::string userId, std::string deviceId) const;
87 
88  std::optional<DeviceKeyInfo> findByEd25519Key(std::string userId, std::string ed25519Key) const;
89  std::optional<DeviceKeyInfo> findByCurve25519Key(std::string userId, std::string curve25519Key) const;
90 
92  immer::flex_vector<std::string> diff(DeviceListTracker that) const;
93  };
94 
95  template<class Archive>
96  void serialize(Archive &ar, DeviceListTracker &t, std::uint32_t const /*version*/)
97  {
98  ar
100  & t.deviceLists
101  ;
102  }
103 }
104 
105 BOOST_CLASS_VERSION(Kazv::DeviceKeyInfo, 0)
106 BOOST_CLASS_VERSION(Kazv::DeviceListTracker, 0)
Definition: crypto.hpp:36
A RangeT is an ordered collection that can be iterated through.
Definition: range-t.hpp:21
Definition: location.hpp:10
DeviceTrustLevel
Definition: device-list-tracker.hpp:27
@ Unseen
Definition: device-list-tracker.hpp:29
@ Seen
Definition: device-list-tracker.hpp:30
@ Verified
Definition: device-list-tracker.hpp:31
@ Blocked
Definition: device-list-tracker.hpp:28
void serialize(Archive &ar, ClientModel &m, std::uint32_t const version)
Definition: client-model.hpp:584
Returns the current devices and identity keys for the given users.
Definition: keys.hpp:130
Definition: device-list-tracker.hpp:35
friend bool operator==(const DeviceKeyInfo &a, const DeviceKeyInfo &b)=default
DeviceTrustLevel trustLevel
Definition: device-list-tracker.hpp:40
std::string curve25519Key
Definition: device-list-tracker.hpp:38
friend bool operator!=(const DeviceKeyInfo &a, const DeviceKeyInfo &b)=default
std::string deviceId
Definition: device-list-tracker.hpp:36
std::optional< std::string > displayName
Definition: device-list-tracker.hpp:39
std::string ed25519Key
Definition: device-list-tracker.hpp:37
Definition: device-list-tracker.hpp:58
void track(RangeT &&userIds)
Definition: device-list-tracker.hpp:64
bool addDevice(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo, Crypto &crypto)
Definition: device-list-tracker.cpp:40
immer::map< std::string, DeviceMapT > deviceLists
Definition: device-list-tracker.hpp:61
immer::map< std::string, DeviceKeyInfo > DeviceMapT
Definition: device-list-tracker.hpp:59
immer::flex_vector< std::string > diff(DeviceListTracker that) const
returns a list of users whose device list has changed
Definition: device-list-tracker.cpp:153
DeviceMapT devicesFor(std::string userId) const
Definition: device-list-tracker.cpp:178
void untrack(RangeT &&userIds)
Definition: device-list-tracker.hpp:72
immer::map< std::string, bool > usersToTrackDeviceLists
Definition: device-list-tracker.hpp:60
void markUpToDate(std::string userId)
Definition: device-list-tracker.cpp:77
std::optional< DeviceKeyInfo > findByCurve25519Key(std::string userId, std::string curve25519Key) const
Definition: device-list-tracker.cpp:108
immer::flex_vector< std::string > outdatedUsers() const
Definition: device-list-tracker.cpp:24
std::optional< DeviceKeyInfo > get(std::string userId, std::string deviceId) const
Definition: device-list-tracker.cpp:82
std::optional< DeviceKeyInfo > findByEd25519Key(std::string userId, std::string ed25519Key) const
Definition: device-list-tracker.cpp:91