Skip to content

Node.js based forum software built for the modern web

5.3 KB
Raw Blame
'use strict';

define('forum/flags/detail', [
	'components', 'benchpress', 'accounts/moderate', 'accounts/delete', 'api', 'modals', 'alerts',
], function (components, Benchpress, AccountModerate, AccountsDelete, api, modals, alerts) {
	const Detail = {};

	Detail.init = function () {
		// Update attributes
		$('#state').val(ajaxify.data.state).removeAttr('disabled');
		$('#assignee').val(ajaxify.data.assignee).removeAttr('disabled');

		$('#content > div').on('click', '[data-action]', function () {
			const action = this.getAttribute('data-action');
			const uid = $(this).parents('[data-uid]').attr('data-uid');
			const noteEl = document.getElementById('note');

			switch (action) {
				case 'assign':
					$('#assignee').val(app.user.uid);
					// falls through

				case 'update': {
					const data = $('#attributes').serializeArray().reduce((memo, cur) => {
						memo[cur.name] = cur.value;
						return memo;
					}, {});

					api.put(`/flags/${ajaxify.data.flagId}`, data).then(({ history }) => {
						alerts.success('[[flags:updated]]');
						Detail.reloadHistory(history);
					}).catch(alerts.error);
					break;
				}

				case 'addEditNote': {
					const noteEl = this.closest('[component="flag/note"]');
					let datetime;
					let value;
					if (noteEl) {
						datetime = noteEl.getAttribute('data-datetime');
						const index = noteEl.getAttribute('data-index');
						value = ajaxify.data.notes[index].content;
					}

					modals.prompt({
						title: `[[flags:${datetime ? 'edit' : 'add'}-note]]`,
						inputType: 'textarea',
						rows: 3,
						value,
						callback: (result) => {
							if (!result) {
								return;
							}

							api.post(`/flags/${ajaxify.data.flagId}/notes`, {
								note: result,
								datetime,
							}).then((payload) => {
								alerts.success('[[flags:note-added]]');
								Detail.reloadNotes(payload.notes);
								Detail.reloadHistory(payload.history);
							}).catch(alerts.error);
						},
					});
					break;
				}

				case 'appendNote':
					api.post(`/flags/${ajaxify.data.flagId}/notes`, {
						note: noteEl.value,
						datetime: parseInt(noteEl.getAttribute('data-datetime'), 10),
					}).then((payload) => {
						alerts.success('[[flags:note-added]]');
						Detail.reloadNotes(payload.notes);
						Detail.reloadHistory(payload.history);

						noteEl.removeAttribute('data-datetime');
					}).catch(alerts.error);
					break;

				case 'delete-note': {
					const datetime = parseInt(this.closest('[data-datetime]').getAttribute('data-datetime'), 10);
					modals.confirm('[[flags:delete-note-confirm]]', function (ok) {
						if (ok) {
							api.del(`/flags/${ajaxify.data.flagId}/notes/${datetime}`, {}).then((payload) => {
								alerts.success('[[flags:note-deleted]]');
								Detail.reloadNotes(payload.notes);
								Detail.reloadHistory(payload.history);
							}).catch(alerts.error);
						}
					});
					break;
				}
				case 'chat':
					require(['chat'], function (chat) {
						chat.newChat(uid);
					});
					break;

				case 'ban':
					AccountModerate.banAccount(uid, ajaxify.refresh);
					break;

				case 'unban':
					AccountModerate.unbanAccount(uid);
					break;

				case 'mute':
					AccountModerate.muteAccount(uid, ajaxify.refresh);
					break;

				case 'unmute':
					AccountModerate.unmuteAccount(uid);
					break;

				case 'delete-account':
					AccountsDelete.account(uid, ajaxify.refresh);
					break;

				case 'delete-content':
					AccountsDelete.content(uid, ajaxify.refresh);
					break;

				case 'delete-all':
					AccountsDelete.purge(uid, ajaxify.refresh);
					break;

				case 'delete-post':
					postAction('delete', api.del, `/posts/${ajaxify.data.target.pid}/state`);
					break;

				case 'purge-post':
					postAction('purge', api.del, `/posts/${ajaxify.data.target.pid}`);
					break;

				case 'restore-post':
					postAction('restore', api.put, `/posts/${ajaxify.data.target.pid}/state`);
					break;

				case 'delete-message': {
					modals.confirm('[[flags:delete-message-confirm]]', function (confirm) {
						if (!confirm) {
							return;
						}
						api.del(`/chats/${ajaxify.data.target.roomId}/messages/${ajaxify.data.target.mid}`, {}).then(ajaxify.refresh).catch(alerts.error);
					});
					break;
				}

				case 'delete-flag': {
					modals.confirm('[[flags:delete-flag-confirm]]', function (ok) {
						if (ok) {
							api.del(`/flags/${ajaxify.data.flagId}`, {}).then(() => {
								alerts.success('[[flags:flag-deleted]]');
								ajaxify.go('flags');
							}).catch(alerts.error);
						}
					});
					break;
				}
			}
		});
	};

	function postAction(action, method, path) {
		modals.confirm(`[[topic:post-${action}-confirm]]`, function (confirm) {
			if (!confirm) {
				return;
			}

			method(path).then(ajaxify.refresh).catch(alerts.error);
		});
	}

	Detail.reloadNotes = function (notes) {
		ajaxify.data.notes = notes;
		Benchpress.render('flags/detail', {
			notes: notes,
		}, 'notes').then(function (html) {
			const wrapperEl = components.get('flag/notes');
			wrapperEl.empty();
			wrapperEl.html(html);
			wrapperEl.find('span.timeago').timeago();
		});
	};

	Detail.reloadHistory = function (history) {
		app.parseAndTranslate('flags/detail', 'history', {
			history: history,
		}, function (html) {
			const wrapperEl = components.get('flag/history');
			wrapperEl.empty();
			wrapperEl.html(html);
			wrapperEl.find('span.timeago').timeago();
		});
	};

	return Detail;
});