This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
LegacyConversationWebservice.java
316 lines (271 loc) · 14.2 KB
/
LegacyConversationWebservice.java
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright 2017 Redlink GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.redlink.smarti.webservice;
import io.redlink.smarti.model.*;
import io.redlink.smarti.model.config.Configuration;
import io.redlink.smarti.model.result.Result;
import io.redlink.smarti.services.ClientService;
import io.redlink.smarti.services.ConfigurationService;
import io.redlink.smarti.services.ConversationService;
import io.redlink.smarti.services.QueryBuilderService;
import io.redlink.smarti.utils.ResponseEntities;
import io.redlink.smarti.webservice.pojo.QueryUpdate;
import io.redlink.smarti.webservice.pojo.TemplateResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @deprecated to be removed in favor of {@link ConversationWebservice}
*/
@CrossOrigin
@RestController
@RequestMapping(value = "legacy/conversation",
produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
@Api("conversation-legacy")
@Deprecated
public class LegacyConversationWebservice {
private final Logger log = LoggerFactory.getLogger(getClass());
@SuppressWarnings("unused")
private enum Vote {
up(1),
down(-1);
private final int delta;
Vote(int delta) {
this.delta = delta;
}
public int getDelta() {
return delta;
}
}
//@Autowired
//private StoreService storeService;
@Autowired
private ConversationService conversationService;
@Autowired
private QueryBuilderService queryBuilderService;
@Autowired
private ConfigurationService configService;
@Autowired
private ClientService clientService;
@ApiOperation(value = "create a conversation")
@ApiResponses({
@ApiResponse(code = 201, message = "Created", response = Conversation.class)
})
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> createConversation(@RequestBody(required = false) Conversation conversation) {
conversation = Optional.ofNullable(conversation).orElseGet(Conversation::new);
// Create a new Conversation -> id must be null
conversation.setId(null);
//TODO: set the owner of the conversation based on the current user
//FIXME for now the owner needs to be parsed with the conversation!
Client client = clientService.get(conversation.getOwner());
if(client == null){
throw new IllegalStateException("Owner for conversation " + conversation.getId() + " not found!");
}
return ResponseEntity.status(HttpStatus.CREATED).body(conversationService.update(client, conversation));
}
@ApiOperation(value = "retrieve a conversation", response = Conversation.class)
@RequestMapping(value = "{id}", method = RequestMethod.GET)
public ResponseEntity<?> getConversation(@PathVariable("id") ObjectId id) {
//TODO get the client for the authenticated user
final Conversation conversation = conversationService.getConversation(null, id);
//TODO: check that the client has the right to access the conversation
if (conversation == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(conversation);
}
}
@ApiOperation(value = "update a conversation", response = Conversation.class)
@RequestMapping(value = "{id}", method = RequestMethod.PUT, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE)
public ResponseEntity<?> updateConversation(@PathVariable("id") ObjectId id,
@RequestBody Conversation conversation) {
// make sure the id is the right one
conversation.setId(id);
//TODO: check that the authenticated user has rights to update messages of this client
Client client = clientService.get(conversation.getOwner());
if(client == null){
throw new IllegalStateException("Owner for conversation " + conversation.getId() + " not found!");
}
//TODO: check that the
// * authenticated user has rights to update messages of this client
// * the user is from the client the stored conversation as as owner
return ResponseEntity.ok(conversationService.update(client, conversation));
}
@ApiOperation(value = "append a message to the conversation", response = Conversation.class)
@RequestMapping(value = "{id}/message", method = RequestMethod.POST, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE)
public ResponseEntity<?> addMessage(@PathVariable("id") ObjectId id,
@RequestBody Message message) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
if (conversation == null) {
return ResponseEntity.notFound().build();
}
//TODO: check that the authenticated user has rights to update messages of this client
Client client = clientService.get(conversation.getOwner());
if(client == null){
throw new IllegalStateException("Owner for conversation " + conversation.getId() + " not found!");
}
return ResponseEntity.ok(conversationService.appendMessage(client, conversation, message));
}
@ApiOperation(value = "up-/down-vote a message within a conversation", response = Conversation.class)
@RequestMapping(value = "{id}/message/{messageId}/{vote}", method = RequestMethod.PUT)
public ResponseEntity<?> rateMessage(@PathVariable("id") ObjectId id,
@PathVariable("messageId") String messageId,
@PathVariable("vote") Vote vote) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
if (conversation == null) {
return ResponseEntity.notFound().build();
}
//TODO: check that the authenticated user has rights to up/down vote the conversation
return ResponseEntity.ok(conversationService.rateMessage(conversation, messageId, vote.getDelta()));
}
@ApiOperation(value = "retrieve the analysis result of the conversation", response = Token.class, responseContainer = "List")
@RequestMapping(value = "{id}/analysis", method = RequestMethod.GET)
public ResponseEntity<?> prepare(@PathVariable("id") ObjectId id) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
//TODO: check that the authenticated user has rights to access the conversation
if (conversation == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(conversation.getTokens());
}
}
@ApiOperation(value = "retrieve the intents of the conversation", response = TemplateResponse.class)
@RequestMapping(value = "{id}/template", method = RequestMethod.GET)
public ResponseEntity<?> query(@PathVariable("id") ObjectId id) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
//TODO: check that the authenticated user has rights to access the conversation
if (conversation == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(TemplateResponse.from(conversation));
}
}
@ApiOperation(value = "retrieve the results for a template from a specific creator", response = Result.class, responseContainer = "List")
@RequestMapping(value = "{id}/template/{template}/{creator}", method = RequestMethod.GET)
public ResponseEntity<?> getResults(@PathVariable("id") ObjectId id,
@PathVariable("template") int templateIdx,
@PathVariable("creator") String creator) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
if (conversation == null) {
return ResponseEntity.notFound().build();
}
//TODO: with authentication in place we need to get the Client from the user and NOT the conversation!!
Client client = clientService.get(conversation.getOwner());
if(client == null){
throw new IllegalStateException("Owner for conversation " + conversation.getId() + " not found!");
}
try {
final Template template = conversation.getTemplates().get(templateIdx);
return ResponseEntity.ok(conversationService.getInlineResults(client, conversation, template, creator));
} catch (IOException e) {
return ResponseEntities.serviceUnavailable(e.getMessage(), e);
} catch (IndexOutOfBoundsException e) {
return ResponseEntity.notFound().build();
}
}
@ApiOperation(value = "update a query based on new slot-assignments", response = Query.class)
@RequestMapping(value = "{id}/query/{template}/{creator}", method = RequestMethod.POST, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getQuery(@PathVariable("id") ObjectId id,
@PathVariable("template") int templateIdx,
@PathVariable("creator") String creator,
@RequestBody QueryUpdate queryUpdate) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
if (conversation == null) {
return ResponseEntity.notFound().build();
}
//TODO: with authentication in place we need to get the Client from the user and NOT the conversation!!
Client client = clientService.get(conversation.getOwner());
if(client == null){
throw new IllegalStateException("Owner for conversation " + conversation.getId() + " not found!");
}
Configuration clientConf = configService.getClientConfiguration(client.getId());
if(clientConf == null){
log.info("Client {} of Conversation {} has no longer a configuration assigned ... returning 404 NOT FOUND",
conversation.getChannelId(), conversation.getId());
return ResponseEntity.notFound().build();
}
final Template template = conversation.getTemplates().get(templateIdx);
if (template == null) return ResponseEntity.notFound().build();
//NOTE: conversationService.getConversation(..) already update the queries if necessary
//so at this place we only need to retrieve the requested query
Optional<Query> query = template.getQueries().stream().filter(q -> Objects.equals(creator, q.getCreator())).findFirst();
return query.isPresent() ? ResponseEntity.ok(query.get()) : ResponseEntity.notFound().build();
}
@ApiOperation(value = "complete a conversation and add it to indexing", response = Conversation.class)
@RequestMapping(value = "{id}/publish", method = RequestMethod.POST)
public ResponseEntity<?> complete(@PathVariable("id") ObjectId id) {
//TODO: get the Client for the currently authenticated user
final Conversation conversation = conversationService.getConversation(null, id); //TODO: parse the client instead of null
if (conversation == null) {
return ResponseEntity.notFound().build();
} else {
//TODO: only the owner of the conversation can publish it!
conversation.getMeta().setStatus(ConversationMeta.Status.Complete);
return ResponseEntity.ok(conversationService.completeConversation(conversation));
}
}
/**
*/
@ApiModel
public static class TemplateResponse {
@ApiModelProperty(value = "Tokens extracted")
private List<Token> tokens = new ArrayList<>();
@ApiModelProperty(value = "Templates for possible queries")
private List<Template> templates = new ArrayList<>();
public List<Token> getTokens() {
return tokens;
}
public void setTokens(List<Token> tokens) {
this.tokens = tokens;
}
public List<Template> getTemplates() {
return templates;
}
public void setTemplates(List<Template> templates) {
this.templates = templates;
}
public static TemplateResponse from(Conversation conversation) {
final TemplateResponse response = new TemplateResponse();
response.tokens = conversation.getTokens();
response.templates = conversation.getTemplates();
return response;
}
}
}