Skip to content

Commit 4f47b5d

Browse files
Nekuromentotbarbugli
authored andcommitted
Add flag and unflag methods (#2)
* Bump version. Add flag and unflag methods * Add flag/unflag tests for messages and users * Use new user_id parameter in tests
1 parent c1a43c1 commit 4f47b5d

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

stream_chat/__pkg__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
__author__ = "Tommaso Barbugli"
22
__copyright__ = "Copyright 2019, Stream.io, Inc"
3-
__version__ = "0.4.0"
3+
__version__ = "0.4.1"
44
__maintainer__ = "Tommaso Barbugli"
55
__email__ = "[email protected]"
66
__status__ = "Production"
7-

stream_chat/channel.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def get_reactions(self, message_id, **options):
189189
"""
190190
return self.client.get("messages/{}/reactions".format(message_id), params=options)
191191

192-
def ban_user(self, user_id, **options):
192+
def ban_user(self, target_id, **options):
193193
"""
194194
Bans a user from this channel
195195
@@ -198,17 +198,19 @@ def ban_user(self, user_id, **options):
198198
:return: The server response
199199
"""
200200
return self.client.ban_user(
201-
user_id, type=self.channel_type, id=self.id, **options
201+
target_id, type=self.channel_type, id=self.id, **options
202202
)
203203

204-
def unban_user(self, user_id):
204+
def unban_user(self, target_id, **options):
205205
"""
206206
Removes the ban for a user on this channel
207207
208208
:param user_id: the ID of the user to unban
209209
:return: The server response
210210
"""
211-
return self.client.unban_user(user_id, type=self.channel_type, id=self.id)
211+
return self.client.unban_user(
212+
target_id, type=self.channel_type, id=self.id, **options
213+
)
212214

213215
def accept_invite(self, user_id):
214216
raise NotImplementedError

stream_chat/client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@ def unban_user(self, target_id, **options):
124124
params.update(options)
125125
return self.delete("moderation/ban", params)
126126

127+
def flag_message(self, target_id, **options):
128+
data = dict(target_message_id=target_id)
129+
data.update(options)
130+
return self.post("moderation/flag", data=data)
131+
132+
def unflag_message(self, target_id, **options):
133+
data = dict(target_message_id=target_id)
134+
data.update(options)
135+
return self.post("moderation/unflag", data=data)
136+
137+
def flag_user(self, target_id, **options):
138+
data = dict(target_user_id=target_id)
139+
data.update(options)
140+
return self.post("moderation/flag", data=data)
141+
142+
def unflag_user(self, target_id, **options):
143+
data = dict(target_user_id=target_id)
144+
data.update(options)
145+
return self.post("moderation/unflag", data=data)
146+
127147
def mute_user(self, target_id, user_id):
128148
"""
129149
Create a mute

stream_chat/tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ def random_user(client):
3737
return user
3838

3939

40+
@pytest.fixture(scope="function")
41+
def server_user(client):
42+
user = {"id": str(uuid.uuid4())}
43+
response = client.update_user(user)
44+
assert "users" in response
45+
assert user["id"] in response["users"]
46+
return user
47+
48+
4049
@pytest.fixture(scope="function")
4150
def random_users(client):
4251
user1 = {"id": str(uuid.uuid4())}

stream_chat/tests/test_channel.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
@pytest.mark.incremental
55
class TestChannel(object):
6-
def test_ban_user(self, channel, random_user):
7-
channel.ban_user(random_user["id"])
6+
def test_ban_user(self, channel, random_user, server_user):
7+
channel.ban_user(random_user["id"], user_id=server_user["id"])
88
channel.ban_user(
99
random_user["id"],
1010
timeout=3600,
1111
reason="offensive language is not allowed here",
12+
user_id=server_user["id"],
1213
)
1314
channel.unban_user(random_user["id"])
1415

stream_chat/tests/test_client.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,19 @@ def test_export_user(self, client, fellowship_of_the_ring):
6363
assert "user" in response
6464
assert response["user"]["name"] == "Gandalf the Grey"
6565

66-
def test_ban_user(self, client, random_user):
67-
client.ban_user(random_user["id"])
66+
def test_ban_user(self, client, random_user, server_user):
67+
client.ban_user(random_user["id"], user_id=server_user["id"])
6868

69-
def test_unban_user(self, client, random_user):
70-
client.ban_user(random_user["id"])
71-
client.unban_user(random_user["id"])
69+
def test_unban_user(self, client, random_user, server_user):
70+
client.ban_user(random_user["id"], user_id=server_user["id"])
71+
client.unban_user(random_user["id"], user_id=server_user["id"])
72+
73+
def test_flag_user(self, client, random_user, server_user):
74+
client.flag_user(random_user["id"], user_id=server_user["id"])
75+
76+
def test_unflag_user(self, client, random_user, server_user):
77+
client.flag_user(random_user["id"], user_id=server_user["id"])
78+
client.unflag_user(random_user["id"], user_id=server_user["id"])
7279

7380
def test_mark_all_read(self, client, random_user):
7481
client.mark_all_read(random_user["id"])
@@ -93,6 +100,17 @@ def test_delete_message(self, client, channel, random_user):
93100
channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"])
94101
client.delete_message(msg_id)
95102

103+
def test_flag_message(self, client, channel, random_user, server_user):
104+
msg_id = str(uuid.uuid4())
105+
channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"])
106+
client.flag_message(msg_id, user_id=server_user["id"])
107+
108+
def test_unflag_message(self, client, channel, random_user, server_user):
109+
msg_id = str(uuid.uuid4())
110+
channel.send_message({"id": msg_id, "text": "helloworld"}, random_user["id"])
111+
client.flag_message(msg_id, user_id=server_user["id"])
112+
client.unflag_message(msg_id, user_id=server_user["id"])
113+
96114
def test_query_users_young_hobbits(self, client, fellowship_of_the_ring):
97115
response = client.query_users({"race": {"$eq": "Hobbit"}}, {"age": -1})
98116
assert len(response["users"]) == 4

0 commit comments

Comments
 (0)