Skip to content

Node.js based forum software built for the modern web

3.5 KB
Raw Blame
'use strict';

define('forum/topic/diffs', [
	'api', 'modals', 'alerts', 'benchpress', 'forum/topic/images',
], function (api, modals, alerts, Benchpress) {
	const Diffs = {};
	const localeStringOpts = { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric' };

	Diffs.open = function (pid) {
		if (!config.enablePostHistory) {
			return;
		}

		api.get(`/posts/${encodeURIComponent(pid)}/diffs`, {}).then((data) => {
			parsePostHistory(data).then(async (html) => {
				const $modal = await modals.dialog({
					title: '[[topic:diffs.title]]',
					message: html,
					size: 'large',
					onEscape: true,
					backdrop: true,
				});

				if (!data.timestamps.length) {
					return;
				}

				const $selectEl = $modal.find('select');
				const $revertEl = $modal.find('button[data-action="restore"]');
				const $deleteEl = $modal.find('button[data-action="delete"]');
				const $postContainer = $modal.find('ul.posts-list');
				const $numberOfDiffCon = $modal.find('.number-of-diffs strong');

				$selectEl.on('change', function () {
					Diffs.load(pid, this.value, $postContainer);
					$revertEl.prop('disabled', data.timestamps.indexOf(this.value) === 0);
					$deleteEl.prop('disabled', data.timestamps.indexOf(this.value) === 0);
				});

				$revertEl.on('click', function () {
					Diffs.restore(pid, $selectEl.val(), $modal);
				});

				$deleteEl.on('click', function () {
					Diffs.delete(pid, $selectEl.val(), $selectEl, $numberOfDiffCon);
				});

				$modal.on('shown.bs.modal', function () {
					Diffs.load(pid, $selectEl.val(), $postContainer);
					$revertEl.prop('disabled', true);
					$deleteEl.prop('disabled', true);
				});
			});
		}).catch(alerts.error);
	};

	Diffs.load = function (pid, since, $postContainer) {
		if (!config.enablePostHistory) {
			return;
		}

		api.get(`/posts/${encodeURIComponent(pid)}/diffs/${since}`, {}).then((data) => {
			data.deleted = !!parseInt(data.deleted, 10);

			app.parseAndTranslate('partials/posts_list', 'posts', {
				posts: [data],
			}, function ($html) {
				$postContainer.empty().append($html);
				$postContainer.find('.timeago').timeago();
			});
		}).catch(alerts.error);
	};

	Diffs.restore = function (pid, since, $modal) {
		if (!config.enablePostHistory) {
			return;
		}

		api.put(`/posts/${encodeURIComponent(pid)}/diffs/${since}`, {}).then(() => {
			$modal.modal('hide');
			alerts.success('[[topic:diffs.post-restored]]');
		}).catch(alerts.error);
	};

	Diffs.delete = function (pid, timestamp, $selectEl, $numberOfDiffCon) {
		api.del(`/posts/${encodeURIComponent(pid)}/diffs/${timestamp}`).then((data) => {
			parsePostHistory(data, 'diffs').then((html) => {
				$selectEl.empty().append(html);
				$selectEl.trigger('change');
				const numberOfDiffs = $selectEl.find('option').length;
				$numberOfDiffCon.text(numberOfDiffs);
				alerts.success('[[topic:diffs.deleted]]');
			});
		}).catch(alerts.error);
	};

	async function parsePostHistory(data, blockName) {
		const params = [{
			diffs: data.revisions.map(function (revision) {
				const timestamp = parseInt(revision.timestamp, 10);

				return {
					username: revision.username,
					timestamp: timestamp,
					pretty: new Date(timestamp).toLocaleString(config.userLang.replace('_', '-'), localeStringOpts),
				};
			}),
			numDiffs: data.timestamps.length,
			editable: data.editable,
			deletable: data.deletable,
		}];

		if (blockName) {
			params.unshift(blockName);
		}

		return await Benchpress.render('modals/post-history', ...params);
	}

	return Diffs;
});