"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.postSave = void 0;

var _validator = _interopRequireDefault(require("validator"));

var _parse = _interopRequireWildcard(require("./parse"));

var _privileges = require("./privileges");

var _event = require("./event");

var _validateEvent = _interopRequireDefault(require("./validateEvent"));

var _reminders = require("./reminders");

var _settings = require("./settings");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const {
  fireHook
} = require.main.require('./src/plugins');

const {
  getTopicField
} = require.main.require('./src/topics');

const winston = require.main.require('winston');

const isMainPost = async ({
  pid,
  tid
}) => {
  const mainPid = await getTopicField(tid, 'mainPid');
  return parseInt(mainPid, 10) === parseInt(pid, 10);
};

const postSave = async data => {
  const {
    post
  } = data;
  let event = (0, _parse.default)(post.content); // delete event if no longer in post

  if (!post.content.match(_parse.inPost)) {
    const existed = await (0, _event.eventExists)(post.pid);

    if (existed) {
      const existingEvent = await (0, _event.getEvent)(post.pid);
      await (0, _reminders.notify)({
        event: existingEvent,
        message: `[[calendar:event_deleted, ${existingEvent.name}]]`
      });
      await (0, _event.deleteEvent)(post.pid);
      winston.verbose(`[plugin-calendar] Event (pid:${post.pid}) deleted`);
    }

    return data;
  }

  const invalid = () => {
    post.content = post.content.replace(/\[(\/?)event\]/g, '[$1event-invalid]');
    return data;
  };

  if (!event) {
    return invalid();
  }

  const [failed, failures] = (0, _validateEvent.default)(event);

  if (failed) {
    const obj = failures.reduce((val, failure) => _objectSpread({}, val, {
      [failure]: event[failure]
    }), {});
    winston.verbose(`[plugin-calendar] Event (pid:${post.pid}) validation failed: `, obj);
    return invalid();
  }

  const main = post.isMain || data.data.isMain || (await isMainPost(post));

  if (!main && (await (0, _settings.getSetting)('mainPostOnly'))) {
    return invalid();
  }

  if (!(await (0, _privileges.canPostEvent)(post.tid, post.uid))) {
    return invalid();
  }

  if (event.mandatory && !(await (0, _privileges.canPostMandatoryEvent)(post.tid, post.uid))) {
    return invalid();
  }

  event.name = _validator.default.escape(event.name);
  event.location = event.location.trim();
  event.description = event.description.trim();
  event.pid = post.pid;
  event.uid = post.uid;
  event.reminders = JSON.stringify(event.reminders);
  event.repeats = JSON.stringify(event.repeats);
  event = await fireHook('filter:plugin-calendar.event.post', event);

  if (event) {
    await (0, _event.saveEvent)(event);
    winston.verbose(`[plugin-calendar] Event (pid:${event.pid}) saved`);
  }

  return data;
};

exports.postSave = postSave;
//# sourceMappingURL=postSave.js.map