forked from Meeds-io/gamification-crowdin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookService.java
More file actions
187 lines (159 loc) · 7.2 KB
/
WebhookService.java
File metadata and controls
187 lines (159 loc) · 7.2 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* 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.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;
import io.meeds.crowdin.gamification.storage.CrowdinConsumerStorage;
import io.meeds.crowdin.gamification.storage.WebHookStorage;
import io.meeds.gamification.utils.Utils;
import org.exoplatform.commons.ObjectAlreadyExistsException;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static io.meeds.crowdin.gamification.utils.Utils.*;
import java.util.List;
@Service
public class WebhookService {
public static final String NOT_FOUND = " wasn't found";
@Autowired
private CrowdinConsumerStorage crowdinConsumerStorage;
@Autowired
private WebHookStorage webHookStorage;
public List<RemoteProject> getProjectsFromWebhookId(long webHookId) throws IllegalAccessException, ObjectNotFoundException {
WebHook webHook = webHookStorage.getWebHookById(webHookId);
if (webHook == null) {
throw new ObjectNotFoundException("webhook with id : " + webHookId + NOT_FOUND);
}
return crowdinConsumerStorage.getProjects(webHook.getToken());
}
public List<RemoteProject> getProjects(String accessToken) throws IllegalAccessException {
return crowdinConsumerStorage.getProjects(accessToken);
}
public RemoteApproval getApproval(String projectId, String translationId) {
WebHook webHook = webHookStorage.getWebhookByProjectId(Long.parseLong(projectId));
if (webHook != null) {
return crowdinConsumerStorage.getApproval(webHook.getToken(), projectId, translationId);
}
return null;
}
public WebHook createWebhook(long projectId,
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");
}
WebHook existsWebHook = webHookStorage.getWebhookByProjectId(projectId);
if (existsWebHook != null) {
throw new ObjectAlreadyExistsException(existsWebHook);
}
WebHook webHook = crowdinConsumerStorage.createWebhook(projectId, CROWDIN_EVENTS, accessToken);
if (webHook != null) {
webHook.setProjectName(projectName);
webHook.setWatchedBy(currentUser);
return webHookStorage.saveWebHook(webHook);
}
return null;
}
public void updateWebHookAccessToken(long webHookId, String accessToken, String currentUser) throws IllegalAccessException,
ObjectNotFoundException {
if (!Utils.isRewardingManager(currentUser)) {
throw new IllegalAccessException(AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS);
}
if (webHookId <= 0) {
throw new IllegalArgumentException("webHook id must be positive");
}
WebHook webHook = webHookStorage.getWebHookById(webHookId);
if (webHook == null) {
throw new ObjectNotFoundException("webhook with id : " + webHookId + NOT_FOUND);
}
webHookStorage.updateWebHookAccessToken(webHookId, encode(accessToken));
}
public List<WebHook> getWebhooks(String currentUser, int offset, int limit, boolean forceUpdate) throws IllegalAccessException {
if (!Utils.isRewardingManager(currentUser)) {
throw new IllegalAccessException(AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS);
}
return getWebhooks(offset, limit, forceUpdate);
}
public WebHook deleteWebhook(long projectId, String currentUser) throws IllegalAccessException, ObjectNotFoundException {
if (!Utils.isRewardingManager(currentUser)) {
throw new IllegalAccessException("The user is not authorized to delete Crowdin hook");
}
WebHook webHook = webHookStorage.getWebhookByProjectId(projectId);
if (webHook == null) {
throw new ObjectNotFoundException("Crowdin hook for project id : " + projectId + NOT_FOUND);
}
String response = crowdinConsumerStorage.deleteWebhook(webHook);
if (response != null) {
return deleteWebhook(projectId);
}
return null;
}
public WebHook deleteWebhook(long projectId) {
return webHookStorage.deleteWebHook(projectId);
}
public List<WebHook> getWebhooks(int offset, int limit, boolean forceUpdate) {
if (forceUpdate) {
forceUpdateWebhooks();
}
return getWebhooks(offset, limit);
}
public List<RemoteDirectory> getProjectDirectories(long remoteProjectId,
String currentUser,
int offset,
int limit) throws IllegalAccessException, ObjectNotFoundException {
if (!Utils.isRewardingManager(currentUser)) {
throw new IllegalAccessException("The user is not authorized to access project directories");
}
WebHook existsWebHook = webHookStorage.getWebhookByProjectId(remoteProjectId);
if (existsWebHook == null) {
throw new ObjectNotFoundException("Webhook with project id '" + remoteProjectId + "' doesn't exist");
}
return crowdinConsumerStorage.getProjectDirectories(remoteProjectId, offset, limit, existsWebHook.getToken());
}
public void forceUpdateWebhooks() {
crowdinConsumerStorage.clearCache();
List<WebHook> webHook = getWebhooks(0, -1);
webHook.forEach(this::forceUpdateWebhook);
}
public List<WebHook> getWebhooks(int offset, int limit) {
return webHookStorage.getWebhooks(offset, limit);
}
public WebHook getWebhookId(long webhookId, String username) throws IllegalAccessException, ObjectNotFoundException {
if (!Utils.isRewardingManager(username)) {
throw new IllegalAccessException(AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS);
}
WebHook webHook = getWebhookId(webhookId);
if (webHook == null) {
throw new ObjectNotFoundException("Webhook doesn't exist");
}
return webHook;
}
public WebHook getWebhookId(long webhookId) {
if (webhookId <= 0) {
throw new IllegalArgumentException("Webhook id is mandatory");
}
return webHookStorage.getWebHookById(webhookId);
}
private void forceUpdateWebhook(WebHook webHook) {
// TODO
}
}