1+ package io .getstream .client ;
2+
3+ import static org .junit .Assert .*;
4+
5+ import com .google .common .collect .ImmutableMap ;
6+ import io .getstream .core .models .*;
7+ import java .util .*;
8+ import org .junit .Before ;
9+ import org .junit .Test ;
10+
11+ public class ModerationTemplateUpdateTest {
12+
13+ // Using the provided API credentials
14+ private static final String apiKey = "mfajp2wqsk9e" ;
15+ private static final String secret = "9kve7vf7jdyqqxnqfqq8beq7vqvhk8z526kvqj8nd6uaenj6cns3v2psdnwucm78" ;
16+
17+ private Client client ;
18+
19+ @ Before
20+ public void setUp () throws Exception {
21+ client = Client .builder (apiKey , secret ).build ();
22+ }
23+
24+ @ Test
25+ public void testUpdateWithDifferentModerationTemplate () throws Exception {
26+ // Create an activity with an initial moderation template
27+ String foreignID = "mod-template-switch-test-" + System .currentTimeMillis ();
28+ Activity activity =
29+ Activity .builder ()
30+ .actor ("stream-test-user" )
31+ .verb ("post" )
32+ .object ("test-object" )
33+ .foreignID (foreignID )
34+ .time (new Date ())
35+ // .moderationTemplate("activity-feed-mod-temp") // Initial template
36+ .moderationTemplate ("reaction-template" ) // Initial template
37+ // .extraField("message", "fuck off")//bad text
38+ .extraField ("message" , "fuckdoff" )//good text
39+ .build ();
40+
41+ // Add the activity to a test feed
42+ FlatFeed feed = client .flatFeed ("user" , "test-user" );
43+ Activity createdActivity = feed .addActivity (activity ).join ();
44+ assertNotNull (createdActivity );
45+
46+ // Get the initial moderation state
47+ // ModerationResponse initialModeration = createdActivity.getModerationResponse();
48+ // System.out.println("Initial moderation template: " + createdActivity.getModerationTemplate());
49+ // if (initialModeration != null) {
50+ // System.out.println("Initial moderation status: " + initialModeration.getStatus());
51+ // System.out.println("Initial moderation action: " + initialModeration.getRecommendedAction());
52+ // }
53+
54+ try {
55+ // Now update the activity with a different moderation template
56+ // Also add a problematic text to trigger moderation
57+ Map <String , Object > set = new ImmutableMap .Builder <String , Object >()
58+ .put ("message" , "fuckffoff" ) // Text that should trigger moderation
59+ .put ("moderation_template" , "activity-feed-mod-temp" ) // Using a different template name
60+ .build ();
61+
62+ // Perform partial update by foreign ID
63+ // Activity updatedActivity = client
64+ // .updateActivityByForeignID(//mod-template-switch-test-1744901529170
65+ // new ForeignIDTimePair(foreignID, createdActivity.getTime()),
66+ // set,
67+ // Collections.emptyList())
68+ // .join();
69+ Activity updatedActivity = client
70+ .updateActivityByID (//mod-template-switch-test-1744901529170
71+ createdActivity .getID (),
72+ set ,
73+ Collections .emptyList ())
74+ .join ();
75+
76+ // Verify the update worked
77+ assertNotNull (updatedActivity );
78+ assertEquals (foreignID , updatedActivity .getForeignID ());
79+ // assertEquals("pissoar", updatedActivity.getExtra().get("text"));
80+
81+ // Check if moderation template was updated
82+ System .out .println ("Updated moderation template: " + updatedActivity .getModerationTemplate ());
83+
84+ // Check if moderation response came back with the update
85+ ModerationResponse updateModeration = updatedActivity .getModerationResponse ();
86+ if (updateModeration != null ) {
87+ System .out .println ("Update response moderation status: " + updateModeration .getStatus ());
88+ System .out .println ("Update response moderation action: " + updateModeration .getRecommendedAction ());
89+ }
90+
91+ // Wait a moment for moderation to complete
92+ Thread .sleep (1000 );
93+
94+ // Get the full activity after update to check moderation
95+ List <Activity > activities = feed .getActivities ().join ();
96+ Activity fetchedActivity = null ;
97+ for (Activity act : activities ) {
98+ if (foreignID .equals (act .getForeignID ())) {
99+ fetchedActivity = act ;
100+ break ;
101+ }
102+ }
103+
104+ assertNotNull ("Could not find the updated activity in feed" , fetchedActivity );
105+
106+ // Check if moderation template was updated
107+ System .out .println ("Fetched moderation template: " + fetchedActivity .getModerationTemplate ());
108+
109+ // Check if moderation was applied
110+ ModerationResponse moderationResponse = fetchedActivity .getModerationResponse ();
111+ if (moderationResponse != null ) {
112+ assertNotNull ("Moderation status should not be null" , moderationResponse .getStatus ());
113+ assertNotNull ("Recommended action should not be null" , moderationResponse .getRecommendedAction ());
114+ System .out .println ("Fetched moderation status: " + moderationResponse .getStatus ());
115+ System .out .println ("Fetched recommended action: " + moderationResponse .getRecommendedAction ());
116+ }
117+ } finally {
118+ // Clean up
119+ feed .removeActivityByForeignID (foreignID ).join ();
120+ }
121+ }
122+
123+ @ Test
124+ public void testBatchUpdateWithDifferentModerationTemplate () throws Exception {
125+ // Create an activity with an initial moderation template
126+ String foreignID = "batch-template-switch-test-" + System .currentTimeMillis ();
127+ Activity activity =
128+ Activity .builder ()
129+ .actor ("test-user" )
130+ .verb ("post" )
131+ .object ("test-object" )
132+ .foreignID (foreignID )
133+ .time (new Date ())
134+ .moderationTemplate ("moderation_template_activity" ) // Initial template
135+ .extraField ("text" , "This is a safe text" )
136+ .build ();
137+
138+ // Add the activity to a test feed
139+ FlatFeed feed = client .flatFeed ("user" , "test-user" );
140+ Activity createdActivity = feed .addActivity (activity ).join ();
141+ assertNotNull (createdActivity );
142+
143+ try {
144+ // Create an update that changes the moderation template
145+ ActivityUpdate update = ActivityUpdate .builder ()
146+ .foreignID (foreignID )
147+ .time (createdActivity .getTime ())
148+ .set (ImmutableMap .of (
149+ "text" , "pissoar" ,
150+ "moderation_template" , "different_moderation_template" )) // Using a different template name
151+ .unset (Collections .emptyList ())
152+ .build ();
153+
154+ try {
155+ // Perform the batch update
156+ List <Activity > updatedActivities = client .updateActivitiesByForeignID (update ).join ();
157+ assertNotNull (updatedActivities );
158+ assertFalse (updatedActivities .isEmpty ());
159+
160+ // Get the updated activity
161+ Activity updatedActivity = updatedActivities .get (0 );
162+ assertEquals (foreignID , updatedActivity .getForeignID ());
163+
164+ // Check if moderation template was updated in response
165+ if (updatedActivity .getModerationTemplate () != null ) {
166+ System .out .println ("Batch updated moderation template: " + updatedActivity .getModerationTemplate ());
167+ }
168+
169+ // Check moderation if it's there
170+ ModerationResponse moderationResponse = updatedActivity .getModerationResponse ();
171+ if (moderationResponse != null ) {
172+ System .out .println ("Batch update moderation status: " + moderationResponse .getStatus ());
173+ System .out .println ("Batch update moderation action: " + moderationResponse .getRecommendedAction ());
174+ }
175+ } catch (Exception e ) {
176+ // If the batch update fails, log the error
177+ System .out .println ("Batch update failed: " + e .getMessage ());
178+ }
179+
180+ // Wait a moment for moderation to complete
181+ Thread .sleep (1000 );
182+
183+ // Verify the activity in the feed
184+ List <Activity > activities = feed .getActivities ().join ();
185+ Activity fetchedActivity = null ;
186+ for (Activity act : activities ) {
187+ if (foreignID .equals (act .getForeignID ())) {
188+ fetchedActivity = act ;
189+ break ;
190+ }
191+ }
192+
193+ assertNotNull ("Could not find the updated activity in feed" , fetchedActivity );
194+ assertEquals ("pissoar" , fetchedActivity .getExtra ().get ("text" ));
195+
196+ // Check the moderation template update in the fetched activity
197+ System .out .println ("Fetched batch moderation template: " + fetchedActivity .getModerationTemplate ());
198+
199+ // Check the moderation response in the fetched activity
200+ ModerationResponse fetchedModeration = fetchedActivity .getModerationResponse ();
201+ if (fetchedModeration != null ) {
202+ System .out .println ("Fetched batch moderation status: " + fetchedModeration .getStatus ());
203+ System .out .println ("Fetched batch moderation action: " + fetchedModeration .getRecommendedAction ());
204+ }
205+ } finally {
206+ // Clean up
207+ feed .removeActivityByForeignID (foreignID ).join ();
208+ }
209+ }
210+ }
0 commit comments