mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
implement follow methods
This commit is contained in:
parent
c69584dc15
commit
423e305072
@ -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 } from "../../models"
|
import { User, Follow } 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"
|
||||||
@ -59,6 +59,43 @@ export default class UserController extends ComplexController {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return user.toObject()
|
return user.toObject()
|
||||||
|
},
|
||||||
|
follow: 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 Follow.findOne({
|
||||||
|
user_id: payload.user_id,
|
||||||
|
to: payload.to,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (follow) {
|
||||||
|
throw new Error("Already following")
|
||||||
|
}
|
||||||
|
|
||||||
|
await Follow.create({
|
||||||
|
user_id: payload.user_id,
|
||||||
|
to: payload.to,
|
||||||
|
})
|
||||||
|
|
||||||
|
global.wsInterface.io.emit(`user.follow`, {
|
||||||
|
...user.toObject(),
|
||||||
|
})
|
||||||
|
global.wsInterface.io.emit(`user.follow.${payload.user_id}`, {
|
||||||
|
...user.toObject(),
|
||||||
|
})
|
||||||
|
|
||||||
|
return "ok"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,6 +186,27 @@ export default class UserController extends ComplexController {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
put = {
|
||||||
|
"/follow_user": {
|
||||||
|
middlewares: ["withAuthentication"],
|
||||||
|
fn: Schematized({
|
||||||
|
required: ["to"],
|
||||||
|
select: ["to"],
|
||||||
|
}, async (req, res) => {
|
||||||
|
const selfUserId = req.user._id.toString()
|
||||||
|
|
||||||
|
const result = this.methods.follow({
|
||||||
|
user_id: selfUserId,
|
||||||
|
to: req.selection.to,
|
||||||
|
}).catch((error) => {
|
||||||
|
return res.status(500).json({ error: error.message })
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json(result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
post = {
|
post = {
|
||||||
"/login": async (req, res) => {
|
"/login": async (req, res) => {
|
||||||
passport.authenticate("local", { session: false }, async (error, user, options) => {
|
passport.authenticate("local", { session: false }, async (error, user, options) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user