mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
use posts v2
This commit is contained in:
parent
75156dc201
commit
7e74e7c1cc
@ -54,14 +54,14 @@ export default class PostsController extends Controller {
|
||||
"/post": {
|
||||
middlewares: ["withAuthentication"],
|
||||
fn: Schematized({
|
||||
required: ["message", "date"],
|
||||
select: ["message", "additions", "type", "data", "date"],
|
||||
required: ["timestamp"],
|
||||
select: ["message", "attachments", "type", "data", "timestamp"],
|
||||
}, async (req, res) => {
|
||||
const post = await CreatePost({
|
||||
user_id: req.user.id,
|
||||
message: req.selection.message,
|
||||
date: req.selection.date,
|
||||
additions: req.selection.additions,
|
||||
timestamp: req.selection.timestamp,
|
||||
attachments: req.selection.attachments,
|
||||
type: req.selection.type,
|
||||
data: req.selection.data,
|
||||
})
|
||||
|
@ -1,15 +1,31 @@
|
||||
import { Post } from "../../../models"
|
||||
import getPostData from "./getPostData"
|
||||
import momentTimezone from "moment-timezone"
|
||||
|
||||
export default async (payload) => {
|
||||
const { user_id, message, additions, type, data, date } = payload
|
||||
let { user_id, message, attachments, type, data, timestamp } = payload
|
||||
|
||||
// check if is a Array and have at least one element
|
||||
const isAttachmentsValid = Array.isArray(attachments) && attachments.length > 0
|
||||
|
||||
if (!isAttachmentsValid && !message) {
|
||||
throw new Error("Cannot create a post without message or attachments")
|
||||
}
|
||||
|
||||
if (message) {
|
||||
message = String(message).toString()
|
||||
}
|
||||
|
||||
const current_timezone = momentTimezone.tz.guess()
|
||||
const created_at = momentTimezone.tz(Date.now(), current_timezone).format()
|
||||
|
||||
const post = new Post({
|
||||
user_id: typeof user_id === "object" ? user_id.toString() : user_id,
|
||||
message: String(message).toString(),
|
||||
additions: additions ?? [],
|
||||
created_at: date,
|
||||
type: type,
|
||||
created_at: created_at,
|
||||
user_id: typeof user_id === "object" ? user_id.toString() : user_id,
|
||||
message: message,
|
||||
attachments: attachments ?? [],
|
||||
timestamp: timestamp,
|
||||
data: data,
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user