1
|
|
|
package com.base.Services; |
2
|
|
|
|
3
|
|
|
import com.base.Base; |
4
|
|
|
import com.base.Exceptions.BaseHttpException; |
5
|
|
|
import com.base.Exceptions.Http.NotFound; |
6
|
|
|
import com.base.Exceptions.MessageNotFound; |
7
|
|
|
import com.base.Exceptions.ThreadNotFound; |
8
|
|
|
import com.base.Helpers; |
9
|
|
|
import com.base.Http.Request.Request; |
10
|
|
|
import com.base.Http.Response.Response; |
11
|
|
|
import com.base.Models.Message; |
12
|
|
|
|
13
|
|
|
import java.io.File; |
14
|
|
|
import java.util.*; |
15
|
|
|
|
16
|
|
|
public class MessageService { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@link Base} |
20
|
|
|
*/ |
21
|
|
|
private Base base; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Construct Instance of Base Class |
25
|
|
|
* |
26
|
|
|
* @param base |
27
|
|
|
*/ |
28
|
|
|
public MessageService(Base base) { |
29
|
|
|
this.base = base; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create Message of Thread |
34
|
|
|
* |
35
|
|
|
* @param teamSlug Message teamSlug |
36
|
|
|
* @param channelSlug Message channelSlug |
37
|
|
|
* @param threadSlug Message threadSlug |
38
|
|
|
* @param content Message content |
39
|
|
|
* @param type Message type |
40
|
|
|
* @return Message |
41
|
|
|
* @throws ThreadNotFound |
42
|
|
|
* @throws BaseHttpException |
43
|
|
|
*/ |
44
|
|
|
public Message createMessage(String teamSlug, String channelSlug, String threadSlug, String content, String type) throws ThreadNotFound, BaseHttpException { |
45
|
|
|
File[] files = {}; |
46
|
|
|
return this.createMessage(teamSlug, channelSlug, threadSlug, content, type, files); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create Message of Thread |
51
|
|
|
* |
52
|
|
|
* @param teamSlug Message teamSlug |
53
|
|
|
* @param channelSlug Message channelSlug |
54
|
|
|
* @param threadSlug Message threadSlug |
55
|
|
|
* @param content Message content |
56
|
|
|
* @param type Message type |
57
|
|
|
* @param files Message files |
58
|
|
|
* @return Message |
59
|
|
|
* @throws ThreadNotFound |
60
|
|
|
* @throws BaseHttpException |
61
|
|
|
*/ |
62
|
|
|
public Message createMessage(String teamSlug, String channelSlug, String threadSlug, String content, String type, File[] files) throws ThreadNotFound, BaseHttpException { |
63
|
|
|
HashMap<String, String> parameters = new HashMap<>(); |
64
|
|
|
|
65
|
|
|
Map<String, File> requestFiles = new HashMap<>(); |
66
|
|
|
|
67
|
|
|
for (int i = 0; i < files.length; i++) { |
68
|
|
|
requestFiles.put("files*" + i, files[i]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
parameters.put("content", content); |
72
|
|
|
parameters.put("type", type); |
73
|
|
|
try { |
74
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/"). |
75
|
|
|
concat(threadSlug).concat("/messages"); |
76
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_POST, parameters, new HashMap<>(), requestFiles); |
77
|
|
|
return (Message) Base.makeModel(Message.class, response.getBody()); |
78
|
|
|
} catch (NotFound e) { |
79
|
|
|
throw new ThreadNotFound(teamSlug); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* List of all thread messages |
85
|
|
|
* |
86
|
|
|
* @param teamSlug Team Slug |
87
|
|
|
* @param channelSlug Channel Slug |
88
|
|
|
* @param threadSlug Thread Slug |
89
|
|
|
* @param page Page number |
90
|
|
|
* @param limit Limit Value |
91
|
|
|
* @return List of Messages |
92
|
|
|
* @throws ThreadNotFound Exception |
93
|
|
|
* @throws BaseHttpException Exception |
94
|
|
|
*/ |
95
|
|
|
public List<Message> getAllMessages(String teamSlug, String channelSlug, String threadSlug, int page, int limit) throws ThreadNotFound, BaseHttpException { |
96
|
|
|
|
97
|
|
|
ArrayList<String> parameters = new ArrayList<>(); |
98
|
|
|
|
99
|
|
|
if (page != 0) { |
100
|
|
|
parameters.add("page=".concat(Integer.toString(page))); |
101
|
|
|
} |
102
|
|
|
if (limit != 0) { |
103
|
|
|
parameters.add("limit=".concat(Integer.toString(limit))); |
104
|
|
|
} |
105
|
|
|
String URL = Helpers.buildUrlWithQuery("/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug) |
106
|
|
|
.concat("/messages/"), parameters); |
107
|
|
|
|
108
|
|
|
try { |
109
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_GET); |
110
|
|
|
Message[] messagesArray = (Message[]) Base.makeModel(Message[].class, response.getBody()); |
111
|
|
|
return new ArrayList<>(Arrays.asList(messagesArray)); |
112
|
|
|
} catch (NotFound e) { |
113
|
|
|
throw new ThreadNotFound(threadSlug); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* List of all thread messages |
119
|
|
|
* |
120
|
|
|
* @param teamSlug Team Slug |
121
|
|
|
* @param channelSlug Channel Slug |
122
|
|
|
* @param threadSlug Thread Slug |
123
|
|
|
* @param page Page number |
124
|
|
|
* @return List of Messages |
125
|
|
|
* @throws ThreadNotFound Exception |
126
|
|
|
* @throws BaseHttpException Exception |
127
|
|
|
*/ |
128
|
|
|
public List<Message> getAllMessages(String teamSlug, String channelSlug, String threadSlug, int page) throws ThreadNotFound, BaseHttpException { |
129
|
|
|
return getAllMessages(teamSlug, channelSlug, threadSlug, page, 0); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* List of all thread messages |
134
|
|
|
* |
135
|
|
|
* @param teamSlug Team Slug |
136
|
|
|
* @param channelSlug Channel Slug |
137
|
|
|
* @param threadSlug Thread Slug |
138
|
|
|
* @return List of Messages |
139
|
|
|
* @throws ThreadNotFound Exception |
140
|
|
|
* @throws BaseHttpException Exception |
141
|
|
|
*/ |
142
|
|
|
public List<Message> getAllMessages(String teamSlug, String channelSlug, String threadSlug) throws ThreadNotFound, BaseHttpException { |
143
|
|
|
return getAllMessages(teamSlug, channelSlug, threadSlug, 0, 0); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Show thread message |
148
|
|
|
* |
149
|
|
|
* @param teamSlug |
150
|
|
|
* @param channelSlug |
151
|
|
|
* @param threadSlug |
152
|
|
|
* @param messageSlug |
153
|
|
|
* @return Message |
154
|
|
|
* @throws MessageNotFound Exception |
155
|
|
|
* @throws BaseHttpException Exception |
156
|
|
|
*/ |
157
|
|
|
public Message getMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws MessageNotFound, BaseHttpException { |
158
|
|
|
try { |
159
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug) |
160
|
|
|
.concat("/messages/").concat(messageSlug); |
161
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_GET); |
162
|
|
|
return (Message) Base.makeModel(Message.class, response.getBody()); |
163
|
|
|
} catch (NotFound e) { |
164
|
|
|
throw new MessageNotFound(messageSlug); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Update thread Message |
170
|
|
|
* |
171
|
|
|
* @param teamSlug |
172
|
|
|
* @param channelSlug |
173
|
|
|
* @param threadSlug |
174
|
|
|
* @param messageSlug |
175
|
|
|
* @param messageContent |
176
|
|
|
* @param type |
177
|
|
|
* @return Message |
178
|
|
|
* @throws BaseHttpException Exception |
179
|
|
|
* @throws MessageNotFound Exception |
180
|
|
|
*/ |
181
|
|
|
public Message updateMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug, String messageContent, String type) throws BaseHttpException, MessageNotFound { |
182
|
|
|
Map<String, String> parameters = new HashMap<>(); |
183
|
|
|
if (!messageContent.isEmpty()) { |
184
|
|
|
parameters.put("content", messageContent); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!type.isEmpty()) { |
188
|
|
|
parameters.put("type", type); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
try { |
192
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug) |
193
|
|
|
.concat("/messages/").concat(messageSlug); |
194
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_PATCH, parameters); |
195
|
|
|
return (Message) Base.makeModel(Message.class, response.getBody()); |
196
|
|
|
} catch (NotFound e) { |
197
|
|
|
throw new MessageNotFound(messageSlug); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Delete Message |
204
|
|
|
* |
205
|
|
|
* @param teamSlug |
206
|
|
|
* @param channelSlug |
207
|
|
|
* @param threadSlug |
208
|
|
|
* @param messageSlug |
209
|
|
|
* @return boolean value |
210
|
|
|
* @throws BaseHttpException Exception |
211
|
|
|
* @throws MessageNotFound Exception |
212
|
|
|
*/ |
213
|
|
|
public boolean deleteMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws BaseHttpException, MessageNotFound { |
214
|
|
|
|
215
|
|
|
try { |
216
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug).concat("/messages/").concat(messageSlug); |
217
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_DELETE); |
|
|
|
|
218
|
|
|
return true; |
219
|
|
|
} catch (NotFound e) { |
220
|
|
|
throw new MessageNotFound(messageSlug); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Star Thread Message |
226
|
|
|
* |
227
|
|
|
* @param teamSlug |
228
|
|
|
* @param channelSlug |
229
|
|
|
* @param threadSlug |
230
|
|
|
* @param messageSlug |
231
|
|
|
* @return boolean Value |
232
|
|
|
* @throws BaseHttpException Exception |
233
|
|
|
* @throws MessageNotFound Exception |
234
|
|
|
*/ |
235
|
|
|
public boolean starThreadMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws BaseHttpException, MessageNotFound { |
236
|
|
|
|
237
|
|
|
try { |
238
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug).concat("/messages/").concat(messageSlug).concat("/star"); |
239
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_POST); |
|
|
|
|
240
|
|
|
return true; |
241
|
|
|
} catch (NotFound e) { |
242
|
|
|
throw new MessageNotFound(messageSlug); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* UnStar Thread Message |
248
|
|
|
* |
249
|
|
|
* @param teamSlug |
250
|
|
|
* @param channelSlug |
251
|
|
|
* @param threadSlug |
252
|
|
|
* @param messageSlug |
253
|
|
|
* @return boolean Value |
254
|
|
|
* @throws BaseHttpException Exception |
255
|
|
|
* @throws MessageNotFound Exception |
256
|
|
|
*/ |
257
|
|
|
public boolean unStarThreadMessage(String teamSlug, String channelSlug, String threadSlug, String messageSlug) throws BaseHttpException, MessageNotFound { |
258
|
|
|
|
259
|
|
|
try { |
260
|
|
|
String URL = "/teams/".concat(teamSlug).concat("/channels/").concat(channelSlug).concat("/threads/").concat(threadSlug).concat("/messages/").concat(messageSlug).concat("/star"); |
261
|
|
|
Response response = this.base.sendRequest(URL, Request.METHOD_DELETE); |
|
|
|
|
262
|
|
|
return true; |
263
|
|
|
} catch (NotFound e) { |
264
|
|
|
throw new MessageNotFound(messageSlug); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|