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  };
42 
43  bool operator==(DeviceKeyInfo a, DeviceKeyInfo b);
44  bool operator!=(DeviceKeyInfo a, DeviceKeyInfo b);
45 
46  template<class Archive>
47  void serialize(Archive &ar, DeviceKeyInfo &i, std::uint32_t const /*version*/)
48  {
49  ar
50  & i.deviceId
51  & i.ed25519Key
52  & i.curve25519Key
53  & i.displayName
54  & i.trustLevel
55  ;
56  }
57 
59  {
60  using DeviceMapT = immer::map<std::string /* deviceId */, DeviceKeyInfo>;
61  immer::map<std::string /* userId */, bool /* outdated */> usersToTrackDeviceLists;
62  immer::map<std::string /* userId */, DeviceMapT> deviceLists;
63 
64  template<class RangeT>
65  void track(RangeT &&userIds) {
66  for (auto userId : std::forward<RangeT>(userIds)) {
68  .set(userId, true);
69  }
70  }
71 
72  template<class RangeT>
73  void untrack(RangeT &&userIds) {
74  for (auto userId : std::forward<RangeT>(userIds)) {
75  usersToTrackDeviceLists = std::move(usersToTrackDeviceLists).erase(userId);
76  }
77  }
78 
79  immer::flex_vector<std::string> outdatedUsers() const;
80 
81  bool addDevice(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo, Crypto &crypto);
82 
83  void markUpToDate(std::string userId);
84 
85  DeviceMapT devicesFor(std::string userId) const;
86 
87  std::optional<DeviceKeyInfo> get(std::string userId, std::string deviceId) const;
88 
89  std::optional<DeviceKeyInfo> findByEd25519Key(std::string userId, std::string ed25519Key) const;
90  std::optional<DeviceKeyInfo> findByCurve25519Key(std::string userId, std::string curve25519Key) const;
91 
93  immer::flex_vector<std::string> diff(DeviceListTracker that) const;
94  };
95 
96  template<class Archive>
97  void serialize(Archive &ar, DeviceListTracker &t, std::uint32_t const /*version*/)
98  {
99  ar
101  & t.deviceLists
102  ;
103  }
104 }
105 
106 BOOST_CLASS_VERSION(Kazv::DeviceKeyInfo, 0)
107 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
bool operator!=(BaseJob a, BaseJob b)
Definition: basejob.cpp:292
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
bool operator==(BaseJob a, BaseJob b)
Definition: basejob.cpp:280
void serialize(Archive &ar, ClientModel &m, std::uint32_t const version)
Definition: client-model.hpp:582
Returns the current devices and identity keys for the given users.
Definition: keys.hpp:130
Definition: device-list-tracker.hpp:35
DeviceTrustLevel trustLevel
Definition: device-list-tracker.hpp:40
std::string curve25519Key
Definition: device-list-tracker.hpp:38
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:59
void track(RangeT &&userIds)
Definition: device-list-tracker.hpp:65
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:62
immer::map< std::string, DeviceKeyInfo > DeviceMapT
Definition: device-list-tracker.hpp:60
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:73
immer::map< std::string, bool > usersToTrackDeviceLists
Definition: device-list-tracker.hpp:61
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