Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refractor public/src/admin/manage/registration.js so there is nothing nested more than 4 levels deep #588

Open
wants to merge 13 commits into
base: f24
Choose a base branch
from
2 changes: 1 addition & 1 deletion public/src/client/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ define('forum/register', [
if (results.every(obj => obj.status === 'rejected')) {
showSuccess(usernameInput, username_notify, successIcon);
} else {
showError(usernameInput, username_notify, '[[error:username-taken]]');
showError(usernameInput, `Username is taken Maybe try "${username}suffix"`);
}

callback();
Expand Down
68 changes: 68 additions & 0 deletions src/admin/manage/registration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

// Define a global `define` if it's not already defined
/* global define */

define(['jquery', 'socket', 'alerts', 'bootbox'], ($, socket, alerts, bootbox) => {
const Registration = {};

Registration.init = () => {
$('.users-list').on('click', '[data-action]', function () {
const parent = $(this).parents('[data-username]');
const action = $(this).attr('data-action');
const username = parent.attr('data-username');
const method = action === 'accept' ? 'user.acceptRegistration' : 'user.rejectRegistration';

socket.emit(method, { username }, (err) => {
if (err) {
alerts.error(err);
} else {
parent.remove();
}
});

return false;
});
$('.invites-list').on('click', '[data-action]', function () {
const parent = $(this).parents('[data-invitation-mail][data-invited-by]');
const email = parent.attr('data-invitation-mail');
const invitedBy = parent.attr('data-invited-by');
const action = $(this).attr('data-action');
const method = 'user.deleteInvitation';

if (action === 'delete') {
bootbox.confirm('[[admin/manage/registration:invitations.confirm-delete]]', (confirm) => {
helper(confirm, email, invitedBy, method, () => {
const nextRow = parent.next();
const thisRowInvitedBy = parent.find('.invited-by');
const nextRowInvitedBy = nextRow.find('.invited-by');

if (nextRowInvitedBy.html() && nextRowInvitedBy.html().length < 2) {
nextRowInvitedBy.html(thisRowInvitedBy.html());
}

parent.remove();
});
});
}

return false;
});
};

return Registration;
});

function helper(confirm, email, invitedBy, method, removeRow) {
if (confirm) {
require(['socket', 'alerts'], (socket, alerts) => {
socket.emit(method, { email, invitedBy }, (err) => {
if (err) {
alerts.error(err);
} else {
removeRow();
}
});
});
}
}
24 changes: 22 additions & 2 deletions src/user/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'uid', 'postcount', 'topiccount', 'reputation', 'profileviews',
'banned', 'banned:expire', 'email:confirmed', 'joindate', 'lastonline',
'lastqueuetime', 'lastposttime', 'followingCount', 'followerCount',
'blocksCount', 'passwordExpiry', 'mutedUntil',
'blocksCount', 'passwordExpiry', 'mutedUntil',

Check failure on line 18 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
];

module.exports = function (User) {
Expand All @@ -25,7 +25,7 @@
'aboutme', 'signature', 'uploadedpicture', 'profileviews', 'reputation',
'postcount', 'topiccount', 'lastposttime', 'banned', 'banned:expire',
'status', 'flags', 'followerCount', 'followingCount', 'cover:url',
'cover:position', 'groupTitle', 'mutedUntil', 'mutedReason',
'cover:position', 'groupTitle', 'mutedUntil', 'mutedReason', 'saved'

Check failure on line 28 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
];

User.guestData = {
Expand Down Expand Up @@ -367,4 +367,24 @@
plugins.hooks.fire('action:user.set', { uid: uid, field: field, value: newValue, type: type });
return newValue;
}

User.addSaved = async function (uid, value) {
const cleanValue = validator.escape(value ? value.toString() : '');

Check failure on line 372 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
let currentValues = await db.getObjectField(`user:${uid}`, field);

Check failure on line 373 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

Check failure on line 373 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

'field' is not defined
if (!currentValues) {

Check failure on line 374 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
currentValues = [];

Check failure on line 375 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces
} else if (typeof currentValues === 'string') {

Check failure on line 376 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
try {

Check failure on line 377 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces
currentValues = JSON.parse(currentValues);

Check failure on line 378 in src/user/data.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces
} catch (err) {
currentValues = [currentValues];
}
}
if (!Array.isArray(currentValues)) {
currentValues = [currentValues];
}
currentValues.push(cleanValue);
await db.setObjectField(`user:${uid}`, field, JSON.stringify(currentValues));
plugins.hooks.fire('action:user.set', { uid, field, value: cleanValue, type: 'append' });
}
};