Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ Style/FrozenStringLiteralComment:

Gemspec/RequireMFA:
Enabled: false
Naming/PredicateMethod:
Enabled: false
45 changes: 44 additions & 1 deletion lib/stream-chat/moderation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class Moderation
MODERATION_ENTITY_TYPES = T.let(
{
user: 'stream:user',
message: 'stream:chat:v1:message'
message: 'stream:chat:v1:message',
userprofile: 'stream:v1:user_profile'
}.freeze,
T::Hash[Symbol, String]
)
Expand All @@ -24,6 +25,48 @@ def initialize(client)
@client = client
end

# Experimental: Check user profile
#
# Warning: This is an experimental feature and the API is subject to change.
#
# This function is used to check a user profile for moderation.
# This will not create any review queue items for the user profile.
# You can just use this to check whether to allow a certain user profile to be created or not.
#
# @param [string] user_id User ID to be checked
# @param [Hash] profile Profile data to be checked
# @option profile [String] :username Username to be checked
# @option profile [String] :image Image URL to be checked
# @return [StreamChat::StreamResponse]
#
# example:
# client.moderation.check_user_profile('user-id', {username: 'bad_username', image: 'https://example.com/profile.jpg'})
sig do
params(
user_id: String,
profile: T::Hash[Symbol, T.nilable(String)]
).returns(StreamChat::StreamResponse)
end
def check_user_profile(user_id, profile)
raise ArgumentError, 'Either username or image must be provided' if profile[:username].nil? && profile[:image].nil?

moderation_payload = {}
moderation_payload[:texts] = [profile[:username]] if profile[:username]
moderation_payload[:images] = [profile[:image]] if profile[:image]

check(
T.must(MODERATION_ENTITY_TYPES[:userprofile]),
user_id,
moderation_payload,
'user_profile:default',
entity_creator_id: user_id,
options: {
force_sync: true,
test_mode: true
}
)
end

# Flags a user with a reason
#
# @param [string] flagged_user_id User ID to be flagged
Expand Down
18 changes: 18 additions & 0 deletions spec/moderation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ def loop_times(times)
expect(response['duration']).not_to be_nil
end

it 'check user profile' do
response = @moderation.check_user_profile(
@test_user_id,
{ username: 'fuck_you_123' }
)
expect(response['duration']).not_to be_nil
expect(response['status']).to eq('complete')
expect(response['recommended_action']).to eq('remove')

response = @moderation.check_user_profile(
@test_user_id,
{ username: 'hi' }
)
expect(response['duration']).not_to be_nil
expect(response['status']).to eq('complete')
expect(response['recommended_action']).to eq('keep')
end

it 'config test' do
# Create moderation config
moderation_config = {
Expand Down
Loading