Skip to content

Commit a3160cc

Browse files
authored
[MOD-531]: check_user_profile support for Ruby SDK (#167)
* add userprofile to moderation entity types * add check_user_profile function * add tests * silence C: Naming/PredicateMethod
1 parent cdad3b8 commit a3160cc

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ Style/FrozenStringLiteralComment:
3535

3636
Gemspec/RequireMFA:
3737
Enabled: false
38+
Naming/PredicateMethod:
39+
Enabled: false

lib/stream-chat/moderation.rb

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class Moderation
1414
MODERATION_ENTITY_TYPES = T.let(
1515
{
1616
user: 'stream:user',
17-
message: 'stream:chat:v1:message'
17+
message: 'stream:chat:v1:message',
18+
userprofile: 'stream:v1:user_profile'
1819
}.freeze,
1920
T::Hash[Symbol, String]
2021
)
@@ -24,6 +25,48 @@ def initialize(client)
2425
@client = client
2526
end
2627

28+
# Experimental: Check user profile
29+
#
30+
# Warning: This is an experimental feature and the API is subject to change.
31+
#
32+
# This function is used to check a user profile for moderation.
33+
# This will not create any review queue items for the user profile.
34+
# You can just use this to check whether to allow a certain user profile to be created or not.
35+
#
36+
# @param [string] user_id User ID to be checked
37+
# @param [Hash] profile Profile data to be checked
38+
# @option profile [String] :username Username to be checked
39+
# @option profile [String] :image Image URL to be checked
40+
# @return [StreamChat::StreamResponse]
41+
#
42+
# example:
43+
# client.moderation.check_user_profile('user-id', {username: 'bad_username', image: 'https://example.com/profile.jpg'})
44+
sig do
45+
params(
46+
user_id: String,
47+
profile: T::Hash[Symbol, T.nilable(String)]
48+
).returns(StreamChat::StreamResponse)
49+
end
50+
def check_user_profile(user_id, profile)
51+
raise ArgumentError, 'Either username or image must be provided' if profile[:username].nil? && profile[:image].nil?
52+
53+
moderation_payload = {}
54+
moderation_payload[:texts] = [profile[:username]] if profile[:username]
55+
moderation_payload[:images] = [profile[:image]] if profile[:image]
56+
57+
check(
58+
T.must(MODERATION_ENTITY_TYPES[:userprofile]),
59+
user_id,
60+
moderation_payload,
61+
'user_profile:default',
62+
entity_creator_id: user_id,
63+
options: {
64+
force_sync: true,
65+
test_mode: true
66+
}
67+
)
68+
end
69+
2770
# Flags a user with a reason
2871
#
2972
# @param [string] flagged_user_id User ID to be flagged

spec/moderation_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ def loop_times(times)
159159
expect(response['duration']).not_to be_nil
160160
end
161161

162+
it 'check user profile' do
163+
response = @moderation.check_user_profile(
164+
@test_user_id,
165+
{ username: 'fuck_you_123' }
166+
)
167+
expect(response['duration']).not_to be_nil
168+
expect(response['status']).to eq('complete')
169+
expect(response['recommended_action']).to eq('remove')
170+
171+
response = @moderation.check_user_profile(
172+
@test_user_id,
173+
{ username: 'hi' }
174+
)
175+
expect(response['duration']).not_to be_nil
176+
expect(response['status']).to eq('complete')
177+
expect(response['recommended_action']).to eq('keep')
178+
end
179+
162180
it 'config test' do
163181
# Create moderation config
164182
moderation_config = {

0 commit comments

Comments
 (0)