update all messages endpoints to also return author_username for easier display

This commit is contained in:
hippoz 2022-04-12 22:34:54 +03:00
parent 94192979a8
commit 5acb3ceaf0
No known key found for this signature in database
GPG key ID: 7C52899193467641
4 changed files with 14 additions and 11 deletions

View file

@ -0,0 +1,3 @@
export const getMessageById = "SELECT messages.id, messages.content, messages.channel_id, messages.created_at, messages.author_id, users.username AS author_username FROM messages JOIN users ON messages.author_id = users.id WHERE messages.id = $1";
export const getMessagesByChannelFirstPage = "SELECT messages.id, messages.content, messages.channel_id, messages.created_at, messages.author_id, users.username AS author_username FROM messages JOIN users ON messages.author_id = users.id WHERE messages.channel_id = $1 ORDER BY id DESC LIMIT 50";
export const getMessagesByChannelPage = "SELECT messages.id, messages.content, messages.channel_id, messages.created_at, messages.author_id, users.username AS author_username FROM messages JOIN users ON messages.author_id = users.id WHERE messages.id < $1 AND messages.channel_id = $2 ORDER BY id DESC LIMIT 50";

View file

@ -2,6 +2,7 @@ import express from "express";
import { body, param, validationResult } from "express-validator";
import { authenticateRoute } from "../../../auth";
import { query } from "../../../database";
import { getMessageById, getMessagesByChannelFirstPage, getMessagesByChannelPage } from "../../../database/templates";
import { errors } from "../../../errors";
import { dispatch, dispatchChannelSubscribe } from "../../../gateway";
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
@ -190,6 +191,7 @@ router.post(
content,
channel_id: channelId,
author_id: authorId,
author_username: req.user.username,
created_at: createdAt
};
@ -218,10 +220,10 @@ router.get(
let finalRows = [];
if (before) {
const result = await query("SELECT id, content, channel_id, author_id, created_at FROM messages WHERE id < $1 AND channel_id = $2 ORDER BY id DESC LIMIT 50", [before, channelId]);
const result = await query(getMessagesByChannelPage, [before, channelId]);
finalRows = result.rows;
} else {
const result = await query("SELECT id, content, channel_id, author_id, created_at FROM messages WHERE channel_id = $1 ORDER BY id DESC LIMIT 50", [channelId]);
const result = await query(getMessagesByChannelFirstPage, [channelId]);
finalRows = result.rows;
}

View file

@ -2,6 +2,7 @@ import express from "express";
import { body, param, validationResult } from "express-validator";
import { authenticateRoute } from "../../../auth";
import { query } from "../../../database";
import { getMessageById } from "../../../database/templates";
import { errors } from "../../../errors";
import { dispatch } from "../../../gateway";
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
@ -64,7 +65,7 @@ router.put(
const { content } = req.body;
const id = parseInt(req.params.id); // TODO: ??
const permissionCheckResult = await query("SELECT author_id, channel_id, created_at FROM messages WHERE id = $1", [id]);
const permissionCheckResult = await query(getMessageById, [id]);
if (permissionCheckResult.rowCount < 1) {
return res.status(404).json({
...errors.NOT_FOUND
@ -84,11 +85,8 @@ router.put(
}
const returnObject = {
id,
content,
channel_id: permissionCheckResult.rows[0].channel_id,
author_id: permissionCheckResult.rows[0].author_id,
created_at: permissionCheckResult.rows[0].created_at
...permissionCheckResult.rows[0],
content
};
dispatch(`channel:${permissionCheckResult.rows[0].channel_id}`, {
@ -111,7 +109,7 @@ router.get(
}
const { id } = req.params;
const result = await query("SELECT id, content, channel_id, author_id, created_at FROM messages WHERE id = $1", [id]);
const result = await query(getMessageById, [id]);
if (result.rowCount < 1) {
return res.status(404).json({
...errors.NOT_FOUND

View file

@ -75,7 +75,7 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6M
###
PUT http://localhost:3000/api/v1/messages/2 HTTP/1.1
PUT http://localhost:3000/api/v1/messages/3 HTTP/1.1
content-type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc
@ -85,7 +85,7 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6M
###
GET http://localhost:3000/api/v1/messages/2
GET http://localhost:3000/api/v1/messages/3
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc
###