feat(follow): follow & unfollow in userinfo page

This commit is contained in:
OldHawk
2018-01-04 12:14:43 +08:00
parent 7c95d3a547
commit 95422aaaec
10 changed files with 157 additions and 9 deletions

View File

@@ -5,10 +5,11 @@
.module('users')
.controller('UserInfoController', UserInfoController);
UserInfoController.$inject = ['$scope', '$state', 'Authentication', 'userResolve', 'ScoreLevelService', '$timeout', 'MeanTorrentConfig',
'DebugConsoleService'];
UserInfoController.$inject = ['$scope', '$rootScope', '$state', 'Authentication', 'userResolve', 'ScoreLevelService', '$timeout', 'MeanTorrentConfig', 'UsersService',
'DebugConsoleService', 'NotifycationService'];
function UserInfoController($scope, $state, Authentication, user, ScoreLevelService, $timeout, MeanTorrentConfig, mtDebug) {
function UserInfoController($scope, $rootScope, $state, Authentication, user, ScoreLevelService, $timeout, MeanTorrentConfig, UsersService,
mtDebug, NotifycationService) {
var vm = this;
vm.authentication = Authentication;
@@ -35,5 +36,57 @@
function isContextUserSelf() {
return vm.user.username === vm.authentication.user.username;
}
/**
* inMyFollowing
* @returns {boolean}
*/
vm.inMyFollowing = function () {
return vm.authentication.user.following.indexOf(vm.user._id) >= 0 ? true : false;
};
/**
* followTo
*/
vm.followTo = function () {
UsersService.userFollowTo({
userId: vm.user._id
}).then(onSuccess)
.catch(onError);
function onSuccess(response) {
mtDebug.info(response);
vm.authentication.user = Authentication.user = response;
$rootScope.$broadcast('user-follow-changed', response);
NotifycationService.showSuccessNotify('FOLLOW_SUCCESSFULLY');
}
function onError(response) {
NotifycationService.showErrorNotify(response.data.message, 'FOLLOW_ERROR');
}
};
/**
* unFollowTo
*/
vm.unFollowTo = function () {
UsersService.userUnfollowTo({
userId: vm.user._id
}).then(onSuccess)
.catch(onError);
function onSuccess(response) {
mtDebug.info(response);
vm.authentication.user = Authentication.user = response;
$rootScope.$broadcast('user-follow-changed', response);
NotifycationService.showSuccessNotify('UNFOLLOW_SUCCESSFULLY');
}
function onError(response) {
NotifycationService.showErrorNotify(response.data.message, 'UNFOLLOW_ERROR');
}
};
}
}());

View File

@@ -55,6 +55,20 @@
unIdle: {
method: 'POST',
url: '/api/users/unIdle'
},
followTo: {
method: 'POST',
url: '/api/users/followTo/:userId',
params: {
userId: '@userId'
}
},
unFollowTo: {
method: 'POST',
url: '/api/users/unFollowTo/:userId',
params: {
userId: '@userId'
}
}
});
@@ -92,6 +106,12 @@
},
userUnIdle: function () {
return this.unIdle().$promise;
},
userFollowTo: function (uid) {
return this.followTo(uid).$promise;
},
userUnfollowTo: function (uid) {
return this.unFollowTo(uid).$promise;
}
});

View File

@@ -1,10 +1,10 @@
<section class="container">
<div class="page-header">
<div class="row">
<div class="col-md-5 col-md-offset-1">
<h3 ng-bind="vm.user.displayName"></h3>
<div class="col-sm-3 col-md-3 col-md-offset-1">
<h3 class="userinfo-name" ng-bind="vm.user.displayName"></h3>
</div>
<div class="col-md-5">
<div class="col-sm-9 col-md-7">
<a class="btn btn-primary pull-right margin-left-10" ng-click="vm.messageTo()" ng-if="!vm.isContextUserSelf()">
<i class="glyphicon glyphicon-envelope"></i>
</a>
@@ -12,6 +12,16 @@
ng-if="vm.authentication.user.isOper">
<i class="glyphicon glyphicon-cog"></i>
</a>
<a class="btn btn-default pull-right btn-bold btn-width-120 margin-left-10"
ng-click="vm.followTo()"
ng-if="!vm.isContextUserSelf() && !vm.inMyFollowing()">
{{ 'STATUS_FIELD.BTN_FOLLOW' | translate}}
</a>
<a class="btn btn-default pull-right btn-bold btn-width-120 margin-left-10"
ng-click="vm.unFollowTo()"
ng-if="!vm.isContextUserSelf() && vm.inMyFollowing()">
{{ 'STATUS_FIELD.BTN_UNFOLLOW' | translate}}
</a>
</div>
</div>
</div>

View File

@@ -144,7 +144,7 @@ exports.changeProfilePicture = function (req, res) {
if (existingImageUrl !== User.schema.path('profileImageURL').defaultValue) {
if (useS3Storage) {
try {
var { region, bucket, key } = amazonS3URI(existingImageUrl);
var {region, bucket, key} = amazonS3URI(existingImageUrl);
var params = {
Bucket: config.aws.s3.bucket,
Key: key
@@ -292,3 +292,39 @@ exports.unIdle = function (req, res, next) {
});
}
};
/**
* followTo
* @param req
* @param res
*/
exports.followTo = function (req, res) {
var user = req.user;
var toUser = req.model;
toUser.followers.push(user._id);
toUser.save();
user.following.push(toUser._id);
user.save(function (err) {
res.json(user);
});
};
/**
* unFollowTo
* @param req
* @param res
*/
exports.unFollowTo = function (req, res) {
var user = req.user;
var toUser = req.model;
toUser.followers.pull(user._id);
toUser.save();
user.following.pull(toUser._id);
user.save(function (err) {
res.json(user);
});
};

View File

@@ -14,6 +14,8 @@ module.exports = function (app) {
app.route('/api/users/warningNumber').get(users.warningNumber);
app.route('/api/users/picture').post(users.changeProfilePicture);
app.route('/api/users/signature').post(users.changeSignature);
app.route('/api/users/followTo/:userId').post(users.followTo);
app.route('/api/users/unFollowTo/:userId').post(users.unFollowTo);
// Finish by binding the user middleware
app.param('userId', users.userByID);