UNPKG

pockybot

Version:

Spark bot that handles team recognition

72 lines (71 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const trigger_1 = require("../../models/trigger"); const database_1 = require("../../models/database"); const command_1 = require("../../models/command"); const constants_1 = require("../../constants"); const xmlMessageParser_1 = require("../parsers/xmlMessageParser"); const location_action_1 = require("../../models/location-action"); const logger_1 = require("../logger"); class UserLocation extends trigger_1.default { constructor(config, getUserLocationService, setUserLocationService, deleteUserLocationService) { super(); this.unknownCommandMessage = `Unknown command. Possible values are ${Object.values(location_action_1.LocationAction).join(', ')}`; this.config = config; this.getUserLocationService = getUserLocationService; this.setUserLocationService = setUserLocationService; this.deleteUserLocationService = deleteUserLocationService; } isToTriggerOn(message) { let parsedMessage = xmlMessageParser_1.default.parseNonPegMessage(message); return parsedMessage.botId === constants_1.default.botId && parsedMessage.command.toLowerCase().startsWith(command_1.Command.UserLocation); } async createMessage(message) { let args; try { args = xmlMessageParser_1.default.parseOutArgs(message); } catch (error) { logger_1.Logger.error(`[UserLocation.createMessage] Error parsing args: ${error.message}`); return { markdown: `Error parsing request: ${error.message}` }; } if (args.length < 3 || args[2].isMention) { return { markdown: `Please specify a command. Possible values are ${Object.values(location_action_1.LocationAction).join(', ')}` }; } let response; if (this.config.checkRole(message.personId, database_1.Role.Admin) || this.config.checkRole(message.personId, database_1.Role.UserLocation)) { response = await this.getAdminMessage(args, message); } else { response = await this.getNonAdminMessage(args, message); } return { markdown: response }; } async getAdminMessage(args, message) { switch (args[2].text.toLowerCase()) { case location_action_1.LocationAction.Get: return await this.getUserLocationService.getUserLocation(args, message.personId); case location_action_1.LocationAction.Set: return await this.setUserLocationService.setUserLocationAdmin(args, message.personId); case location_action_1.LocationAction.Delete: return await this.deleteUserLocationService.deleteUserLocationAdmin(args, message.personId); default: return this.unknownCommandMessage; } } async getNonAdminMessage(args, message) { switch (args[2].text.toLowerCase()) { case location_action_1.LocationAction.Get: return await this.getUserLocationService.getUserLocation(args, message.personId); case location_action_1.LocationAction.Set: return await this.setUserLocationService.setUserLocationNonAdmin(args, message.personId); case location_action_1.LocationAction.Delete: return await this.deleteUserLocationService.deleteUserLocationNonAdmin(args, message.personId); default: return this.unknownCommandMessage; } } } exports.default = UserLocation;