implement connected_users_following endpoint

This commit is contained in:
srgooglo 2022-10-28 22:05:33 +00:00
parent cfe31ba5b5
commit 4b21c898f6
2 changed files with 37 additions and 1 deletions

View File

@ -5,11 +5,12 @@ import bcrypt from "bcrypt"
import SessionController from "../SessionController"
import { User, UserFollow } from "../../models"
import { User } from "../../models"
import { Token, Schematized } from "../../lib"
import createUser from "./methods/createUser"
import updatePassword from "./methods/updatePassword"
import getConnectedUsersFollowing from "./methods/getConnectedUsersFollowing"
const AllowedPublicUpdateFields = [
"fullName",
@ -110,6 +111,16 @@ export default class UserController extends Controller {
return res.json(req.user)
},
},
"/connected_users_following": {
middlewares: ["withAuthentication"],
fn: async (req, res) => {
const users = await getConnectedUsersFollowing({
from_user_id: req.user._id.toString(),
})
return res.json(users)
}
},
"/user/username-available": async (req, res) => {
const user = await User.findOne({
username: req.query.username,

View File

@ -0,0 +1,25 @@
import { UserFollow } from "../../../models"
export default async (payload = {}) => {
const { from_user_id } = payload
// get all the users that are following
const following = await UserFollow.find({
user_id: from_user_id,
})
// check if following users are connected
const connectedUsers = []
following.forEach((follow) => {
const connectedClient = global.wsInterface.clients.find((client) => {
return client.user_id === follow.to
})
if (connectedClient) {
connectedUsers.push(connectedClient.user_id)
}
})
return connectedUsers
}