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

@@ -701,7 +701,13 @@
ACTIVE_IDLE_CONFIRM_BODY_TEXT: 'Are you sure want to active your account with {{score}} scores?',
ACTIVE_IDLE_SUCCESSFULLY: 'Active account successfully',
ACTIVE_IDLE_ERROR: 'Active account failed',
FOLLOW_SUCCESSFULLY: 'Follow successfully',
FOLLOW_ERROR: 'Follow failed',
UNFOLLOW_SUCCESSFULLY: 'Unfollow successfully',
UNFOLLOW_ERROR: 'Unfollow failed',
STATUS_FIELD: {
BTN_FOLLOW: 'Follow',
BTN_UNFOLLOW: 'Unfollow',
PICTURE: 'Profile picture',
RESET_DEFAULT_PICTURE: 'Reset to default picture',
ADD_VIP_MONTHS: '+ VIP a month',

View File

@@ -701,7 +701,13 @@
ACTIVE_IDLE_CONFIRM_BODY_TEXT: '您确认要使用 {{score}} 积分来重新激活您的帐户?',
ACTIVE_IDLE_SUCCESSFULLY: '激活帐户成功',
ACTIVE_IDLE_ERROR: '激活帐户失败',
FOLLOW_SUCCESSFULLY: '添加关注成功',
FOLLOW_ERROR: '添加关注失败',
UNFOLLOW_SUCCESSFULLY: '取消关注成功',
UNFOLLOW_ERROR: '取消关注失败',
STATUS_FIELD: {
BTN_FOLLOW: '关注',
BTN_UNFOLLOW: '取消关注',
PICTURE: '头像',
RESET_DEFAULT_PICTURE: '重置为默认图片',
ADD_VIP_MONTHS: '+ VIP一个月',

View File

@@ -76,6 +76,13 @@
vm.getCountUnread();
});
/**
* user-follow-changed
*/
$scope.$on('user-follow-changed', function (event, u) {
vm.user = Authentication.user = u;
});
/**
* getInvitationsCount
*/
@@ -97,9 +104,9 @@
*/
vm.getFollowCount = function (item) {
if (item.state.indexOf('followers') >= 0) {
return vm.user.followers.length;
return vm.user ? vm.user.followers.length : 0;
} else {
return vm.user.following.length;
return vm.user ? vm.user.following.length : 0;
}
};

View File

@@ -109,4 +109,8 @@
border-color: @state-success-text;
}
}
}
.btn-bold {
font-weight: 500;
}

View File

@@ -1537,6 +1537,10 @@ body {
}
}
.userinfo-name {
margin-top: 8px;
}
.lang-list {
margin-left: 10px;
line-height: 2;

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);