forked from Meeds-io/gamification-crowdin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuggestionApprovedTriggerPlugin.java
More file actions
105 lines (89 loc) · 4.18 KB
/
SuggestionApprovedTriggerPlugin.java
File metadata and controls
105 lines (89 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* 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.plugin;
import io.meeds.crowdin.gamification.model.Event;
import io.meeds.crowdin.gamification.model.RemoteApproval;
import io.meeds.crowdin.gamification.services.CrowdinTriggerService;
import io.meeds.crowdin.gamification.services.WebhookService;
import io.meeds.gamification.service.RealizationService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import static io.meeds.crowdin.gamification.utils.Utils.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class SuggestionApprovedTriggerPlugin extends CrowdinTriggerPlugin {
@Autowired
private CrowdinTriggerService crowdinTriggerService;
@Autowired
private RealizationService realizationService;
@Autowired
private WebhookService webhookService;
@PostConstruct
public void init() {
crowdinTriggerService.addPlugin(this);
}
@Override
public List<Event> getEvents(String trigger, Map<String, Object> payload) {
String objectId = constructObjectIdAsJsonString(payload, TRANSLATION);
List<Event> 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))));
// 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;
}
@Override
public String getEventName() {
return SUGGESTION_APPROVED_TRIGGER;
}
@Override
public String getCancellingEventName() {
return SUGGESTION_DISAPPROVED_TRIGGER;
}
@Override
public String getProjectId(Map<String, Object> payload) {
return extractSubItem(payload, TRANSLATION, STRING, PROJECT, ID);
}
}