Debian Codegraph @

src/dpp/presence.cpp

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22 #include <dpp/presence.h>
23 #include <dpp/discordevents.h>
24 #include <dpp/discordclient.h>
25 #include <dpp/json.h>
26
27 namespace dpp {
28
29 using json = nlohmann::json;
30
31 std::string activity::get_large_asset_url(uint16_t size, const image_type format) const {
32 if (!this->assets.large_image.empty() && this->application_id &&
33 this->assets.large_image.find(':') == std::string::npos) { // make sure it's not a prefixed proxy image
34 return utility::cdn_endpoint_url({ i_jpg, i_png, i_webp },
35 "app-assets/" + std::to_string(this->application_id) + "/" + this->assets.large_image,
36 format, size);
37 } else {
38 return std::string();
39 }
40 }
41
42 std::string activity::get_small_asset_url(uint16_t size, const image_type format) const {
43 if (!this->assets.small_image.empty() && this->application_id &&
44 this->assets.small_image.find(':') == std::string::npos) { // make sure it's not a prefixed proxy image
45 return utility::cdn_endpoint_url({ i_jpg, i_png, i_webp },
46 "app-assets/" + std::to_string(this->application_id) + "/" + this->assets.small_image,
47 format, size);
48 } else {
49 return std::string();
50 }
51 }
52
53 activity_party::activity_party() : id(0), current_size(0), maximum_size(0)
54 {
55 }
56
57 activity::activity(const activity_type typ, const std::string& nam, const std::string& stat, const std::string& url_) :
58 name(nam), state(stat), url(url_), type(typ), created_at(0), start(0), end(0), application_id(0), flags(0), is_instance(false)
59 {
60 }
61
62 activity::activity(): created_at(0), start(0), end(0), application_id(0), flags(0), is_instance(false)
63 {
64 }
65
66 presence::presence() : user_id(0), guild_id(0), flags(0)
67 {
68 }
69
70 presence::presence(presence_status status, activity_type type, const std::string& activity_description) {
71 dpp::activity a;
72
73 /* Even if type is custom, a name is still required.
74 * We'll just set the name as this activity_description as it won't be used if custom either way. */
75 a.name = activity_description;
76
77 /* If the type is custom, set the state as "activity_description" */
78 if (type == activity_type::at_custom) {
79 a.state = activity_description;
80 }
81
82 a.type = type;
83 activities.clear();
84 activities.emplace_back(a);
85 flags &= PF_CLEAR_STATUS;
86 if (status == ps_online) {
87 flags |= p_status_online;
88 } else if (status == ps_idle) {
89 flags |= p_status_idle;
90 } else if (status == ps_dnd) {
91 flags |= p_status_dnd;
92 }
93 }
94
95 presence::presence(presence_status status, const activity &a) {
96 activities.clear();
97 activities.emplace_back(a);
98 flags &= PF_CLEAR_STATUS;
99 if (status == ps_online) {
100 flags |= p_status_online;
101 } else if (status == ps_idle) {
102 flags |= p_status_idle;
103 } else if (status == ps_dnd) {
104 flags |= p_status_dnd;
105 }
106 }
107
108 presence::~presence() = default;
109
110 presence& presence::fill_from_json_impl(nlohmann::json* j) {
111 guild_id = snowflake_not_null(j, "guild_id");
112 user_id = snowflake_not_null(&((*j)["user"]), "id");
113
114 auto f = j->find("client_status");
115 if (f != j->end()) {
116
117 bool update_desktop = false, update_web = false, update_mobile = false;
118 std::string desktop, mobile, web;
119
120 if (f->find("desktop") != f->end()) {
121 desktop = string_not_null(&((*j)["client_status"]), "desktop");
122 update_desktop = true;
123 }
124 if (f->find("mobile") != f->end()) {
125 mobile = string_not_null(&((*j)["client_status"]), "mobile");
126 update_mobile = true;
127 }
128 if (f->find("web") != f->end()) {
129 web = string_not_null(&((*j)["client_status"]), "web");
130 update_web = true;
131 }
132
133 if (update_desktop) {
134 flags &= PF_CLEAR_DESKTOP;
135 if (desktop == "online") {
136 flags |= p_desktop_online;
137 } else if (desktop == "idle") {
138 flags |= p_desktop_idle;
139 } else if (desktop == "dnd") {
140 flags |= p_desktop_dnd;
141 }
142 }
143
144 if (update_mobile) {
145 flags &= PF_CLEAR_MOBILE;
146 if (mobile == "online") {
147 flags |= p_mobile_online;
148 } else if (mobile == "idle") {
149 flags |= p_mobile_idle;
150 } else if (mobile == "dnd") {
151 flags |= p_mobile_dnd;
152 }
153 }
154
155 if (update_web) {
156 flags &= PF_CLEAR_WEB;
157 if (web == "online") {
158 flags |= p_web_online;
159 } else if (web == "idle") {
160 flags |= p_web_idle;
161 } else if (web == "dnd") {
162 flags |= p_web_dnd;
163 }
164 }
165 }
166
167 if (j->contains("status")) {
168 flags &= PF_CLEAR_STATUS;
169 std::string main = string_not_null(j, "status");
170 if (main == "online") {
171 flags |= p_status_online;
172 } else if (main == "idle") {
173 flags |= p_status_idle;
174 } else if (main == "dnd") {
175 flags |= p_status_dnd;
176 }
177 }
178
179
180 if (j->contains("activities")) {
181 activities.clear();
182 for (auto & act : (*j)["activities"]) {
183 activity a;
184 a.name = string_not_null(&act, "name");
185 a.details = string_not_null(&act, "details");
186 if (act.find("assets") != act.end()) {
187 a.assets.large_image = string_not_null(&act["assets"], "large_image");
188 a.assets.large_text = string_not_null(&act["assets"], "large_text");
189 a.assets.small_image = string_not_null(&act["assets"], "small_image");
190 a.assets.small_text = string_not_null(&act["assets"], "small_text");
191 }
192 a.state = string_not_null(&act, "state");
193 a.type = (activity_type)int8_not_null(&act, "type");
194 a.url = string_not_null(&act, "url");
195 if (act.find("buttons") != act.end()) {
196 for (auto &b : act["buttons"]) {
197 activity_button btn;
198 if (b.is_string()) { // its may be just a string (label) because normal bots cannot access the button URLs
199 btn.label = b.get<std::string>();;
200 } else {
201 btn.label = string_not_null(&b, "label");
202 btn.url = string_not_null(&b, "url");
203 }
204 a.buttons.push_back(btn);
205 }
206 }
207 if (act.find("emoji") != act.end()) {
208 a.emoji.name = string_not_null(&act["emoji"], "name");
209 a.emoji.id = snowflake_not_null(&act["emoji"], "id");
210 if (bool_not_null(&act["emoji"], "animated")) {
211 a.emoji.flags |= e_animated;
212 }
213 }
214 if (act.find("party") != act.end()) {
215 a.party.id = snowflake_not_null(&act["party"], "id");
216 if (act["party"].find("size") != act["party"].end()) { // "size" is an array of two integers
217 try {
218 a.party.current_size = act["party"]["size"][0].get<int32_t>();
219 a.party.maximum_size = act["party"]["size"][1].get<int32_t>();
220 } catch ([[maybe_unused]] std::exception &exception) {}
221 }
222 }
223 if (act.find("secrets") != act.end()) {
224 a.secrets.join = string_not_null(&act["secret"], "join");
225 a.secrets.spectate = string_not_null(&act["secret"], "spectate");
226 a.secrets.match = string_not_null(&act["secret"], "match");
227 }
228 a.created_at = int64_not_null(&act, "created_at");
229 if (act.find("timestamps") != act.end()) {
230 a.start = int64_not_null(&act["timestamps"], "start");
231 a.end = int64_not_null(&act["timestamps"], "end");
232 }
233 a.application_id = snowflake_not_null(&act, "application_id");
234 a.flags = int8_not_null(&act, "flags");
235 a.is_instance = bool_not_null(&act, "instance");
236
237 activities.push_back(a);
238 }
239 }
240
241 return *this;
242 }
243
244 json presence::to_json_impl(bool with_id) const {
245 std::map<presence_status, std::string> status_name_mapping = {
246 {ps_online, "online"},
247 {ps_offline, "offline"},
248 {ps_idle, "idle"},
249 {ps_invisible, "invisible"},
250 {ps_dnd, "dnd"}
251 };
252 json j({
253
254 {"op", ft_presence},
255 {"d",
256 {
257 { "status", status_name_mapping[status()] },
258 { "since", json::value_t::null },
259 { "afk", false }
260 }
261 }
262 });
263 if (activities.size()) {
264 for(const auto& i : activities){
265 json j2({
266 { "name", i.name },
267 { "type", i.type }
268 });
269 if (!i.url.empty()) j2["url"] = i.url;
270
271 if (i.type == activity_type::at_custom) {
272 if (!i.state.empty()) j2["state"] = i.state; /* When type is custom, bot needs to use "state" */
273 } else {
274 if (!i.state.empty()) j2["details"] = i.state; /* Otherwise, the bot needs to use "details" */
275 }
276
277 j["d"]["activities"].push_back(j2);
278 }
279 /*j["d"]["game"] = json({ // use activities instead.
280 { "name", activities[0].name },
281 { "type", activities[0].type }
282 });*/
283 }
284
285 return j;
286 }
287
288 json presence::to_json(bool with_id) const {
289 return to_json_impl(with_id);
290 }
291
292 presence_status presence::desktop_status() const {
293 return (presence_status)((flags >> PF_SHIFT_DESKTOP) & PF_STATUS_MASK);
294 }
295
296 presence_status presence::web_status() const {
297 return (presence_status)((flags >> PF_SHIFT_WEB) & PF_STATUS_MASK);
298 }
299
300 presence_status presence::mobile_status() const {
301 return (presence_status)((flags >> PF_SHIFT_MOBILE) & PF_STATUS_MASK);
302 }
303
304 presence_status presence::status() const {
305 return (presence_status)((flags >> PF_SHIFT_MAIN) & PF_STATUS_MASK);
306 }
307
308 }