Skip to content

Commit

Permalink
feat: Apply 4 Filters - Meeds-io/MIPs#121
Browse files Browse the repository at this point in the history
- Project Filter
- Directory Filter
- Language Filter
- Human Filter
  • Loading branch information
sergeByishimo committed Mar 20, 2024
1 parent 9c7171d commit 5657de9
Show file tree
Hide file tree
Showing 28 changed files with 833 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,11 @@ public class Event {

String projectId;

String languageId;

boolean mustBeHuman;

String directoryId;

boolean isCancelling;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.meeds.gamification.crowdin.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RemoteDirectory {
private long id;
private long projectId;
private String path;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.meeds.gamification.crowdin.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RemoteLanguage {
private String id;
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RemoteProject implements Cloneable {
private long id;
private String identifier;
private String name;
private String description;
private String avatarUrl;
private long id;
private String identifier;
private String name;
private String description;
private String avatarUrl;
private List<RemoteLanguage> languages;

@Override
public RemoteProject clone() {
return new RemoteProject(id, identifier, name, description, avatarUrl);
return new RemoteProject(id, identifier, name, description, avatarUrl, languages);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.meeds.gamification.crowdin.plugin;

import io.meeds.gamification.plugin.EventPlugin;
import io.meeds.gamification.service.EventService;
import jakarta.annotation.PostConstruct;
import org.apache.commons.collections.CollectionUtils;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

import static io.meeds.gamification.crowdin.utils.Utils.*;
@Component
public class CrowdinEventPlugin extends EventPlugin {

private static final Log LOG = ExoLogger.getLogger(CrowdinEventPlugin.class);

public static final String EVENT_TYPE = "crowdin";

@Autowired
private EventService eventService;

@PostConstruct
public void initData() {
eventService.addPlugin(this);
}

@Override
public String getEventType() {
return EVENT_TYPE;
}

@Override
public List<String> getTriggers() {
return List.of(
STRING_COMMENT_CREATED_EVENT_NAME,
SUGGESTION_ADDED_EVENT_NAME,
SUGGESTION_APPROVED_EVENT_NAME,
APPROVE_SUGGESTION_EVENT_NAME
);
}

@Override
public boolean isValidEvent(Map<String, String> eventProperties, String triggerDetails) {
LOG.info("isValidEvent: started");
LOG.info("eventProperties: " + eventProperties);
LOG.info("triggerDetails: " + triggerDetails);

String desiredProjectId = eventProperties.get(PROJECT_ID);

String desiredMustBeHuman = eventProperties.get(MUST_BE_HUMAN);

List<String> desiredDirectoryIds =
eventProperties.get(DIRECTORY_IDS) != null ? Arrays.asList(eventProperties.get(DIRECTORY_IDS)
.split(","))
: Collections.emptyList();

List<String> desiredLanguageIds =
eventProperties.get(LANGUAGE_IDS) != null ? Arrays.asList(eventProperties.get(LANGUAGE_IDS)
.split(","))
: Collections.emptyList();

LOG.info("desiredDirectoryIds: " + desiredDirectoryIds);
LOG.info("desiredLanguageIds: " + desiredLanguageIds);


Map<String, String> triggerDetailsMop = stringToMap(triggerDetails);
LOG.info("triggerDetailsMop: " + triggerDetailsMop);

return desiredProjectId.equals(triggerDetailsMop.get(PROJECT_ID))
&& desiredMustBeHuman.equals(triggerDetailsMop.get(MUST_BE_HUMAN))
&& (CollectionUtils.isEmpty(desiredDirectoryIds) || desiredDirectoryIds.contains(triggerDetailsMop.get(DIRECTORY_ID)))
&& (CollectionUtils.isEmpty(desiredLanguageIds) || desiredLanguageIds.contains(triggerDetailsMop.get(LANGUAGE_ID)));
}

private static Map<String, String> stringToMap(String mapAsString) {
Map<String, String> map = new HashMap<>();
mapAsString = mapAsString.substring(1, mapAsString.length() - 1);
String[] pairs = mapAsString.split(", ");
for (String pair : pairs) {
String[] keyValue = pair.split(": ");
String key = keyValue[0].trim();
String value = keyValue[1].trim();
map.put(key, value);
}
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ public abstract List<Event> getEvents(
* @return object name
*/
public abstract String getPayloadObjectName();
public abstract String getProjectId(Map<String, Object> payload);
public abstract boolean batchQueryRemoteTranslations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
import java.util.List;
import java.util.Map;

import static io.meeds.gamification.crowdin.utils.Utils.extractSubItem;
import static io.meeds.gamification.crowdin.utils.Utils.*;

@Component
public class StringCommentCreatedTriggerPlugin extends CrowdinTriggerPlugin {

protected String EVENT_PAYLOAD_OBJECT_NAME = "comment";
protected String EVENT_TITLE = "stringCommentCreated";
protected String EVENT_TRIGGER = "stringComment.created";
protected String CANCELLING_EVENT_TRIGGER = "stringComment.deleted";

Expand All @@ -30,12 +29,15 @@ public void initData() {

@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload, Object object) {
return Collections.singletonList(new Event(EVENT_TITLE,
extractSubItem(payload, getPayloadObjectName(), "user", "username"),
extractSubItem(payload, getPayloadObjectName(), "user", "username"),
extractSubItem(payload, getPayloadObjectName(), "string", "url"),
return Collections.singletonList(new Event(STRING_COMMENT_CREATED_EVENT_NAME,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "user", "username"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "user", "username"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "url"),
EVENT_PAYLOAD_OBJECT_NAME,
extractSubItem(payload, getPayloadObjectName(), "string", "project", "id"),
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "targetLanguage", "id"),
true,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "file", "directoryId"),
trigger.equals(CANCELLING_EVENT_TRIGGER)));
}

Expand All @@ -54,6 +56,11 @@ public String getPayloadObjectName() {
return EVENT_PAYLOAD_OBJECT_NAME;
}

@Override
public String getProjectId(Map<String, Object> payload) {
return extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "project", "id");
}

@Override
public boolean batchQueryRemoteTranslations() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
import java.util.List;
import java.util.Map;

import static io.meeds.gamification.crowdin.utils.Utils.extractSubItem;
import static io.meeds.gamification.crowdin.utils.Utils.*;

@Component
public class SuggestionAddedTriggerPlugin extends CrowdinTriggerPlugin {
private static final Log LOG = ExoLogger.getLogger(SuggestionAddedTriggerPlugin.class);
protected String EVENT_PAYLOAD_OBJECT_NAME = "translation";
protected String EVENT_TITLE = "suggestionAdded";
protected String EVENT_TRIGGER = "suggestion.added";
protected String CROWDIN_EVENT_TRIGGER = "suggestion.added";
protected String CANCELLING_EVENT_TRIGGER = "suggestion.deleted";

@Autowired
Expand All @@ -32,23 +31,22 @@ public void initData() {

@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload, Object object) {
if (extractSubItem(payload, getPayloadObjectName(), "provider") != null) {
LOG.warn("Crowdin event {} translation provider is TM or MT", EVENT_TRIGGER);
return Collections.emptyList();
}

return Collections.singletonList(new Event(EVENT_TITLE,
extractSubItem(payload, getPayloadObjectName(), "user", "username"),
extractSubItem(payload, getPayloadObjectName(), "user", "username"),
extractSubItem(payload, getPayloadObjectName(), "string", "url"),
return Collections.singletonList(new Event(SUGGESTION_ADDED_EVENT_NAME,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "user", "username"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "user", "username"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "url"),
EVENT_PAYLOAD_OBJECT_NAME,
extractSubItem(payload, getPayloadObjectName(), "string", "project", "id"),
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "targetLanguage", "id"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "provider") == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "file", "directoryId"),
trigger.equals(CANCELLING_EVENT_TRIGGER)));
}

@Override
public String getEventName() {
return EVENT_TRIGGER;
return CROWDIN_EVENT_TRIGGER;
}

@Override
Expand All @@ -61,6 +59,11 @@ public String getPayloadObjectName() {
return EVENT_PAYLOAD_OBJECT_NAME;
}

@Override
public String getProjectId(Map<String, Object> payload) {
return extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "project", "id");
}

@Override
public boolean batchQueryRemoteTranslations() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
import java.util.List;
import java.util.Map;

import static io.meeds.gamification.crowdin.utils.Utils.extractSubItem;
import static io.meeds.gamification.crowdin.utils.Utils.*;

@Component
public class SuggestionApprovedTriggerPlugin extends CrowdinTriggerPlugin {
private static final Log LOG = ExoLogger.getLogger(SuggestionAddedTriggerPlugin.class);
protected String EVENT_PAYLOAD_OBJECT_NAME = "translation";
protected String SUGGESTION_APPROVED_EVENT_TITLE = "suggestionApproved";
protected String APPROVE_SUGGESTION_EVENT_TITLE = "approveSuggestion";
protected String EVENT_TRIGGER = "suggestion.approved";
protected String CROWDIN_EVENT_TRIGGER = "suggestion.approved";
protected String CANCELLING_EVENT_TRIGGER = "suggestion.disapproved";

@Autowired
Expand All @@ -36,18 +34,17 @@ public void initData() {
@SuppressWarnings("unchecked")
@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload, Object object) {
if (extractSubItem(payload, getPayloadObjectName(), "provider") != null) {
LOG.warn("Crowdin event {} translation provider is TM or MT", EVENT_TRIGGER);
return Collections.emptyList();
}

List<Event> eventList = new ArrayList<>();
eventList.add(new Event(APPROVE_SUGGESTION_EVENT_TITLE,
extractSubItem(payload, getPayloadObjectName(), "user", "username"),
extractSubItem(payload, getPayloadObjectName(), "user", "username"),
extractSubItem(payload, getPayloadObjectName(), "string", "url"),
eventList.add(new Event(APPROVE_SUGGESTION_EVENT_NAME,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "user", "username"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "user", "username"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "url"),
EVENT_PAYLOAD_OBJECT_NAME,
extractSubItem(payload, getPayloadObjectName(), "string", "project", "id"),
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "targetLanguage", "id"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "provider") == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "file", "directoryId"),
trigger.equals(CANCELLING_EVENT_TRIGGER)));

if (object == null) {
Expand All @@ -69,12 +66,15 @@ public List<Event> getEvents(String trigger, Map<String, Object> payload, Object
}

if (translationById != null) {
eventList.add(new Event(SUGGESTION_APPROVED_EVENT_TITLE,
eventList.add(new Event(SUGGESTION_APPROVED_EVENT_NAME,
translationById.getUsername(),
translationById.getUsername(),
extractSubItem(payload, getPayloadObjectName(), "string", "url"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "url"),
EVENT_PAYLOAD_OBJECT_NAME,
extractSubItem(payload, getPayloadObjectName(), "string", "project", "id"),
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "targetLanguage", "id"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "provider") == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "file", "directoryId"),
trigger.equals(CANCELLING_EVENT_TRIGGER)));
}
}
Expand All @@ -84,7 +84,7 @@ public List<Event> getEvents(String trigger, Map<String, Object> payload, Object

@Override
public String getEventName() {
return EVENT_TRIGGER;
return CROWDIN_EVENT_TRIGGER;
}

@Override
Expand All @@ -97,6 +97,11 @@ public String getPayloadObjectName() {
return EVENT_PAYLOAD_OBJECT_NAME;
}

@Override
public String getProjectId(Map<String, Object> payload) {
return extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "project", "id");
}

@Override
public boolean batchQueryRemoteTranslations() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
@Tag(name = "webhooks", description = "An endpoint to receive crowdin webhooks")
public class CrowdinWebhookController {

private static final Log LOG = ExoLogger.getLogger(CrowdinWebhookController.class);

@Autowired
private CrowdinTriggerService crowdinTriggerService;
@PostMapping
Expand All @@ -29,7 +27,6 @@ public ResponseEntity<Object> crowdinEvent(
@RequestHeader("authorization") String bearerToken,
@RequestBody String payload) {
try {
LOG.info("payload: " + payload);
crowdinTriggerService.handleTriggerAsync(bearerToken, payload);
return ResponseEntity.status(HttpStatus.OK).build();
} catch (Exception e) {
Expand Down
Loading

0 comments on commit 5657de9

Please sign in to comment.