-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(mapping): mapping api * revmap * security * better naming * fix * rename * more tests * security tests * security tests * doc
- Loading branch information
1 parent
aae6946
commit b94d3e5
Showing
11 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Endpoint { | ||
channel: { name: string; version: string } | ||
identity: string | ||
sender: string | ||
thread: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Endpoint } from '@botpress/messaging-base' | ||
import { Response } from 'express' | ||
import { ApiManager } from '../base/api-manager' | ||
import { ClientApiRequest } from '../base/auth/client' | ||
import { ChannelService } from '../channels/service' | ||
import { ConversationService } from '../conversations/service' | ||
import { makeMapRequestSchema, Schema } from './schema' | ||
import { MappingService } from './service' | ||
|
||
export class MappingApi { | ||
constructor( | ||
private channels: ChannelService, | ||
private conversations: ConversationService, | ||
private mapping: MappingService | ||
) {} | ||
|
||
setup(router: ApiManager) { | ||
router.post('/endpoints/map', makeMapRequestSchema(this.channels.list()), this.map.bind(this)) | ||
router.get('/endpoints/conversation/:conversationId', Schema.Api.List, this.list.bind(this)) | ||
} | ||
|
||
async map(req: ClientApiRequest, res: Response) { | ||
const endpoint = req.body as Endpoint | ||
|
||
const channel = this.channels.getByNameAndVersion(endpoint.channel.name, endpoint.channel.version) | ||
const { conversationId } = await this.mapping.getMapping(req.clientId, channel.meta.id, endpoint) | ||
|
||
res.send({ conversationId }) | ||
} | ||
|
||
async list(req: ClientApiRequest, res: Response) { | ||
const { conversationId } = req.params | ||
|
||
const conversation = await this.conversations.fetch(conversationId) | ||
if (!conversation || conversation.clientId !== req.clientId) { | ||
return res.sendStatus(404) | ||
} | ||
|
||
const convmaps = await this.mapping.convmap.listByConversationId(conversationId) | ||
const endpoints = [] | ||
|
||
for (const convmap of convmaps) { | ||
const endpoint = await this.mapping.getEndpoint(convmap.threadId) | ||
const tunnel = await this.mapping.tunnels.get(convmap.tunnelId) | ||
const channel = this.channels.getById(tunnel!.channelId) | ||
|
||
endpoints.push({ channel: { name: channel.meta.name, version: channel.meta.version }, ...endpoint }) | ||
} | ||
|
||
res.send(endpoints) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Channel } from '@botpress/messaging-channels' | ||
import Joi from 'joi' | ||
import { ReqSchema } from '../base/schema' | ||
|
||
export const makeMapRequestSchema = (channels: Channel[]) => { | ||
return ReqSchema({ | ||
body: { | ||
channel: Joi.alternatives( | ||
channels.map((x) => | ||
Joi.object({ | ||
name: Joi.string().valid(x.meta.name).required(), | ||
version: Joi.string().valid(x.meta.version).required() | ||
}) | ||
) | ||
).required(), | ||
identity: Joi.string().required(), | ||
sender: Joi.string().required(), | ||
thread: Joi.string().required() | ||
} | ||
}) | ||
} | ||
|
||
const Api = { | ||
List: ReqSchema({ | ||
params: { | ||
conversationId: Joi.string().guid().required() | ||
} | ||
}) | ||
} | ||
|
||
export const Schema = { Api } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters