Bug Fixes

- fixed Chatrooms
- fixed ChatMessages
pull/1/head
Trivernis 5 years ago
parent e1a9287641
commit 47d775fd2c

@ -3,7 +3,7 @@ import {Chatroom} from "./Chatroom";
import {User} from "./User"; import {User} from "./User";
export class ChatMessage { export class ChatMessage {
constructor(public author: User, public chat: Chatroom, public timestamp: number, public content: string) { constructor(public author: User, public chat: Chatroom, public createdAt: number, public content: string) {
} }
/** /**

@ -4,7 +4,9 @@ import {User} from "./User";
export class Chatroom { export class Chatroom {
constructor(private id: number) {} constructor(private readonly id: number) {
this.id = Number(id);
}
/** /**
* Returns if the chat exists. * Returns if the chat exists.
@ -29,7 +31,7 @@ export class Chatroom {
}); });
const chatMembers = []; const chatMembers = [];
for (const row of result) { for (const row of result) {
chatMembers.push(new User(row)); chatMembers.push(new User(row.id, row));
} }
return chatMembers; return chatMembers;
} }
@ -40,8 +42,8 @@ export class Chatroom {
* @param offset - the offset of messages to return * @param offset - the offset of messages to return
* @param containing - filter by containing * @param containing - filter by containing
*/ */
public async messages(limit?: number, offset?: number, containing?: string) { public async messages({first, offset, containing}: {first?: number, offset?: number, containing?: string}) {
const lim = limit || 16; const lim = first || 16;
const offs = offset || 0; const offs = offset || 0;
const result = await queryHelper.all({ const result = await queryHelper.all({
@ -51,7 +53,7 @@ export class Chatroom {
const messages = []; const messages = [];
for (const row of result) { for (const row of result) {
messages.push(new ChatMessage(new User(row.author), this, row.timestamp, row.content)); messages.push(new ChatMessage(new User(row.author), this, row.created_at, row.content));
} }
if (containing) { if (containing) {
return messages.filter((x) => x.content.includes(containing)); return messages.filter((x) => x.content.includes(containing));

@ -5,6 +5,7 @@ export abstract class DataObject {
protected dataLoaded: boolean = false; protected dataLoaded: boolean = false;
constructor(public id: number, protected row?: any) { constructor(public id: number, protected row?: any) {
this.id = Number(id);
} }
/** /**

@ -140,7 +140,7 @@ namespace dataaccess {
*/ */
export async function createChat(...members: number[]): Promise<Chatroom> { export async function createChat(...members: number[]): Promise<Chatroom> {
const idResult = await queryHelper.first({ const idResult = await queryHelper.first({
text: "INSERT INTO chats (id) values (nextval('chats_id_seq'::regclass)) RETURNING *;", text: "INSERT INTO chats (id) values (default) RETURNING *;",
}); });
const id = idResult.id; const id = idResult.id;
const transaction = await queryHelper.createTransaction(); const transaction = await queryHelper.createTransaction();
@ -149,14 +149,15 @@ namespace dataaccess {
for (const member of members) { for (const member of members) {
await transaction.query({ await transaction.query({
name: "chat-member-insert", name: "chat-member-insert",
text: "INSERT INTO chat_members (ABSOLUTE chat, member) VALUES ($1, $2);", text: "INSERT INTO chat_members (chat, member) VALUES ($1, $2);",
values: [member], values: [id, member],
}); });
} }
await transaction.commit(); await transaction.commit();
} catch (err) { } catch (err) {
globals.logger.warn(`Failed to insert chatmember into database: ${err.message}`); globals.logger.warn(`Failed to insert chatmember into database: ${err.message}`);
globals.logger.debug(err.stack); globals.logger.debug(err.stack);
await transaction.rollback();
} finally { } finally {
transaction.release(); transaction.release();
} }
@ -173,10 +174,10 @@ namespace dataaccess {
const chat = new Chatroom(chatId); const chat = new Chatroom(chatId);
if ((await chat.exists())) { if ((await chat.exists())) {
const result = await queryHelper.first({ const result = await queryHelper.first({
text: "INSERT INTO chat_messages (chat, author, content, created_at) values ($1, $2, $3) RETURNING *", text: "INSERT INTO chat_messages (chat, author, content) values ($1, $2, $3) RETURNING *",
values: [chatId, authorId, content], values: [chatId, authorId, content],
}); });
return new ChatMessage(new User(result.author), chat, result.timestamp, result.content); return new ChatMessage(new User(result.author), chat, result.created_at, result.content);
} else { } else {
throw new ChatNotFoundError(chatId); throw new ChatNotFoundError(chatId);
} }

@ -11,9 +11,6 @@ type Query {
"returns the chat object for a chat id" "returns the chat object for a chat id"
getChat(chatId: ID!): ChatRoom getChat(chatId: ID!): ChatRoom
"Creates a chat between the user (and optional an other user)"
createChat(members: [ID!]): ChatRoom
"returns the request object for a request id" "returns the request object for a request id"
getRequest(requestId: ID!): Request getRequest(requestId: ID!): Request
@ -60,6 +57,9 @@ type Mutation {
"delete the post for a given post id" "delete the post for a given post id"
deletePost(postId: ID!): Boolean deletePost(postId: ID!): Boolean
"Creates a chat between the user (and optional an other user)"
createChat(members: [ID!]): ChatRoom
} }
"represents a single user account" "represents a single user account"
@ -141,7 +141,7 @@ type ChatRoom {
members: [User!] members: [User!]
"return a specfic range of messages posted in the chat" "return a specfic range of messages posted in the chat"
getMessages(first: Int, offset: Int): [ChatMessage] messages(first: Int = 10, offset: Int, containing: String): [ChatMessage]!
"id of the chat" "id of the chat"
id: ID! id: ID!
@ -149,16 +149,16 @@ type ChatRoom {
type ChatMessage { type ChatMessage {
"The author of the chat message." "The author of the chat message."
author: User author: User!
"The chatroom the message was posted in" "The chatroom the message was posted in"
chat: ChatRoom chat: ChatRoom!
"The timestamp when the message was posted (epoch)." "The timestamp when the message was posted (epoch)."
timestamp: Int createdAt: String!
"The content of the message." "The content of the message."
content: String content: String!
"The content of the message rendered by markdown-it." "The content of the message rendered by markdown-it."
htmlContent: String htmlContent: String

@ -181,7 +181,7 @@ class HomeRoute extends Route {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async sendChatMessage({chatId, content}: {chatId: number, content: string}) { async sendMessage({chatId, content}: {chatId: number, content: string}) {
if (!req.session.userId) { if (!req.session.userId) {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }

@ -52,7 +52,7 @@ END$$;
DO $$ BEGIN DO $$ BEGIN
CREATE TABLE IF NOT EXISTS "user_sessions" ( CREATE TABLE IF NOT EXISTS "user_sessions" (
"sid" varchar NOT NULL COLLATE "default", "sid" varchar NOT NULL,
"sess" json NOT NULL, "sess" json NOT NULL,
"expire" timestamp(6) NOT NULL, "expire" timestamp(6) NOT NULL,
PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE

Loading…
Cancel
Save