mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
implement follow methods
This commit is contained in:
parent
9fc04da329
commit
ebc0909c15
@ -1,7 +1,7 @@
|
|||||||
import { ComplexController } from "linebridge/dist/classes"
|
import { ComplexController } from "linebridge/dist/classes"
|
||||||
import passport from "passport"
|
import passport from "passport"
|
||||||
|
|
||||||
import { User, Follow } from "../../models"
|
import { User, UserFollow } from "../../models"
|
||||||
import { Token, Schematized, createUser } from "../../lib"
|
import { Token, Schematized, createUser } from "../../lib"
|
||||||
import SessionController from "../SessionController"
|
import SessionController from "../SessionController"
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
@ -74,7 +74,7 @@ export default class UserController extends ComplexController {
|
|||||||
throw new Error("User not found")
|
throw new Error("User not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
const follow = await Follow.findOne({
|
const follow = await UserFollow.findOne({
|
||||||
user_id: payload.user_id,
|
user_id: payload.user_id,
|
||||||
to: payload.to,
|
to: payload.to,
|
||||||
})
|
})
|
||||||
@ -83,11 +83,13 @@ export default class UserController extends ComplexController {
|
|||||||
throw new Error("Already following")
|
throw new Error("Already following")
|
||||||
}
|
}
|
||||||
|
|
||||||
await Follow.create({
|
const newFollow = await UserFollow.create({
|
||||||
user_id: payload.user_id,
|
user_id: payload.user_id,
|
||||||
to: payload.to,
|
to: payload.to,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
await newFollow.save()
|
||||||
|
|
||||||
global.wsInterface.io.emit(`user.follow`, {
|
global.wsInterface.io.emit(`user.follow`, {
|
||||||
...user.toObject(),
|
...user.toObject(),
|
||||||
})
|
})
|
||||||
@ -95,11 +97,65 @@ export default class UserController extends ComplexController {
|
|||||||
...user.toObject(),
|
...user.toObject(),
|
||||||
})
|
})
|
||||||
|
|
||||||
return "ok"
|
return {
|
||||||
}
|
following: true,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
unfollow: async (payload) => {
|
||||||
|
if (typeof payload.user_id === "undefined") {
|
||||||
|
throw new Error("No user_id provided")
|
||||||
|
}
|
||||||
|
if (typeof payload.to === "undefined") {
|
||||||
|
throw new Error("No to provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await User.findById(payload.user_id)
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new Error("User not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
const follow = await UserFollow.findOne({
|
||||||
|
user_id: payload.user_id,
|
||||||
|
to: payload.to,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!follow) {
|
||||||
|
throw new Error("Not following")
|
||||||
|
}
|
||||||
|
|
||||||
|
await follow.remove()
|
||||||
|
|
||||||
|
global.wsInterface.io.emit(`user.unfollow`, {
|
||||||
|
...user.toObject(),
|
||||||
|
})
|
||||||
|
global.wsInterface.io.emit(`user.unfollow.${payload.user_id}`, {
|
||||||
|
...user.toObject(),
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
following: false,
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
get = {
|
get = {
|
||||||
|
"/is_followed": {
|
||||||
|
middlewares: ["withAuthentication"],
|
||||||
|
fn: Schematized({
|
||||||
|
required: ["user_id"],
|
||||||
|
select: ["user_id"]
|
||||||
|
}, async (req, res) => {
|
||||||
|
const isFollowed = await UserFollow.findOne({
|
||||||
|
user_id: req.user._id.toString(),
|
||||||
|
to: req.selection.user_id,
|
||||||
|
}).catch(() => false)
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
isFollowed: Boolean(isFollowed),
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
},
|
||||||
"/self": {
|
"/self": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
@ -111,7 +167,7 @@ export default class UserController extends ComplexController {
|
|||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
select: ["_id", "username"],
|
select: ["_id", "username"],
|
||||||
}, async (req, res) => {
|
}, async (req, res) => {
|
||||||
const user = await User.findOne(req.selection)
|
let user = await User.findOne(req.selection)
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ error: "User not exists" })
|
return res.status(404).json({ error: "User not exists" })
|
||||||
@ -190,19 +246,54 @@ export default class UserController extends ComplexController {
|
|||||||
"/follow_user": {
|
"/follow_user": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
required: ["to"],
|
select: ["user_id", "username"],
|
||||||
select: ["to"],
|
|
||||||
}, async (req, res) => {
|
}, async (req, res) => {
|
||||||
const selfUserId = req.user._id.toString()
|
const selfUserId = req.user._id.toString()
|
||||||
|
let targetUserId = null
|
||||||
|
let result = null
|
||||||
|
|
||||||
const result = this.methods.follow({
|
if (typeof req.selection.user_id === "undefined" && typeof req.selection.username === "undefined") {
|
||||||
|
return res.status(400).json({ message: "No user_id or username provided" })
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof req.selection.user_id !== "undefined") {
|
||||||
|
targetUserId = req.selection.user_id
|
||||||
|
} else {
|
||||||
|
const user = await User.findOne({ username: req.selection.username })
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ message: "User not found" })
|
||||||
|
}
|
||||||
|
|
||||||
|
targetUserId = user._id.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if already following
|
||||||
|
const isFollowed = await UserFollow.findOne({
|
||||||
user_id: selfUserId,
|
user_id: selfUserId,
|
||||||
to: req.selection.to,
|
to: targetUserId,
|
||||||
}).catch((error) => {
|
|
||||||
return res.status(500).json({ error: error.message })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return res.json(result)
|
// if already following, delete
|
||||||
|
if (isFollowed) {
|
||||||
|
result = await this.methods.unfollow({
|
||||||
|
user_id: selfUserId,
|
||||||
|
to: targetUserId,
|
||||||
|
}).catch((error) => {
|
||||||
|
return res.status(500).json({ message: error.message })
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
result = await this.methods.follow({
|
||||||
|
user_id: selfUserId,
|
||||||
|
to: targetUserId,
|
||||||
|
}).catch((error) => {
|
||||||
|
return res.status(500).json({ message: error.message })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
following: result.following
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user