UNPKG

pockybot

Version:

Spark bot that handles team recognition

73 lines (72 loc) 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultDeleteUserLocationService = void 0; const location_action_1 = require("../../models/location-action"); const logger_1 = require("../logger"); const constants_1 = require("../../constants"); const command_1 = require("../../models/command"); class DefaultDeleteUserLocationService { constructor(dbLocation) { this.dbLocation = dbLocation; } async deleteUserLocationAdmin(args, personId) { if (args.length < 4) { return 'Please specify a list of mentions/me'; } const users = args.filter((item, index) => index > 2); if (users.length === 1 && !users[0].isMention) { if (users[0].text !== 'me') { return `Invalid person ${users[0].text}. Either specify 'me' or mention a person`; } try { await this.dbLocation.deleteUserLocation(personId); return 'Location has been deleted'; } catch (error) { logger_1.Logger.error(`[UserLocation.setUserLocation] Error deleting location for user ${personId}: ${error.message}`); return 'Error deleting location'; } } if (users.some(x => !x.isMention)) { return 'Mixed mentions and non mentions not allowed'; } const usersPromise = users.map(async (x) => { try { await this.dbLocation.deleteUserLocation(x.userId); return { user: x.text, success: true }; } catch (error) { logger_1.Logger.error(`[UserLocation.setUserLocation] Error deleting location for user ${x.userId}: ${error.message}`); return { user: x.text, success: false }; } }); const result = await Promise.all(usersPromise); if (result.some(x => !x.success)) { return `Location deleting was unsuccessful for user(s): ${result.filter(x => !x.success).map(x => `'${x.user}'`).join(', ')}`; } return 'Location has been deleted'; } async deleteUserLocationNonAdmin(args, personId) { if (args.length !== 4) { return `Usage: \`@${constants_1.default.botName} ${command_1.Command.UserLocation} ${location_action_1.LocationAction.Delete} me\``; } if (args[3].isMention || args[3].text.toLowerCase() !== 'me') { return 'Permission denied. You are only allowed to delete the location for yourself (use \'me\')'; } try { await this.dbLocation.deleteUserLocation(personId); return 'Location has been deleted'; } catch (error) { logger_1.Logger.error(`[UserLocation.setUserLocation] Error deleting location for user ${personId}: ${error.message}`); return 'Error deleting location'; } } } exports.DefaultDeleteUserLocationService = DefaultDeleteUserLocationService;