-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
modmail.py
364 lines (273 loc) · 13 KB
/
modmail.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
"""Provide models for new modmail."""
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from ...const import API_PATH
from ...util import _deprecate_args, snake_case_keys
from .base import RedditBase
if TYPE_CHECKING: # pragma: no cover
import asyncpraw
class ModmailConversation(RedditBase):
"""A class for modmail conversations.
.. include:: ../../typical_attributes.rst
==================== ===============================================================
Attribute Description
==================== ===============================================================
``authors`` Provides an ordered list of :class:`.Redditor` instances. The
authors of each message in the modmail conversation.
``id`` The ID of the :class:`.ModmailConversation`.
``is_highlighted`` Whether or not the :class:`.ModmailConversation` is
highlighted.
``is_internal`` Whether or not the :class:`.ModmailConversation` is a private
mod conversation.
``last_mod_update`` Time of the last mod message reply, represented in the `ISO
8601`_ standard with timezone.
``last_updated`` Time of the last message reply, represented in the `ISO 8601`_
standard with timezone.
``last_user_update`` Time of the last user message reply, represented in the `ISO
8601`_ standard with timezone.
``num_messages`` The number of messages in the :class:`.ModmailConversation`.
``obj_ids`` Provides a list of dictionaries representing mod actions on the
:class:`.ModmailConversation`. Each dict contains attributes of
``"key"`` and ``"id"``. The key can be either ``""messages"``
or ``"ModAction"``. ``"ModAction"`` represents
archiving/highlighting etc.
``owner`` Provides an instance of :class:`.Subreddit`. The subreddit that
the :class:`.ModmailConversation` belongs to.
``participant`` Provides an instance of :class:`.Redditor`. The participating
user in the :class:`.ModmailConversation`.
``subject`` The subject of the :class:`.ModmailConversation`.
==================== ===============================================================
.. _iso 8601: https://en.wikipedia.org/wiki/ISO_8601
"""
STR_FIELD = "id"
@staticmethod
def _convert_conversation_objects(data, reddit):
"""Convert messages and mod actions to Async PRAW objects."""
result = {"messages": [], "modActions": []}
for thing in data["objIds"]:
key = thing["key"]
thing_data = data[key][thing["id"]]
result[key].append(reddit._objector.objectify(thing_data))
data.update(result)
@staticmethod
def _convert_user_summary(data, reddit):
"""Convert dictionaries of recent user history to Async PRAW objects."""
parsers = {
"recentComments": reddit._objector.parsers[reddit.config.kinds["comment"]],
"recentConvos": ModmailConversation,
"recentPosts": reddit._objector.parsers[reddit.config.kinds["submission"]],
}
for kind, parser in parsers.items():
objects = []
for thing_id, summary in data[kind].items():
thing = parser(reddit, id=thing_id.rsplit("_", 1)[-1])
# I have no idea how to cover this
if parser is not ModmailConversation: # pragma: no cover
del summary["permalink"]
for key, value in summary.items():
setattr(thing, key, value)
objects.append(thing)
# Sort by id, oldest to newest
data[kind] = sorted(objects, key=lambda x: int(x.id, base=36), reverse=True)
@classmethod
def parse( # pylint: disable=arguments-differ
cls,
data: Dict[str, Any],
reddit: "asyncpraw.Reddit",
convert_objects: bool = True,
):
"""Return an instance of :class:`.ModmailConversation` from ``data``.
:param data: The structured data.
:param reddit: An instance of :class:`.Reddit`.
:param convert_objects: If ``True``, convert message and mod action data into
objects (default: ``True``).
"""
data["authors"] = [
reddit._objector.objectify(author) for author in data["authors"]
]
for entity in "owner", "participant":
data[entity] = reddit._objector.objectify(data[entity])
if data.get("user"):
cls._convert_user_summary(data["user"], reddit)
data["user"] = reddit._objector.objectify(data["user"])
data = snake_case_keys(data)
return cls(reddit, _data=data)
def __init__(
self,
reddit: "asyncpraw.Reddit",
id: Optional[str] = None, # pylint: disable=redefined-builtin
mark_read: bool = False,
_data: Optional[Dict[str, Any]] = None,
):
"""Initialize a :class:`.ModmailConversation` instance.
:param mark_read: If ``True``, conversation is marked as read (default:
``False``).
"""
if bool(id) == bool(_data):
raise TypeError("Either 'id' or '_data' must be provided.")
if id:
self.id = id
super().__init__(reddit, _data=_data)
self._info_params = {"markRead": True} if mark_read else None
def _build_conversation_list(self, other_conversations):
"""Return a comma-separated list of conversation IDs."""
conversations = [self] + (other_conversations or [])
return ",".join(conversation.id for conversation in conversations)
def _fetch_info(self):
return "modmail_conversation", {"id": self.id}, self._info_params
async def _fetch_data(self):
name, fields, params = self._fetch_info()
path = API_PATH[name].format(**fields)
return await self._reddit.request(method="GET", params=params, path=path)
async def _fetch(self):
data = await self._fetch_data()
other = self._reddit._objector.objectify(data)
self.__dict__.update(other.__dict__)
self._fetched = True
async def archive(self):
"""Archive the conversation.
For example:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.archive()
"""
await self._reddit.post(API_PATH["modmail_archive"].format(id=self.id))
async def highlight(self):
"""Highlight the conversation.
For example:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.highlight()
"""
await self._reddit.post(API_PATH["modmail_highlight"].format(id=self.id))
@_deprecate_args("num_days")
async def mute(self, *, num_days=3):
"""Mute the non-mod user associated with the conversation.
:param num_days: Duration of mute in days. Valid options are ``3``, ``7``, or
``28`` (default: ``3``).
For example:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.mute()
To mute for 7 days:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.mute(num_days=7)
"""
if num_days != 3: # no need to pass params if it's the default
params = {"num_hours": num_days * 24}
else:
params = {}
await self._reddit.request(
method="POST",
params=params,
path=API_PATH["modmail_mute"].format(id=self.id),
)
@_deprecate_args("other_conversations")
async def read(
self, *, other_conversations: Optional[List["ModmailConversation"]] = None
): # noqa: D207, D301
"""Mark the conversation(s) as read.
:param other_conversations: A list of other conversations to mark (default:
``None``).
For example, to mark the conversation as read along with other recent
conversations from the same user:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail.conversation("2gmz")
await conversation.read(other_conversations=conversation.user.recent_convos)
"""
data = {"conversationIds": self._build_conversation_list(other_conversations)}
await self._reddit.post(API_PATH["modmail_read"], data=data)
@_deprecate_args("body", "author_hidden", "internal")
async def reply(
self, *, author_hidden: bool = False, body: str, internal: bool = False
) -> "ModmailMessage":
"""Reply to the conversation.
:param author_hidden: When ``True``, author is hidden from non-moderators
(default: ``False``).
:param body: The Markdown formatted content for a message.
:param internal: When ``True``, message is a private moderator note, hidden from
non-moderators (default: ``False``).
:returns: A :class:`.ModmailMessage` object for the newly created message.
For example, to reply to the non-mod user while hiding your username:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.reply(body="Message body", author_hidden=True)
To create a private moderator note on the conversation:
.. code-block:: python
await conversation.reply(body="Message body", internal=True)
"""
data = {
"body": body,
"isAuthorHidden": author_hidden,
"isInternal": internal,
}
response = await self._reddit.post(
API_PATH["modmail_conversation"].format(id=self.id), data=data
)
message_id = response["conversation"]["objIds"][-1]["id"]
message_data = response["messages"][message_id]
return self._reddit._objector.objectify(message_data)
async def unarchive(self):
"""Unarchive the conversation.
For example:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.unarchive()
"""
await self._reddit.post(API_PATH["modmail_unarchive"].format(id=self.id))
async def unhighlight(self):
"""Un-highlight the conversation.
For example:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.unhighlight()
"""
await self._reddit.delete(API_PATH["modmail_highlight"].format(id=self.id))
async def unmute(self):
"""Unmute the non-mod user associated with the conversation.
For example:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail("2gmz")
await conversation.unmute()
"""
await self._reddit.request(
method="POST", path=API_PATH["modmail_unmute"].format(id=self.id)
)
@_deprecate_args("other_conversations")
async def unread(
self, *, other_conversations: Optional[List["ModmailConversation"]] = None
): # noqa: D207, D301
"""Mark the conversation(s) as unread.
:param other_conversations: A list of other conversations to mark (default:
``None``).
For example, to mark the conversation as unread along with other recent
conversations from the same user:
.. code-block:: python
subreddit = await reddit.subreddit("test")
conversation = await subreddit.modmail.conversation("2gmz")
await conversation.unread(other_conversations=conversation.user.recent_convos)
"""
data = {"conversationIds": self._build_conversation_list(other_conversations)}
await self._reddit.post(API_PATH["modmail_unread"], data=data)
class ModmailObject(RedditBase):
"""A base class for objects within a modmail conversation."""
AUTHOR_ATTRIBUTE = "author"
STR_FIELD = "id"
def __setattr__(self, attribute: str, value: Any):
"""Objectify the AUTHOR_ATTRIBUTE attribute."""
if attribute == self.AUTHOR_ATTRIBUTE:
value = self._reddit._objector.objectify(value)
super().__setattr__(attribute, value)
class ModmailAction(ModmailObject):
"""A class for moderator actions on modmail conversations."""
class ModmailMessage(ModmailObject):
"""A class for modmail messages."""