refactor(writeapi): update route prefix to api/v3, default error option

v1.18.x
Julian Lam 4 years ago
parent f67824719c
commit 3ea1aa4780

@ -54,7 +54,7 @@ define('admin/manage/admins-mods', ['translator', 'benchpress', 'autocomplete',
$('.global-moderator-area').prepend(html);
$('#no-global-mods-warning').addClass('hidden');
});
}, err => app.alertError(err.status.message));
}, 'default');
});
$('.global-moderator-area').on('click', '.remove-user-icon', function () {
@ -69,7 +69,7 @@ define('admin/manage/admins-mods', ['translator', 'benchpress', 'autocomplete',
if (!$('.global-moderator-area').children().length) {
$('#no-global-mods-warning').removeClass('hidden');
}
}, err => app.alertError(err.status.message));
}, 'default');
}
});
});

@ -139,7 +139,7 @@ define('admin/manage/group', [
}
api.del('/groups/' + ajaxify.data.group.slug + '/membership/' + uid, undefined, () => {
userRow.slideUp().remove();
}, err => app.alertError(err.status.message));
}, 'default');
});
break;
default:

@ -98,7 +98,7 @@ define('admin/manage/users', ['translator', 'benchpress', 'autocomplete', 'api']
app.parseAndTranslate('admin/partials/manage_user_groups', { users: [{ groups: [ui.item.group] }] }, function (html) {
$('[data-uid=' + uid + '] .group-area').append(html.find('.group-area').html());
});
}, err => app.alertError(err.status.message));
}, 'default');
});
});
modal.on('click', '.group-area a', function () {
@ -110,7 +110,7 @@ define('admin/manage/users', ['translator', 'benchpress', 'autocomplete', 'api']
var uid = $(this).parents('[data-uid]').attr('data-uid');
api.del('/groups/' + utils.slugify(groupName) + '/membership/' + uid, undefined, () => {
groupCard.remove();
}, err => app.alertError(err.status.message));
}, 'default');
return false;
});
});

@ -55,7 +55,7 @@ define('forum/account/edit', [
}
updateHeader(res.picture);
}, err => app.alertError(err.status.message));
}, 'default');
return false;
}

@ -29,7 +29,7 @@ define('forum/account/edit/email', ['forum/account/header', 'api'], function (he
api.put('/users/' + userData.uid, userData, (res) => {
btn.removeClass('disabled').find('i').addClass('hide');
ajaxify.go('user/' + res.userslug + '/edit');
}, err => app.alertError(err.status.message));
}, 'default');
return false;
});

@ -75,7 +75,7 @@ define('forum/account/edit/password', ['forum/account/header', 'translator', 'zx
} else {
ajaxify.go('user/' + ajaxify.data.userslug + '/edit');
}
}, err => app.alertError(err.status.message)).always(() => {
}, 'default').always(() => {
btn.removeClass('disabled').find('i').addClass('hide');
currentPassword.val('');
password.val('');

@ -36,7 +36,7 @@ define('forum/account/edit/username', ['forum/account/header', 'api'], function
}
ajaxify.go('user/' + userslug + '/edit');
}, err => app.alertError(err.status.message));
}, 'default');
return false;
});

@ -120,7 +120,7 @@ define('forum/account/header', [
components.get('account/follow').toggleClass('hide', type === 'follow');
components.get('account/unfollow').toggleClass('hide', type === 'unfollow');
app.alertSuccess('[[global:alert.' + type + ', ' + ajaxify.data.username + ']]');
}, err => app.alertError(err.status.message));
}, 'default');
return false;
}
@ -158,7 +158,7 @@ define('forum/account/header', [
}
ajaxify.refresh();
}, err => app.alertError(err.status.message));
}, 'default');
},
},
},
@ -169,7 +169,7 @@ define('forum/account/header', [
function unbanAccount() {
api.del('/users/' + ajaxify.data.theirid + '/ban', undefined, () => {
ajaxify.refresh();
}, err => app.alertError(err.status.message));
}, 'default');
}
function flagAccount() {

@ -106,11 +106,11 @@ define('forum/groups/details', [
break;
case 'join': // intentional fall-throughs!
api.put('/groups/' + ajaxify.data.group.slug + '/membership/' + (uid || app.user.uid), undefined, () => ajaxify.refresh(), err => app.alertError(err.status.message));
api.put('/groups/' + ajaxify.data.group.slug + '/membership/' + (uid || app.user.uid), undefined, () => ajaxify.refresh(), 'default');
break;
case 'leave':
api.del('/groups/' + ajaxify.data.group.slug + '/membership/' + (uid || app.user.uid), undefined, () => ajaxify.refresh(), err => app.alertError(err.status.message));
api.del('/groups/' + ajaxify.data.group.slug + '/membership/' + (uid || app.user.uid), undefined, () => ajaxify.refresh(), 'default');
break;
case 'accept': // intentional fall-throughs!

@ -14,7 +14,7 @@ define('forum/groups/list', ['forum/infinitescroll', 'benchpress', 'api'], funct
name: name,
}, (res) => {
ajaxify.go('groups/' + res.slug);
}, err => app.alertError(err.status.message));
}, 'default');
}
});
});

