// 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: err.message }); } } /** * @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": [ * { "idx":10003, "account_id":"6516_2006_0xef59f6cc4d190a0ae576c46d4583e92b61174340", "name":"iij", "sex":0, "head_id":50006, "head_frame":60000, "level":1, "bceg":0, "gold":18588, "diamond":10000, "rank":1, "ring_id":0, "last_login_time":1670490960, "online":1, "guildInfo":{ "idx":22, "gname":"Guild2", "logo":null, "gowner":"6516_2006_0xef59f6cc4d190a0ae576c46d4583e92b61174340", "gownername":"iij", "gmaxmember":100, "announce":null, "countmember":1 } * } * ] * } * * @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 用户名or用户ID * @apiParam {String} last_idx 上次查找的最后一个用户ID * @apiParam {String} limit 本次查找的用户数量 * * @apiSuccess {Number} code 状态码 * @apiSuccess {Object[]} users 用户列表 * * @apiSuccessExample {json} Success-Response: * { * "code": 200, * "result": { * "users": [ * { * "uid": '', * "name": '' * } * ], * "last_idx": 10001 * } * * @apiErrorExample {json} Error-Response: * { * "code": 500, * "msg": "Failed to find user." * } * */ async findUser({ name, last_idx = 0, limit = 10 }, { uid }, next) { try { const users = await this.friendService.findUser( name, last_idx, limit, uid ); next(null, { code: Code.OK, result: users }); } catch (err) { next(null, { code: Code.FAIL, msg: "Failed to find user." }); } } }