Skip to content

Commit

Permalink
fix: Bug fixes & Improvements - Meeds-io/MIPs#121 (#10)
Browse files Browse the repository at this point in the history
* fix: Selecting Project & Directories - Meeds-io/MIPs#121

* fix: Bug fixes & Improvements - Meeds-io/MIPs#121

- Cancelling deleted translation suggestions
- Fixed Languages Filter UI bug
- Improved suggestions approved event by removing unnecessary network
request (graphql)

* fix: human translation bug fix - Meeds-io/MIPs#121

Accept both human and machine translations when you don't check the box for human translation
  • Loading branch information
sergeByishimo authored Mar 31, 2024
1 parent 7bc3553 commit a50a321
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 287 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
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;

Expand All @@ -15,8 +13,6 @@
@Component
public class CrowdinEventPlugin extends EventPlugin {

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

public static final String EVENT_TYPE = "crowdin";

@Autowired
Expand Down Expand Up @@ -44,9 +40,6 @@ public List<String> getTriggers() {

@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);

Expand All @@ -62,15 +55,10 @@ public boolean isValidEvent(Map<String, String> eventProperties, String triggerD
.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))
&& (desiredMustBeHuman.equals("false") || 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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,15 @@ public abstract class CrowdinTriggerPlugin extends BaseComponentPlugin {
/**
* Gets List of triggered events
*
* @param trigger trigger event name
* @param payload payload The raw payload of the webhook request.
* @param object additional processing object
* @return List of triggered events
*/
public abstract List<Event> getEvents(
String trigger,
Map<String, Object> payload,
Object object);
Map<String, Object> payload);

public abstract String getEventName();
public abstract String getCancellingEventName();

/**
* Gets json object event key name in the payload
*
* @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 @@ -28,16 +28,16 @@ public void initData() {
}

@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload, Object object) {
public List<Event> getEvents(String trigger, Map<String, Object> payload) {
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"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, USER, USERNAME),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, USER, USERNAME),
constructObjectIdAsJsonString(payload, EVENT_PAYLOAD_OBJECT_NAME),
EVENT_PAYLOAD_OBJECT_NAME,
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "targetLanguage", "id"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, TARGET_LANGUAGE, ID),
true,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "file", "directoryId"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, FILE, DIRECTORY_ID),
trigger.equals(CANCELLING_EVENT_TRIGGER)));
}

Expand All @@ -51,18 +51,8 @@ public String getCancellingEventName() {
return CANCELLING_EVENT_TRIGGER;
}

@Override
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;
return extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, PROJECT, ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.meeds.gamification.crowdin.model.Event;
import io.meeds.gamification.crowdin.services.CrowdinTriggerService;
import io.meeds.gamification.model.RealizationDTO;
import io.meeds.gamification.service.RealizationService;
import jakarta.annotation.PostConstruct;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
Expand All @@ -24,24 +26,52 @@ public class SuggestionAddedTriggerPlugin extends CrowdinTriggerPlugin {
@Autowired
private CrowdinTriggerService crowdinTriggerService;

@Autowired
private RealizationService realizationService;

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

@SuppressWarnings("unchecked")
@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload, Object object) {
public List<Event> getEvents(String trigger, Map<String, Object> payload) {
String objectId = constructObjectIdAsJsonString(payload, EVENT_PAYLOAD_OBJECT_NAME);

if (trigger.equals(CROWDIN_EVENT_TRIGGER)) {
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),
objectId,
EVENT_PAYLOAD_OBJECT_NAME,
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, TARGET_LANGUAGE, ID),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, PROVIDER) == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, FILE, DIRECTORY_ID),
false));
} else if (trigger.equals(CANCELLING_EVENT_TRIGGER)) {
List<RealizationDTO> realizations = realizationService.
findRealizationsByObjectIdAndObjectType(objectId, EVENT_PAYLOAD_OBJECT_NAME);

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,
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 ( ! realizations.isEmpty()) {
String earnerId = realizations.get(0).getEarnerId();

return Collections.singletonList(new Event(SUGGESTION_ADDED_EVENT_NAME,
earnerId,
earnerId,
objectId,
EVENT_PAYLOAD_OBJECT_NAME,
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, TARGET_LANGUAGE, ID),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, PROVIDER) == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, FILE, DIRECTORY_ID),
true));
}
} else {
LOG.error("Unknown crowdin hook event {}", trigger);
}
return Collections.emptyList();
}

@Override
Expand All @@ -54,18 +84,8 @@ public String getCancellingEventName() {
return CANCELLING_EVENT_TRIGGER;
}

@Override
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;
return extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, PROJECT, ID);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.meeds.gamification.crowdin.plugin;

import io.meeds.gamification.crowdin.model.Event;
import io.meeds.gamification.crowdin.model.RemoteTranslation;
import io.meeds.gamification.crowdin.services.CrowdinTriggerService;
import io.meeds.gamification.model.RealizationDTO;
import io.meeds.gamification.service.RealizationService;
import jakarta.annotation.PostConstruct;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand All @@ -26,57 +26,47 @@ public class SuggestionApprovedTriggerPlugin extends CrowdinTriggerPlugin {
@Autowired
private CrowdinTriggerService crowdinTriggerService;

@Autowired
private RealizationService realizationService;

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

@SuppressWarnings("unchecked")
@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload, Object object) {
public List<Event> getEvents(String trigger, Map<String, Object> payload) {
String objectId = constructObjectIdAsJsonString(payload, EVENT_PAYLOAD_OBJECT_NAME);

List<Event> eventList = new ArrayList<>();
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"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, USER, USERNAME),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, USER, USERNAME),
objectId,
EVENT_PAYLOAD_OBJECT_NAME,
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"),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, TARGET_LANGUAGE, ID),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, PROVIDER) == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, FILE, DIRECTORY_ID),
trigger.equals(CANCELLING_EVENT_TRIGGER)));

if (object == null) {
return eventList;
}
List<RealizationDTO> realizations = realizationService.
findRealizationsByObjectIdAndObjectType(objectId, EVENT_PAYLOAD_OBJECT_NAME);

if ( ! realizations.isEmpty()) {
String earnerId = realizations.get(0).getEarnerId();
eventList.add(new Event(SUGGESTION_APPROVED_EVENT_NAME,
earnerId,
earnerId,
objectId,
EVENT_PAYLOAD_OBJECT_NAME,
getProjectId(payload),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, TARGET_LANGUAGE, ID),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, PROVIDER) == null,
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, FILE, DIRECTORY_ID),
trigger.equals(CANCELLING_EVENT_TRIGGER)));

List<RemoteTranslation> remoteTranslations = (List<RemoteTranslation>) object;
LOG.debug("remoteTranslations: " + remoteTranslations.size());
String translationId = extractSubItem(payload, getPayloadObjectName(), "id");
if (translationId != null) {

RemoteTranslation translationById = null;

for (RemoteTranslation translation : remoteTranslations) {
if (translation.getId() == Long.parseLong(translationId)) {
translationById = translation;
break;
}
}

if (translationById != null) {
eventList.add(new Event(SUGGESTION_APPROVED_EVENT_NAME,
translationById.getUsername(),
translationById.getUsername(),
extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, "string", "url"),
EVENT_PAYLOAD_OBJECT_NAME,
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)));
}
}

return eventList;
Expand All @@ -92,18 +82,8 @@ public String getCancellingEventName() {
return CANCELLING_EVENT_TRIGGER;
}

@Override
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;
return extractSubItem(payload, EVENT_PAYLOAD_OBJECT_NAME, STRING, PROJECT, ID);
}
}
Loading

0 comments on commit a50a321

Please sign in to comment.