Skip to content

Commit 614b0ed

Browse files
committed
add tests
1 parent 8101023 commit 614b0ed

File tree

2 files changed

+144
-12
lines changed

2 files changed

+144
-12
lines changed

lib/stream-chat/moderation.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,31 @@ def submit_action(action_type, item_id, **options)
178178
})
179179
end
180180

181+
# rubocop:disable Metrics/ParameterLists
181182
# Checks content for moderation
182183
#
183-
# @param [string] entity_type Type of entity to be checked
184-
# @param [string] entity_id ID of the entity to be checked
184+
# @param [string] entity_type Type of entity to be checked E.g., stream:user, stream:chat:v1:message, or any custom string
185+
# @param [string] entity_id ID of the entity to be checked. This is mainly for tracking purposes
185186
# @param [string] entity_creator_id ID of the entity creator
186187
# @param [Hash] moderation_payload Content to be checked for moderation
188+
# @option moderation_payload [Array<String>] :texts Array of texts to be checked for moderation
189+
# @option moderation_payload [Array<String>] :images Array of images to be checked for moderation
190+
# @option moderation_payload [Array<String>] :videos Array of videos to be checked for moderation
191+
# @option moderation_payload [Hash] :custom Additional custom data
187192
# @param [string] config_key Key of the moderation config to use
188193
# @param [Hash] options Additional options
189194
# @option options [Boolean] :force_sync Force synchronous check
190-
sig { params(entity_type: String, entity_id: String, entity_creator_id: String, moderation_payload: T.untyped, config_key: String, options: T.untyped).returns(StreamChat::StreamResponse) }
191-
def check(params = {})
192-
entity_type = params[:entity_type]
193-
entity_id = params[:entity_id]
194-
entity_creator_id = params[:entity_creator_id]
195-
moderation_payload = params[:moderation_payload]
196-
config_key = params[:config_key]
197-
options = params[:options] || {}
198-
195+
sig do
196+
params(
197+
entity_type: String,
198+
entity_id: String,
199+
entity_creator_id: String,
200+
moderation_payload: T::Hash[Symbol, T.any(T::Array[String], T::Hash[String, T.untyped])],
201+
config_key: String,
202+
options: T::Hash[Symbol, T::Boolean]
203+
).returns(StreamChat::StreamResponse)
204+
end
205+
def check(entity_type, entity_id, entity_creator_id, moderation_payload, config_key, options = {})
199206
@client.post('api/v2/moderation/check', data: {
200207
entity_type: entity_type,
201208
entity_id: entity_id,
@@ -205,7 +212,7 @@ def check(params = {})
205212
options: options
206213
})
207214
end
208-
215+
# rubocop:enable Metrics/ParameterLists
209216
# Adds custom flags to an entity
210217
#
211218
# @param [string] entity_type Type of entity to be checked

spec/client_spec.rb

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,129 @@ def loop_times(times)
872872
end
873873
end
874874
end
875+
876+
describe 'moderation' do
877+
before(:each) do
878+
@moderation = @client.moderation
879+
@test_user_id = SecureRandom.uuid
880+
@test_message_id = SecureRandom.uuid
881+
@test_config_key = SecureRandom.uuid
882+
end
883+
884+
it 'flagging a user and message' do
885+
msg_response = @channel.send_message({ id: @test_message_id, text: 'Test message' }, @test_user_id)
886+
expect(msg_response['message']['id']).to eq(@test_message_id)
887+
expect(msg_response['message']['user']['id']).to eq(@test_user_id)
888+
response = @moderation.flag_user(
889+
@test_user_id,
890+
'inappropriate_behavior',
891+
user_id: @random_user[:id],
892+
custom: { severity: 'high' }
893+
)
894+
expect(response['duration']).not_to be_nil
895+
response = @moderation.flag_message(
896+
@test_message_id,
897+
'inappropriate_content',
898+
user_id: @random_user[:id],
899+
custom: { category: 'spam' }
900+
)
901+
expect(response['duration']).not_to be_nil
902+
end
903+
904+
it 'mute a user and unmute a user' do
905+
@channel.send_message({ id: @test_message_id, text: 'Test message' }, @test_user_id)
906+
testuserid1 = @random_user[:id]
907+
response = @moderation.mute_user(
908+
@test_user_id,
909+
user_id: testuserid1,
910+
timeout: 60
911+
)
912+
expect(response['duration']).not_to be_nil
913+
expect(response['mutes'][0]['user']['id']).to eq(testuserid1)
914+
response = @moderation.unmute_user(
915+
@test_user_id,
916+
user_id: @random_user[:id]
917+
)
918+
expect(response['duration']).not_to be_nil
919+
920+
response = @moderation.get_user_moderation_report(
921+
@test_user_id,
922+
include_user_blocks: true,
923+
include_user_mutes: true
924+
)
925+
expect(response['duration']).not_to be_nil
926+
end
927+
928+
it 'adds custom flags to an entity' do
929+
testuserid1 = @random_user[:id]
930+
testmsgid1 = SecureRandom.uuid
931+
@channel.send_message({ id: testmsgid1, text: 'Test message' }, testuserid1)
932+
entity_type = 'stream:chat:v1:message'
933+
entity_id = testmsgid1
934+
entity_creator_id = testuserid1
935+
moderation_payload = {
936+
'texts' => ['Test message'],
937+
'custom' => { 'original_message_type' => 'regular' }
938+
}
939+
flags = [{ type: 'custom_check_text', value: 'test_flag' }]
940+
941+
response = @moderation.add_custom_flags(entity_type, entity_id, entity_creator_id, moderation_payload, flags)
942+
expect(response['duration']).not_to be_nil
943+
response = @moderation.add_custom_message_flags(
944+
testmsgid1,
945+
[{ type: 'custom_check_text', value: 'test_flag' }]
946+
)
947+
expect(response['duration']).not_to be_nil
948+
end
949+
950+
it 'config test' do
951+
blocklist_name = "blocklist-#{SecureRandom.uuid}"
952+
words = %w[pretty crazy]
953+
954+
# Create blocklist
955+
response = @client.create_blocklist(blocklist_name, words)
956+
expect(response['duration']).not_to be_nil
957+
958+
# Create moderation config
959+
moderation_config = {
960+
key: "chat:team:#{@channel.id}",
961+
block_list_config: {
962+
enabled: true,
963+
rules: [
964+
{
965+
name: response['blocklist']['name'],
966+
action: 'flag'
967+
}
968+
]
969+
}
970+
}
971+
@moderation.upsert_config(moderation_config)
972+
response = @moderation.get_config("chat:team:#{@channel.id}")
973+
expect(response['config']['key']).to eq("chat:team:#{@channel.id}")
974+
975+
response = @moderation.query_configs(
976+
{ key: "chat:team:#{@channel.id}" },
977+
[]
978+
)
979+
expect(response).not_to be_nil
980+
981+
# Send message that should be blocked
982+
983+
response = @channel.send_message(
984+
{ text: 'crazy game ever' },
985+
@random_user[:id],
986+
force_moderation: true
987+
)
988+
989+
# # Verify message appears in review queue
990+
queue_response = @moderation.query_review_queue(
991+
{ entity_type: 'stream:chat:v1:message' },
992+
{ created_at: -1 },
993+
limit: 1
994+
)
995+
expect(queue_response['items'][0]['entity_id']).to eq(response['message']['id'])
996+
997+
@moderation.delete_config("chat:team:#{@channel.id}")
998+
end
999+
end
8751000
end

0 commit comments

Comments
 (0)