Fix websocket to websockets global variable references to support ng

This commit is contained in:
SrGooglo 2025-04-24 06:10:50 +00:00
parent 7b7e7b9bb7
commit 1f0c1d5033
23 changed files with 214 additions and 208 deletions

View File

@ -89,11 +89,15 @@ export default async (payload = {}, req) => {
// broadcast post to all users
if (visibility === "public") {
global.websocket.senders.toTopic("realtime:feed", "post:new", result[0])
global.websockets.senders.toTopic(
"realtime:feed",
"post:new",
result[0],
)
}
if (visibility === "private") {
const userSockets = await global.websocket.find.clientsByUserId(
const userSockets = await global.websockets.find.clientsByUserId(
post.user_id,
)

View File

@ -42,7 +42,7 @@ export default async (payload = {}) => {
// broadcast post to all users
if (post.visibility === "public") {
global.websocket.senders.toTopic(
global.websockets.senders.toTopic(
"realtime:feed",
"post:delete",
post_id,
@ -50,7 +50,7 @@ export default async (payload = {}) => {
}
if (post.visibility === "private") {
const userSockets = await global.websocket.find.clientsByUserId(
const userSockets = await global.websockets.find.clientsByUserId(
post.user_id,
)

View File

@ -24,10 +24,10 @@ export default async (payload = {}) => {
}
await VotePoll.deleteOne({
_id: vote._id
_id: vote._id,
})
global.websocket.io.of("/").emit(`post.poll.vote.deleted`, vote)
global.websockets.io.of("/").emit(`post.poll.vote.deleted`, vote)
return vote
}

View File

@ -37,7 +37,7 @@ export default async (post_id, update) => {
})
if (post.visibility === "public") {
global.websocket.senders.toTopic(
global.websockets.senders.toTopic(
"realtime:feed",
`post:update`,
result[0],
@ -45,7 +45,7 @@ export default async (post_id, update) => {
}
if (post.visibility === "private") {
const userSockets = await global.websocket.find.clientsByUserId(
const userSockets = await global.websockets.find.clientsByUserId(
post.user_id,
)

View File

@ -51,7 +51,7 @@ export default async (payload = {}) => {
post = (await stage({ posts: post, for_user_id: payload.user_id }))[0]
if (post.visibility === "public") {
global.websocket.senders.toTopic("realtime:feed", `post:update`, post)
global.websockets.senders.toTopic("realtime:feed", `post:update`, post)
}
return {

View File

View File

@ -1,5 +1,5 @@
//import { Server } from "../../../../linebridge/server/src"
import { Server } from "linebridge"
import { Server } from "../../../../linebridge/server/src"
//import { Server } from "linebridge"
import DbManager from "@shared-classes/DbManager"
import RedisClient from "@shared-classes/RedisClient"
@ -10,10 +10,11 @@ import SharedMiddlewares from "@shared-middlewares"
export default class API extends Server {
static refName = "posts"
static useEngine = "hyper-express-ng"
static enableWebsockets = true
static listen_port = process.env.HTTP_LISTEN_PORT ?? 3001
//static useEngine = "hyper-express-ng"
static websockets = true
static listenPort = process.env.HTTP_LISTEN_PORT ?? 3001
// static useMiddlewares = ["logs"]
static bypassCors = true
middlewares = {
...SharedMiddlewares,

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withOptionalAuthentication"],
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
const result = await Posts.data({
post_id: req.params.post_id,
@ -9,5 +9,5 @@ export default {
})
return result
}
},
}

View File

@ -1,7 +1,8 @@
import PostClass from "@classes/posts"
import { Post } from "@db_models"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req, res) => {
// check if post is owned or if is admin
const post = await Post.findById(req.params.post_id).catch(() => {
@ -21,7 +22,7 @@ export default {
}
return await PostClass.delete({
post_id: req.params.post_id
post_id: req.params.post_id,
})
}
},
}

View File

@ -1,13 +1,13 @@
import PostClass from "@classes/posts"
export default {
middlewares: ["withOptionalAuthentication"],
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req) => {
return await PostClass.replies({
post_id: req.params.post_id,
for_user_id: req.auth?.session.user_id,
trim: req.query.trim,
limit: req.query.limit
limit: req.query.limit,
})
}
},
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req, res) => {
const result = await Posts.toggleLike({
post_id: req.params.post_id,
@ -10,5 +10,5 @@ export default {
})
return result
}
},
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req, res) => {
const result = await Posts.toggleSave({
post_id: req.params.post_id,
@ -10,5 +10,5 @@ export default {
})
return result
}
},
}

View File

@ -15,7 +15,7 @@ const MaxStringsLengths = {
}
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
let update = {}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
const result = await Posts.deleteVotePoll({
user_id: req.auth.session.user_id,
@ -10,5 +10,5 @@ export default {
})
return result
}
},
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
const result = await Posts.votePoll({
user_id: req.auth.session.user_id,
@ -10,5 +10,5 @@ export default {
})
return result
}
},
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withOptionalAuthentication"],
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
const payload = {
limit: req.query?.limit,
@ -15,5 +15,5 @@ export default {
const result = await Posts.globalTimeline(payload)
return result
}
},
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withOptionalAuthentication"],
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
const payload = {
limit: req.query?.limit,
@ -15,5 +15,5 @@ export default {
const result = await Posts.timeline(payload)
return result
}
},
}

View File

@ -1,12 +1,12 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
return await Posts.getLiked({
trim: req.query.trim,
limit: req.query.limit,
user_id: req.auth.session.user_id
user_id: req.auth.session.user_id,
})
}
},
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req, res) => {
const result = await Posts.create(
{

View File

@ -1,12 +1,12 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withAuthentication"],
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
return await Posts.getSaved({
trim: req.query.trim,
limit: req.query.limit,
user_id: req.auth.session.user_id
user_id: req.auth.session.user_id,
})
}
},
}

View File

@ -2,7 +2,7 @@ import { Post } from "@db_models"
import stage from "@classes/posts/methods/stage"
export default {
middlewares: ["withOptionalAuthentication"],
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req) => {
const { limit, trim } = req.query

View File

@ -12,34 +12,34 @@ export default async (req) => {
{
$match: {
message: { $regex: /#/gi },
created_at: { $gte: startDate }
}
created_at: { $gte: startDate },
},
},
{
$project: {
hashtags: {
$regexFindAll: {
input: "$message",
regex: /#[a-zA-Z0-9_]+/g
}
}
}
regex: /#[a-zA-Z0-9_]+/g,
},
},
},
},
{ $unwind: "$hashtags" },
{
$project: {
hashtag: { $substr: ["$hashtags.match", 1, -1] }
}
hashtag: { $substr: ["$hashtags.match", 1, -1] },
},
},
{
$group: {
_id: "$hashtag",
count: { $sum: 1 }
}
count: { $sum: 1 },
},
},
{ $sort: { count: -1 } },
{ $limit: maxHashtags }
{ $limit: maxHashtags },
])
return trendings.map(({ _id, count }) => ({ hashtag: _id, count }));
return trendings.map(({ _id, count }) => ({ hashtag: _id, count }))
}

View File

@ -1,7 +1,7 @@
import Posts from "@classes/posts"
export default {
middlewares: ["withOptionalAuthentication"],
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
return await Posts.fromUserId({
skip: req.query.skip,
@ -9,5 +9,5 @@ export default {
user_id: req.params.user_id,
for_user_id: req.auth?.session?.user_id,
})
}
},
}