@ -88,7 +88,7 @@ define('forum/groups/memberlist', ['api'], function (api) {
$.when(requests)
.done(done)
.fail(err => app.alertError(err.status.message));
.fail('default');
}
}

@ -338,7 +338,7 @@ define('forum/topic/postTools', [
function bookmarkPost(button, pid) {
var method = button.attr('data-bookmarked') === 'false' ? 'put' : 'del';
api[method](`/posts/${pid}/bookmark`, undefined, undefined, err => app.alertError(err.status.message));
api[method](`/posts/${pid}/bookmark`, undefined, undefined, 'default');
return false;
}
@ -385,7 +385,7 @@ define('forum/topic/postTools', [
const route = action === 'purge' ? '' : '/state';
const method = action === 'restore' ? 'put' : 'del';
api[method](`/posts/${pid}${route}`, undefined, undefined, err => app.alertError(err.status.message));
api[method](`/posts/${pid}${route}`, undefined, undefined, 'default');
});
});
}

@ -183,7 +183,7 @@ define('forum/topic/threadTools', [
const method = command === 'restore' ? 'put' : 'del';
const suffix = command !== 'purge' ? '/state' : '';
api[method](`/topics/${tid}${suffix}`, undefined, undefined, err => app.alertError(err.status.message));
api[method](`/topics/${tid}${suffix}`, undefined, undefined, 'default');
});
});
}

