Skip to content

Commit

Permalink
Tomlm/create conversation (#1233)
Browse files Browse the repository at this point in the history
* tweak conversationAPI to relax restrictions

* Updated validation to omit checks for members

* Removed linting rule override
  • Loading branch information
Justin Wilaby authored Jan 16, 2019
1 parent 023c955 commit 0f2e107
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 37 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cd BotFramework-Emulator
> npm version 5.6.0 or greater is required.
```
npm i -g lerna@3.4.0 webpack@4.8.x jest
npm i -g lerna@3.4.0 webpack@4.8.x webpack-cli jest
```

> **NOTE:** If you are using Linux, building the Emulator might result in an error due to a missing package: **libXScrnSaver**. If you run into this error, install the package using your OS's package manager and retry:
Expand Down
8 changes: 5 additions & 3 deletions packages/app/main/src/restServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

import { SharedConstants } from '@bfemulator/app-shared';
import { BotEmulator, Conversation } from '@bfemulator/emulator-core';
import ConversationSet from '@bfemulator/emulator-core/lib/facility/conversationSet';
import LogLevel from '@bfemulator/emulator-core/lib/types/log/level';
import { networkRequestItem, networkResponseItem, textItem } from '@bfemulator/emulator-core/lib/types/log/util';
import ConversationSet from '@bfemulator/emulator-core/lib/facility/conversationSet';
import { IEndpointService } from 'botframework-config';
import { createServer, Request, Response, Route, Server } from 'restify';
import CORS from 'restify-cors-middleware';
Expand Down Expand Up @@ -171,6 +171,8 @@ function getConversationId(req: ConversationAwareRequest): string {
}

function hasLiveChat(conversationId: string, conversationSet: ConversationSet): boolean {
return !!conversationSet.conversationById(conversationId) ||
!!conversationSet.conversationById(conversationId + '|livechat');
if (conversationId.endsWith('|livechat')) {
return !!conversationSet.conversationById(conversationId);
}
return !!conversationSet.conversationById(conversationId + '|livechat');
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ describe('The conversations middleware', () => {
emulator.facilities.users.currentUserId = '456';
emulator.facilities.logger = { logActivity: () => null } as any;
emulator.facilities.attachments = new Attachments();
emulator.options = {
tunnelingServiceUrl: 'https://localhost:8888'
};
emulator.options = {};
});

it('should create a new conversation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

import * as HttpStatus from 'http-status-codes';
import * as Restify from 'restify';

import BotEmulator from '../../botEmulator';
import BotEndpoint from '../../facility/botEndpoint';
import Conversation from '../../facility/conversation';
import ConversationParameters from '../../types/activity/conversationParameters';
import createConversationResponse from '../../utils/createResponse/conversation';
import sendErrorResponse from '../../utils/sendErrorResponse';
import uniqueId from '../../utils/uniqueId';
import { validateCreateConversationRequest } from './errorCondition/createConversationValidator';

export default function createConversation(botEmulator: BotEmulator) {
Expand All @@ -49,8 +49,7 @@ export default function createConversation(botEmulator: BotEmulator) {
const conversationParameters = req.body as ConversationParameters;
const error = validateCreateConversationRequest(
conversationParameters,
botEndpoint,
botEmulator.facilities.users.currentUserId);
botEndpoint);

if (error) {
sendErrorResponse(req, res, next, error.toAPIException());
Expand All @@ -76,7 +75,9 @@ function getConversation(params: ConversationParameters, emulator: BotEmulator,
}

if (!conversation) {
const { id, name } = params.members[0];
const { members = [] } = params;
const [member] = members;
const { id = uniqueId(), name = 'User' } = (member || {});
conversation = emulator.facilities.conversations.newConversation(
emulator,
endpoint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@ import { ErrorCodes } from '../../../types/errorCodes';
import createAPIException from '../../../utils/createResponse/apiException';

class CreateConversationError {
public static MEMBERS_MISSING = new CreateConversationError(
ErrorCodes.MissingProperty,
'The "members" parameter is required.');

public static TOO_MANY_MEMBERS = new CreateConversationError(
ErrorCodes.BadSyntax,
'The Emulator only supports creating conversation with 1 member.');

public static WRONG_USER = new CreateConversationError(
ErrorCodes.BadSyntax,
'The Emulator only supports creating conversation with the current user.'
);

public static BOT_MISSING = new CreateConversationError(
ErrorCodes.MissingProperty,
'The "Bot" parameter is required'
);

public static BOT_ID_MISMATCH = new CreateConversationError(
ErrorCodes.BadArgument,
'conversationParameters.bot.id doesn\'t match security bot id'
);

public static APP_ID_MISSING = new CreateConversationError(
ErrorCodes.MissingProperty,
'The Emulator only supports bot-created conversation with AppID-bearing bot'
Expand All @@ -49,29 +36,18 @@ class CreateConversationError {

Object.freeze(CreateConversationError);

function validateCreateConversationRequest(params: ConversationParameters, endpoint: BotEndpoint, userId: string)
function validateCreateConversationRequest(params: ConversationParameters, endpoint: BotEndpoint)
: CreateConversationError {
if (!params.members) {
return CreateConversationError.MEMBERS_MISSING;
}

if (params.members.length !== 1) {
if (params.members && params.members.length > 1) {
return CreateConversationError.TOO_MANY_MEMBERS;
}

if ('' + params.members[0].id !== '' + userId) {
return CreateConversationError.WRONG_USER;
}

if (!params.bot) {
return CreateConversationError.BOT_MISSING;

}

if (params.bot.id !== endpoint.botId) {
return CreateConversationError.BOT_ID_MISMATCH;
}

if (!endpoint) {
return CreateConversationError.APP_ID_MISSING;
}
Expand Down

0 comments on commit 0f2e107

Please sign in to comment.