UNPKG

@microsoft/botbuilder-m365

Version:

M365 extensions for Microsoft BotBuilder, Alpha release.

104 lines 4.88 kB
"use strict"; /** * @module botbuilder-m365 */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultTurnStateManager = void 0; const TurnState_1 = require("./TurnState"); class DefaultTurnStateManager { loadState(storage, context) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { // Compute state keys const activity = context.activity; const channelId = activity === null || activity === void 0 ? void 0 : activity.channelId; const botId = (_a = activity === null || activity === void 0 ? void 0 : activity.recipient) === null || _a === void 0 ? void 0 : _a.id; const conversationId = (_b = activity === null || activity === void 0 ? void 0 : activity.conversation) === null || _b === void 0 ? void 0 : _b.id; const userId = (_c = activity === null || activity === void 0 ? void 0 : activity.from) === null || _c === void 0 ? void 0 : _c.id; if (!channelId) { throw new Error('missing context.activity.channelId'); } if (!botId) { throw new Error('missing context.activity.recipient.id'); } if (!conversationId) { throw new Error('missing context.activity.conversation.id'); } if (!userId) { throw new Error('missing context.activity.from.id'); } const conversationKey = `${channelId}/${botId}/conversations/${conversationId}`; const userKey = `${channelId}/${botId}/users/${userId}`; // Read items from storage provider (if configured) const items = storage ? yield storage.read([conversationKey, userKey]) : {}; // Map items to state object const state = { conversation: new TurnState_1.TurnStateEntry(items[conversationKey], conversationKey), user: new TurnState_1.TurnStateEntry(items[userKey], userKey), temp: new TurnState_1.TurnStateEntry({}) }; return state; }); } saveState(storage, context, state) { return __awaiter(this, void 0, void 0, function* () { // Find changes and deletions let changes; let deletions; for (const key in state) { if (Object.prototype.hasOwnProperty.call(state, key)) { const entry = state[key]; if (entry.storageKey) { if (entry.isDeleted) { // Add to deletion list if (deletions) { deletions.push(entry.storageKey); } else { deletions = [entry.storageKey]; } } else if (entry.hasChanged) { // Add to change set if (!changes) { changes = {}; } changes[entry.storageKey] = entry.value; } } } } // Do we have a storage provider? if (storage) { // Apply changes const promises = []; if (changes) { promises.push(storage.write(changes)); } // Apply deletions if (deletions) { promises.push(storage.delete(deletions)); } // Wait for completion if (promises.length > 0) { yield Promise.all(promises); } } }); } } exports.DefaultTurnStateManager = DefaultTurnStateManager; //# sourceMappingURL=DefaultTurnStateManager.js.map