1414require 'stream-chat/version'
1515require 'stream-chat/util'
1616require 'stream-chat/types'
17+ require 'stream-chat/moderation'
1718
1819module StreamChat
1920 DEFAULT_BLOCKLIST = 'profanity_en_2020_v1'
2021 SOFT_DELETE = 'soft'
2122 HARD_DELETE = 'hard'
2223
23- # ModerationV2 class provides all the endpoints related to moderation v2
24- class ModerationV2
25- extend T ::Sig
26-
27- MODERATION_ENTITY_TYPES = {
28- user : 'stream:user' ,
29- message : 'stream:chat:v1:message'
30- } . freeze
31-
32- sig { params ( client : Client ) . void }
33- def initialize ( client )
34- @client = client
35- end
36-
37- # Flags a user with a reason
38- #
39- # @param [string] flagged_user_id User ID to be flagged
40- # @param [string] reason Reason for flagging the user
41- # @param [Hash] options Additional options for flagging the user
42- # @option options [String] :user_id User ID of the user who is flagging the target user
43- # @option options [Hash] :custom Additional data to be stored with the flag
44- sig { params ( flagged_user_id : String , reason : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
45- def flag_user ( flagged_user_id , reason , **options )
46- flag ( MODERATION_ENTITY_TYPES [ :user ] , flagged_user_id , '' , reason , **options )
47- end
48-
49- # Flags a message with a reason
50- #
51- # @param [string] message_id Message ID to be flagged
52- # @param [string] reason Reason for flagging the message
53- # @param [Hash] options Additional options for flagging the message
54- # @option options [String] :user_id User ID of the user who is flagging the target message
55- # @option options [Hash] :custom Additional data to be stored with the flag
56- sig { params ( message_id : String , reason : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
57- def flag_message ( message_id , reason , **options )
58- flag ( MODERATION_ENTITY_TYPES [ :message ] , message_id , '' , reason , **options )
59- end
60-
61- # Flags an entity with a reason
62- #
63- # @param [string] entity_type Entity type to be flagged
64- # @param [string] entity_id Entity ID to be flagged
65- # @param [string] entity_creator_id User ID of the entity creator
66- # @param [string] reason Reason for flagging the entity
67- # @param [Hash] options Additional options for flagging the entity
68- # @option options [String] :user_id User ID of the user who is flagging the target entity
69- # @option options [Hash] :moderation_payload Content to be flagged
70- # @option options [Hash] :custom Additional data to be stored with the flag
71- sig { params ( entity_type : String , entity_id : String , entity_creator_id : String , reason : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
72- def flag ( entity_type , entity_id , entity_creator_id , reason , **options )
73- @client . post ( 'api/v2/moderation/flag' , data : {
74- entity_type : entity_type ,
75- entity_id : entity_id ,
76- entity_creator_id : entity_creator_id ,
77- reason : reason ,
78- **options
79- } )
80- end
81-
82- # Mutes a user
83- #
84- # @param [string] target_id User ID to be muted
85- # @param [Hash] options Additional options for muting the user
86- # @option options [String] :user_id User ID of the user who is muting the target user
87- # @option options [Integer] :timeout Timeout for the mute in minutes
88- sig { params ( target_id : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
89- def mute_user ( target_id , **options )
90- @client . post ( 'api/v2/moderation/mute' , data : {
91- target_ids : [ target_id ] ,
92- **options
93- } )
94- end
95-
96- # Unmutes a user
97- #
98- # @param [string] target_id User ID to be unmuted
99- # @param [Hash] options Additional options for unmuting the user
100- # @option options [String] :user_id User ID of the user who is unmuting the target user
101- sig { params ( target_id : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
102- def unmute_user ( target_id , **options )
103- @client . post ( 'api/v2/moderation/unmute' , data : {
104- target_ids : [ target_id ] ,
105- **options
106- } )
107- end
108-
109- # Gets moderation report for a user
110- #
111- # @param [string] user_id User ID for which moderation report is to be fetched
112- # @param [Hash] options Additional options for fetching the moderation report
113- # @option options [Boolean] :create_user_if_not_exists Create user if not exists
114- # @option options [Boolean] :include_user_blocks Include user blocks
115- # @option options [Boolean] :include_user_mutes Include user mutes
116- sig { params ( user_id : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
117- def get_user_moderation_report ( user_id , **options )
118- @client . get ( 'api/v2/moderation/user_report' , params : {
119- user_id : user_id ,
120- **options
121- } )
122- end
123-
124- # Queries review queue
125- #
126- # @param [Hash] filter_conditions Filter conditions for querying review queue
127- # @param [Array] sort Sort conditions for querying review queue
128- # @param [Hash] options Pagination options for querying review queue
129- sig { params ( filter_conditions : T . untyped , sort : T . untyped , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
130- def query_review_queue ( filter_conditions = { } , sort = [ ] , **options )
131- @client . post ( 'api/v2/moderation/review_queue' , data : {
132- filter : filter_conditions ,
133- sort : StreamChat . get_sort_fields ( sort ) ,
134- **options
135- } )
136- end
137-
138- # Upserts moderation config
139- #
140- # @param [Hash] config Moderation config to be upserted
141- sig { params ( config : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
142- def upsert_config ( config )
143- @client . post ( 'api/v2/moderation/config' , data : config )
144- end
145-
146- # Gets moderation config
147- #
148- # @param [string] key Key for which moderation config is to be fetched
149- # @param [Hash] data Additional data
150- # @option data [String] :team Team name
151- sig { params ( key : String , data : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
152- def get_config ( key , data = { } )
153- @client . get ( "api/v2/moderation/config/#{ key } " , params : data )
154- end
155-
156- # Deletes moderation config
157- #
158- # @param [string] key Key for which moderation config is to be deleted
159- # @param [Hash] data Additional data
160- # @option data [String] :team Team name
161- sig { params ( key : String , data : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
162- def delete_config ( key , data = { } )
163- @client . delete ( "api/v2/moderation/config/#{ key } " , params : data )
164- end
165-
166- # Queries moderation configs
167- #
168- # @param [Hash] filter_conditions Filter conditions for querying moderation configs
169- # @param [Array] sort Sort conditions for querying moderation configs
170- # @param [Hash] options Additional options for querying moderation configs
171- sig { params ( filter_conditions : T . untyped , sort : T . untyped , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
172- def query_configs ( filter_conditions , sort , **options )
173- @client . post ( 'api/v2/moderation/configs' , data : {
174- filter : filter_conditions ,
175- sort : sort ,
176- **options
177- } )
178- end
179-
180- # Submits a moderation action
181- #
182- # @param [string] action_type Type of action to submit
183- # @param [string] item_id ID of the item to submit action for
184- # @param [Hash] options Additional options for submitting the action
185- sig { params ( action_type : String , item_id : String , options : T . untyped ) . returns ( StreamChat ::StreamResponse ) }
186- def submit_action ( action_type , item_id , **options )
187- @client . post ( 'api/v2/moderation/submit_action' , data : {
188- action_type : action_type ,
189- item_id : item_id ,
190- **options
191- } )
192- end
193-
194- # Checks content for moderation
195- #
196- # @param [string] entity_type Type of entity to be checked
197- # @param [string] entity_id ID of the entity to be checked
198- # @param [string] entity_creator_id ID of the entity creator
199- # @param [Hash] moderation_payload Content to be checked for moderation
200- # @param [string] config_key Key of the moderation config to use
201- # @param [Hash] options Additional options
202- # @option options [Boolean] :force_sync Force synchronous check
203- 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 ) }
204- def check ( params = { } )
205- entity_type = params [ :entity_type ]
206- entity_id = params [ :entity_id ]
207- entity_creator_id = params [ :entity_creator_id ]
208- moderation_payload = params [ :moderation_payload ]
209- config_key = params [ :config_key ]
210- options = params [ :options ] || { }
211-
212- @client . post ( 'api/v2/moderation/check' , data : {
213- entity_type : entity_type ,
214- entity_id : entity_id ,
215- entity_creator_id : entity_creator_id ,
216- moderation_payload : moderation_payload ,
217- config_key : config_key ,
218- options : options
219- } )
220- end
221-
222- # Adds custom flags to an entity
223- #
224- # @param [string] entity_type Type of entity to be checked
225- # @param [string] entity_id ID of the entity to be checked
226- # @param [string] entity_creator_id ID of the entity creator
227- # @param [Hash] moderation_payload Content to be checked for moderation
228- # @param [Array] flags Array of custom flags to add
229- sig { params ( entity_type : String , entity_id : String , entity_creator_id : String , moderation_payload : T . untyped , flags : T ::Array [ T . untyped ] ) . returns ( StreamChat ::StreamResponse ) }
230- def add_custom_flags ( entity_type , entity_id , entity_creator_id , moderation_payload , flags )
231- @client . post ( 'api/v2/moderation/custom_check' , data : {
232- entity_type : entity_type ,
233- entity_id : entity_id ,
234- entity_creator_id : entity_creator_id ,
235- moderation_payload : moderation_payload ,
236- flags : flags
237- } )
238- end
239-
240- # Adds custom flags to a message
241- #
242- # @param [string] message_id Message ID to be flagged
243- # @param [Array] flags Array of custom flags to add
244- sig { params ( message_id : String , flags : T ::Array [ T . untyped ] ) . returns ( StreamChat ::StreamResponse ) }
245- def add_custom_message_flags ( message_id , flags )
246- add_custom_flags ( MODERATION_ENTITY_TYPES [ :message ] , message_id , '' , { } , flags )
247- end
248- end
249-
25024 class Client
25125 extend T ::Sig
25226
@@ -262,7 +36,7 @@ class Client
26236 sig { returns ( Faraday ::Connection ) }
26337 attr_reader :conn
26438
265- sig { returns ( ModerationV2 ) }
39+ sig { returns ( Moderation ) }
26640 attr_reader :moderation
26741
26842 # initializes a Stream Chat API Client
@@ -294,7 +68,7 @@ def initialize(api_key, api_secret, timeout = nil, **options)
29468 end
29569 end
29670 @conn = T . let ( conn , Faraday ::Connection )
297- @moderation = T . let ( ModerationV2 . new ( self ) , ModerationV2 )
71+ @moderation = T . let ( Moderation . new ( self ) , Moderation )
29872 end
29973
30074 # initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET
0 commit comments