Ring Daemon
Loading...
Searching...
No Matches
jami_contact.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2026 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include "string_utils.h"
20
21#include <opendht/infohash.h>
22#include <opendht/value.h>
23#include <opendht/default_types.h>
24
25#include <msgpack.hpp>
26#include <json/json.h>
27
28#include <map>
29#include <string>
30#include <ctime>
31
32namespace jami {
33
34struct Contact
35{
38
41
43 bool confirmed {false};
44
46 bool banned {false};
47
49 std::string conversationId {};
50
52 bool isActive() const { return added > removed; }
53 bool isBanned() const { return not isActive() and banned; }
54
55 Contact() = default;
56 Contact(const Json::Value& json)
57 {
58 added = json["added"].asLargestUInt();
59 removed = json["removed"].asLargestUInt();
60 confirmed = json["confirmed"].asBool();
61 banned = json["banned"].asBool();
62 conversationId = json["conversationId"].asString();
63 }
64
69 bool update(const Contact& c)
70 {
71 const auto copy = *this;
72 auto isMoreRecent = std::max(c.added, c.removed) > std::max(added, removed);
73 if (isMoreRecent) {
74 added = c.added;
75 removed = c.removed;
76 banned = c.banned;
79 } else if (isActive() && added == c.added) {
81 }
82 return hasDifferentState(copy);
83 }
84
85 bool hasDifferentState(const Contact& other) const
86 {
87 return other.isActive() != isActive() or other.isBanned() != isBanned() or other.confirmed != confirmed;
88 }
89
90 Json::Value toJson() const
91 {
92 Json::Value json;
93 json["added"] = Json::Int64(added);
94 if (removed) {
95 json["removed"] = Json::Int64(removed);
96 }
97 if (confirmed)
98 json["confirmed"] = confirmed;
99 if (banned)
100 json["banned"] = banned;
101 json["conversationId"] = conversationId;
102 return json;
103 }
104
105 std::map<std::string, std::string> toMap() const
106 {
107 std::map<std::string, std::string> result {{"added", std::to_string(added)},
108 {"removed", std::to_string(removed)},
109 {"conversationId", conversationId}};
110
111 if (isActive())
112 result.emplace("confirmed", confirmed ? TRUE_STR : FALSE_STR);
113 if (isBanned())
114 result.emplace("banned", TRUE_STR);
115
116 return result;
117 }
118
119 MSGPACK_DEFINE_MAP(added, removed, confirmed, banned, conversationId)
120};
121
123{
124 std::shared_ptr<dht::crypto::PublicKey> device;
125 std::string conversationId;
127 std::vector<uint8_t> payload;
128 MSGPACK_DEFINE_MAP(device, conversationId, received, payload)
129};
130
131struct DeviceAnnouncement : public dht::SignedValue<DeviceAnnouncement>
132{
133private:
134 using BaseClass = dht::SignedValue<DeviceAnnouncement>;
135
136public:
137 static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
138 dht::InfoHash dev;
139 std::shared_ptr<dht::crypto::PublicKey> pk;
140 MSGPACK_DEFINE_MAP(dev, pk)
141};
142
144{
145 std::string name;
146 MSGPACK_DEFINE_MAP(name)
147};
148
149struct DeviceSync : public dht::EncryptedValue<DeviceSync>
150{
151 static const constexpr dht::ValueType& TYPE = dht::ValueType::USER_DATA;
153 std::string device_name;
154 std::map<dht::PkId, KnownDeviceSync> devices;
155 std::map<dht::InfoHash, Contact> peers;
156 std::map<dht::InfoHash, TrustRequest> trust_requests;
157 MSGPACK_DEFINE_MAP(date, device_name, devices, peers, trust_requests)
158};
159
161{
162 using clock = std::chrono::system_clock;
163 using time_point = clock::time_point;
164
166 std::shared_ptr<dht::crypto::Certificate> certificate;
167
169 std::string name {};
170
172 time_point last_sync {time_point::min()};
173
174 KnownDevice(const std::shared_ptr<dht::crypto::Certificate>& cert,
175 const std::string& n = {},
176 time_point sync = time_point::min())
178 , name(n)
179 , last_sync(sync)
180 {}
181};
182
183} // namespace jami
Definition Address.h:25
static constexpr const char TRUE_STR[]
void emitSignal(Args... args)
Definition jami_signal.h:64
static constexpr const char FALSE_STR[]
bool confirmed
True if we got confirmation that this contact also added us.
time_t added
Time of contact addition.
bool isBanned() const
Contact()=default
bool isActive() const
True if the contact is an active contact (not banned nor removed)
time_t removed
Time of contact removal.
Json::Value toJson() const
bool banned
True if the contact is banned (if not active)
bool hasDifferentState(const Contact &other) const
std::string conversationId
Non empty if a swarm is linked.
Contact(const Json::Value &json)
std::map< std::string, std::string > toMap() const
bool update(const Contact &c)
Update this contact using other known contact information, return true if contact state was changed.
std::shared_ptr< dht::crypto::PublicKey > pk
static const constexpr dht::ValueType & TYPE
std::map< dht::InfoHash, Contact > peers
static const constexpr dht::ValueType & TYPE
std::map< dht::InfoHash, TrustRequest > trust_requests
std::string device_name
std::map< dht::PkId, KnownDeviceSync > devices
std::shared_ptr< dht::crypto::Certificate > certificate
Device certificate.
clock::time_point time_point
time_point last_sync
Time of last received device sync.
std::string name
Device name.
std::chrono::system_clock clock
KnownDevice(const std::shared_ptr< dht::crypto::Certificate > &cert, const std::string &n={}, time_point sync=time_point::min())
std::vector< uint8_t > payload
std::shared_ptr< dht::crypto::PublicKey > device
std::string conversationId