Skip to content

Commit

Permalink
fix: middlewares in scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 8, 2018
1 parent e7340c9 commit 9b1cbf5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
42 changes: 21 additions & 21 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ export default class Alice {
const sessionId = selectSessionId(req)
const session = this.sessions.findOrCreate(sessionId)

/*
* Initializing context of the request
*/
const ctxDefaultParams = {
req,
session,
sendResponse: sendResponse || null,
/*
* if Alice is listening on express.js port, add this server instance
* to the context
*/
server: this.server || null,
middlewares: this.middlewares,
}
const ctxInstance = new Ctx(ctxDefaultParams)
const ctxWithMiddlewares = await applyMiddlewares(this.middlewares, ctxInstance)

/* check whether current scene is not defined */
if (!session.getData('currentScene')) {
session.setData('currentScene', null)
Expand All @@ -129,12 +146,12 @@ export default class Alice {
*/
if (matchedScene) {
if (matchedScene.isLeaveCommand(requestedCommandName)) {
matchedScene.handleRequest(req, sendResponse, session)
await matchedScene.handleRequest(req, sendResponse, ctxWithMiddlewares)
session.setData('currentScene', null)
return true
} else {
const sceneResponse = await matchedScene.handleRequest(
req, sendResponse, session,
req, sendResponse, ctxWithMiddlewares,
)
if (sceneResponse) {
return true
Expand All @@ -150,31 +167,14 @@ export default class Alice {
if (matchedScene) {
session.setData('currentScene', matchedScene.name)
const sceneResponse = await matchedScene.handleRequest(
req, sendResponse, session,
req, sendResponse, ctxWithMiddlewares,
)
if (sceneResponse) {
return true
}
}
}

/*
* Initializing context of the request
*/
const ctxDefaultParams = {
req,
session,
sendResponse: sendResponse || null,
/*
* if Alice is listening on express.js port, add this server instance
* to the context
*/
server: this.server || null,
middlewares: this.middlewares,
}
const ctxInstance = new Ctx(ctxDefaultParams)
const ctxWithMiddlewares = await applyMiddlewares(this.middlewares, ctxInstance)

const requestedCommands = await this.commands.search(ctxWithMiddlewares)
/*
* Если новая сессия, то запускаем стартовую команду
Expand Down Expand Up @@ -216,7 +216,7 @@ export default class Alice {
public async handleRequest(
req: WebhookRequest,
sendResponse?: (res: WebhookResponse) => void,
) {
): Promise<any> {
return await this.handleRequestBody(req, sendResponse)
}

Expand Down
4 changes: 2 additions & 2 deletions src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default class Ctx implements CtxInterface {
public replyBuilder: ReplyBuilder
public buttonBuilder: ButtonBuilder

public sendResponse: (response: string) => void
public enterScene: () => void
public sendResponse: (response: WebhookResponse) => void
public enterScene: (sceneName: string) => void
public leaveScene: () => void
constructor(params) {
const {
Expand Down
32 changes: 17 additions & 15 deletions src/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Command from './Command'
import Ctx from './ctx'

import { configInterface } from './types/alice'
import { WebhookRequest, WebhookResponse } from './types/webhook'
import { CtxInterface } from 'ctx'

const selectCommand = (req) => req.request.command

Expand Down Expand Up @@ -70,32 +72,32 @@ export default class Scene extends Alice {
return this.leaveCommand.name.toLowerCase() === commandName.toLowerCase()
}

public async handleRequest(req, sendResponse, session) {
const requestedCommandName = selectCommand(req)
const requestedCommands = this.commands.search(requestedCommandName)
public async handleRequest(
req: WebhookRequest,
sendResponse: (res: WebhookResponse) => void,
ctx: CtxInterface,
): Promise<any> {

if (this.isLeaveCommand(requestedCommandName)) {
ctx.sendResponse = sendResponse
ctx.leaveScene = super._handleLeaveScene
ctx.enterScene = super._handleEnterScene

const requestedCommands = await this.commands.search(ctx)

if (this.isLeaveCommand(ctx.message)) {
this._handleLeaveScene()
}

const ctx = new Ctx({
req,
sendResponse: sendResponse || null,
leaveScene: super._handleLeaveScene,
enterScene: super._handleEnterScene,
session,
})

if (requestedCommands.length !== 0) {
const requestedCommand = requestedCommands[0]
return await requestedCommand.callback.call(this, ctx)
return await requestedCommand.callback(ctx)
}

if (this.anyCallback) {
return this.anyCallback(ctx)
return await this.anyCallback(ctx)
}

return null
return Promise.resolve()
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/types/ctx.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { WebhookRequest } from './webhook'
import { WebhookRequest, WebhookResponse } from './webhook'
// import { SessionInterface } from './types/session'
import ReplyBuilder from '../replyBuilder'
import ButtonBuilder from '../buttonBuilder'

export interface CtxInterface {
req: WebhookRequest
Expand All @@ -7,14 +10,14 @@ export interface CtxInterface {
userId: string
payload: {}
message: string
// session: Session
session: {}

// command?: Command

// replyBuilder: ReplyBuilder
// buttonBuilder: ButtonBuilder
replyBuilder: ReplyBuilder
buttonBuilder: ButtonBuilder

sendResponse: (response: string) => void
enterScene: () => void
sendResponse: (response: WebhookResponse) => void
enterScene: (sceneName: string) => void
leaveScene: () => void
}

0 comments on commit 9b1cbf5

Please sign in to comment.