From ab6b726ce6a3ad332c539308fd0e3fb4f4443286 Mon Sep 17 00:00:00 2001 From: Serge Byishimo Date: Wed, 8 Jan 2025 14:45:12 +0200 Subject: [PATCH 1/4] fix: Crowdin proofreader events are not well triggered - Meeds-io/meeds#2655 The suggestion.approved webhook from Crowdin no longer provides the proofreader information in the payload. Instead, it only includes the translator details. Now to get proofreader details, we call the Crowdin API after receiving the webhook. --- .../gamification/model/RemoteApproval.java | 41 ++++++++++ .../SuggestionApprovedTriggerPlugin.java | 75 ++++++++++++------- .../gamification/services/WebhookService.java | 21 +++++- .../storage/CrowdinConsumerStorage.java | 47 +++++++++--- .../crowdin/gamification/utils/Utils.java | 10 ++- 5 files changed, 149 insertions(+), 45 deletions(-) create mode 100644 gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java new file mode 100644 index 00000000..19ae3449 --- /dev/null +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java @@ -0,0 +1,41 @@ +/* + * This file is part of the Meeds project (https://meeds.io/). + * + * Copyright (C) 2020 - 2024 Meeds Lab contact@meedslab.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package io.meeds.crowdin.gamification.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class RemoteApproval implements Cloneable { + + private long id; + + private String userName; + + private String translationId; + + private String stringId; + + private String languageId; + + +} diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java index fd2d9df0..f76312e2 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java @@ -19,10 +19,16 @@ package io.meeds.crowdin.gamification.plugin; import io.meeds.crowdin.gamification.model.Event; +import io.meeds.crowdin.gamification.model.RemoteApproval; +import io.meeds.crowdin.gamification.rest.builder.WebHookBuilder; import io.meeds.crowdin.gamification.services.CrowdinTriggerService; +import io.meeds.crowdin.gamification.services.WebhookService; import io.meeds.gamification.model.RealizationDTO; import io.meeds.gamification.service.RealizationService; import jakarta.annotation.PostConstruct; +import org.exoplatform.commons.exception.ObjectNotFoundException; +import org.exoplatform.services.log.ExoLogger; +import org.exoplatform.services.log.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -35,12 +41,17 @@ @Component public class SuggestionApprovedTriggerPlugin extends CrowdinTriggerPlugin { + private static final Log LOG = ExoLogger.getLogger(SuggestionApprovedTriggerPlugin.class); + @Autowired private CrowdinTriggerService crowdinTriggerService; @Autowired private RealizationService realizationService; + @Autowired + private WebhookService webhookService; + @PostConstruct public void init() { crowdinTriggerService.addPlugin(this); @@ -51,34 +62,42 @@ public List getEvents(String trigger, Map payload) { String objectId = constructObjectIdAsJsonString(payload, TRANSLATION); List eventList = new ArrayList<>(); - eventList.add(new Event(APPROVE_SUGGESTION_EVENT_NAME, - extractSubItem(payload, TRANSLATION, USER, USERNAME), - extractSubItem(payload, TRANSLATION, USER, USERNAME), - objectId, - TRANSLATION, - getProjectId(payload), - extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), - extractSubItem(payload, TRANSLATION, PROVIDER) == null, - extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), - trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), - countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); - - List realizations = realizationService.findRealizationsByObjectIdAndObjectType(objectId, TRANSLATION); - - if (!realizations.isEmpty()) { - String earnerId = realizations.get(0).getEarnerId(); - eventList.add(new Event(SUGGESTION_APPROVED_EVENT_NAME, - earnerId, - earnerId, - objectId, - TRANSLATION, - getProjectId(payload), - extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), - extractSubItem(payload, TRANSLATION, PROVIDER) == null, - extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), - trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), - countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); - + eventList.add(new Event(SUGGESTION_APPROVED_EVENT_NAME, + extractSubItem(payload, TRANSLATION, USER, USERNAME), + extractSubItem(payload, TRANSLATION, USER, USERNAME), + objectId, + TRANSLATION, + getProjectId(payload), + extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), + extractSubItem(payload, TRANSLATION, PROVIDER) == null, + extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), + trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), + countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); + + + // Retrieve who made that approval by calling Crowdin API + try { + String translationId = extractSubItem(payload, TRANSLATION, ID); + + RemoteApproval remoteApproval = webhookService.getApproval(getProjectId(payload), translationId); + + if (remoteApproval != null) { + eventList.add(new Event(APPROVE_SUGGESTION_EVENT_NAME, + remoteApproval.getUserName(), + remoteApproval.getUserName(), + objectId, + TRANSLATION, + getProjectId(payload), + extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), + extractSubItem(payload, TRANSLATION, PROVIDER) == null, + extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), + trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), + countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); + + } + + } catch (IllegalAccessException | ObjectNotFoundException e) { + LOG.error(e); } return eventList; diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java index 39d7688d..483d8ba5 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java @@ -18,6 +18,7 @@ */ package io.meeds.crowdin.gamification.services; +import io.meeds.crowdin.gamification.model.RemoteApproval; import io.meeds.crowdin.gamification.model.RemoteDirectory; import io.meeds.crowdin.gamification.model.RemoteProject; import io.meeds.crowdin.gamification.model.WebHook; @@ -56,10 +57,22 @@ public List getProjects(String accessToken) throws IllegalAccessE return crowdinConsumerStorage.getProjects(accessToken); } + public RemoteApproval getApproval(String projectId, String translationId) + throws IllegalAccessException, ObjectNotFoundException { + + WebHook webHook = webHookStorage.getWebhookByProjectId(Long.parseLong(projectId)); + + if (webHook == null) { + throw new ObjectNotFoundException("Webhook with project id '" + projectId + "' doesn't exist"); + } + + return crowdinConsumerStorage.getApproval(webHook.getToken(), projectId, translationId); + } + public WebHook createWebhook(long projectId, - String projectName, - String accessToken, - String currentUser) throws ObjectAlreadyExistsException, IllegalAccessException { + String projectName, + String accessToken, + String currentUser) throws ObjectAlreadyExistsException, IllegalAccessException { if (!Utils.isRewardingManager(currentUser)) { throw new IllegalAccessException("The user is not authorized to create Crowdin hook"); } @@ -80,7 +93,7 @@ public WebHook createWebhook(long projectId, } public void updateWebHookAccessToken(long webHookId, String accessToken, String currentUser) throws IllegalAccessException, - ObjectNotFoundException { + ObjectNotFoundException { if (!Utils.isRewardingManager(currentUser)) { throw new IllegalAccessException(AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS); } diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java index ce7733cd..03c6458c 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java @@ -18,6 +18,7 @@ */ package io.meeds.crowdin.gamification.storage; +import io.meeds.crowdin.gamification.model.*; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; @@ -39,10 +40,6 @@ import org.exoplatform.services.log.Log; import io.meeds.crowdin.gamification.exception.CrowdinConnectionException; -import io.meeds.crowdin.gamification.model.RemoteDirectory; -import io.meeds.crowdin.gamification.model.RemoteLanguage; -import io.meeds.crowdin.gamification.model.RemoteProject; -import io.meeds.crowdin.gamification.model.WebHook; import org.json.JSONArray; import org.json.JSONObject; @@ -235,7 +232,7 @@ private String processDelete(URI uri, String accessToken) throws CrowdinConnecti private String processRequest(HttpClient httpClient, HttpRequestBase request) throws IOException, CrowdinConnectionException { HttpResponse response = httpClient.execute(request); boolean isSuccess = response != null - && (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300); + && (response.getStatusLine().getStatusCode() >= 200 && response.getStatusLine().getStatusCode() < 300); if (isSuccess) { return processSuccessResponse(response); } else if (response != null && response.getStatusLine().getStatusCode() == 404) { @@ -250,8 +247,8 @@ private String processSuccessResponse(HttpResponse response) throws IOException if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) { return String.valueOf(HttpStatus.SC_NO_CONTENT); } else if ((response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED - || response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) && response.getEntity() != null - && response.getEntity().getContentLength() != 0) { + || response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) && response.getEntity() != null + && response.getEntity().getContentLength() != 0) { try (InputStream is = response.getEntity().getContent()) { return IOUtils.toString(is, StandardCharsets.UTF_8); } @@ -281,8 +278,8 @@ private HttpClient getHttpClient() { if (client == null) { HttpClientConnectionManager clientConnectionManager = getClientConnectionManager(); HttpClientBuilder httpClientBuilder = HttpClients.custom() - .setConnectionManager(clientConnectionManager) - .setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()); + .setConnectionManager(clientConnectionManager) + .setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()); client = httpClientBuilder.build(); } return client; @@ -380,6 +377,38 @@ public List getProjectDirectories(long remoteProjectId, } } + public RemoteApproval getApproval(String accessToken, String projectId, String translationId) + throws IllegalAccessException { + + try { + + URI uri = URI.create(CROWDIN_API_URL + PROJECTS + projectId + APPROVALS + translationId); + + String response = processGet(uri, accessToken); + JSONObject jsonObject = new JSONObject(response).getJSONObject("data"); + + RemoteApproval remoteApproval = new RemoteApproval(); + + JSONObject userJsonObject = jsonObject.getJSONObject("user"); + + remoteApproval.setId(jsonObject.getInt("id")); + + remoteApproval.setUserName(userJsonObject.getString("username")); + remoteApproval.setTranslationId(jsonObject.getString("translationId")); + remoteApproval.setLanguageId(jsonObject.getString("translationId")); + remoteApproval.setStringId(jsonObject.getString("stringId")); + remoteApproval.setLanguageId(jsonObject.getString("languageId")); + + return remoteApproval; + + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException(e); + } catch (CrowdinConnectionException e) { + throw new IllegalAccessException(TOKEN_EXPIRED_OR_INVALID); + } + } + + public void clearCache() { // implemented in cached storage } diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java index d24c440c..7a0e75c1 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java @@ -18,7 +18,7 @@ public class Utils { public static final String CONNECTOR_NAME = "crowdin"; public static final String[] CROWDIN_EVENTS = new String[] { "stringComment.created", - "stringComment.deleted", "suggestion.added", "suggestion.deleted", "suggestion.approved", "suggestion.disapproved" }; + "stringComment.deleted", "suggestion.added", "suggestion.deleted", "suggestion.approved", "suggestion.disapproved" }; public static final String PROJECT_ID = "projectId"; @@ -34,6 +34,8 @@ public class Utils { public static final String WEBHOOKS = "/webhooks/"; + public static final String APPROVALS = "/approvals/"; + public static final String AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS = "The user is not authorized to access crowdin Hooks"; public static final String GAMIFICATION_GENERIC_EVENT = "exo.gamification.generic.action"; @@ -57,7 +59,7 @@ public class Utils { public static final String LANGUAGE_ID = "languageId"; public static final String MUST_BE_HUMAN = "mustBeHuman"; - + public static final String TOTAL_TARGET_ITEM = "totalTargetItem"; public static final String STRING = "string"; @@ -65,7 +67,7 @@ public class Utils { public static final String TARGET_LANGUAGE = "targetLanguage"; public static final String PROJECT = "project"; - + public static final String TEXT = "text"; public static final String ID = "id"; @@ -180,7 +182,7 @@ public static String constructObjectIdAsJsonString(Map payload, String sourceLanguageId = extractSubItem(payload, payloadObjectName, STRING, PROJECT, SOURCE_LANGUAGE_ID); String targetLanguageId = extractSubItem(payload, payloadObjectName, TARGET_LANGUAGE, ID); return "{\"id\":" + id + ",\"stringUrl\":\"https://crowdin.com/editor/" + projectSlug + "/" + fileId + "/" + sourceLanguageId - + "-" + targetLanguageId + "?view=comfortable#" + stringId + "\"}"; + + "-" + targetLanguageId + "?view=comfortable#" + stringId + "\"}"; } public static int countWords(String text) { From 391b742c86214a115d0dc609551da14fe7460d0b Mon Sep 17 00:00:00 2001 From: Serge Byishimo Date: Thu, 16 Jan 2025 17:43:26 +0200 Subject: [PATCH 2/4] fix: Suggested Changes by @AzmiTouil - Meeds-io/meeds#2655 Suggested Changes by @AzmiTouil --- .../crowdin/gamification/model/RemoteApproval.java | 2 +- .../plugin/SuggestionApprovedTriggerPlugin.java | 1 - .../gamification/services/WebhookService.java | 12 ++++-------- .../gamification/storage/CrowdinConsumerStorage.java | 7 +++---- .../io/meeds/crowdin/gamification/utils/Utils.java | 2 +- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java index 19ae3449..15681879 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/model/RemoteApproval.java @@ -1,7 +1,7 @@ /* * This file is part of the Meeds project (https://meeds.io/). * - * Copyright (C) 2020 - 2024 Meeds Lab contact@meedslab.com + * Copyright (C) 2020 - 2025 Meeds Lab contact@meedslab.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java index f76312e2..bf4f09d8 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java @@ -78,7 +78,6 @@ public List getEvents(String trigger, Map payload) { // Retrieve who made that approval by calling Crowdin API try { String translationId = extractSubItem(payload, TRANSLATION, ID); - RemoteApproval remoteApproval = webhookService.getApproval(getProjectId(payload), translationId); if (remoteApproval != null) { diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java index 483d8ba5..abeac12e 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/services/WebhookService.java @@ -57,16 +57,12 @@ public List getProjects(String accessToken) throws IllegalAccessE return crowdinConsumerStorage.getProjects(accessToken); } - public RemoteApproval getApproval(String projectId, String translationId) - throws IllegalAccessException, ObjectNotFoundException { - + public RemoteApproval getApproval(String projectId, String translationId) { WebHook webHook = webHookStorage.getWebhookByProjectId(Long.parseLong(projectId)); - - if (webHook == null) { - throw new ObjectNotFoundException("Webhook with project id '" + projectId + "' doesn't exist"); + if (webHook != null) { + return crowdinConsumerStorage.getApproval(webHook.getToken(), projectId, translationId); } - - return crowdinConsumerStorage.getApproval(webHook.getToken(), projectId, translationId); + return null; } public WebHook createWebhook(long projectId, diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java index 03c6458c..e484d15e 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java @@ -378,7 +378,7 @@ public List getProjectDirectories(long remoteProjectId, } public RemoteApproval getApproval(String accessToken, String projectId, String translationId) - throws IllegalAccessException { + { try { @@ -401,10 +401,9 @@ public RemoteApproval getApproval(String accessToken, String projectId, String t return remoteApproval; - } catch (IllegalArgumentException e) { - throw new IllegalArgumentException(e); } catch (CrowdinConnectionException e) { - throw new IllegalAccessException(TOKEN_EXPIRED_OR_INVALID); + LOG.warn("Unable to retrieve approval for crowdin translation with id {}.", translationId, e); + return null; } } diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java index 7a0e75c1..3a6f0797 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/utils/Utils.java @@ -34,7 +34,7 @@ public class Utils { public static final String WEBHOOKS = "/webhooks/"; - public static final String APPROVALS = "/approvals/"; + public static final String APPROVALS = "/approvals/"; public static final String AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS = "The user is not authorized to access crowdin Hooks"; From 0e3315a60e6f74a953684d5c21bdd108819db031 Mon Sep 17 00:00:00 2001 From: Serge Byishimo Date: Thu, 16 Jan 2025 17:54:17 +0200 Subject: [PATCH 3/4] fix: exception java.lang.IllegalAccessException is never thrown in body of corresponding try statement - Meeds-io/meeds#2655 exception java.lang.IllegalAccessException is never thrown in body of corresponding try statement --- .../plugin/SuggestionApprovedTriggerPlugin.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java index bf4f09d8..8186fc4a 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java @@ -76,7 +76,6 @@ public List getEvents(String trigger, Map payload) { // Retrieve who made that approval by calling Crowdin API - try { String translationId = extractSubItem(payload, TRANSLATION, ID); RemoteApproval remoteApproval = webhookService.getApproval(getProjectId(payload), translationId); @@ -95,11 +94,7 @@ public List getEvents(String trigger, Map payload) { } - } catch (IllegalAccessException | ObjectNotFoundException e) { - LOG.error(e); - } - - return eventList; + return eventList; } @Override From fd9b60e8f2130be267845e66319888ebcef5aafd Mon Sep 17 00:00:00 2001 From: Azmi Touil Date: Fri, 17 Jan 2025 10:03:43 +0100 Subject: [PATCH 4/4] Remove useless imports and format code --- .../SuggestionApprovedTriggerPlugin.java | 67 ++++++++----------- .../storage/CrowdinConsumerStorage.java | 6 +- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java index 8186fc4a..2489c8fe 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/plugin/SuggestionApprovedTriggerPlugin.java @@ -20,15 +20,10 @@ import io.meeds.crowdin.gamification.model.Event; import io.meeds.crowdin.gamification.model.RemoteApproval; -import io.meeds.crowdin.gamification.rest.builder.WebHookBuilder; import io.meeds.crowdin.gamification.services.CrowdinTriggerService; import io.meeds.crowdin.gamification.services.WebhookService; -import io.meeds.gamification.model.RealizationDTO; import io.meeds.gamification.service.RealizationService; import jakarta.annotation.PostConstruct; -import org.exoplatform.commons.exception.ObjectNotFoundException; -import org.exoplatform.services.log.ExoLogger; -import org.exoplatform.services.log.Log; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -41,8 +36,6 @@ @Component public class SuggestionApprovedTriggerPlugin extends CrowdinTriggerPlugin { - private static final Log LOG = ExoLogger.getLogger(SuggestionApprovedTriggerPlugin.class); - @Autowired private CrowdinTriggerService crowdinTriggerService; @@ -50,7 +43,7 @@ public class SuggestionApprovedTriggerPlugin extends CrowdinTriggerPlugin { private RealizationService realizationService; @Autowired - private WebhookService webhookService; + private WebhookService webhookService; @PostConstruct public void init() { @@ -63,38 +56,36 @@ public List getEvents(String trigger, Map payload) { List eventList = new ArrayList<>(); eventList.add(new Event(SUGGESTION_APPROVED_EVENT_NAME, - extractSubItem(payload, TRANSLATION, USER, USERNAME), - extractSubItem(payload, TRANSLATION, USER, USERNAME), - objectId, - TRANSLATION, - getProjectId(payload), - extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), - extractSubItem(payload, TRANSLATION, PROVIDER) == null, - extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), - trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), - countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); - + extractSubItem(payload, TRANSLATION, USER, USERNAME), + extractSubItem(payload, TRANSLATION, USER, USERNAME), + objectId, + TRANSLATION, + getProjectId(payload), + extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), + extractSubItem(payload, TRANSLATION, PROVIDER) == null, + extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), + trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), + countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); // Retrieve who made that approval by calling Crowdin API - String translationId = extractSubItem(payload, TRANSLATION, ID); - RemoteApproval remoteApproval = webhookService.getApproval(getProjectId(payload), translationId); - - if (remoteApproval != null) { - eventList.add(new Event(APPROVE_SUGGESTION_EVENT_NAME, - remoteApproval.getUserName(), - remoteApproval.getUserName(), - objectId, - TRANSLATION, - getProjectId(payload), - extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), - extractSubItem(payload, TRANSLATION, PROVIDER) == null, - extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), - trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), - countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); - - } - - return eventList; + String translationId = extractSubItem(payload, TRANSLATION, ID); + RemoteApproval remoteApproval = webhookService.getApproval(getProjectId(payload), translationId); + + if (remoteApproval != null) { + eventList.add(new Event(APPROVE_SUGGESTION_EVENT_NAME, + remoteApproval.getUserName(), + remoteApproval.getUserName(), + objectId, + TRANSLATION, + getProjectId(payload), + extractSubItem(payload, TRANSLATION, TARGET_LANGUAGE, ID), + extractSubItem(payload, TRANSLATION, PROVIDER) == null, + extractSubItem(payload, TRANSLATION, STRING, FILE, DIRECTORY_ID), + trigger.equals(SUGGESTION_DISAPPROVED_TRIGGER), + countWords(extractSubItem(payload, TRANSLATION, STRING, TEXT)))); + + } + return eventList; } @Override diff --git a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java index e484d15e..1587b717 100644 --- a/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java +++ b/gamification-crowdin-services/src/main/java/io/meeds/crowdin/gamification/storage/CrowdinConsumerStorage.java @@ -377,11 +377,8 @@ public List getProjectDirectories(long remoteProjectId, } } - public RemoteApproval getApproval(String accessToken, String projectId, String translationId) - { - + public RemoteApproval getApproval(String accessToken, String projectId, String translationId) { try { - URI uri = URI.create(CROWDIN_API_URL + PROJECTS + projectId + APPROVALS + translationId); String response = processGet(uri, accessToken); @@ -400,7 +397,6 @@ public RemoteApproval getApproval(String accessToken, String projectId, String t remoteApproval.setLanguageId(jsonObject.getString("languageId")); return remoteApproval; - } catch (CrowdinConnectionException e) { LOG.warn("Unable to retrieve approval for crowdin translation with id {}.", translationId, e); return null;