User.h
1 /*
2  * Copyright 2016 Gigatribe
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GIGA_CORE_USER_H_
18 #define GIGA_CORE_USER_H_
19 
20 #include "UserRelation.h"
21 #include "FolderNode.h"
22 #include "../utils/Crypto.h"
23 
24 #include <boost/optional.hpp>
25 #include <cpprest/http_client.h>
26 #include <chrono>
27 #include <mutex>
28 
29 namespace giga
30 {
31 class Application;
32 
33 namespace data
34 {
35 struct User;
36 struct UsersRelation;
37 }
38 
39 namespace core
40 {
41 
45 class Country final {
46 public:
47  explicit
48  Country(const utility::string_t& code) : code(code) {}
49 
50  Country(Country&&) = default;
51  Country(const Country&) = default;
52  ~Country() = default;
53  Country& operator=(const Country&) = default;
54  Country& operator=(Country&&) = default;
55 
56  const utility::string_t code;
57 };
58 
62 struct Language final {
63  explicit
64  Language(const utility::string_t& code) : code(code) {}
65 
66  Language(Language&&) = default;
67  Language(const Language&) = default;
68  ~Language() = default;
69  Language& operator=(const Language&)= default;
70  Language& operator=(Language&&) = default;
71 
72  const utility::string_t code;
73 };
74 
79 class User final
80 {
81 public:
82  enum class Activity
83  {
84  very_active = 0, active, inactive
85  };
86  enum class SeederStatus
87  {
88  no = 0, yes, to_some_people
89  };
90  enum class ReportedState {
91  pending = 0, probation, checkup, suspended, free,
92  no_report
93  };
94  enum class UserGender {
95  male = 0, female, unknown
96  };
97 
98 public:
99  User() = default;
100  ~User() = default;
101  User(User&&) = default;
102  User(const User&) = default;
103  User& operator=(const User&);
104  User& operator=(User&& rhs) noexcept;
105 
106  explicit User(std::shared_ptr<data::User> u, const Application& app, std::shared_ptr<data::UsersRelation> r = nullptr);
107 
108 public:
109  uint64_t
110  id () const;
111 
112  const utility::string_t&
113  login () const;
114 
115  web::uri
116  avatarUri () const;
117 
118  web::uri
119  bigAvatarUri () const;
120 
121  std::chrono::system_clock::time_point
122  creationDate () const;
123 
124  std::chrono::system_clock::time_point
125  lastConnectionDate () const;
126 
127  Activity
128  activity () const;
129 
130  bool
131  isUnlimited () const;
132 
136  SeederStatus
137  isSeeder () const;
138 
139  uint64_t
140  contactCount () const;
141 
148  const std::vector<utility::string_t>&
149  tags () const;
150 
151  Country
152  country () const;
153 
154  Language
155  language () const;
156 
157  const utility::string_t&
158  description () const;
159 
160 public:
166  class ContactData final {
167  friend User;
168  public:
169  ~ContactData() = default;
170  ContactData(ContactData&&) = default;
171  ContactData(const ContactData&) = default;
172  ContactData& operator=(const ContactData&) = default;
173  ContactData& operator=(ContactData&&) = default;
174 
175  private:
176  explicit ContactData(std::shared_ptr<data::User> u, const Application& app);
177 
178  public:
179  UserGender
180  gender() const;
181 
182  const boost::optional<utility::string_t>&
183  name() const;
184 
185  const boost::optional<std::chrono::system_clock::time_point>
186  birthDate() const;
187 
188  uint64_t
189  maxContact() const;
190 
194  const FolderNode&
195  node() const;
196 
197  private:
198  std::shared_ptr<data::User> _data;
199  FolderNode _root;
200  };
201 
207  class PersonalData {
208  friend User;
209  friend UserRelation;
210  public:
211  ~PersonalData() = default;
212  PersonalData(PersonalData&&) = default;
213  PersonalData(const PersonalData&) = default;
214  PersonalData& operator=(const PersonalData&) = default;
215  PersonalData& operator=(PersonalData&&) = default;
216 
217  private:
218  explicit PersonalData(std::shared_ptr<data::User> u, const utility::string_t& password);
219 
220  public:
221  ReportedState
222  reportedState() const;
223 
224  bool
225  isEmailValidated() const;
226 
227  const utility::string_t&
228  email() const;
229 
230  const utility::string_t&
231  nextEmail() const;
232 
233  const std::string&
234  nodeKeyClear() const;
235 
236  uint64_t
237  maxStorage() const;
238 
239  uint64_t
240  standatdMaxStorage() const;
241 
242  uint64_t
243  downloaded() const;
244 
245  uint64_t
246  dlAvailable() const;
247 
248 // ExternalAccount
249 // externalAccount() const;
250 // Offer
251 // offer() const;
252 
253  private:
254  std::shared_ptr<data::User> _data;
255  std::string _nodeKeyClear;
256  Rsa _rsaKeys;
257  };
258 
262  bool
263  hasContactData() const;
264 
274  ContactData&
275  contactData();
276 
280  bool
281  hasPersonalData() const;
282 
291  const PersonalData&
292  personalData() const;
293 
297  bool
298  hasRelation() const;
299 
315  relation() const;
316 
317  //
318  // Setters
319  //
320 
326  User
327  invite();
328 
334  User
335  block();
336 
342  User
344 
350  void
351  suggest(const User& contact);
352 
358  void
359  removeRelation();
360 
361 private:
362  friend class giga::Application; // core::User& Application::authenticate (const utility::string_t&, const utility::string_t&);
363 
364  PersonalData&
365  initializePersonalData (const utility::string_t& password);
366 
367 private:
368  std::shared_ptr<data::User> _data;
369  std::shared_ptr<data::UsersRelation> _relation;
370  boost::optional<PersonalData> _private;
371  boost::optional<ContactData> _protected;
372  boost::optional<giga::Rsa> _publicKey;
373  const Application* _app = nullptr;
374 };
375 
376 } /* namespace core */
377 } /* namespace giga */
378 
379 #endif /* GIGA_CORE_USER_H_ */
User block()
The currentUser blocks this user.
Definition: User.cpp:402
void suggest(const User &contact)
Suggest contact to this user.
Definition: User.cpp:413
bool hasContactData() const
Definition: User.cpp:314
Definition: User.h:207
const FolderNode & node() const
Definition: User.cpp:223
Definition: User.h:62
Definition: UserRelation.h:45
bool hasRelation() const
Definition: User.cpp:368
const PersonalData & personalData() const
Get the personal data of the currentUser.
Definition: User.cpp:356
User acceptInvitation()
Accept an invitation received from this user to the currentUser.
Definition: User.cpp:423
ContactData & contactData()
Get the data shared by this user.
Definition: User.cpp:320
void removeRelation()
Remove a relation. Make sure this user hasRelation() const
Definition: User.cpp:440
const std::vector< utility::string_t > & tags() const
Definition: User.cpp:149
Definition: Crypto.h:27
Definition: User.h:45
SeederStatus isSeeder() const
Definition: User.cpp:135
UserRelation relation() const
Get the UserRelation data between this user and the currentUser.
Definition: User.cpp:374
Definition: User.h:166
A user with an account on GiGa.GG. Cf: https://giga.gg/users/profile/1704781.
Definition: User.h:79
Definition: FolderNode.h:27
bool hasPersonalData() const
Definition: User.cpp:338
User invite()
Send an invitation from the currentUser to this user.
Definition: User.cpp:384
Definition: Application.h:45