Skip to content

Commit 6c1d51f

Browse files
committed
Add Moderation v2 API Support
1 parent ac11fc1 commit 6c1d51f

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed

lib/stream-chat/client.rb

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,226 @@ module StreamChat
2020
SOFT_DELETE = 'soft'
2121
HARD_DELETE = 'hard'
2222

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(entity_type, entity_id, entity_creator_id, moderation_payload, config_key, **options)
205+
@client.post('api/v2/moderation/check', data: {
206+
entity_type: entity_type,
207+
entity_id: entity_id,
208+
entity_creator_id: entity_creator_id,
209+
moderation_payload: moderation_payload,
210+
config_key: config_key,
211+
options: options
212+
})
213+
end
214+
215+
# Adds custom flags to an entity
216+
#
217+
# @param [string] entity_type Type of entity to be checked
218+
# @param [string] entity_id ID of the entity to be checked
219+
# @param [string] entity_creator_id ID of the entity creator
220+
# @param [Hash] moderation_payload Content to be checked for moderation
221+
# @param [Array] flags Array of custom flags to add
222+
sig { params(entity_type: String, entity_id: String, entity_creator_id: String, moderation_payload: T.untyped, flags: T::Array[T.untyped]).returns(StreamChat::StreamResponse) }
223+
def add_custom_flags(entity_type, entity_id, entity_creator_id, moderation_payload, flags)
224+
@client.post('api/v2/moderation/custom_check', data: {
225+
entity_type: entity_type,
226+
entity_id: entity_id,
227+
entity_creator_id: entity_creator_id,
228+
moderation_payload: moderation_payload,
229+
flags: flags
230+
})
231+
end
232+
233+
# Adds custom flags to a message
234+
#
235+
# @param [string] message_id Message ID to be flagged
236+
# @param [Array] flags Array of custom flags to add
237+
sig { params(message_id: String, flags: T::Array[T.untyped]).returns(StreamChat::StreamResponse) }
238+
def add_custom_message_flags(message_id, flags)
239+
add_custom_flags(MODERATION_ENTITY_TYPES[:message], message_id, '', {}, flags)
240+
end
241+
end
242+
23243
class Client
24244
extend T::Sig
25245

@@ -35,6 +255,9 @@ class Client
35255
sig { returns(Faraday::Connection) }
36256
attr_reader :conn
37257

258+
sig { returns(ModerationV2) }
259+
attr_reader :moderation
260+
38261
# initializes a Stream Chat API Client
39262
#
40263
# @param [string] api_key your application api_key
@@ -64,6 +287,7 @@ def initialize(api_key, api_secret, timeout = nil, **options)
64287
end
65288
end
66289
@conn = T.let(conn, Faraday::Connection)
290+
@moderation = T.let(ModerationV2.new(self), ModerationV2)
67291
end
68292

69293
# initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET

0 commit comments

Comments
 (0)