-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathclients.py
771 lines (655 loc) · 26.2 KB
/
clients.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import secrets
import sys
from json import JSONDecodeError
from typing import Any, Dict, Union, Optional
import discord
from discord import Member, DMChannel, TextChannel, Message
from discord.ext import commands
from aiohttp import ClientResponseError, ClientResponse
from motor.motor_asyncio import AsyncIOMotorClient
from pymongo.errors import ConfigurationError
from core.models import InvalidConfigError, getLogger
logger = getLogger(__name__)
class GitHub:
"""
The client for interacting with GitHub API.
Parameters
----------
bot : Bot
The Modmail bot.
access_token : str, optional
GitHub's access token.
username : str, optional
GitHub username.
avatar_url : str, optional
URL to the avatar in GitHub.
url : str, optional
URL to the GitHub profile.
Attributes
----------
bot : Bot
The Modmail bot.
access_token : str
GitHub's access token.
username : str
GitHub username.
avatar_url : str
URL to the avatar in GitHub.
url : str
URL to the GitHub profile.
Class Attributes
----------------
BASE : str
GitHub API base URL.
REPO : str
Modmail repo URL for GitHub API.
HEAD : str
Modmail HEAD URL for GitHub API.
MERGE_URL : str
URL for merging upstream to master.
FORK_URL : str
URL to fork Modmail.
STAR_URL : str
URL to star Modmail.
"""
BASE = "https://api.github.com"
REPO = BASE + "/repos/modmail-dev/modmail"
MERGE_URL = BASE + "/repos/{username}/modmail/merges"
FORK_URL = REPO + "/forks"
STAR_URL = BASE + "/user/starred/modmail-dev/modmail"
def __init__(self, bot, access_token: str = "", username: str = "", **kwargs):
self.bot = bot
self.session = bot.session
self.headers: Optional[dict] = None
self.access_token = access_token
self.username = username
self.avatar_url: str = kwargs.pop("avatar_url", "")
self.url: str = kwargs.pop("url", "")
if self.access_token:
self.headers = {"Authorization": "token " + str(access_token)}
@property
def BRANCH(self) -> str:
return "master" if not self.bot.version.is_prerelease else "development"
async def request(
self,
url: str,
method: str = "GET",
payload: dict = None,
headers: dict = None,
return_response: bool = False,
read_before_return: bool = False,
) -> Union[ClientResponse, Dict[str, Any], str]:
"""
Makes a HTTP request.
Parameters
----------
url : str
The destination URL of the request.
method : str
The HTTP method (POST, GET, PUT, DELETE, FETCH, etc.).
payload : Dict[str, Any]
The json payload to be sent along the request.
headers : Dict[str, str]
Additional headers to `headers`.
return_response : bool
Whether the `ClientResponse` object should be returned.
read_before_return : bool
Whether to perform `.read()` method before returning the `ClientResponse` object.
Only valid if `return_response` is set to `True`.
Returns
-------
ClientResponse or Dict[str, Any] or List[Any] or str
`ClientResponse` if `return_response` is `True`.
`Dict[str, Any]` if the returned data is a json object.
`List[Any]` if the returned data is a json list.
`str` if the returned data is not a valid json data,
the raw response.
"""
if headers is not None:
headers.update(self.headers)
else:
headers = self.headers
async with self.session.request(method, url, headers=headers, json=payload) as resp:
if return_response:
if read_before_return:
await resp.read()
return resp
return await self._get_response_data(resp)
@staticmethod
async def _get_response_data(response: ClientResponse) -> Union[Dict[str, Any], str]:
"""
Internal method to convert the response data to `dict` if the data is a
json object, or to `str` (raw response) if the data is not a valid json.
"""
try:
return await response.json()
except (JSONDecodeError, ClientResponseError):
return await response.text()
def filter_valid(self, data) -> Dict[str, Any]:
"""
Filters configuration keys that are accepted.
Parameters
----------
data : Dict[str, Any]
The data that needs to be cleaned.
Returns
-------
Dict[str, Any]
Filtered `data` to keep only the accepted pairs.
"""
valid_keys = self.bot.config.valid_keys.difference(self.bot.config.protected_keys)
return {k: v for k, v in data.items() if k in valid_keys}
async def update_repository(self, sha: str = None) -> Dict[str, Any]:
"""
Update the repository from Modmail main repo.
Parameters
----------
sha : Optional[str]
The commit SHA to update the repository. If `None`, the latest
commit SHA will be fetched.
Returns
-------
Dict[str, Any]
A dictionary that contains response data.
"""
if not self.username:
raise commands.CommandInvokeError("Username not found.")
if sha is None:
resp = await self.request(self.REPO + "/git/refs/heads/" + self.BRANCH)
sha = resp["object"]["sha"]
payload = {"base": self.BRANCH, "head": sha, "commit_message": "Updating bot"}
merge_url = self.MERGE_URL.format(username=self.username)
resp = await self.request(
merge_url,
method="POST",
payload=payload,
return_response=True,
read_before_return=True,
)
repo_url = self.BASE + f"/repos/{self.username}/modmail"
status_map = {
201: "Successful response.",
204: "Already merged.",
403: "Forbidden.",
404: f"Repository '{repo_url}' not found.",
409: "There is a merge conflict.",
422: "Validation failed.",
}
# source https://docs.github.com/en/rest/branches/branches#merge-a-branch
status = resp.status
data = await self._get_response_data(resp)
if status in (201, 204):
return data
args = (resp.request_info, resp.history)
try:
# try to get the response error message if any
message = data.get("message")
except AttributeError:
message = None
kwargs = {
"status": status,
"message": message if message else status_map.get(status),
}
# just raise
raise ClientResponseError(*args, **kwargs)
async def fork_repository(self) -> None:
"""
Forks Modmail's repository.
"""
await self.request(self.FORK_URL, method="POST", return_response=True)
async def has_starred(self) -> bool:
"""
Checks if shared Modmail.
Returns
-------
bool
`True`, if Modmail was starred.
Otherwise `False`.
"""
resp = await self.request(self.STAR_URL, return_response=True)
return resp.status == 204
async def star_repository(self) -> None:
"""
Stars Modmail's repository.
"""
await self.request(
self.STAR_URL,
method="PUT",
headers={"Content-Length": "0"},
return_response=True,
)
@classmethod
async def login(cls, bot) -> "GitHub":
"""
Logs in to GitHub with configuration variable information.
Parameters
----------
bot : Bot
The Modmail bot.
Returns
-------
GitHub
The newly created `GitHub` object.
"""
self = cls(bot, bot.config.get("github_token"))
resp: Dict[str, Any] = await self.request(self.BASE + "/user")
if resp.get("login"):
self.username = resp["login"]
self.avatar_url = resp["avatar_url"]
self.url = resp["html_url"]
logger.info(f"GitHub logged in to: {self.username}")
return self
else:
raise InvalidConfigError("Invalid github token")
class ApiClient:
"""
This class represents the general request class for all type of clients.
Parameters
----------
bot : Bot
The Modmail bot.
Attributes
----------
bot : Bot
The Modmail bot.
session : ClientSession
The bot's current running `ClientSession`.
"""
def __init__(self, bot, db):
self.bot = bot
self.db = db
self.session = bot.session
async def request(
self,
url: str,
method: str = "GET",
payload: dict = None,
return_response: bool = False,
headers: dict = None,
) -> Union[ClientResponse, dict, str]:
"""
Makes a HTTP request.
Parameters
----------
url : str
The destination URL of the request.
method : str
The HTTP method (POST, GET, PUT, DELETE, FETCH, etc.).
payload : Dict[str, Any]
The json payload to be sent along the request.
return_response : bool
Whether the `ClientResponse` object should be returned.
headers : Dict[str, str]
Additional headers to `headers`.
Returns
-------
ClientResponse or Dict[str, Any] or List[Any] or str
`ClientResponse` if `return_response` is `True`.
`dict` if the returned data is a json object.
`list` if the returned data is a json list.
`str` if the returned data is not a valid json data,
the raw response.
"""
async with self.session.request(method, url, headers=headers, json=payload) as resp:
if return_response:
return resp
try:
return await resp.json()
except (JSONDecodeError, ClientResponseError):
return await resp.text()
@property
def logs(self):
return self.db.logs
async def setup_indexes(self):
return NotImplemented
async def validate_database_connection(self):
return NotImplemented
async def get_user_logs(self, user_id: Union[str, int]) -> list:
return NotImplemented
async def find_log_entry(self, key: str) -> list:
return NotImplemented
async def get_latest_user_logs(self, user_id: Union[str, int]):
return NotImplemented
async def get_responded_logs(self, user_id: Union[str, int]) -> list:
return NotImplemented
async def get_open_logs(self) -> list:
return NotImplemented
async def get_log(self, channel_id: Union[str, int]) -> dict:
return NotImplemented
async def get_log_link(self, channel_id: Union[str, int]) -> str:
return NotImplemented
async def create_log_entry(self, recipient: Member, channel: TextChannel, creator: Member) -> str:
return NotImplemented
async def delete_log_entry(self, key: str) -> bool:
return NotImplemented
async def get_config(self) -> dict:
return NotImplemented
async def update_config(self, data: dict):
return NotImplemented
async def edit_message(self, message_id: Union[int, str], new_content: str):
return NotImplemented
async def append_log(
self,
message: Message,
*,
message_id: str = "",
channel_id: str = "",
type_: str = "thread_message",
) -> dict:
return NotImplemented
async def post_log(self, channel_id: Union[int, str], data: dict) -> dict:
return NotImplemented
async def search_closed_by(self, user_id: Union[int, str]):
return NotImplemented
async def search_by_text(self, text: str, limit: Optional[int]):
return NotImplemented
async def create_note(self, recipient: Member, message: Message, message_id: Union[int, str]):
return NotImplemented
async def find_notes(self, recipient: Member):
return NotImplemented
async def update_note_ids(self, ids: dict):
return NotImplemented
async def delete_note(self, message_id: Union[int, str]):
return NotImplemented
async def edit_note(self, message_id: Union[int, str], message: str):
return NotImplemented
def get_plugin_partition(self, cog):
return NotImplemented
async def update_repository(self) -> dict:
return NotImplemented
async def get_user_info(self) -> Optional[dict]:
return NotImplemented
class MongoDBClient(ApiClient):
def __init__(self, bot):
mongo_uri = bot.config["connection_uri"]
if mongo_uri is None:
mongo_uri = bot.config["mongo_uri"]
if mongo_uri is not None:
logger.warning(
"You're using the old config MONGO_URI, "
"consider switching to the new CONNECTION_URI config."
)
else:
logger.critical("A Mongo URI is necessary for the bot to function.")
raise RuntimeError
try:
db = AsyncIOMotorClient(mongo_uri).modmail_bot
except ConfigurationError as e:
logger.critical(
"Your MongoDB CONNECTION_URI might be copied wrong, try re-copying from the source again. "
"Otherwise noted in the following message:\n%s",
e,
)
sys.exit(0)
super().__init__(bot, db)
async def setup_indexes(self):
"""Setup text indexes so we can use the $search operator"""
coll = self.db.logs
index_name = "messages.content_text_messages.author.name_text_key_text"
index_info = await coll.index_information()
# Backwards compatibility
old_index = "messages.content_text_messages.author.name_text"
if old_index in index_info:
logger.info("Dropping old index: %s", old_index)
await coll.drop_index(old_index)
if index_name not in index_info:
logger.info('Creating "text" index for logs collection.')
logger.info("Name: %s", index_name)
await coll.create_index(
[("messages.content", "text"), ("messages.author.name", "text"), ("key", "text")]
)
logger.debug("Successfully configured and verified database indexes.")
async def validate_database_connection(self, *, ssl_retry=True):
try:
await self.db.command("buildinfo")
except Exception as exc:
logger.critical("Something went wrong while connecting to the database.")
message = f"{type(exc).__name__}: {str(exc)}"
logger.critical(message)
if "CERTIFICATE_VERIFY_FAILED" in message and ssl_retry:
mongo_uri = self.bot.config["connection_uri"]
if mongo_uri is None:
mongo_uri = self.bot.config["mongo_uri"]
for _ in range(3):
logger.warning(
"FAILED TO VERIFY SSL CERTIFICATE, ATTEMPTING TO START WITHOUT SSL (UNSAFE)."
)
logger.warning(
"To fix this warning, check there's no proxies blocking SSL cert verification, "
'run "Certificate.command" on MacOS, '
'and check certifi is up to date "pip3 install --upgrade certifi".'
)
self.db = AsyncIOMotorClient(mongo_uri, tlsAllowInvalidCertificates=True).modmail_bot
return await self.validate_database_connection(ssl_retry=False)
if "ServerSelectionTimeoutError" in message:
logger.critical(
"This may have been caused by not whitelisting "
"IPs correctly. Make sure to whitelist all "
"IPs (0.0.0.0/0) https://i.imgur.com/mILuQ5U.png"
)
if "OperationFailure" in message:
logger.critical(
"This is due to having invalid credentials in your MongoDB CONNECTION_URI. "
"Remember you need to substitute `<password>` with your actual password."
)
logger.critical(
"Be sure to URL encode your username and password (not the entire URL!!), "
"https://www.urlencoder.io/, if this issue persists, try changing your username and password "
"to only include alphanumeric characters, no symbols."
""
)
raise
else:
logger.debug("Successfully connected to the database.")
logger.line("debug")
async def get_user_logs(self, user_id: Union[str, int]) -> list:
query = {"recipient.id": str(user_id), "guild_id": str(self.bot.guild_id)}
projection = {"messages": {"$slice": 5}}
logger.debug("Retrieving user %s logs.", user_id)
return await self.logs.find(query, projection).to_list(None)
async def find_log_entry(self, key: str) -> list:
query = {"key": key}
projection = {"messages": {"$slice": 5}}
logger.debug(f"Retrieving log ID {key}.")
return await self.logs.find(query, projection).to_list(None)
async def get_latest_user_logs(self, user_id: Union[str, int]):
query = {"recipient.id": str(user_id), "guild_id": str(self.bot.guild_id), "open": False}
projection = {"messages": {"$slice": 5}}
logger.debug("Retrieving user %s latest logs.", user_id)
return await self.logs.find_one(query, projection, limit=1, sort=[("closed_at", -1)])
async def get_responded_logs(self, user_id: Union[str, int]) -> list:
query = {
"open": False,
"messages": {
"$elemMatch": {
"author.id": str(user_id),
"author.mod": True,
"type": {"$in": ["anonymous", "thread_message"]},
}
},
}
return await self.logs.find(query).to_list(None)
async def get_open_logs(self) -> list:
query = {"open": True}
return await self.logs.find(query).to_list(None)
async def get_log(self, channel_id: Union[str, int]) -> dict:
logger.debug("Retrieving channel %s logs.", channel_id)
return await self.logs.find_one({"channel_id": str(channel_id)})
async def get_log_link(self, channel_id: Union[str, int]) -> str:
doc = await self.get_log(channel_id)
logger.debug("Retrieving log link for channel %s.", channel_id)
prefix = self.bot.config["log_url_prefix"].strip("/")
if prefix == "NONE":
prefix = ""
return f"{self.bot.config['log_url'].strip('/')}{'/' + prefix if prefix else ''}/{doc['key']}"
async def create_log_entry(self, recipient: Member, channel: TextChannel, creator: Member) -> str:
key = secrets.token_hex(6)
await self.logs.insert_one(
{
"_id": key,
"key": key,
"open": True,
"created_at": str(discord.utils.utcnow()),
"closed_at": None,
"channel_id": str(channel.id),
"guild_id": str(self.bot.guild_id),
"bot_id": str(self.bot.user.id),
"recipient": {
"id": str(recipient.id),
"name": recipient.name,
"discriminator": recipient.discriminator,
"avatar_url": recipient.display_avatar.url,
"mod": False,
},
"creator": {
"id": str(creator.id),
"name": creator.name,
"discriminator": creator.discriminator,
"avatar_url": creator.display_avatar.url,
"mod": isinstance(creator, Member),
},
"closer": None,
"messages": [],
}
)
logger.debug("Created a log entry, key %s.", key)
prefix = self.bot.config["log_url_prefix"].strip("/")
if prefix == "NONE":
prefix = ""
return f"{self.bot.config['log_url'].strip('/')}{'/' + prefix if prefix else ''}/{key}"
async def delete_log_entry(self, key: str) -> bool:
result = await self.logs.delete_one({"key": key})
return result.deleted_count == 1
async def get_config(self) -> dict:
conf = await self.db.config.find_one({"bot_id": self.bot.user.id})
if conf is None:
logger.debug("Creating a new config entry for bot %s.", self.bot.user.id)
await self.db.config.insert_one({"bot_id": self.bot.user.id})
return {"bot_id": self.bot.user.id}
return conf
async def update_config(self, data: dict):
toset = self.bot.config.filter_valid(data)
unset = self.bot.config.filter_valid({k: 1 for k in self.bot.config.all_keys if k not in data})
if toset and unset:
return await self.db.config.update_one(
{"bot_id": self.bot.user.id}, {"$set": toset, "$unset": unset}
)
if toset:
return await self.db.config.update_one({"bot_id": self.bot.user.id}, {"$set": toset})
if unset:
return await self.db.config.update_one({"bot_id": self.bot.user.id}, {"$unset": unset})
async def edit_message(self, message_id: Union[int, str], new_content: str) -> None:
await self.logs.update_one(
{"messages.message_id": str(message_id)},
{"$set": {"messages.$.content": new_content, "messages.$.edited": True}},
)
async def append_log(
self,
message: Message,
*,
message_id: str = "",
channel_id: str = "",
type_: str = "thread_message",
) -> dict:
channel_id = str(channel_id) or str(message.channel.id)
message_id = str(message_id) or str(message.id)
data = {
"timestamp": str(message.created_at),
"message_id": message_id,
"author": {
"id": str(message.author.id),
"name": message.author.name,
"discriminator": message.author.discriminator,
"avatar_url": message.author.display_avatar.url,
"mod": not isinstance(message.channel, DMChannel),
},
"content": message.content,
"type": type_,
"attachments": [
{
"id": a.id,
"filename": a.filename,
"is_image": a.width is not None,
"size": a.size,
"url": a.url,
}
for a in message.attachments
],
}
return await self.logs.find_one_and_update(
{"channel_id": channel_id}, {"$push": {"messages": data}}, return_document=True
)
async def post_log(self, channel_id: Union[int, str], data: dict) -> dict:
return await self.logs.find_one_and_update(
{"channel_id": str(channel_id)}, {"$set": data}, return_document=True
)
async def search_closed_by(self, user_id: Union[int, str]):
return await self.logs.find(
{"guild_id": str(self.bot.guild_id), "open": False, "closer.id": str(user_id)},
{"messages": {"$slice": 5}},
).to_list(None)
async def search_by_text(self, text: str, limit: Optional[int]):
return await self.bot.db.logs.find(
{
"guild_id": str(self.bot.guild_id),
"open": False,
"$text": {"$search": f'"{text}"'},
},
{"messages": {"$slice": 5}},
).to_list(limit)
async def create_note(self, recipient: Member, message: Message, message_id: Union[int, str]):
await self.db.notes.insert_one(
{
"recipient": str(recipient.id),
"author": {
"id": str(message.author.id),
"name": message.author.name,
"discriminator": message.author.discriminator,
"avatar_url": message.author.display_avatar.url,
},
"message": message.content,
"message_id": str(message_id),
}
)
async def find_notes(self, recipient: Member):
return await self.db.notes.find({"recipient": str(recipient.id)}).to_list(None)
async def update_note_ids(self, ids: dict):
for object_id, message_id in ids.items():
await self.db.notes.update_one({"_id": object_id}, {"$set": {"message_id": message_id}})
async def delete_note(self, message_id: Union[int, str]):
await self.db.notes.delete_one({"message_id": str(message_id)})
async def edit_note(self, message_id: Union[int, str], message: str):
await self.db.notes.update_one({"message_id": str(message_id)}, {"$set": {"message": message}})
def get_plugin_partition(self, cog):
cls_name = cog.__class__.__name__
return self.db.plugins[cls_name]
async def update_repository(self) -> dict:
user = await GitHub.login(self.bot)
data = await user.update_repository()
return {
"data": data,
"user": {
"username": user.username,
"avatar_url": user.avatar_url,
"url": user.url,
},
}
async def get_user_info(self) -> Optional[dict]:
try:
user = await GitHub.login(self.bot)
except InvalidConfigError:
return None
else:
return {
"user": {
"username": user.username,
"avatar_url": user.avatar_url,
"url": user.url,
}
}
class PluginDatabaseClient:
def __init__(self, bot):
self.bot = bot
def get_partition(self, cog):
cls_name = cog.__class__.__name__
return self.bot.api.db.plugins[cls_name]