libkazv
Loading...
Searching...
No Matches
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
24namespace Kazv
25{
33
35 {
36 std::string deviceId;
37 std::string ed25519Key;
38 std::string curve25519Key;
39 std::optional<std::string> displayName;
45 bool deleted{false};
46 friend bool operator==(const DeviceKeyInfo &a, const DeviceKeyInfo &b) = default;
47 friend bool operator!=(const DeviceKeyInfo &a, const DeviceKeyInfo &b) = default;
48 };
49
50 template<class Archive>
51 void serialize(Archive &ar, DeviceKeyInfo &i, std::uint32_t const version)
52 {
53 ar
54 & i.deviceId
55 & i.ed25519Key
57 & i.displayName
58 & i.trustLevel
59 ;
60 if (version >= 1) {
61 ar & i.deleted;
62 } else {
63 i.deleted = false;
64 }
65 }
66
68 {
69 using DeviceMapT = immer::map<std::string /* deviceId */, DeviceKeyInfo>;
70 immer::map<std::string /* userId */, bool /* outdated */> usersToTrackDeviceLists;
71 immer::map<std::string /* userId */, DeviceMapT> deviceLists;
72
73 template<class RangeT>
74 void track(RangeT &&userIds) {
75 for (auto userId : std::forward<RangeT>(userIds)) {
77 .set(userId, true);
78 }
79 }
80
81 template<class RangeT>
82 void untrack(RangeT &&userIds) {
83 for (auto userId : std::forward<RangeT>(userIds)) {
84 usersToTrackDeviceLists = std::move(usersToTrackDeviceLists).erase(userId);
85 }
86 }
87
88 immer::flex_vector<std::string> outdatedUsers() const;
89
90 std::optional<DeviceKeyInfo> verifyDeviceInfo(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo) const;
91
92 bool addDevice(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo);
93
94 void addVerifiedDeviceKeyInfo(std::string userId, std::string deviceId, DeviceKeyInfo info);
95
99 void markDeviceAsDeleted(std::string userId, std::string deviceId);
100
101 void markUpToDate(std::string userId);
102
103 DeviceMapT devicesFor(std::string userId) const;
104
105 std::optional<DeviceKeyInfo> get(std::string userId, std::string deviceId) const;
106
107 std::optional<DeviceKeyInfo> findByEd25519Key(std::string userId, std::string ed25519Key) const;
108 std::optional<DeviceKeyInfo> findByCurve25519Key(std::string userId, std::string curve25519Key) const;
109
120 std::pair<FindByOlmEventStatus, DeviceKeyInfo> findByOlmEvent(Event e) const;
121
123 immer::flex_vector<std::string> diff(DeviceListTracker that) const;
124 };
125
126 template<class Archive>
127 void serialize(Archive &ar, DeviceListTracker &t, std::uint32_t const /*version*/)
128 {
129 ar
131 & t.deviceLists
132 ;
133 }
134}
135
136BOOST_CLASS_VERSION(Kazv::DeviceKeyInfo, 1)
137BOOST_CLASS_VERSION(Kazv::DeviceListTracker, 0)
Definition event.hpp:21
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:704
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
bool deleted
Whether this device has been deleted by its owner.
Definition device-list-tracker.hpp:45
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:68
void track(RangeT &&userIds)
Definition device-list-tracker.hpp:74
void addVerifiedDeviceKeyInfo(std::string userId, std::string deviceId, DeviceKeyInfo info)
Definition device-list-tracker.cpp:127
std::optional< DeviceKeyInfo > verifyDeviceInfo(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo) const
Definition device-list-tracker.cpp:66
immer::map< std::string, DeviceMapT > deviceLists
Definition device-list-tracker.hpp:71
immer::map< std::string, DeviceKeyInfo > DeviceMapT
Definition device-list-tracker.hpp:69
immer::flex_vector< std::string > diff(DeviceListTracker that) const
returns a list of users whose device list has changed
Definition device-list-tracker.cpp:244
DeviceMapT devicesFor(std::string userId) const
Definition device-list-tracker.cpp:269
FindByOlmEventStatus
Definition device-list-tracker.hpp:111
@ InTracker
The sending device info is already in tracker.
Definition device-list-tracker.hpp:115
@ InEvent
The sending device info is not in tracker, but it is in the event and ready to be added to the tracke...
Definition device-list-tracker.hpp:118
@ NotFound
The sending device info cannot be found.
Definition device-list-tracker.hpp:113
void untrack(RangeT &&userIds)
Definition device-list-tracker.hpp:82
immer::map< std::string, bool > usersToTrackDeviceLists
Definition device-list-tracker.hpp:70
void markUpToDate(std::string userId)
Definition device-list-tracker.cpp:145
std::optional< DeviceKeyInfo > findByCurve25519Key(std::string userId, std::string curve25519Key) const
Definition device-list-tracker.cpp:176
bool addDevice(std::string userId, std::string deviceId, Api::QueryKeysJob::DeviceInformation deviceInfo)
Definition device-list-tracker.cpp:113
void markDeviceAsDeleted(std::string userId, std::string deviceId)
Mark a device as deleted by its owner.
Definition device-list-tracker.cpp:135
std::pair< FindByOlmEventStatus, DeviceKeyInfo > findByOlmEvent(Event e) const
Definition device-list-tracker.cpp:196
immer::flex_vector< std::string > outdatedUsers() const
Definition device-list-tracker.cpp:51
std::optional< DeviceKeyInfo > get(std::string userId, std::string deviceId) const
Definition device-list-tracker.cpp:150
std::optional< DeviceKeyInfo > findByEd25519Key(std::string userId, std::string ed25519Key) const
Definition device-list-tracker.cpp:159