@ -2,7 +2,7 @@
define('api', () => {
const api = {};
const baseUrl = config.relative_path + '/api/v1';
const baseUrl = config.relative_path + '/api/v3';
function call(options, onSuccess, onError) {
$.ajax(options)
@ -12,7 +12,9 @@ define('api', () => {
}
})
.fail((ev) => {
if (onError) {
if (onError === 'default') {
app.alertError(ev.responseJSON.status.message);
} else if (onError) {
onError(ev.responseJSON);
}
});

@ -8,7 +8,7 @@ const Write = module.exports;
Write.reload = (params) => {
const router = params.router;
router.use('/api/v1', function (req, res, next) {
router.use('/api/v3', function (req, res, next) {
// if (req.protocol !== 'https') {
// res.set('Upgrade', 'TLS/1.0, HTTP/1.1');
// return helpers.formatApiResponse(426, res);
@ -20,20 +20,20 @@ Write.reload = (params) => {
next();
});
router.use('/api/v1/users', require('./users')());
router.use('/api/v1/groups', require('./groups')());
router.use('/api/v1/categories', require('./categories')());
router.use('/api/v1/topics', require('./topics')());
router.use('/api/v1/posts', require('./posts')());
// router.use('/api/v1/util', require('./util')());
router.use('/api/v3/users', require('./users')());
router.use('/api/v3/groups', require('./groups')());
router.use('/api/v3/categories', require('./categories')());
router.use('/api/v3/topics', require('./topics')());
router.use('/api/v3/posts', require('./posts')());
// router.use('/api/v3/util', require('./util')());
router.get('/api/v1/ping', function (req, res) {
router.get('/api/v3/ping', function (req, res) {
helpers.formatApiResponse(200, res, {
pong: true,
});
});
router.post('/api/v1/ping', middleware.authenticate, function (req, res) {
router.post('/api/v3/ping', middleware.authenticate, function (req, res) {
helpers.formatApiResponse(200, res, {
uid: req.user.uid,
});

@ -13,7 +13,7 @@ const sockets = require('..');
const Categories = module.exports;
Categories.create = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v1/categories');
sockets.warnDeprecated(socket, 'POST /api/v3/categories');
if (!data) {
throw new Error('[[error:invalid-data]]');
@ -40,7 +40,7 @@ Categories.getNames = async function () {
};
Categories.purge = async function (socket, cid) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/categories/:cid');
sockets.warnDeprecated(socket, 'DELETE /api/v3/categories/:cid');
const name = await categories.getCategoryField(cid, 'name');
await categories.purge(cid, socket.uid);
@ -54,7 +54,7 @@ Categories.purge = async function (socket, cid) {
};
Categories.update = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/categories/:cid');
sockets.warnDeprecated(socket, 'PUT /api/v3/categories/:cid');
if (!data) {
throw new Error('[[error:invalid-data]]');

@ -6,7 +6,7 @@ const sockets = require('..');
const Groups = module.exports;
Groups.create = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v1/groups');
sockets.warnDeprecated(socket, 'POST /api/v3/groups');
if (!data) {
throw new Error('[[error:invalid-data]]');
@ -24,7 +24,7 @@ Groups.create = async function (socket, data) {
};
Groups.join = async (socket, data) => {
sockets.warnDeprecated(socket, 'PUT /api/v1/groups/:slug/membership/:uid');
sockets.warnDeprecated(socket, 'PUT /api/v3/groups/:slug/membership/:uid');
if (!data) {
throw new Error('[[error:invalid-data]]');
@ -39,7 +39,7 @@ Groups.join = async (socket, data) => {
};
Groups.leave = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/groups/:slug/membership/:uid');
sockets.warnDeprecated(socket, 'DELETE /api/v3/groups/:slug/membership/:uid');
if (!data) {
throw new Error('[[error:invalid-data]]');

@ -58,7 +58,7 @@ User.removeAdmins = async function (socket, uids) {
};
User.createUser = async function (socket, userData) {
sockets.warnDeprecated(socket, 'POST /api/v1/users');
sockets.warnDeprecated(socket, 'POST /api/v3/users');
if (!userData) {
throw new Error('[[error:invalid-data]]');
@ -139,7 +139,7 @@ User.deleteUsersContent = async function (socket, uids) {
};
User.deleteUsersAndContent = async function (socket, uids) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/users or DELETE /api/v1/users/:uid');
sockets.warnDeprecated(socket, 'DELETE /api/v3/users or DELETE /api/v3/users/:uid');
await canDeleteUids(uids);
deleteUsers(socket, uids, async function (uid) {

@ -19,7 +19,7 @@ SocketGroups.before = async (socket, method, data) => {
};
SocketGroups.join = async (socket, data) => {
sockets.warnDeprecated(socket, 'PUT /api/v1/groups/:slug/membership/:uid');
sockets.warnDeprecated(socket, 'PUT /api/v3/groups/:slug/membership/:uid');
if (socket.uid <= 0) {
throw new Error('[[error:invalid-uid]]');
@ -69,7 +69,7 @@ SocketGroups.join = async (socket, data) => {
};
SocketGroups.leave = async (socket, data) => {
sockets.warnDeprecated(socket, 'DELETE /api/v1/groups/:slug/membership/:uid');
sockets.warnDeprecated(socket, 'DELETE /api/v3/groups/:slug/membership/:uid');
if (socket.uid <= 0) {
throw new Error('[[error:invalid-uid]]');
@ -282,7 +282,7 @@ SocketGroups.kick = async (socket, data) => {
};
SocketGroups.create = async (socket, data) => {
sockets.warnDeprecated(socket, 'POST /api/v1/groups');
sockets.warnDeprecated(socket, 'POST /api/v3/groups');
if (!socket.uid) {
throw new Error('[[error:no-privileges]]');

@ -24,7 +24,7 @@ require('./posts/tools')(SocketPosts);
require('./posts/diffs')(SocketPosts);
SocketPosts.reply = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v1/topics/:tid');
sockets.warnDeprecated(socket, 'POST /api/v3/topics/:tid');
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
throw new Error('[[error:invalid-data]]');

@ -6,12 +6,12 @@ const sockets = require('..');
module.exports = function (SocketPosts) {
SocketPosts.bookmark = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/posts/:pid/bookmark');
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/bookmark');
return await helpers.postCommand(socket, 'bookmark', 'bookmarked', '', data);
};
SocketPosts.unbookmark = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/posts/:pid/bookmark');
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/bookmark');
return await helpers.postCommand(socket, 'unbookmark', 'bookmarked', '', data);
};
};

@ -12,7 +12,7 @@ const websockets = require('../index');
module.exports = function (SocketPosts) {
SocketPosts.edit = async function (socket, data) {
websockets.warnDeprecated(socket, 'PUT /api/v1/posts/:pid');
websockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid');
if (!socket.uid) {
throw new Error('[[error:not-logged-in]]');

@ -64,7 +64,7 @@ module.exports = function (SocketPosts) {
};
SocketPosts.delete = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/posts/:pid/state');
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/state');
await deleteOrRestore(socket, data, {
command: 'delete',
@ -74,7 +74,7 @@ module.exports = function (SocketPosts) {
};
SocketPosts.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/posts/:pid/state');
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/state');
await deleteOrRestore(socket, data, {
command: 'restore',
@ -123,7 +123,7 @@ module.exports = function (SocketPosts) {
}
SocketPosts.purge = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/posts/:pid');
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid');
if (!data || !parseInt(data.pid, 10)) {
throw new Error('[[error:invalid-data]]');

@ -63,17 +63,17 @@ module.exports = function (SocketPosts) {
};
SocketPosts.upvote = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/posts/:pid/vote');
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/vote');
return await helpers.postCommand(socket, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
};
SocketPosts.downvote = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/posts/:pid/vote');
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/vote');
return await helpers.postCommand(socket, 'downvote', 'voted', '', data);
};
SocketPosts.unvote = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/posts/:pid/vote');
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/vote');
return await helpers.postCommand(socket, 'unvote', 'voted', '', data);
};
};

@ -19,7 +19,7 @@ require('./topics/tags')(SocketTopics);
require('./topics/merge')(SocketTopics);
SocketTopics.post = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v1/topics');
sockets.warnDeprecated(socket, 'POST /api/v3/topics');
if (!data) {
throw new Error('[[error:invalid-data]]');
@ -83,12 +83,12 @@ SocketTopics.changeWatching = async function (socket, data) {
throw new Error('[[error:invalid-command]]');
}
sockets.warnDeprecated(socket, 'PUT/DELETE /api/v1/topics/:tid/(follow|ignore)');
sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/topics/:tid/(follow|ignore)');
await followCommand(topics[data.type], socket, data.tid);
};
SocketTopics.follow = async function (socket, tid) {
sockets.warnDeprecated(socket, 'PUT /api/v1/topics/:tid/follow');
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/:tid/follow');
await followCommand(topics.follow, socket, tid);
};

@ -35,37 +35,37 @@ module.exports = function (SocketTopics) {
};
SocketTopics.delete = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/topics/state');
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/state');
await SocketTopics.doTopicAction('delete', 'event:topic_deleted', socket, data);
};
SocketTopics.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/topics/state');
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/state');
await SocketTopics.doTopicAction('restore', 'event:topic_restored', socket, data);
};
SocketTopics.purge = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/topics');
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics');
await SocketTopics.doTopicAction('purge', 'event:topic_purged', socket, data);
};
SocketTopics.lock = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/topics/lock');
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/lock');
await SocketTopics.doTopicAction('lock', 'event:topic_locked', socket, data);
};
SocketTopics.unlock = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/topics/lock');
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/lock');
await SocketTopics.doTopicAction('unlock', 'event:topic_unlocked', socket, data);
};
SocketTopics.pin = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/topics/pin');
sockets.warnDeprecated(socket, 'PUT /api/v3/topics/pin');
await SocketTopics.doTopicAction('pin', 'event:topic_pinned', socket, data);
};
SocketTopics.unpin = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/topics/pin');
sockets.warnDeprecated(socket, 'DELETE /api/v3/topics/pin');
await SocketTopics.doTopicAction('unpin', 'event:topic_unpinned', socket, data);
};

@ -158,7 +158,7 @@ SocketUser.isFollowing = async function (socket, data) {
};
SocketUser.follow = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v1/users/follow');
sockets.warnDeprecated(socket, 'POST /api/v3/users/follow');
if (!socket.uid || !data) {
throw new Error('[[error:invalid-data]]');
@ -182,7 +182,7 @@ SocketUser.follow = async function (socket, data) {
};
SocketUser.unfollow = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/users/unfollow');
sockets.warnDeprecated(socket, 'DELETE /api/v3/users/unfollow');
if (!socket.uid || !data) {
throw new Error('[[error:invalid-data]]');

@ -11,7 +11,7 @@ const flags = require('../../flags');
module.exports = function (SocketUser) {
SocketUser.banUsers = async function (socket, data) {
websockets.warnDeprecated(socket, 'PUT /api/v1/users/:uid/ban');
websockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid/ban');
if (!data || !Array.isArray(data.uids)) {
throw new Error('[[error:invalid-data]]');
@ -40,7 +40,7 @@ module.exports = function (SocketUser) {
};
SocketUser.unbanUsers = async function (socket, uids) {
websockets.warnDeprecated(socket, 'DELETE /api/v1/users/:uid/ban');
websockets.warnDeprecated(socket, 'DELETE /api/v3/users/:uid/ban');
await toggleBan(socket.uid, uids, async function (uid) {
await user.bans.unban(uid);

@ -13,7 +13,7 @@ const sockets = require('..');
module.exports = function (SocketUser) {
SocketUser.changeUsernameEmail = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/users/:uid');
sockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid');
if (!data || !data.uid || !socket.uid) {
throw new Error('[[error:invalid-data]]');
@ -78,7 +78,7 @@ module.exports = function (SocketUser) {
}
SocketUser.changePassword = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/users/:uid/password');
sockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid/password');
if (!socket.uid) {
throw new Error('[[error:invalid-uid]]');
@ -97,7 +97,7 @@ module.exports = function (SocketUser) {
};
SocketUser.updateProfile = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v1/users/:uid');
sockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid');
if (!socket.uid) {
throw new Error('[[error:invalid-uid]]');

Loading…
Cancel
Save