mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
move some admin endpoints to a independent controller
This commit is contained in:
parent
aaececd91e
commit
2640b57925
@ -0,0 +1,58 @@
|
|||||||
|
import { User, Session, Post } from "@shared-classes/DbModels"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
method: "GET",
|
||||||
|
route: "/accounts_statistics",
|
||||||
|
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||||
|
fn: async (req, res) => {
|
||||||
|
// get number of users registered,
|
||||||
|
const users = await User.count()
|
||||||
|
|
||||||
|
// calculate the last 5 days logins from diferent users
|
||||||
|
let last5D_logins = await Session.find({
|
||||||
|
date: {
|
||||||
|
$gte: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000),
|
||||||
|
$lte: new Date(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const last5D_logins_counts = []
|
||||||
|
|
||||||
|
// filter from different users
|
||||||
|
last5D_logins.forEach((session) => {
|
||||||
|
if (!last5D_logins_counts.includes(session.user_id)) {
|
||||||
|
last5D_logins_counts.push(session.user_id)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// calculate active users within 1 week (using postings)
|
||||||
|
const active_1w_posts_users = await Post.count({
|
||||||
|
user_id: {
|
||||||
|
$in: last5D_logins_counts
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
$gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
|
||||||
|
$lte: new Date(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// calculate total posts
|
||||||
|
const total_posts = await Post.count()
|
||||||
|
|
||||||
|
// calculate total post (1week)
|
||||||
|
const total_posts_1w = await Post.count({
|
||||||
|
created_at: {
|
||||||
|
$gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
|
||||||
|
$lte: new Date(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
accounts_registered: users,
|
||||||
|
last5D_logins: last5D_logins_counts.length,
|
||||||
|
active_1w_posts_users: active_1w_posts_users,
|
||||||
|
total_posts: total_posts,
|
||||||
|
total_posts_1w: total_posts_1w,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
import { FeaturedWallpaper } from "@shared-classes/DbModels"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
method: "DELETE",
|
||||||
|
route: "/featured_wallpaper/:id",
|
||||||
|
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||||
|
fn: async (req, res) => {
|
||||||
|
const id = req.params.id
|
||||||
|
|
||||||
|
const wallpaper = await FeaturedWallpaper.findById(id)
|
||||||
|
|
||||||
|
if (!wallpaper) {
|
||||||
|
return res.status(404).json({
|
||||||
|
error: "Cannot find wallpaper"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await FeaturedWallpaper.deleteOne({
|
||||||
|
_id: id
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
done: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
import { FeaturedWallpaper } from "@shared-classes/DbModels"
|
||||||
|
import momentTimezone from "moment-timezone"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
method: "PUT",
|
||||||
|
route: "/featured_wallpaper",
|
||||||
|
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||||
|
fn: async (req, res) => {
|
||||||
|
const data = req.body.wallpaper
|
||||||
|
|
||||||
|
if (!data) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "Invalid data"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to find if data._id exists, else create a new one
|
||||||
|
let wallpaper = null
|
||||||
|
|
||||||
|
if (data._id) {
|
||||||
|
wallpaper = await FeaturedWallpaper.findOne({
|
||||||
|
_id: data._id
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
wallpaper = new FeaturedWallpaper()
|
||||||
|
}
|
||||||
|
|
||||||
|
const current_timezone = momentTimezone.tz.guess()
|
||||||
|
|
||||||
|
wallpaper.active = data.active ?? wallpaper.active ?? true
|
||||||
|
wallpaper.date = data.date ?? momentTimezone.tz(Date.now(), current_timezone).format()
|
||||||
|
wallpaper.url = data.url ?? wallpaper.url
|
||||||
|
wallpaper.author = data.author ?? wallpaper.author
|
||||||
|
|
||||||
|
await wallpaper.save()
|
||||||
|
|
||||||
|
return res.json(wallpaper)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
import { User } from "@shared-classes/DbModels"
|
||||||
|
|
||||||
|
export default {
|
||||||
|
method: "POST",
|
||||||
|
route: "/update_data/:user_id",
|
||||||
|
middlewares: ["withAuthentication", "onlyAdmin"],
|
||||||
|
fn: async (req, res) => {
|
||||||
|
const targetUserId = req.params.user_id
|
||||||
|
|
||||||
|
const user = await User.findById(targetUserId).catch((err) => {
|
||||||
|
return false
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ error: "No user founded" })
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateKeys = Object.keys(req.body.update)
|
||||||
|
|
||||||
|
updateKeys.forEach((key) => {
|
||||||
|
user[key] = req.body.update[key]
|
||||||
|
})
|
||||||
|
|
||||||
|
await user.save()
|
||||||
|
|
||||||
|
global.websocket_instance.io.emit(`user.update`, {
|
||||||
|
...user.toObject(),
|
||||||
|
})
|
||||||
|
global.websocket_instance.io.emit(`user.update.${targetUserId}`, {
|
||||||
|
...user.toObject(),
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json(user.toObject())
|
||||||
|
}
|
||||||
|
}
|
@ -4,9 +4,19 @@ export default {
|
|||||||
method: "GET",
|
method: "GET",
|
||||||
route: "/featured_wallpapers",
|
route: "/featured_wallpapers",
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
const featuredWallpapers = await FeaturedWallpaper.find({})
|
const { all } = req.query
|
||||||
|
|
||||||
|
const query = {
|
||||||
|
active: true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (all) {
|
||||||
|
delete query.active
|
||||||
|
}
|
||||||
|
|
||||||
|
const featuredWallpapers = await FeaturedWallpaper.find(query)
|
||||||
.sort({ date: -1 })
|
.sort({ date: -1 })
|
||||||
.limit(10)
|
.limit(all ? undefined : 10)
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
error: err.message
|
error: err.message
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
import { Schematized } from "@lib"
|
|
||||||
import { FeaturedWallpaper } from "@shared-classes/DbModels"
|
|
||||||
|
|
||||||
export default {
|
|
||||||
method: "POST",
|
|
||||||
route: "/featured_wallpaper",
|
|
||||||
middlewares: ["withAuthentication", "onlyAdmin"],
|
|
||||||
fn: Schematized({
|
|
||||||
select: ["url", "date", "author"],
|
|
||||||
required: ["url"],
|
|
||||||
}, async (req, res) => {
|
|
||||||
const newFeaturedWallpaper = new FeaturedWallpaper(req.selection)
|
|
||||||
|
|
||||||
const result = await newFeaturedWallpaper.save().catch((err) => {
|
|
||||||
res.status(400).json({ message: err.message })
|
|
||||||
return null
|
|
||||||
})
|
|
||||||
|
|
||||||
if (result) {
|
|
||||||
return res.json(newFeaturedWallpaper)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
@ -47,8 +47,24 @@ export default {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
let selectedSearchers = []
|
||||||
|
|
||||||
|
if (!Array.isArray(params.select)) {
|
||||||
|
selectedSearchers = searchers
|
||||||
|
} else {
|
||||||
|
const findedSearchers = []
|
||||||
|
|
||||||
|
for (const searcher of searchers) {
|
||||||
|
if (params.select.includes(searcher.id)) {
|
||||||
|
findedSearchers.push(searcher)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedSearchers = findedSearchers
|
||||||
|
}
|
||||||
|
|
||||||
await pmap(
|
await pmap(
|
||||||
searchers,
|
selectedSearchers,
|
||||||
async (searcher) => {
|
async (searcher) => {
|
||||||
let results = await searcher.model.find(searcher.query)
|
let results = await searcher.model.find(searcher.query)
|
||||||
.limit(searcher.limit ?? 5)
|
.limit(searcher.limit ?? 5)
|
||||||
|
@ -28,6 +28,10 @@ export default {
|
|||||||
return res.status(404).json({ error: "User not exists" })
|
return res.status(404).json({ error: "User not exists" })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (req.user.roles.includes("admin")) {
|
||||||
|
return res.json(user)
|
||||||
|
}
|
||||||
|
|
||||||
if (req.user._id.toString() !== user._id.toString()) {
|
if (req.user._id.toString() !== user._id.toString()) {
|
||||||
user = lodash.pick(user, publicGetters)
|
user = lodash.pick(user, publicGetters)
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ export default {
|
|||||||
name: "FeaturedWallpaper",
|
name: "FeaturedWallpaper",
|
||||||
collection: "featuredWallpapers",
|
collection: "featuredWallpapers",
|
||||||
schema: {
|
schema: {
|
||||||
|
active: { type: Boolean, default: true },
|
||||||
date: { type: Date, default: Date.now },
|
date: { type: Date, default: Date.now },
|
||||||
url: { type: String, required: true },
|
url: { type: String, required: true },
|
||||||
author: { type: String },
|
author: { type: String },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user