From 6121aff29f8429fe35be315d292b2708c13575f9 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 23 Sep 2019 21:18:09 +0200 Subject: [PATCH] Added chat data objects --- package-lock.json | 41 +++++------------- src/lib/dataaccess/ChatMessage.ts | 7 ++++ src/lib/dataaccess/Chatroom.ts | 51 +++++++++++++++++++++++ src/public/graphql/schema.graphql | 3 ++ src/views/home/index.pug | 2 +- src/views/{home => includes}/stylebar.pug | 0 src/views/login/index.pug | 2 +- src/views/login/stylebar.pug | 2 - src/views/register/index.pug | 2 +- src/views/register/stylebar.pug | 2 - 10 files changed, 75 insertions(+), 37 deletions(-) create mode 100644 src/lib/dataaccess/ChatMessage.ts create mode 100644 src/lib/dataaccess/Chatroom.ts rename src/views/{home => includes}/stylebar.pug (100%) delete mode 100644 src/views/login/stylebar.pug delete mode 100644 src/views/register/stylebar.pug diff --git a/package-lock.json b/package-lock.json index 99cd62f..c82d4b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2526,8 +2526,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -2548,14 +2547,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2570,20 +2567,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2700,8 +2694,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2713,7 +2706,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2728,7 +2720,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2736,14 +2727,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2762,7 +2751,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2843,8 +2831,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -2856,7 +2843,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -2942,8 +2928,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -2979,7 +2964,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2999,7 +2983,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3043,14 +3026,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, diff --git a/src/lib/dataaccess/ChatMessage.ts b/src/lib/dataaccess/ChatMessage.ts new file mode 100644 index 0000000..5930679 --- /dev/null +++ b/src/lib/dataaccess/ChatMessage.ts @@ -0,0 +1,7 @@ +import {Chatroom} from "./Chatroom"; +import {User} from "./User"; + +export class ChatMessage { + constructor(public author: User, public chat: Chatroom, public timestamp: number, public content: string) { + } +} diff --git a/src/lib/dataaccess/Chatroom.ts b/src/lib/dataaccess/Chatroom.ts new file mode 100644 index 0000000..c6fe144 --- /dev/null +++ b/src/lib/dataaccess/Chatroom.ts @@ -0,0 +1,51 @@ +import {ChatMessage} from "./ChatMessage"; +import {queryHelper} from "./index"; +import {User} from "./User"; + +export class Chatroom { + + constructor(private id: number) {} + + /** + * Returns all members of a chatroom. + */ + public async members(): Promise { + const result = await queryHelper.all({ + text: `SELECT * FROM chat_members + JOIN users ON (chat_members.member = users.id) + WHERE chat_members.chat = $1;`, + values: [this.id], + }); + const chatMembers = []; + for (const row of result) { + chatMembers.push(new User(row)); + } + return chatMembers; + } + + /** + * Returns messages of the chat + * @param limit - the limit of messages to return + * @param offset - the offset of messages to return + * @param containing - filter by containing + */ + public async messages(limit?: number, offset?: number, containing?: string) { + const lim = limit || 16; + const offs = offset || 0; + + const result = await queryHelper.all({ + text: "SELECT * FROM chat_messages WHERE chat = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3", + values: [this.id, lim, offs], + }); + + const messages = []; + for (const row of result) { + messages.push(new ChatMessage(new User(row.author), this, row.timestamp, row.content)); + } + if (containing) { + return messages.filter((x) => x.content.includes(containing)); + } else { + return messages; + } + } +} diff --git a/src/public/graphql/schema.graphql b/src/public/graphql/schema.graphql index ff60fd3..c5b03c3 100644 --- a/src/public/graphql/schema.graphql +++ b/src/public/graphql/schema.graphql @@ -19,6 +19,9 @@ type Query { } type Mutation { + "Accepts the usage of cookies." + acceptCookies: Boolean + "Upvote/downvote a Post" vote(postId: ID!, type: [VoteType!]!): Boolean diff --git a/src/views/home/index.pug b/src/views/home/index.pug index 0fc45ad..7da0198 100644 --- a/src/views/home/index.pug +++ b/src/views/home/index.pug @@ -4,6 +4,6 @@ html include ../includes/head body div#content - include stylebar + include ../includes/stylebar include feed include friends diff --git a/src/views/home/stylebar.pug b/src/views/includes/stylebar.pug similarity index 100% rename from src/views/home/stylebar.pug rename to src/views/includes/stylebar.pug diff --git a/src/views/login/index.pug b/src/views/login/index.pug index 41fe877..81c7585 100644 --- a/src/views/login/index.pug +++ b/src/views/login/index.pug @@ -4,5 +4,5 @@ html include ../includes/head body div#content - include stylebar + include ../includes/stylebar include login diff --git a/src/views/login/stylebar.pug b/src/views/login/stylebar.pug deleted file mode 100644 index c6c36e3..0000000 --- a/src/views/login/stylebar.pug +++ /dev/null @@ -1,2 +0,0 @@ -div.stylebar - h1 Greenvironment diff --git a/src/views/register/index.pug b/src/views/register/index.pug index d8e9dd1..7ade6a7 100644 --- a/src/views/register/index.pug +++ b/src/views/register/index.pug @@ -4,5 +4,5 @@ html include ../includes/head body div#content - include stylebar + include ../includes/stylebar include register diff --git a/src/views/register/stylebar.pug b/src/views/register/stylebar.pug deleted file mode 100644 index c6c36e3..0000000 --- a/src/views/register/stylebar.pug +++ /dev/null @@ -1,2 +0,0 @@ -div.stylebar - h1 Greenvironment