Skip to content

Commit eadf16f

Browse files
authored
Merge pull request #172 from MerleLiuKun/feat-block-dm
Feat block dm
2 parents 4a0885a + 4eb0311 commit eadf16f

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

pytwitter/api.py

+26
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,32 @@ def unblock_user(self, user_id: str, target_user_id: str) -> dict:
20352035
data = self._parse_response(resp)
20362036
return data
20372037

2038+
def block_user_dm(self, target_user_id: str):
2039+
"""
2040+
Allows the authenticated user to block direct messages (DMs) from the target user.
2041+
:param target_user_id: target user id
2042+
:return: blocked status
2043+
"""
2044+
resp = self._request(
2045+
url=f"{self.BASE_URL_V2}/users/{target_user_id}/dm/block",
2046+
verb="POST",
2047+
)
2048+
data = self._parse_response(resp)
2049+
return data
2050+
2051+
def unblock_user_dm(self, target_user_id: str):
2052+
"""
2053+
Allows the authenticated user to unblock direct messages (DMs) from the target user.
2054+
:param target_user_id: target user id
2055+
:return: unblocked status
2056+
"""
2057+
resp = self._request(
2058+
url=f"{self.BASE_URL_V2}/users/{target_user_id}/dm/unblock",
2059+
verb="POST",
2060+
)
2061+
data = self._parse_response(resp)
2062+
return data
2063+
20382064
def get_user_muting(
20392065
self,
20402066
user_id: str,

tests/apis/test_users.py

+21
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,27 @@ def test_block_and_unblock_user(api_with_user):
153153
assert not resp["data"]["blocking"]
154154

155155

156+
@responses.activate
157+
def test_block_and_unblock_user_dm(api_with_user):
158+
target_user_id = "123456"
159+
160+
responses.add(
161+
method=responses.POST,
162+
url=f"https://api.twitter.com/2/users/{target_user_id}/dm/block",
163+
json={"data": {"blocked": True}},
164+
)
165+
resp = api_with_user.block_user_dm(target_user_id=target_user_id)
166+
assert resp["data"]["blocked"]
167+
168+
responses.add(
169+
method=responses.POST,
170+
url=f"https://api.twitter.com/2/users/{target_user_id}/dm/unblock",
171+
json={"data": {"blocked": False}},
172+
)
173+
resp = api_with_user.unblock_user_dm(target_user_id=target_user_id)
174+
assert not resp["data"]["blocked"]
175+
176+
156177
@responses.activate
157178
def test_get_blocking_users(api_with_user, helpers):
158179
users_data = helpers.load_json_data(

0 commit comments

Comments
 (0)