Skip to content

Commit

Permalink
feat: added POST and DELETE /api/v1/users/:uid/follow routes
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Oct 8, 2020
1 parent 7aed174 commit b5bbcba
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
47 changes: 47 additions & 0 deletions openapi.yaml
Expand Up @@ -195,6 +195,53 @@ paths:
$ref: '#/components/schemas/Status'
response:
type: object
'/{uid}/follow':
post:
tags:
- users
summary: follows a user
parameters:
- in: path
name: uid
schema:
type: integer
required: true
description: uid of the user to follow
responses:
'200':
description: successfully followed user
content:
application/json:
schema:
type: object
properties:
status:
$ref: '#/components/schemas/Status'
response:
type: object
delete:
tags:
- users
summary: unfollows a user
parameters:
- in: path
name: uid
schema:
type: integer
required: true
description: uid of the user to unfollow
responses:
'200':
description: successfully unfollowed user
content:
application/json:
schema:
type: object
properties:
status:
$ref: '#/components/schemas/Status'
response:
type: object
components:
schemas:
Status:
Expand Down
15 changes: 8 additions & 7 deletions public/src/client/account/header.js
Expand Up @@ -115,17 +115,18 @@ define('forum/account/header', [
}

function toggleFollow(type) {
socket.emit('user.' + type, {
uid: ajaxify.data.uid,
}, function (err) {
if (err) {
return app.alertError(err.message);
}

$.ajax({
url: config.relative_path + '/api/v1/users/' + ajaxify.data.uid + '/' + type,
method: type === 'follow' ? 'post' : 'delete',
}).done(function () {
components.get('account/follow').toggleClass('hide', type === 'follow');
components.get('account/unfollow').toggleClass('hide', type === 'unfollow');
app.alertSuccess('[[global:alert.' + type + ', ' + ajaxify.data.username + ']]');
}).fail(function (ev) {
console.log(ev);
app.alertError(ev.responseJSON.status.message);
});

return false;
}

Expand Down
36 changes: 36 additions & 0 deletions src/controllers/write/users.js
Expand Up @@ -2,7 +2,9 @@

const user = require('../../user');
const groups = require('../../groups');
const plugins = require('../../plugins');
const privileges = require('../../privileges');
const notifications = require('../../notifications');
const meta = require('../../meta');
const events = require('../../events');
const helpers = require('../helpers');
Expand Down Expand Up @@ -119,3 +121,37 @@ Users.changePassword = async (req, res) => {

helpers.formatApiResponse(200, res);
};

Users.follow = async (req, res) => {
await user.follow(req.user.uid, req.params.uid);
plugins.fireHook('action:user.follow', {
fromUid: req.user.uid,
toUid: req.params.uid,
});

const userData = await user.getUserFields(req.user.uid, ['username', 'userslug']);
const notifObj = await notifications.create({
type: 'follow',
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + req.params.uid + ':uid:' + req.user.uid,
from: req.user.uid,
path: '/uid/' + req.params.uid + '/followers',
mergeId: 'notifications:user_started_following_you',
});
if (!notifObj) {
return;
}
notifObj.user = userData;
await notifications.push(notifObj, [req.params.uid]);

helpers.formatApiResponse(200, res);
};

Users.unfollow = async (req, res) => {
await user.unfollow(req.user.uid, req.params.uid);
plugins.fireHook('action:user.unfollow', {
fromUid: req.user.uid,
toUid: req.params.uid,
});
helpers.formatApiResponse(200, res);
};
3 changes: 3 additions & 0 deletions src/routes/write/users.js
Expand Up @@ -29,6 +29,9 @@ function authenticatedRoutes() {

setupApiRoute(router, '/:uid/password', middleware, [...middlewares, middleware.checkRequired.bind(null, ['newPassword'])], 'put', controllers.write.users.changePassword);

setupApiRoute(router, '/:uid/follow', middleware, [...middlewares], 'post', controllers.write.users.follow);
setupApiRoute(router, '/:uid/unfollow', middleware, [...middlewares], 'delete', controllers.write.users.unfollow);

// app.put('/:uid/follow', apiMiddleware.requireUser, function(req, res) {
// Users.follow(req.user.uid, req.params.uid, function(err) {
// return errorHandler.handle(err, res);
Expand Down
5 changes: 5 additions & 0 deletions src/socket.io/user.js
Expand Up @@ -18,6 +18,7 @@ const userController = require('../controllers/user');
const privileges = require('../privileges');
const utils = require('../utils');
const flags = require('../flags');
const sockets = require('.');

const SocketUser = module.exports;

Expand Down Expand Up @@ -157,6 +158,8 @@ SocketUser.isFollowing = async function (socket, data) {
};

SocketUser.follow = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v1/users/follow');

if (!socket.uid || !data) {
throw new Error('[[error:invalid-data]]');
}
Expand All @@ -179,6 +182,8 @@ SocketUser.follow = async function (socket, data) {
};

SocketUser.unfollow = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v1/users/unfollow');

if (!socket.uid || !data) {
throw new Error('[[error:invalid-data]]');
}
Expand Down

0 comments on commit b5bbcba

Please sign in to comment.