1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const botbuilder_core_1 = require("botbuilder-core");
|
11 | const statusCodeError_1 = require("./statusCodeError");
|
12 | const activityValidator_1 = require("./activityValidator");
|
13 | class ChannelServiceRoutes {
|
14 | |
15 |
|
16 |
|
17 | constructor(channelServiceHandler) {
|
18 | this.channelServiceHandler = channelServiceHandler;
|
19 | this.channelServiceHandler = channelServiceHandler;
|
20 | }
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | register(server, basePath = '') {
|
27 | server.post(basePath + '/v3/conversations/:conversationId/activities', this.processSendToConversation.bind(this));
|
28 | server.post(basePath + '/v3/conversations/:conversationId/activities/:activityId', this.processReplyToActivity.bind(this));
|
29 | server.put(basePath + '/v3/conversations/:conversationId/activities/:activityId', this.processUpdateActivity.bind(this));
|
30 | server.get(basePath + '/v3/conversations/:conversationId/activities/:activityId/members', this.processGetActivityMembers.bind(this));
|
31 | server.post(basePath + '/v3/conversations', this.processCreateConversation.bind(this));
|
32 | server.get(basePath + '/v3/conversations', this.processGetConversations.bind(this));
|
33 | server.get(basePath + '/v3/conversations/:conversationId/members', this.processGetConversationMembers.bind(this));
|
34 | server.get(basePath + '/v3/conversations/:conversationId/pagedmembers', this.processGetConversationPagedMembers.bind(this));
|
35 | server.post(basePath + '/v3/conversations/:conversationId/activities/history', this.processSendConversationHistory.bind(this));
|
36 | server.post(basePath + '/v3/conversations/:conversationId/attachments', this.processUploadAttachment.bind(this));
|
37 | server.del(basePath + '/v3/conversations/:conversationId/members/:memberId', this.processDeleteConversationMember.bind(this));
|
38 | server.del(basePath + '/v3/conversations/:conversationId/activities/:activityId', this.processDeleteActivity.bind(this));
|
39 | }
|
40 | processSendToConversation(req, res) {
|
41 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
42 | ChannelServiceRoutes.readActivity(req)
|
43 | .then((activity) => {
|
44 | this.channelServiceHandler.handleSendToConversation(authHeader, req.params.conversationId, activity)
|
45 | .then((resourceResponse) => {
|
46 | res.status(200);
|
47 | if (resourceResponse) {
|
48 | res.send(resourceResponse);
|
49 | }
|
50 | res.end();
|
51 | })
|
52 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
53 | })
|
54 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
55 | }
|
56 | processReplyToActivity(req, res) {
|
57 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
58 | ChannelServiceRoutes.readActivity(req)
|
59 | .then((activity) => {
|
60 | this.channelServiceHandler.handleReplyToActivity(authHeader, req.params.conversationId, req.params.activityId, activity)
|
61 | .then((resourceResponse) => {
|
62 | res.status(200);
|
63 | if (resourceResponse) {
|
64 | res.send(resourceResponse);
|
65 | }
|
66 | res.end();
|
67 | })
|
68 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
69 | })
|
70 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
71 | }
|
72 | processUpdateActivity(req, res) {
|
73 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
74 | ChannelServiceRoutes.readActivity(req)
|
75 | .then((activity) => {
|
76 | this.channelServiceHandler.handleUpdateActivity(authHeader, req.params.conversationId, req.params.activityId, activity)
|
77 | .then((resourceResponse) => {
|
78 | res.status(200);
|
79 | if (resourceResponse) {
|
80 | res.send(resourceResponse);
|
81 | }
|
82 | res.end();
|
83 | })
|
84 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
85 | })
|
86 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
87 | }
|
88 | processDeleteActivity(req, res) {
|
89 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
90 | this.channelServiceHandler.handleDeleteActivity(authHeader, req.params.conversationId, req.params.activityId)
|
91 | .then(() => {
|
92 | res.status(200);
|
93 | res.end();
|
94 | })
|
95 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
96 | }
|
97 | processGetActivityMembers(req, res) {
|
98 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
99 | this.channelServiceHandler.handleGetActivityMembers(authHeader, req.params.conversationId, req.params.activityId)
|
100 | .then((channelAccounts) => {
|
101 | if (channelAccounts) {
|
102 | res.send(channelAccounts);
|
103 | }
|
104 | res.status(200);
|
105 | res.end();
|
106 | })
|
107 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
108 | }
|
109 | processCreateConversation(req, res) {
|
110 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
111 | ChannelServiceRoutes.readBody(req)
|
112 | .then((conversationParameters) => {
|
113 | this.channelServiceHandler.handleCreateConversation(authHeader, conversationParameters)
|
114 | .then((conversationResourceResponse) => {
|
115 | if (conversationResourceResponse) {
|
116 | res.send(conversationResourceResponse);
|
117 | }
|
118 | res.status(201);
|
119 | res.end();
|
120 | })
|
121 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
122 | });
|
123 | }
|
124 | processGetConversations(req, res) {
|
125 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
126 | this.channelServiceHandler.handleGetConversations(authHeader, req.params.conversationId, req.query.continuationToken)
|
127 | .then((conversationsResult) => {
|
128 | if (conversationsResult) {
|
129 | res.send(conversationsResult);
|
130 | }
|
131 | res.status(200);
|
132 | res.end();
|
133 | })
|
134 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
135 | }
|
136 | processGetConversationMembers(req, res) {
|
137 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
138 | this.channelServiceHandler.handleGetConversationMembers(authHeader, req.params.conversationId)
|
139 | .then((channelAccounts) => {
|
140 | res.status(200);
|
141 | if (channelAccounts) {
|
142 | res.send(channelAccounts);
|
143 | }
|
144 | res.end();
|
145 | })
|
146 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
147 | }
|
148 | processGetConversationPagedMembers(req, res) {
|
149 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
150 | let pageSize = parseInt(req.query.pageSize);
|
151 | if (isNaN(pageSize)) {
|
152 | pageSize = undefined;
|
153 | }
|
154 | this.channelServiceHandler.handleGetConversationPagedMembers(authHeader, req.params.conversationId, pageSize, req.query.continuationToken)
|
155 | .then((pagedMembersResult) => {
|
156 | res.status(200);
|
157 | if (pagedMembersResult) {
|
158 | res.send(pagedMembersResult);
|
159 | }
|
160 | res.end();
|
161 | })
|
162 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
163 | }
|
164 | processDeleteConversationMember(req, res) {
|
165 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
166 | this.channelServiceHandler.handleDeleteConversationMember(authHeader, req.params.conversationId, req.params.memberId)
|
167 | .then((resourceResponse) => {
|
168 | res.status(200);
|
169 | res.end();
|
170 | })
|
171 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
172 | }
|
173 | processSendConversationHistory(req, res) {
|
174 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
175 | ChannelServiceRoutes.readBody(req)
|
176 | .then((transcript) => {
|
177 | this.channelServiceHandler.handleSendConversationHistory(authHeader, req.params.conversationId, transcript)
|
178 | .then((resourceResponse) => {
|
179 | if (resourceResponse) {
|
180 | res.send(resourceResponse);
|
181 | }
|
182 | res.status(200);
|
183 | res.end();
|
184 | })
|
185 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
186 | })
|
187 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
188 | }
|
189 | processUploadAttachment(req, res) {
|
190 | const authHeader = req.headers.authorization || req.headers.Authorization || '';
|
191 | ChannelServiceRoutes.readBody(req)
|
192 | .then((attachmentData) => {
|
193 | this.channelServiceHandler.handleUploadAttachment(authHeader, req.params.conversationId, attachmentData)
|
194 | .then((resourceResponse) => {
|
195 | if (resourceResponse) {
|
196 | res.send(resourceResponse);
|
197 | }
|
198 | res.status(200);
|
199 | res.end();
|
200 | })
|
201 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
202 | })
|
203 | .catch(err => { ChannelServiceRoutes.handleError(err, res); });
|
204 | }
|
205 | static readActivity(req) {
|
206 | return new Promise((resolve, reject) => {
|
207 | if (req.body) {
|
208 | try {
|
209 | const activity = activityValidator_1.validateAndFixActivity(req.body);
|
210 | resolve(activity);
|
211 | }
|
212 | catch (err) {
|
213 | reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message));
|
214 | }
|
215 | }
|
216 | else {
|
217 | let requestData = '';
|
218 | req.on('data', (chunk) => {
|
219 | requestData += chunk;
|
220 | });
|
221 | req.on('end', () => {
|
222 | try {
|
223 | const body = JSON.parse(requestData);
|
224 | const activity = activityValidator_1.validateAndFixActivity(body);
|
225 | resolve(activity);
|
226 | }
|
227 | catch (err) {
|
228 | reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message));
|
229 | }
|
230 | });
|
231 | }
|
232 | });
|
233 | }
|
234 | static readBody(req) {
|
235 | return new Promise((resolve, reject) => {
|
236 | if (req.body) {
|
237 | try {
|
238 | resolve(req.body);
|
239 | }
|
240 | catch (err) {
|
241 | reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message));
|
242 | }
|
243 | }
|
244 | else {
|
245 | let requestData = '';
|
246 | req.on('data', (chunk) => {
|
247 | requestData += chunk;
|
248 | });
|
249 | req.on('end', () => {
|
250 | try {
|
251 | const body = JSON.parse(requestData);
|
252 | resolve(body);
|
253 | }
|
254 | catch (err) {
|
255 | reject(new statusCodeError_1.StatusCodeError(botbuilder_core_1.StatusCodes.BAD_REQUEST, err.message));
|
256 | }
|
257 | });
|
258 | }
|
259 | });
|
260 | }
|
261 | static handleError(err, res) {
|
262 | if (err instanceof statusCodeError_1.StatusCodeError) {
|
263 | res.send(err.message);
|
264 | res.status(err.statusCode);
|
265 | }
|
266 | else {
|
267 | res.status(500);
|
268 | }
|
269 | res.end();
|
270 | }
|
271 | }
|
272 | exports.ChannelServiceRoutes = ChannelServiceRoutes;
|
273 |
|
\ | No newline at end of file |