r2/game-server/app/servers/friend/handler/friendHandler.js
lightings be0a4bb224 ...
2023-05-18 14:34:55 +08:00

393 lines
9.7 KiB
JavaScript

// friendHandler.js
const FriendService = require("../../../services/friendService");
const Code = require("shared/code");
module.exports = function (app) {
return new FriendHandler(app);
};
class FriendHandler {
constructor(app) {
this.app = app;
this.friendService = new FriendService(app);
}
/**
* @api {post} friend.friendHandler.sendFriendRequest sendFriendRequest 发送好友请求
* @apiGroup Friend
*
* @apiParam {String} fid 好友ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 成功信息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "Friend request sent successfully."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to send friend request."
* }
*
*/
async sendFriendRequest({ fid }, { uid }, next) {
try {
await this.friendService.sendFriendRequest(uid, fid);
next(null, { code: Code.OK, msg: "Friend request sent successfully." });
} catch (err) {
next(null, { code: Code.FAIL, msg: err.message });
}
}
/**
* @api {post} friend.friendHandler.acceptFriendRequest acceptFriendRequest 接受好友请求
* @apiGroup Friend
*
* @apiParam {String} fid 好友ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 成功信息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "Friend request accepted successfully."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to accept friend request."
* }
*
*/
async acceptFriendRequest({ fid }, { uid }, next) {
try {
await this.friendService.acceptFriendRequest(uid, fid);
next(null, {
code: Code.OK,
msg: "Friend request accepted successfully.",
});
} catch (err) {
next(null, { code: Code.FAIL, msg: "Failed to accept friend request." });
}
}
/** @api {post} friend.friendHandler.rejectFriendRequest rejectFriendRequest 拒绝好友请求
* @apiGroup Friend
*
* @apiParam {String} fid 好友ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 成功信息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "Friend request rejected successfully."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to reject friend request."
* }
*
*/
async rejectFriendRequest({ fid }, { uid }, next) {
try {
await this.friendService.rejectFriendRequest(uid, fid);
next(null, {
code: Code.OK,
msg: "Friend request rejected successfully.",
});
} catch (err) {
next(null, { code: Code.FAIL, msg: "Failed to reject friend request." });
}
}
/**
* @api {post} friend.friendHandler.getFriendList getFriendList 获取好友列表
* @apiGroup Friend
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {Object[]} friendList 好友列表
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "friendList": [
* {
* idx: 10001,
* account_id: '6513_2006_md9nVaGIooStTM1D56NLblLosFhWhgxB',
* name: 'xiaobei',
* sex: 0,
* head_id: 50007,
* head_frame: 60003,
* level: 1,
* bceg: 0,
* gold: 99102.31999999992,
* diamond: 2000
* }
* ]
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to get friend list."
* }
*
*/
async getFriendList(msg, { uid }, next) {
try {
const friendList = await this.friendService.getFriendList(uid);
next(null, { code: Code.OK, friendList });
} catch (err) {
next(null, { code: Code.FAIL, msg: "Failed to get friend list." });
}
}
/**
* @api {post} friend.friendHandler.addToBlackList addToBlackList 添加黑名单
* @apiGroup Friend
*
* @apiParam {String} bid 黑名单ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 成功信息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "User added to blacklist successfully."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to add user to blacklist."
* }
*/
async addToBlackList({ bid }, { uid }, next) {
try {
await this.friendService.addToBlackList(uid, bid);
next(null, {
code: Code.OK,
msg: "User added to blacklist successfully.",
});
} catch (err) {
next(null, { code: Code.FAIL, msg: "Failed to add user to blacklist." });
}
}
/**
* @api {post} friend.friendHandler.removeFromBlackList removeFromBlackList 从黑名单移除
* @apiGroup Friend
*
* @apiParam {String} bid 黑名单ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 成功信息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "User removed from blacklist successfully."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to remove user from blacklist."
* }
*/
async removeFromBlackList({ bid }, { uid }, next) {
try {
await this.friendService.removeFromBlackList(uid, bid);
next(null, {
code: Code.OK,
msg: "User removed from blacklist successfully.",
});
} catch (err) {
next(null, {
code: Code.FAIL,
msg: "Failed to remove user from blacklist.",
});
}
}
/**
* @api {post} friend.friendHandler.getBlackList getBlackList 获取黑名单列表
* @apiGroup Friend
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {Object[]} blackList 黑名单列表
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "blackList": [
* {
* "account_id": "5f9f5b9b0b5c4b0b8c8c0b5c",
* }
* ]
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to get blacklist."
* }
*
*/
async getBlackList(msg, { uid }, next) {
try {
const blackList = await this.friendService.getBlackList(uid);
next(null, { code: Code.OK, blackList });
} catch (err) {
next(null, { code: Code.FAIL, msg: "Failed to get blacklist." });
}
}
/**
* @api {post} friend.friendHandler.getPendingFriendRequests getPendingFriendRequests 获取好友请求列表
* @apiGroup Friend
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {Object[]} friendRequests 好友请求列表
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "friendRequests": [
* {
* "account_id": "5f9f5b9b0b5c4b0b8c8c0b5c",
* }
* ]
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to get pending friend requests."
* }
*/
async getPendingFriendRequests(msg, { uid }, next) {
try {
const friendRequests = await this.friendService.getPendingFriendRequests(uid);
next(null, { code: Code.OK, friendRequests });
} catch (err) {
next(null, {
code: Code.FAIL,
msg: "Failed to get pending friend requests.",
});
}
}
/**
* @api {post} friend.friendHandler.getPendingFriendInvitations getPendingFriendInvitations 获取好友邀请列表
* @apiGroup Friend
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {Object[]} friendInvitations 好友邀请列表
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "friendInvitations": [
* {
* "account_id": "5f9f5b9b0b5c4b0b8c8c0b5c",
* }
* ]
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to get pending friend invitations."
* }
*/
async getPendingFriendInvitations(msg, { uid }, next) {
try {
const friendInvitations = await this.friendService.getPendingFriendInvitations(uid);
next(null, { code: Code.OK, friendInvitations });
} catch (err) {
next(null, {
code: Code.FAIL,
msg: "Failed to get pending friend invitations.",
});
}
}
/**
* @api {post} friend.friendHandler.deleteFriend deleteFriend 删除好友
* @apiGroup Friend
*
* @apiParam {String} fid 好友ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 成功信息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "Friend deleted successfully."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to delete friend."
* }
*/
async deleteFriend({ fid }, { uid }, next) {
try {
await this.friendService.deleteFriend(uid, fid);
next(null, { code: Code.OK, msg: "Friend deleted successfully." });
} catch (err) {
next(null, { code: Code.FAIL, msg: "Failed to delete friend." });
}
}
/**
* @api {post} friend.friendHandler.findUser findUser 查找用户
* @apiGroup Friend
*
* @apiParam {String} name 用户名
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {Object[]} users 用户列表
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "users": [
* {
* "user_idx": "5f9f5b9b0b5c4b0b8c8c0b5c",
* }
* ]
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500,
* "msg": "Failed to find user."
* }
*
*/
async findUser({name}, {uid}, next) {
try {
const users = await this.friendService.findUser(name, uid);
next(null, {code: Code.OK, users});
} catch (err) {
next(null, {code: Code.FAIL, msg: "Failed to find user."});
}
}
}