|
2 | 2 | import requests |
3 | 3 | import random |
4 | 4 |
|
| 5 | +from flask import current_app |
| 6 | + |
5 | 7 | from .settings import config |
6 | 8 | from .utils import get_app_description, generate_image_url, who_am_i |
7 | 9 | import appstore # break the circular dependency to import get_topic_url_for_app from discourse |
@@ -221,17 +223,53 @@ def report_app_unlisted(app_name, developer_name, app_id, affected_app_uuid = No |
221 | 223 |
|
222 | 224 | send_admin_discord_webhook(request_data) |
223 | 225 |
|
| 226 | +def truncate_string_to_length(string, length): |
| 227 | + if len(string) <= length: |
| 228 | + return string |
| 229 | + |
| 230 | + return string[:(length - 1)] + '…' |
| 231 | + |
| 232 | +def truncate_data(embed): |
| 233 | + if 'title' in embed: |
| 234 | + embed['title'] = truncate_string_to_length(embed['title'], 256) |
| 235 | + |
| 236 | + if 'description' in embed: |
| 237 | + embed['description'] = truncate_string_to_length(embed['description'], 4096) |
| 238 | + |
| 239 | + for field in embed['fields']: |
| 240 | + if 'name' in field: |
| 241 | + field['name'] = truncate_string_to_length(field['name'], 256) |
| 242 | + |
| 243 | + if 'value' in field: |
| 244 | + field['value'] = truncate_string_to_length(field['value'], 1024) |
| 245 | + |
| 246 | + if 'author' in embed and 'name' in embed['author']: |
| 247 | + embed['author']['name'] = truncate_string_to_length(embed['author']['name'], 256) |
| 248 | + |
| 249 | + if 'footer' in embed and 'text' in embed['footer']: |
| 250 | + embed['footer']['text'] = truncate_string_to_length(embed['footer']['text'], 2048) |
| 251 | + |
| 252 | + return embed |
| 253 | + |
224 | 254 | def send_discord_webhook(request_data, is_generated = False): |
| 255 | + request_data['embeds'][0] = truncate_data(request_data['embeds'][0]) |
225 | 256 | if not is_generated: |
226 | 257 | if config['DISCORD_HOOK_URL'] is not None: |
227 | 258 | headers = {'Content-Type': 'application/json'} |
228 | | - requests.post(config['DISCORD_HOOK_URL'], data=json.dumps(request_data), headers=headers) |
| 259 | + r = requests.post(config['DISCORD_HOOK_URL'], data=json.dumps(request_data), headers=headers) |
| 260 | + if r.status_code != 200: |
| 261 | + current_app.logger.warning(f"Discord returned {r.status_code} with message: {r.text}") |
229 | 262 | else: |
230 | 263 | if config['DISCORD_GENERATED_HOOK_URL'] is not None: |
231 | 264 | headers = {'Content-Type': 'application/json'} |
232 | | - requests.post(config['DISCORD_GENERATED_HOOK_URL'], data=json.dumps(request_data), headers=headers) |
| 265 | + r = requests.post(config['DISCORD_GENERATED_HOOK_URL'], data=json.dumps(request_data), headers=headers) |
| 266 | + if r.status_code != 200: |
| 267 | + current_app.logger.warning(f"Discord returned {r.status_code} with message: {r.text}") |
233 | 268 |
|
234 | 269 | def send_admin_discord_webhook(request_data): |
235 | 270 | if config['DISCORD_ADMIN_HOOK_URL'] is not None: |
| 271 | + request_data['embeds'][0] = truncate_data(request_data['embeds'][0]) |
236 | 272 | headers = {'Content-Type': 'application/json'} |
237 | | - requests.post(config['DISCORD_ADMIN_HOOK_URL'], data=json.dumps(request_data), headers=headers) |
| 273 | + r = requests.post(config['DISCORD_ADMIN_HOOK_URL'], data=json.dumps(request_data), headers=headers) |
| 274 | + if r.status_code != 200: |
| 275 | + current_app.logger.warning(f"Discord returned {r.status_code} with message: {r.text}") |
0 commit comments