Skip to content

Commit dadbe6e

Browse files
authored
Merge pull request #1960 from praw-dev/modmail_fix
Fix modmail reply bug
2 parents 8bf6693 + 5984516 commit dadbe6e

File tree

4 files changed

+236
-3
lines changed

4 files changed

+236
-3
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ PRAW follows `semantic versioning <http://semver.org/>`_.
66
Unreleased
77
----------
88

9+
**Fixed**
10+
11+
- An issue with replying to a modmail conversation results in a error.
12+
913
7.7.0 (2023/02/25)
1014
------------------
1115

praw/models/reddit/modmail.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,15 @@ def reply(
257257
response = self._reddit.post(
258258
API_PATH["modmail_conversation"].format(id=self.id), data=data
259259
)
260-
message_id = response["conversation"]["objIds"][-1]["id"]
261-
message_data = response["messages"][message_id]
262-
return self._reddit._objector.objectify(message_data)
260+
if isinstance(response, dict):
261+
# Reddit recently changed the response format, so we need to handle both in case they change it back
262+
message_id = response["conversation"]["objIds"][-1]["id"]
263+
message_data = response["messages"][message_id]
264+
return self._reddit._objector.objectify(message_data)
265+
else:
266+
for message in response.messages:
267+
if message.id == response.obj_ids[-1]["id"]:
268+
return message
263269

264270
def unarchive(self):
265271
"""Unarchive the conversation.
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
{
2+
"http_interactions": [
3+
{
4+
"recorded_at": "2023-07-11T20:33:14",
5+
"request": {
6+
"body": {
7+
"encoding": "utf-8",
8+
"string": "grant_type=refresh_token&refresh_token=<REFRESH_TOKEN>"
9+
},
10+
"headers": {
11+
"Accept": [
12+
"*/*"
13+
],
14+
"Accept-Encoding": [
15+
"identity"
16+
],
17+
"Authorization": [
18+
"Basic <BASIC_AUTH>"
19+
],
20+
"Connection": [
21+
"close"
22+
],
23+
"Content-Length": [
24+
"82"
25+
],
26+
"Content-Type": [
27+
"application/x-www-form-urlencoded"
28+
],
29+
"User-Agent": [
30+
"<USER_AGENT> PRAW/7.7.1.dev0 prawcore/2.3.0"
31+
]
32+
},
33+
"method": "POST",
34+
"uri": "https://www.reddit.com/api/v1/access_token"
35+
},
36+
"response": {
37+
"body": {
38+
"encoding": "UTF-8",
39+
"string": "{\"access_token\": \"<ACCESS_TOKEN>\", \"token_type\": \"bearer\", \"expires_in\": 86400, \"refresh_token\": \"<REFRESH_TOKEN>\", \"scope\": \"creddits modnote modcontributors modmail modconfig subscribe structuredstyles vote wikiedit mysubreddits submit modlog modposts modflair save modothers read privatemessages report identity livemanage account modtraffic wikiread edit modwiki modself history flair\"}"
40+
},
41+
"headers": {
42+
"Accept-Ranges": [
43+
"bytes"
44+
],
45+
"Cache-Control": [
46+
"private, max-age=3600"
47+
],
48+
"Connection": [
49+
"close"
50+
],
51+
"Content-Length": [
52+
"1527"
53+
],
54+
"Date": [
55+
"Tue, 11 Jul 2023 20:33:14 GMT"
56+
],
57+
"NEL": [
58+
"{\"report_to\": \"w3-reporting-nel\", \"max_age\": 14400, \"include_subdomains\": false, \"success_fraction\": 1.0, \"failure_fraction\": 1.0}"
59+
],
60+
"Report-To": [
61+
"{\"group\": \"w3-reporting-nel\", \"max_age\": 14400, \"include_subdomains\": true, \"endpoints\": [{ \"url\": \"https://w3-reporting-nel.reddit.com/reports\" }]}, {\"group\": \"w3-reporting\", \"max_age\": 14400, \"include_subdomains\": true, \"endpoints\": [{ \"url\": \"https://w3-reporting.reddit.com/reports\" }]}, {\"group\": \"w3-reporting-csp\", \"max_age\": 14400, \"include_subdomains\": true, \"endpoints\": [{ \"url\": \"https://w3-reporting-csp.reddit.com/reports\" }]}"
62+
],
63+
"Server": [
64+
"snooserv"
65+
],
66+
"Set-Cookie": [
67+
"edgebucket=lUgiOW4d9OwlvT1wgL; Domain=reddit.com; Max-Age=63071999; Path=/; secure"
68+
],
69+
"Strict-Transport-Security": [
70+
"max-age=31536000; includeSubdomains"
71+
],
72+
"Vary": [
73+
"accept-encoding"
74+
],
75+
"Via": [
76+
"1.1 varnish"
77+
],
78+
"X-Content-Type-Options": [
79+
"nosniff"
80+
],
81+
"X-Frame-Options": [
82+
"SAMEORIGIN"
83+
],
84+
"X-XSS-Protection": [
85+
"1; mode=block"
86+
],
87+
"content-type": [
88+
"application/json; charset=UTF-8"
89+
],
90+
"x-moose": [
91+
"majestic"
92+
]
93+
},
94+
"status": {
95+
"code": 200,
96+
"message": "OK"
97+
},
98+
"url": "https://www.reddit.com/api/v1/access_token"
99+
}
100+
},
101+
{
102+
"recorded_at": "2023-07-11T20:33:14",
103+
"request": {
104+
"body": {
105+
"encoding": "utf-8",
106+
"string": "api_type=json&body=A+message&isAuthorHidden=False&isInternal=True"
107+
},
108+
"headers": {
109+
"Accept": [
110+
"*/*"
111+
],
112+
"Accept-Encoding": [
113+
"identity"
114+
],
115+
"Authorization": [
116+
"bearer <ACCESS_TOKEN>"
117+
],
118+
"Connection": [
119+
"keep-alive"
120+
],
121+
"Content-Length": [
122+
"65"
123+
],
124+
"Content-Type": [
125+
"application/x-www-form-urlencoded"
126+
],
127+
"Cookie": [
128+
"edgebucket=lUgiOW4d9OwlvT1wgL"
129+
],
130+
"User-Agent": [
131+
"<USER_AGENT> PRAW/7.7.1.dev0 prawcore/2.3.0"
132+
]
133+
},
134+
"method": "POST",
135+
"uri": "https://oauth.reddit.com/api/mod/conversations/1mahha?raw_json=1"
136+
},
137+
"response": {
138+
"body": {
139+
"encoding": "UTF-8",
140+
"string": "{\"conversation\": {\"isAuto\": false, \"participant\": {\"isMod\": true, \"isAdmin\": false, \"name\": \"<USERNAME>\", \"isOp\": true, \"isParticipant\": true, \"isApproved\": false, \"isHidden\": false, \"id\": 561251419067, \"isDeleted\": false}, \"objIds\": [{\"id\": \"2bjn0i\", \"key\": \"messages\"}, {\"id\": \"2bjn0k\", \"key\": \"messages\"}, {\"id\": \"2bjn2z\", \"key\": \"messages\"}], \"isRepliable\": true, \"lastUserUpdate\": null, \"isInternal\": false, \"lastModUpdate\": \"2023-07-11T20:33:14.617000+0000\", \"authors\": [{\"isMod\": true, \"isAdmin\": false, \"name\": \"<USERNAME>\", \"isOp\": true, \"isParticipant\": true, \"isApproved\": false, \"isHidden\": false, \"id\": 561251419067, \"isDeleted\": false}], \"lastUpdated\": \"2023-07-11T20:33:14.617000+0000\", \"participantSubreddit\": {}, \"legacyFirstMessageId\": \"1wttevy\", \"state\": 1, \"conversationType\": \"sr_user\", \"lastUnread\": \"2023-07-11T00:00:00.000000+0000\", \"owner\": {\"displayName\": \"<TEST_SUBREDDIT>\", \"type\": \"subreddit\", \"id\": \"t5_29ey0j\"}, \"subject\": \"test\", \"id\": \"1mahha\", \"isHighlighted\": false, \"numMessages\": 3}, \"participantSubreddit\": {}, \"messages\": {\"2bjn0k\": {\"body\": \"<!-- SC_OFF --><div class=\\\"md\\\"><p>additional test</p>\\n</div><!-- SC_ON -->\", \"author\": {\"name\": \"<USERNAME>\", \"isApproved\": false, \"isMod\": true, \"isAdmin\": false, \"isOp\": true, \"isParticipant\": true, \"isHidden\": false, \"id\": 561251419067, \"isDeleted\": false}, \"isInternal\": true, \"date\": \"2023-07-11T20:32:12.025000+0000\", \"bodyMarkdown\": \"additional test\", \"id\": \"2bjn0k\", \"participatingAs\": \"moderator\"}, \"2bjn0i\": {\"body\": \"<!-- SC_OFF --><div class=\\\"md\\\"><p>testing</p>\\n</div><!-- SC_ON -->\", \"author\": {\"name\": \"<USERNAME>\", \"isApproved\": false, \"isMod\": true, \"isAdmin\": false, \"isOp\": true, \"isParticipant\": true, \"isHidden\": true, \"id\": 561251419067, \"isDeleted\": false}, \"isInternal\": false, \"date\": \"2023-07-11T20:32:11.586000+0000\", \"bodyMarkdown\": \"testing\", \"id\": \"2bjn0i\", \"participatingAs\": \"moderator\"}, \"2bjn2z\": {\"body\": \"<!-- SC_OFF --><div class=\\\"md\\\"><p>A message</p>\\n</div><!-- SC_ON -->\", \"author\": {\"name\": \"<USERNAME>\", \"isApproved\": false, \"isMod\": true, \"isAdmin\": false, \"isOp\": true, \"isParticipant\": true, \"isHidden\": false, \"id\": 561251419067, \"isDeleted\": false}, \"isInternal\": true, \"date\": \"2023-07-11T20:33:14.617000+0000\", \"bodyMarkdown\": \"A message\", \"id\": \"2bjn2z\", \"participatingAs\": \"moderator\"}}, \"user\": {\"recentComments\": {\"t1_i6yklz7\": {\"comment\": \"test reply\", \"date\": \"2022-05-01T22:37:21.936000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/uflrmv/test_post/i6yklz7/\", \"title\": \"Test post\"}}, \"muteStatus\": {\"muteCount\": 0, \"isMuted\": false, \"endDate\": null, \"reason\": \"\"}, \"name\": \"<USERNAME>\", \"created\": \"2020-07-04T21:34:49.063000+00:00\", \"banStatus\": {\"endDate\": null, \"reason\": \"\", \"isBanned\": false, \"isPermanent\": false}, \"isSuspended\": false, \"approveStatus\": {\"isApproved\": false}, \"isShadowBanned\": false, \"recentPosts\": {\"t3_z3wwe8\": {\"date\": \"2022-11-24T22:47:02.992000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3wwe8/test_post/\", \"title\": \"Test post\"}, \"t3_z4lkt4\": {\"date\": \"2022-11-25T19:16:07.058000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z4lkt4/test_post/\", \"title\": \"Test post\"}, \"t3_z3x0le\": {\"date\": \"2022-11-24T22:52:25.348000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3x0le/test_post/\", \"title\": \"Test post\"}, \"t3_z3xa9p\": {\"date\": \"2022-11-24T23:04:17.179000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3xa9p/test_post/\", \"title\": \"Test post\"}, \"t3_z3wslj\": {\"date\": \"2022-11-24T22:42:19.611000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3wslj/test_post/\", \"title\": \"Test post\"}, \"t3_z3wtr9\": {\"date\": \"2022-11-24T22:43:43.212000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3wtr9/test_post/\", \"title\": \"Test post\"}, \"t3_z3wv0z\": {\"date\": \"2022-11-24T22:45:18.381000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3wv0z/test_post/\", \"title\": \"Test post\"}, \"t3_z3x7gi\": {\"date\": \"2022-11-24T23:00:51.261000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3x7gi/test_post/\", \"title\": \"Test post\"}, \"t3_z3x64t\": {\"date\": \"2022-11-24T22:59:35.632000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/z3x64t/test_post/\", \"title\": \"Test post\"}, \"t3_14lt78w\": {\"date\": \"2023-06-29T02:57:46.846000+00:00\", \"permalink\": \"https://www.reddit.com/r/<TEST_SUBREDDIT>/comments/14lt78w/hi/\", \"title\": \"hi\"}}, \"recentConvos\": {\"fjhla\": {\"date\": \"2020-07-16T01:15:55.263000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/fjhla\", \"id\": \"fjhla\", \"subject\": \"Spam\"}, \"1magps\": {\"date\": \"2023-07-11T20:18:46.102000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/1magps\", \"id\": \"1magps\", \"subject\": \"test\"}, \"1magq3\": {\"date\": \"2023-07-11T20:28:57.787000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/1magq3\", \"id\": \"1magq3\", \"subject\": \"test\"}, \"1l7pjk\": {\"date\": \"2023-06-25T17:16:07.135000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/1l7pjk\", \"id\": \"1l7pjk\", \"subject\": \"invitation to moderate /r/<TEST_SUBREDDIT>\"}, \"1mahha\": {\"date\": \"2023-07-11T20:33:14.617000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/1mahha\", \"id\": \"1mahha\", \"subject\": \"test\"}, \"19u06q\": {\"date\": \"2022-11-20T19:21:19.387000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/19u06q\", \"id\": \"19u06q\", \"subject\": \"invitation to moderate /r/<TEST_SUBREDDIT>\"}, \"1mahgy\": {\"date\": \"2023-07-11T20:32:00.840000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/1mahgy\", \"id\": \"1mahgy\", \"subject\": \"test\"}, \"fjhnq\": {\"date\": \"2020-07-16T01:15:07.219000+0000\", \"permalink\": \"https://mod.reddit.com/mail/perma/fjhnq\", \"id\": \"fjhnq\", \"subject\": \"Spam\"}}, \"id\": \"t2_75u2lqkb\"}, \"modActions\": {}}"
141+
},
142+
"headers": {
143+
"Accept-Ranges": [
144+
"bytes"
145+
],
146+
"Connection": [
147+
"keep-alive"
148+
],
149+
"Content-Length": [
150+
"5735"
151+
],
152+
"Date": [
153+
"Tue, 11 Jul 2023 20:33:14 GMT"
154+
],
155+
"NEL": [
156+
"{\"report_to\": \"w3-reporting-nel\", \"max_age\": 14400, \"include_subdomains\": false, \"success_fraction\": 1.0, \"failure_fraction\": 1.0}"
157+
],
158+
"Report-To": [
159+
"{\"group\": \"w3-reporting-nel\", \"max_age\": 14400, \"include_subdomains\": true, \"endpoints\": [{ \"url\": \"https://w3-reporting-nel.reddit.com/reports\" }]}, {\"group\": \"w3-reporting\", \"max_age\": 14400, \"include_subdomains\": true, \"endpoints\": [{ \"url\": \"https://w3-reporting.reddit.com/reports\" }]}, {\"group\": \"w3-reporting-csp\", \"max_age\": 14400, \"include_subdomains\": true, \"endpoints\": [{ \"url\": \"https://w3-reporting-csp.reddit.com/reports\" }]}"
160+
],
161+
"Server": [
162+
"snooserv"
163+
],
164+
"Strict-Transport-Security": [
165+
"max-age=31536000; includeSubdomains"
166+
],
167+
"Vary": [
168+
"accept-encoding"
169+
],
170+
"Via": [
171+
"1.1 varnish"
172+
],
173+
"X-Content-Type-Options": [
174+
"nosniff"
175+
],
176+
"X-Frame-Options": [
177+
"SAMEORIGIN"
178+
],
179+
"X-XSS-Protection": [
180+
"1; mode=block"
181+
],
182+
"cache-control": [
183+
"private, s-maxage=0, max-age=0, must-revalidate, no-store"
184+
],
185+
"content-type": [
186+
"application/json; charset=UTF-8"
187+
],
188+
"expires": [
189+
"-1"
190+
],
191+
"set-cookie": [
192+
"loid=000000000075u2lqkb.2.1593898363221.Z0FBQUFBQmtyYnlLd2NOTXNkaDdPRzNNU3NVZkdtbVlKNndTaHk2bWs2NjI0NXlqdHZEZlhTWGVhWHU3UVBOODJ2Y28ydXJqNG5Ydll4a0ZqbGxrT3ZzWkl1d1QzUzdWLURhZnZSemsxSFNyeG1lMGlOSDM2NVF3akw1bHNpd3A0VnFPeEFxbjFzWWQ; Domain=reddit.com; Max-Age=63071999; Path=/; expires=Thu, 10-Jul-2025 20:33:14 GMT; secure",
193+
"session_tracker=coofkkigherfoqbqdh.0.1689107594576.Z0FBQUFBQmtyYnlLdGM1Q1lGb2xvbFJWMUlnNUlHWDN4MjFZdldXZUhLUEZmT3NEdVAxVy1YTzd0c2EzQjNsZ215SEpxYlRXdlhMU0o1UWlpbHZreTBGNUdvd1lEOGI2RWNzZmRmMktFTzQ1S1R4bG0xcVRIMmFfVHpGYnc3dXBoRHhEUmtkLTk3TGg; Domain=reddit.com; Max-Age=7199; Path=/; expires=Tue, 11-Jul-2023 22:33:14 GMT; secure"
194+
],
195+
"x-moose": [
196+
"majestic"
197+
],
198+
"x-ratelimit-remaining": [
199+
"995"
200+
],
201+
"x-ratelimit-reset": [
202+
"406"
203+
],
204+
"x-ratelimit-used": [
205+
"1"
206+
]
207+
},
208+
"status": {
209+
"code": 201,
210+
"message": "Created"
211+
},
212+
"url": "https://oauth.reddit.com/api/mod/conversations/1mahha?raw_json=1"
213+
}
214+
}
215+
],
216+
"recorded_with": "betamax/0.8.1"
217+
}

tests/integration/models/reddit/test_modmail.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def test_reply(self, reddit):
5555
reply = conversation.reply(body="A message")
5656
assert isinstance(reply, ModmailMessage)
5757

58+
def test_reply__internal(self, reddit):
59+
reddit.read_only = False
60+
conversation = reddit.subreddit("all").modmail("1mahha")
61+
reply = conversation.reply(internal=True, body="A message")
62+
assert isinstance(reply, ModmailMessage)
63+
5864
def test_unarchive(self, reddit):
5965
reddit.read_only = False
6066
conversation = reddit.subreddit("all").modmail("ik72")

0 commit comments

Comments
 (0)