|
| 1 | +package io.getstream; |
| 2 | + |
| 3 | +import io.getstream.models.*; |
| 4 | +import io.getstream.services.Feed; |
| 5 | +import io.getstream.services.Feeds; |
| 6 | +import io.getstream.services.ModerationImpl; |
| 7 | +import io.getstream.services.framework.StreamSDKClient; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import org.apache.commons.lang3.RandomStringUtils; |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +public class ModerationTest { |
| 18 | + static StreamSDKClient client; |
| 19 | + static ModerationImpl moderation; |
| 20 | + static Feeds feeds; |
| 21 | + static String testUserId; |
| 22 | + static String testUserId2; |
| 23 | + static String testModeratorId; |
| 24 | + static String testFeedId; |
| 25 | + static String testActivityId; |
| 26 | + |
| 27 | + @BeforeAll |
| 28 | + static void setup() throws Exception { |
| 29 | + client = new StreamSDKClient(); |
| 30 | + moderation = new ModerationImpl(client.getHttpClient()); |
| 31 | + feeds = client.feeds(); |
| 32 | + |
| 33 | + // Create test users |
| 34 | + testUserId = "test-user-" + RandomStringUtils.randomAlphanumeric(8); |
| 35 | + testUserId2 = "test-user-2-" + RandomStringUtils.randomAlphanumeric(8); |
| 36 | + testModeratorId = "moderator-" + RandomStringUtils.randomAlphanumeric(8); |
| 37 | + |
| 38 | + Map<String, UserRequest> usersMap = new HashMap<>(); |
| 39 | + usersMap.put( |
| 40 | + testUserId, UserRequest.builder().id(testUserId).name("Test User 1").role("user").build()); |
| 41 | + usersMap.put( |
| 42 | + testUserId2, |
| 43 | + UserRequest.builder().id(testUserId2).name("Test User 2").role("user").build()); |
| 44 | + usersMap.put( |
| 45 | + testModeratorId, |
| 46 | + UserRequest.builder().id(testModeratorId).name("Moderator").role("admin").build()); |
| 47 | + |
| 48 | + UpdateUsersRequest updateUsersRequest = UpdateUsersRequest.builder().users(usersMap).build(); |
| 49 | + client.updateUsers(updateUsersRequest).execute(); |
| 50 | + |
| 51 | + // Create a test feed |
| 52 | + Feed testFeed = new Feed("user", testUserId, feeds); |
| 53 | + GetOrCreateFeedRequest feedRequest = |
| 54 | + GetOrCreateFeedRequest.builder().userID(testUserId).build(); |
| 55 | + GetOrCreateFeedResponse feedResponse = testFeed.getOrCreate(feedRequest).getData(); |
| 56 | + testFeedId = feedResponse.getFeed().getFeed(); |
| 57 | + } |
| 58 | + |
| 59 | + @DisplayName("Can ban user with reason") |
| 60 | + @Test |
| 61 | + void testBanWithReason() throws Exception { |
| 62 | + // snippet-start: BanWithReason |
| 63 | + BanRequest request = |
| 64 | + BanRequest.builder() |
| 65 | + .targetUserID(testUserId) |
| 66 | + .reason("spam") |
| 67 | + .timeout(60) // 60 minutes |
| 68 | + .bannedByID(testModeratorId) |
| 69 | + .build(); |
| 70 | + |
| 71 | + BanResponse response = moderation.ban(request).execute().getData(); |
| 72 | + // snippet-end: BanWithReason |
| 73 | + |
| 74 | + Assertions.assertNotNull(response); |
| 75 | + } |
| 76 | + |
| 77 | + @DisplayName("Can flag activity") |
| 78 | + @Test |
| 79 | + void testFlagActivity() throws Exception { |
| 80 | + // Create an activity first |
| 81 | + AddActivityRequest activity = |
| 82 | + AddActivityRequest.builder() |
| 83 | + .type("post") |
| 84 | + .text("This is a test activity that might need moderation") |
| 85 | + .userID(testUserId) |
| 86 | + .feeds(List.of(testFeedId)) |
| 87 | + .build(); |
| 88 | + |
| 89 | + AddActivityResponse createResponse = feeds.addActivity(activity).execute().getData(); |
| 90 | + String activityId = createResponse.getActivity().getId(); |
| 91 | + testActivityId = activityId; |
| 92 | + |
| 93 | + // snippet-start: FlagActivity |
| 94 | + FlagRequest request = |
| 95 | + FlagRequest.builder() |
| 96 | + .entityType("activity") |
| 97 | + .entityID(activityId) |
| 98 | + .entityCreatorID(testUserId) |
| 99 | + .reason("inappropriate content") |
| 100 | + .userID(testUserId2) |
| 101 | + .build(); |
| 102 | + |
| 103 | + FlagResponse response = moderation.flag(request).execute().getData(); |
| 104 | + // snippet-end: FlagActivity |
| 105 | + |
| 106 | + Assertions.assertNotNull(response); |
| 107 | + } |
| 108 | + |
| 109 | + @DisplayName("Can mute user") |
| 110 | + @Test |
| 111 | + void testMuteUser() throws Exception { |
| 112 | + // snippet-start: MuteUser |
| 113 | + MuteRequest request = |
| 114 | + MuteRequest.builder() |
| 115 | + .targetIds(List.of(testUserId, testUserId2)) |
| 116 | + .timeout(30) // 30 minutes |
| 117 | + .userID(testModeratorId) |
| 118 | + .build(); |
| 119 | + |
| 120 | + MuteResponse response = moderation.mute(request).execute().getData(); |
| 121 | + // snippet-end: MuteUser |
| 122 | + |
| 123 | + Assertions.assertNotNull(response); |
| 124 | + } |
| 125 | + |
| 126 | + @DisplayName("Can flag user") |
| 127 | + @Test |
| 128 | + void testFlagUser() throws Exception { |
| 129 | + // snippet-start: FlagUser |
| 130 | + FlagRequest request = |
| 131 | + FlagRequest.builder() |
| 132 | + .entityType("user") |
| 133 | + .entityID(testUserId) |
| 134 | + .entityCreatorID(testUserId) |
| 135 | + .reason("spam") |
| 136 | + .userID(testUserId2) |
| 137 | + .build(); |
| 138 | + |
| 139 | + FlagResponse response = moderation.flag(request).execute().getData(); |
| 140 | + // snippet-end: FlagUser |
| 141 | + |
| 142 | + Assertions.assertNotNull(response); |
| 143 | + } |
| 144 | + |
| 145 | + @DisplayName("Can check activity content") |
| 146 | + @Test |
| 147 | + void testCheckActivityContent() throws Exception { |
| 148 | + // Create an activity first |
| 149 | + AddActivityRequest activity = |
| 150 | + AddActivityRequest.builder() |
| 151 | + .type("post") |
| 152 | + .text("This is some content to moderate") |
| 153 | + .userID(testUserId) |
| 154 | + .feeds(List.of(testFeedId)) |
| 155 | + .build(); |
| 156 | + |
| 157 | + AddActivityResponse createResponse = feeds.addActivity(activity).execute().getData(); |
| 158 | + String activityId = createResponse.getActivity().getId(); |
| 159 | + |
| 160 | + // snippet-start: CheckActivityContent |
| 161 | + ModerationPayload payload = |
| 162 | + ModerationPayload.builder().texts(List.of("This is some content to moderate")).build(); |
| 163 | + |
| 164 | + CheckRequest request = |
| 165 | + CheckRequest.builder() |
| 166 | + .entityType("activity") |
| 167 | + .entityID(activityId) |
| 168 | + .entityCreatorID(testUserId) |
| 169 | + .moderationPayload(payload) |
| 170 | + .build(); |
| 171 | + |
| 172 | + CheckResponse response = moderation.check(request).execute().getData(); |
| 173 | + // snippet-end: CheckActivityContent |
| 174 | + |
| 175 | + Assertions.assertNotNull(response); |
| 176 | + } |
| 177 | + |
| 178 | + @DisplayName("Can query review queue with filter") |
| 179 | + @Test |
| 180 | + void testQueryReviewQueueWithFilter() throws Exception { |
| 181 | + // snippet-start: QueryReviewQueueWithFilter |
| 182 | + Map<String, Object> filter = new HashMap<>(); |
| 183 | + filter.put("status", "pending"); |
| 184 | + |
| 185 | + QueryReviewQueueRequest request = |
| 186 | + QueryReviewQueueRequest.builder().filter(filter).limit(25).build(); |
| 187 | + |
| 188 | + QueryReviewQueueResponse response = moderation.queryReviewQueue(request).execute().getData(); |
| 189 | + // snippet-end: QueryReviewQueueWithFilter |
| 190 | + |
| 191 | + Assertions.assertNotNull(response); |
| 192 | + } |
| 193 | + |
| 194 | + @DisplayName("Can unban user") |
| 195 | + @Test |
| 196 | + void testUnbanUser() throws Exception { |
| 197 | + // First ban the user |
| 198 | + BanRequest banRequest = |
| 199 | + BanRequest.builder() |
| 200 | + .targetUserID(testUserId) |
| 201 | + .reason("test") |
| 202 | + .bannedByID(testModeratorId) |
| 203 | + .build(); |
| 204 | + moderation.ban(banRequest).execute(); |
| 205 | + |
| 206 | + // snippet-start: UnbanUser |
| 207 | + UnbanRequest request = |
| 208 | + UnbanRequest.builder() |
| 209 | + .TargetUserID(testUserId) |
| 210 | + .unbannedByID(testModeratorId) |
| 211 | + .build(); |
| 212 | + |
| 213 | + UnbanResponse response = moderation.unban(request).execute().getData(); |
| 214 | + // snippet-end: UnbanUser |
| 215 | + |
| 216 | + Assertions.assertNotNull(response); |
| 217 | + } |
| 218 | + |
| 219 | + @DisplayName("Can query moderation logs") |
| 220 | + @Test |
| 221 | + void testQueryModerationLogs() throws Exception { |
| 222 | + // snippet-start: QueryModerationLogs |
| 223 | + QueryModerationLogsRequest request = QueryModerationLogsRequest.builder().limit(10).build(); |
| 224 | + |
| 225 | + QueryModerationLogsResponse response = |
| 226 | + moderation.queryModerationLogs(request).execute().getData(); |
| 227 | + // snippet-end: QueryModerationLogs |
| 228 | + |
| 229 | + Assertions.assertNotNull(response); |
| 230 | + } |
| 231 | +} |
| 232 | + |
0 commit comments