aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
user.hpp
1 //
2 // user.hpp
3 // ********
4 //
5 // Copyright (c) 2019 Sharon W (sharon at aegis dot gg)
6 //
7 // Distributed under the MIT License. (See accompanying file LICENSE)
8 //
9 
10 #pragma once
11 
12 #include "aegis/config.hpp"
13 #include "aegis/utility.hpp"
14 #if !defined(AEGIS_DISABLE_ALL_CACHE)
15 #include "aegis/snowflake.hpp"
16 #include "aegis/gateway/objects/presence.hpp"
17 #include "aegis/fwd.hpp"
18 #include <nlohmann/json.hpp>
19 #include <string>
20 #include <queue>
21 #include <memory>
22 #include <set>
23 #include <shared_mutex>
24 
25 namespace aegis
26 {
27 
28 #if (AEGIS_HAS_STD_SHARED_MUTEX == 1)
29 using shared_mutex = std::shared_mutex;
30 #else
31 using shared_mutex = std::shared_timed_mutex;
32 #endif
33 
34 using json = nlohmann::json;
35 
37 class user
38 {
39 public:
41 
42  explicit user(snowflake id) : _member_id(id) {}
43 
45  struct guild_info
46  {
47  guild_info(snowflake _id) : id(_id) {};
48  snowflake id;
49  std::vector<snowflake> roles;
50  lib::optional<std::string> nickname;
51  uint64_t joined_at = 0;
52  bool deaf = false;
53  bool mute = false;
54  };
55 
57 
61  AEGIS_DECL std::string get_name(snowflake guild_id) noexcept;
62 
64 
67  std::string get_username() const noexcept
68  {
69  std::shared_lock<shared_mutex> l(_m);
70  std::string _username = _name;
71  return std::move(_username);
72  }
73 
75 
78  uint16_t get_discriminator() const noexcept
79  {
80  return _discriminator;
81  }
82 
84 
87  std::string get_avatar() const noexcept
88  {
89  std::shared_lock<shared_mutex> l(_m);
90  std::string t_avatar = _avatar;
91  return std::move(t_avatar);
92  }
93 
95 
98  bool is_bot() const noexcept
99  {
100  return _is_bot;
101  }
102 
104 
107  bool is_mfa_enabled() const noexcept
108  {
109  return _mfa_enabled;
110  }
111 
113 
116  AEGIS_DECL std::string get_mention() const noexcept;
118 
122  AEGIS_DECL guild_info & get_guild_info(snowflake guild_id) noexcept;
123 
124  AEGIS_DECL guild_info & get_guild_info_nolock(snowflake guild_id) noexcept;
125 
126  AEGIS_DECL guild_info * get_guild_info_nocreate(snowflake guild_id) const noexcept;
127 
129 
132  AEGIS_DECL std::string get_full_name() const noexcept;
133 
135 
138  snowflake get_id() const noexcept
139  {
140  return _member_id;
141  }
142 
144 
147  bool has_dm() const noexcept
148  {
149  return !!get_dm_id();
150  }
151 
154 
157  snowflake get_dm_id() const noexcept
158  {
159  return _dm_id;
160  }
161 
163 
166  void set_dm_id(snowflake _id) noexcept
167  {
168  _dm_id = _id;
169  }
170 
172 
175  shared_mutex & mtx() noexcept
176  {
177  return _m;
178  }
179 
180 private:
181  friend class core;
182  friend class guild;
183  friend class gateway::objects::message;
184 
185  AEGIS_DECL void _load_data(gateway::objects::user mbr);
186 
187  snowflake _member_id = 0;
188  snowflake _dm_id = 0;
189  presence::user_status _status = presence::user_status::Offline;
190  std::string _name;
191  uint16_t _discriminator = 0;
192  std::string _avatar;
193  bool _is_bot = false;
194  bool _mfa_enabled = false;
195  std::vector<std::unique_ptr<guild_info>> guilds;
196  mutable shared_mutex _m;
197 
199  AEGIS_DECL void _load(guild * _guild, const json & obj, shards::shard * _shard, bool self_add = true);
200 
202  AEGIS_DECL void _load_nolock(guild * _guild, const json & obj, shards::shard * _shard, bool self_add = true, bool guild_lock = true);
203 
205  AEGIS_DECL guild_info & _join(snowflake guild_id);
206 
208  AEGIS_DECL guild_info & _join_nolock(snowflake guild_id);
209 
211  void leave(snowflake guild_id)
212  {
213  std::unique_lock<shared_mutex> l(mtx());
214  guilds.erase(std::find_if(std::begin(guilds), std::end(guilds), [&guild_id](const std::unique_ptr<guild_info> & gi)
215  {
216  if (gi->id == guild_id)
217  return true;
218  return false;
219  }));
220  }
221 };
222 
223 }
224 
225 #else
226 
227 namespace aegis
228 {
229 
230 class user
231 {
232 public:
233  enum member_status
234  {
235  Offline,
236  Online,
237  Idle,
238  DoNotDisturb
239  };
240 };
241 
242 }
243 #endif
Stores creation time and extra data specific to Discord for entities.
Definition: snowflake.hpp:21
Definition: user.hpp:35
Class for performing actions pertaining to a specified guild.
Definition: guild.hpp:159
uint16_t get_discriminator() const noexcept
Get the discriminator of this user.
Definition: user.hpp:78
snowflake get_dm_id() const noexcept
Definition: user.hpp:157
Primary class for managing a bot interface.
Definition: core.hpp:157
std::string get_mention() const noexcept
Builds a mention for this user.
Definition: user.cpp:206
bool is_mfa_enabled() const noexcept
Get the status of multi factor authentication.
Definition: user.hpp:107
Definition: message.hpp:57
bool mute
Definition: user.hpp:53
void set_dm_id(snowflake _id) noexcept
Set the DM channel id for the user.
Definition: user.hpp:166
shared_mutex & mtx() noexcept
Definition: user.hpp:175
snowflake get_id() const noexcept
Get the snowflake of this user.
Definition: user.hpp:138
Stores user-specific and guild-specific attributes of users.
Definition: user.hpp:37
Definition: presence.hpp:27
snowflake id
Definition: user.hpp:47
bool has_dm() const noexcept
Whether the DM channel id for this user has been cached yet.
Definition: user.hpp:147
std::string get_avatar() const noexcept
Get the avatar hash of this user.
Definition: user.hpp:87
std::string get_username() const noexcept
Get the nickname of this user.
Definition: user.hpp:67
uint64_t joined_at
Definition: user.hpp:51
Member owned guild information.
Definition: user.hpp:45
std::string get_name(snowflake guild_id) noexcept
Get the nickname of this user.
Definition: user.cpp:159
bool is_bot() const noexcept
Check whether user is a bot.
Definition: user.hpp:98
guild_info & get_guild_info(snowflake guild_id) noexcept
Get the member owned guild information object.
Definition: user.cpp:102
bool deaf
Definition: user.hpp:52
std::string get_full_name() const noexcept
Get the full name (username#discriminator) of this user.
Definition: user.cpp:25
Tracks websocket shards and their connections.
Definition: shard.hpp:42
lib::optional< std::string > nickname
Definition: user.hpp:50