Added Sequelize

- added sequelize for database management
- added sequelize models under dataaccess/datamodels
- removed postgres-only stuff
pull/2/head
Trivernis 5 years ago
parent b671845337
commit 57338355a7

@ -8,10 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Connection to Postgres Database
- Graphql Schema
- default-config file and generation of config file on startup
- DTOs
- Home Route
- database caching
- session management
- Sequelize modules and integration

624
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -35,6 +35,7 @@
"@types/markdown-it": "0.0.9",
"@types/node": "^12.7.8",
"@types/pg": "^7.11.0",
"@types/sequelize": "^4.28.5",
"@types/socket.io": "^2.1.2",
"@types/winston": "^2.4.4",
"delete": "^1.1.0",
@ -49,7 +50,7 @@
},
"dependencies": {
"compression": "^1.7.4",
"connect-pg-simple": "^6.0.1",
"connect-session-sequelize": "^6.0.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"express": "^4.17.1",
@ -57,7 +58,6 @@
"express-session": "^1.16.2",
"express-socket.io-session": "^1.3.5",
"fs-extra": "^8.1.0",
"g": "^2.0.1",
"graphql": "^14.4.2",
"graphql-import": "^0.7.1",
"http-status": "^1.3.2",
@ -66,7 +66,9 @@
"markdown-it-emoji": "^1.4.0",
"pg": "^7.12.1",
"pug": "^2.0.4",
"sequelize": "^5.19.6",
"socket.io": "^2.2.0",
"sqlite3": "^4.1.0",
"winston": "^3.2.1"
}
}

@ -1,5 +1,4 @@
import * as compression from "compression";
import connectPgSimple = require("connect-pg-simple");
import * as cookieParser from "cookie-parser";
import * as cors from "cors";
import * as express from "express";
@ -10,33 +9,35 @@ import {buildSchema} from "graphql";
import {importSchema} from "graphql-import";
import * as http from "http";
import * as path from "path";
import {Sequelize} from "sequelize";
import * as socketIo from "socket.io";
import {resolver} from "./graphql/resolvers";
import dataaccess, {queryHelper} from "./lib/dataaccess";
import dataaccess from "./lib/dataaccess";
import globals from "./lib/globals";
import routes from "./routes";
import * as fsx from "fs-extra";
const SequelizeStore = require("connect-session-sequelize")(session.Store);
const logger = globals.logger;
const PgSession = connectPgSimple(session);
class App {
public app: express.Application;
public io: socketIo.Server;
public server: http.Server;
public readonly sequelize: Sequelize;
constructor() {
this.app = express();
this.server = new http.Server(this.app);
this.io = socketIo(this.server);
this.sequelize = new Sequelize(globals.config.database.connectionUri);
}
/**
* initializes everything that needs to be initialized asynchronous.
*/
public async init() {
await dataaccess.init();
await routes.ioListeners(this.io);
await dataaccess.init(this.sequelize);
const appSession = session({
cookie: {
@ -46,12 +47,14 @@ class App {
resave: false,
saveUninitialized: false,
secret: globals.config.session.secret,
store: new PgSession({
pool: dataaccess.pool,
tableName: "user_sessions",
}),
store: new SequelizeStore({db: this.sequelize}),
});
const force = fsx.existsSync("sqz-force");
logger.info(`Sequelize Table force: ${force}`);
await this.sequelize.sync({force, logging: (msg) => logger.silly(msg)});
await routes.ioListeners(this.io);
this.io.use(sharedsession(appSession, {autoSave: true}));
this.app.set("views", path.join(__dirname, "views"));

@ -1,10 +1,6 @@
# database connection info
database:
host:
port:
user:
password:
database:
connectionUri: "sqlite://:memory:"
# http server configuration
server:

@ -1,7 +1,9 @@
import {GraphQLError} from "graphql";
import * as status from "http-status";
import {Sequelize} from "sequelize";
import dataaccess from "../lib/dataaccess";
import {Chatroom} from "../lib/dataaccess/Chatroom";
import * as models from "../lib/dataaccess/datamodels";
import {Post} from "../lib/dataaccess/Post";
import {Profile} from "../lib/dataaccess/Profile";
import {User} from "../lib/dataaccess/User";
@ -17,9 +19,10 @@ import {is} from "../lib/regex";
*/
export function resolver(req: any, res: any): any {
return {
getSelf() {
async getSelf() {
if (req.session.userId) {
return new Profile(req.session.userId);
const user = await models.SqUser.findByPk(req.session.userId);
return user.profile;
} else {
res.status(status.UNAUTHORIZED);
return new NotLoggedInGqlError();
@ -29,7 +32,8 @@ export function resolver(req: any, res: any): any {
if (handle) {
return await dataaccess.getUserByHandle(handle);
} else if (userId) {
return new User(userId);
const user = await models.SqUser.findByPk(userId);
return user.user;
} else {
res.status(status.BAD_REQUEST);
return new GraphQLError("No userId or handle provided.");
@ -45,7 +49,8 @@ export function resolver(req: any, res: any): any {
},
async getChat({chatId}: { chatId: number }) {
if (chatId) {
return new Chatroom(chatId);
const chat = await models.SqChat.findByPk(chatId);
return new Chatroom(chat);
} else {
res.status(status.BAD_REQUEST);
return new GraphQLError("No chatId given.");
@ -105,7 +110,8 @@ export function resolver(req: any, res: any): any {
async vote({postId, type}: { postId: number, type: dataaccess.VoteType }) {
if (postId && type) {
if (req.session.userId) {
return await (new Post(postId)).vote(req.session.userId, type);
const post = await models.SqPost.findByPk(postId);
return await (post.post).vote(req.session.userId, type);
} else {
res.status(status.UNAUTHORIZED);
return new NotLoggedInGqlError();
@ -132,7 +138,7 @@ export function resolver(req: any, res: any): any {
},
async deletePost({postId}: { postId: number }) {
if (postId) {
const post = new Post(postId);
const post = (await models.SqPost.findByPk(postId)).post;
if ((await post.author()).id === req.session.userId) {
return await dataaccess.deletePost(post.id);
} else {

@ -206,6 +206,9 @@ type ChatRoom {
}
type ChatMessage {
"Id of the chat message"
id: ID!
"The author of the chat message."
author: User!

@ -1,212 +0,0 @@
/**
* @author Trivernis
* @remarks
*
* Taken from {@link https://github.com/Trivernis/whooshy}
*/
import * as fsx from "fs-extra";
import {Pool, PoolClient, QueryConfig, QueryResult} from "pg";
import globals from "./globals";
const logger = globals.logger;
export interface IAdvancedQueryConfig extends QueryConfig {
cache?: boolean;
}
/**
* Transaction class to wrap SQL transactions.
*/
export class SqlTransaction {
/**
* Constructor.
* @param client
*/
constructor(private client: PoolClient) {
}
/**
* Begins the transaction.
*/
public async begin() {
return await this.client.query("BEGIN");
}
/**
* Commits the transaction
*/
public async commit() {
return await this.client.query("COMMIT");
}
/**
* Rolls back the transaction
*/
public async rollback() {
return await this.client.query("ROLLBACK");
}
/**
* Executes a query inside the transaction.
* @param query
*/
public async query(query: QueryConfig) {
return await this.client.query(query);
}
/**
* Releases the client back to the pool.
*/
public release() {
this.client.release();
}
}
/**
* Query helper for easyer fetching of a specific row count.
*/
export class QueryHelper {
private pool: Pool;
/**
* Constructor.
* @param pgPool
* @param [tableCreationFile]
* @param [tableUpdateFile]
*/
constructor(pgPool: Pool, private tableCreationFile?: string, private tableUpdateFile?: string) {
this.pool = pgPool;
}
/**
* Async init function
*/
public async init() {
await this.pool.connect();
await this.createTables();
await this.updateTableDefinitions();
}
/**
* creates all tables needed if a filepath was given with the constructor
*/
public async createTables() {
if (this.tableCreationFile) {
logger.info("Creating nonexistent tables...");
const tableSql = await fsx.readFile(this.tableCreationFile, "utf-8");
const trans = await this.createTransaction();
await trans.begin();
try {
await trans.query({text: tableSql});
await trans.commit();
} catch (err) {
globals.logger.error(`Error on table creation ${err.message}`);
globals.logger.debug(err.stack);
await trans.rollback();
} finally {
trans.release();
}
}
}
/**
* Updates the definition of the tables if the table update file was passed in the constructor
*/
public async updateTableDefinitions() {
if (this.tableUpdateFile) {
logger.info("Updating table definitions...");
const tableSql = await fsx.readFile(this.tableUpdateFile, "utf-8");
const trans = await this.createTransaction();
await trans.begin();
try {
await trans.query({text: tableSql});
await trans.commit();
} catch (err) {
globals.logger.error(`Error on table update ${err.message}`);
globals.logger.debug(err.stack);
await trans.rollback();
} finally {
trans.release();
}
}
}
/**
* executes the sql query with values and returns all results.
* @param query
*/
public async all(query: IAdvancedQueryConfig): Promise<any[]> {
const result = await this.query(query);
return result.rows;
}
/**
* executes the sql query with values and returns the first result.
* @param query
*/
public async first(query: IAdvancedQueryConfig): Promise<any> {
const result = await this.query(query);
if (result.rows && result.rows.length > 0) {
return result.rows[0];
}
}
/**
* Creates a new Transaction to be uses with error handling.
*/
public async createTransaction() {
const client: PoolClient = await this.pool.connect();
return new SqlTransaction(client);
}
/**
* Queries the database with error handling.
* @param query - the sql and values to execute
*/
private async query(query: IAdvancedQueryConfig): Promise<QueryResult|{rows: any}> {
try {
query.text = query.text.replace(/[\r\n]/g, " ");
globals.logger.silly(`Executing sql '${JSON.stringify(query)}'`);
if (query.cache) {
const key = globals.cache.hashKey(JSON.stringify(query));
const cacheResult = globals.cache.get(key);
if (cacheResult) {
return cacheResult;
} else {
const result = await this.pool.query(query);
globals.cache.set(key, result);
return result;
}
} else {
return await this.pool.query(query);
}
} catch (err) {
logger.debug(`Error on query "${JSON.stringify(query)}".`);
logger.error(`Sql query failed: ${err}`);
logger.verbose(err.stack);
return {
rows: null,
};
}
}
}
/**
* Returns the parameterized value sql for inserting
* @param columnCount
* @param rowCount
* @param [offset]
*/
export function buildSqlParameters(columnCount: number, rowCount: number, offset?: number): string {
let sql = "";
for (let i = 0; i < rowCount; i++) {
sql += "(";
for (let j = 0; j < columnCount; j++) {
sql += `$${(i * columnCount) + j + 1 + offset},`;
}
sql = sql.replace(/,$/, "") + "),";
}
return sql.replace(/,$/, "");
}

@ -20,6 +20,7 @@ abstract class Route {
protected ions?: Namespace;
public abstract async init(...params: any): Promise<any>;
public abstract async destroy(...params: any): Promise<any>;
}

@ -1,31 +1,38 @@
import markdown from "../markdown";
import {Chatroom} from "./Chatroom";
import * as models from "./datamodels/models";
import {User} from "./User";
export class ChatMessage {
constructor(
public readonly author: User,
public readonly chat: Chatroom,
public readonly createdAt: number,
public readonly content: string) {}
public id: number;
public content: string;
public createdAt: Date;
constructor(private message: models.ChatMessage) {
this.id = message.id;
this.content = message.content;
this.createdAt = message.createdAt;
}
/**
* returns the author of the chat message.
*/
public async author(): Promise<User> {
return new User(await this.message.getAuthor());
}
/**
* The content rendered by markdown-it.
* Returns the rendered html content of the chat message.
*/
public htmlContent(): string {
return markdown.renderInline(this.content);
}
/**
* Returns resolved and rendered content of the chat message.
* returns the chatroom for the chatmessage.
*/
public resolvedContent() {
return {
author: this.author.id,
chat: this.chat.id,
content: this.content,
createdAt: this.createdAt,
htmlContent: this.htmlContent(),
};
public async chat(): Promise<Chatroom> {
return (await this.message.getChat()).chatroom;
}
}

@ -1,44 +1,22 @@
import globals from "../globals";
import {ChatMessage} from "./ChatMessage";
import {queryHelper} from "./index";
import {SqChat} from "./datamodels";
import {User} from "./User";
export class Chatroom {
public readonly id: number;
public namespace: string;
constructor(public readonly id: number) {
this.id = Number(id);
this.namespace = `/chat/${id}`;
}
/**
* Returns if the chat exists.
*/
public async exists(): Promise<boolean> {
const result = await queryHelper.first({
text: "SELECT id FROM chats WHERE id = $1",
values: [this.id],
});
return !!result.id;
constructor(private chat: SqChat) {
this.id = chat.id;
this.namespace = `/chat/${chat.id}`;
}
/**
* Returns all members of a chatroom.
*/
public async members(): Promise<User[]> {
const result = await queryHelper.all({
cache: true,
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) {
const user = new User(row.id, row);
chatMembers.push(user);
}
return chatMembers;
const members = await this.chat.getMembers();
return members.map((m) => new User(m));
}
/**
@ -47,30 +25,14 @@ export class Chatroom {
* @param offset - the offset of messages to return
* @param containing - filter by containing
*/
public async messages({first, offset, containing}: {first?: number, offset?: number, containing?: string}) {
public async messages({first, offset, containing}: { first?: number, offset?: number, containing?: string }) {
const lim = first || 16;
const offs = offset || 0;
const result = await queryHelper.all({
cache: true,
text: "SELECT * FROM chat_messages WHERE chat = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
values: [this.id, lim, offs],
});
const messages = [];
const users: any = {};
for (const row of result) {
if (!users[row.author]) {
const user = new User(row.author);
await user.exists();
users[row.author] = user;
}
messages.push(new ChatMessage(users[row.author], this, row.created_at, row.content));
}
const messages = await this.chat.getMessages({limit: lim, offset: offs});
if (containing) {
return messages.filter((x) => x.content.includes(containing));
return messages.filter((x) => x.content.includes(containing)).map((m) => m.message);
} else {
return messages;
return messages.map((m) => m.message);
}
}
}

@ -1,40 +0,0 @@
/**
* abstact DataObject class
*/
import {EventEmitter} from "events";
export abstract class DataObject extends EventEmitter {
protected dataLoaded: boolean = false;
private loadingData: boolean = false;
constructor(public id: number, protected row?: any) {
super();
this.id = Number(id);
}
/**
* Returns if the object extists by trying to load data.
*/
public async exists() {
await this.loadDataIfNotExists();
return this.dataLoaded;
}
protected abstract loadData(): Promise<void>;
/**
* Loads data from the database if data has not been loaded
*/
protected async loadDataIfNotExists() {
if (!this.dataLoaded && !this.loadingData) {
this.loadingData = true;
await this.loadData();
this.loadingData = false;
this.emit("loaded");
} else if (this.loadingData) {
return new Promise((res) => {
this.on("loaded", () => res());
});
}
}
}

@ -1,106 +1,68 @@
import markdown from "../markdown";
import {DataObject} from "./DataObject";
import {queryHelper} from "./index";
import {SqPost, SqPostVotes} from "./datamodels";
import {PostVotes} from "./datamodels/models";
import dataaccess from "./index";
import {User} from "./User";
export class Post extends DataObject {
export class Post {
public readonly id: number;
private $createdAt: string;
private $content: string;
private $author: number;
private $type: string;
public createdAt: Date;
public content: string;
public type: string;
/**
* Returns the resolved data of the post.
*/
public async resolvedData() {
await this.loadDataIfNotExists();
return {
authorId: this.$author,
content: this.$content,
createdAt: this.$createdAt,
id: this.id,
type: this.$type,
};
private post: SqPost;
constructor(post: SqPost) {
this.id = post.id;
this.createdAt = post.createdAt;
this.post = post;
this.type = "";
this.content = post.content;
}
/**
* Returns the upvotes of a post.
*/
public async upvotes(): Promise<number> {
const result = await queryHelper.first({
cache: true,
text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'UPVOTE'",
values: [this.id],
});
return result.count;
return PostVotes.count({where: {voteType: dataaccess.VoteType.UPVOTE, post_id: this.id}});
}
/**
* Returns the downvotes of the post
*/
public async downvotes(): Promise<number> {
const result = await queryHelper.first({
cache: true,
text: "SELECT COUNT(*) count FROM votes WHERE item_id = $1 AND vote_type = 'DOWNVOTE'",
values: [this.id],
});
return result.count;
}
/**
* The content of the post (markdown)
*/
public async content(): Promise<string> {
await this.loadDataIfNotExists();
return this.$content;
return PostVotes.count({where: {voteType: dataaccess.VoteType.DOWNVOTE, post_id: this.id}});
}
/**
* the content rendered by markdown-it.
*/
public async htmlContent(): Promise<string> {
await this.loadDataIfNotExists();
return markdown.render(this.$content);
}
/**
* The date the post was created at.
*/
public async createdAt(): Promise<string> {
await this.loadDataIfNotExists();
return this.$createdAt;
return markdown.render(this.content);
}
/**
* The autor of the post.
*/
public async author(): Promise<User> {
await this.loadDataIfNotExists();
return new User(this.$author);
return new User(await this.post.getUser());
}
/**
* Deletes the post.
*/
public async delete(): Promise<void> {
const query = await queryHelper.first({
text: "DELETE FROM posts WHERE id = $1",
values: [this.id],
});
await this.post.destroy();
}
/**
* The type of vote the user performed on the post.
*/
public async userVote(userId: number): Promise<dataaccess.VoteType> {
const result = await queryHelper.first({
cache: true,
text: "SELECT vote_type FROM votes WHERE user_id = $1 AND item_id = $2",
values: [userId, this.id],
});
if (result) {
return result.vote_type;
const votes = await this.post.getVotes({where: {userId}});
if (votes.length >= 1) {
return votes[0].voteType;
} else {
return null;
}
@ -112,48 +74,10 @@ export class Post extends DataObject {
* @param type
*/
public async vote(userId: number, type: dataaccess.VoteType): Promise<dataaccess.VoteType> {
const uVote = await this.userVote(userId);
if (uVote === type) {
await queryHelper.first({
text: "DELETE FROM votes WHERE item_id = $1 AND user_id = $2",
values: [this.id, userId],
});
} else {
if (uVote) {
await queryHelper.first({
text: "UPDATE votes SET vote_type = $1 WHERE user_id = $2 AND item_id = $3",
values: [type, userId, this.id],
});
} else {
await queryHelper.first({
text: "INSERT INTO votes (user_id, item_id, vote_type) values ($1, $2, $3)",
values: [userId, this.id, type],
});
}
return type;
}
}
/**
* Loads the data from the database if needed.
*/
protected async loadData(): Promise<void> {
let result: any;
if (this.row) {
result = this.row;
} else {
result = await queryHelper.first({
cache: true,
text: "SELECT * FROM posts WHERE posts.id = $1",
values: [this.id],
});
}
if (result) {
this.$author = result.author;
this.$content = result.content;
this.$createdAt = result.created_at;
this.$type = result.type;
this.dataLoaded = true;
}
const [vote, _] = await SqPostVotes
.findOrCreate({where: {userId}, defaults: {voteType: type, postId: this.post.id}});
vote.voteType = type;
await vote.save();
return vote.voteType;
}
}

@ -1,10 +1,61 @@
import {RequestNotFoundError} from "../errors/RequestNotFoundError";
import {Chatroom} from "./Chatroom";
import dataaccess, {queryHelper} from "./index";
import {User} from "./User";
import {Request} from "./Request";
import {SqUser} from "./datamodels";
import dataaccess from "./index";
import * as wrappers from "./wrappers";
export class Profile extends User {
export class Profile {
public id: number;
public name: string;
public handle: string;
public email: string;
public greenpoints: number;
public joinedAt: Date;
protected user: SqUser;
constructor(user: SqUser) {
this.name = user.username;
this.handle = user.handle;
this.email = user.email;
this.greenpoints = user.rankpoints;
this.joinedAt = user.joinedAt;
this.id = user.id;
this.user = user;
}
/**
* Returns the number of posts the user created
*/
public async numberOfPosts(): Promise<number> {
return this.user.countPosts();
}
/**
* Returns all friends of the user.
*/
public async friends(): Promise<wrappers.User[]> {
const result = await this.user.getFriends();
const userFriends = [];
for (const friend of result) {
userFriends.push(new wrappers.User(friend));
}
return userFriends;
}
/**
* Returns all posts for a user.
*/
public async posts({first, offset}: { first: number, offset: number }): Promise<wrappers.Post[]> {
const postRes = await this.user.getPosts();
const posts = [];
for (const post of postRes) {
posts.push(new wrappers.Post(post));
}
return posts;
}
/**
* Returns all chatrooms (with pagination).
@ -12,19 +63,14 @@ export class Profile extends User {
* @param first
* @param offset
*/
public async chats({first, offset}: {first: number, offset?: number}): Promise<Chatroom[]> {
if (!(await this.exists())) {
return [];
}
public async chats({first, offset}: { first: number, offset?: number }): Promise<Chatroom[]> {
first = first || 10;
offset = offset || 0;
const result = await queryHelper.all({
text: "SELECT chat FROM chat_members WHERE member = $1 LIMIT $2 OFFSET $3",
values: [this.id, first, offset],
});
const result = await this.user.getChats();
if (result) {
return result.map((row) => new Chatroom(row.chat));
return result.map((chat) => new Chatroom(chat));
} else {
return [];
}
@ -34,24 +80,14 @@ export class Profile extends User {
* Returns all open requests the user has send.
*/
public async sentRequests() {
const result = await queryHelper.all({
cache: true,
text: "SELECT * FROM requests WHERE sender = $1",
values: [this.id],
});
return this.getRequests(result);
return this.user.getSentRequests();
}
/**
* Returns all received requests of the user.
*/
public async receivedRequests() {
const result = await queryHelper.all({
cache: true,
text: "SELECT * FROM requests WHERE receiver = $1",
values: [this.id],
});
return this.getRequests(result);
return this.user.getReceivedRequests();
}
/**
@ -59,11 +95,9 @@ export class Profile extends User {
* @param points
*/
public async setGreenpoints(points: number): Promise<number> {
const result = await queryHelper.first({
text: "UPDATE users SET greenpoints = $1 WHERE id = $2 RETURNING greenpoints",
values: [points, this.id],
});
return result.greenpoints;
this.user.rankpoints = points;
await this.user.save();
return this.user.rankpoints;
}
/**
@ -71,22 +105,18 @@ export class Profile extends User {
* @param email
*/
public async setEmail(email: string): Promise<string> {
const result = await queryHelper.first({
text: "UPDATE users SET email = $1 WHERE users.id = $2 RETURNING email",
values: [email, this.id],
});
return result.email;
this.user.email = email;
await this.user.save();
return this.user.email;
}
/**
* Updates the handle of the user
*/
public async setHandle(handle: string): Promise<string> {
const result = await queryHelper.first({
text: "UPDATE users SET handle = $1 WHERE id = $2",
values: [handle, this.id],
});
return result.handle;
this.user.handle = handle;
await this.user.save();
return this.user.handle;
}
/**
@ -94,11 +124,9 @@ export class Profile extends User {
* @param name
*/
public async setName(name: string): Promise<string> {
const result = await queryHelper.first({
text: "UPDATE users SET name = $1 WHERE id = $2",
values: [name, this.id],
});
return result.name;
this.user.username = name;
await this.user.save();
return this.user.username;
}
/**
@ -107,10 +135,10 @@ export class Profile extends User {
* @param type
*/
public async denyRequest(sender: number, type: dataaccess.RequestType) {
await queryHelper.first({
text: "DELETE FROM requests WHERE receiver = $1 AND sender = $2 AND type = $3",
values: [this.id, sender, type],
});
const request = await this.user.getReceivedRequests({where: {senderId: sender, requestType: type}});
if (request[0]) {
await request[0].destroy();
}
}
/**
@ -119,45 +147,15 @@ export class Profile extends User {
* @param type
*/
public async acceptRequest(sender: number, type: dataaccess.RequestType) {
const exists = await queryHelper.first({
cache: true,
text: "SELECT 1 FROM requests WHERE receiver = $1 AND sender = $2 AND type = $3",
values: [this.id, sender, type],
});
if (exists) {
if (type === dataaccess.RequestType.FRIENDREQUEST) {
await queryHelper.first({
text: "INSERT INTO user_friends (user_id, friend_id) VALUES ($1, $2)",
values: [this.id, sender],
});
const requests = await this.user.getReceivedRequests({where: {senderId: sender, requestType: type}});
if (requests.length > 0) {
const request = requests[0];
if (request.requestType === dataaccess.RequestType.FRIENDREQUEST) {
await this.user.addFriend(sender);
await request.destroy();
}
} else {
throw new RequestNotFoundError(sender, this.id, type);
}
}
/**
* Returns request wrapper for a row database request result.
* @param rows
*/
private getRequests(rows: any) {
const requests = [];
const requestUsers: any = {};
for (const row of rows) {
let sender = requestUsers[row.sender];
if (!sender) {
sender = new User(row.sender);
requestUsers[row.sender] = sender;
}
let receiver = requestUsers[row.receiver];
if (!receiver) {
receiver = new User(row.receiver);
requestUsers[row.receiver] = receiver;
}
requests.push(new Request(sender, receiver, row.type));
}
return requests;
}
}

@ -1,24 +0,0 @@
import dataaccess from "./index";
import {User} from "./User";
/**
* Represents a request to a user.
*/
export class Request {
constructor(
public readonly sender: User,
public readonly receiver: User,
public readonly type: dataaccess.RequestType) {
}
/**
* Returns the resolved request data.
*/
public resolvedData() {
return {
receiverId: this.receiver.id,
senderId: this.sender.id,
type: this.type,
};
}
}

@ -1,84 +1,39 @@
import globals from "../globals";
import {DataObject} from "./DataObject";
import {queryHelper} from "./index";
import {Post} from "./Post";
import {SqUser} from "./datamodels";
import * as wrappers from "./wrappers";
export class User extends DataObject {
private $name: string;
private $handle: string;
private $email: string;
private $greenpoints: number;
private $joinedAt: string;
private $exists: boolean;
export class User {
public id: number;
public name: string;
public handle: string;
public greenpoints: number;
public joinedAt: Date;
/**
* The name of the user
*/
public async name(): Promise<string> {
await this.loadDataIfNotExists();
return this.$name;
}
/**
* The unique handle of the user.
*/
public async handle(): Promise<string> {
await this.loadDataIfNotExists();
return this.$handle;
}
/**
* The email of the user
*/
public async email(): Promise<string> {
await this.loadDataIfNotExists();
return this.$email;
}
protected user: SqUser;
/**
* The number of greenpoints of the user
*/
public async greenpoints(): Promise<number> {
await this.loadDataIfNotExists();
return this.$greenpoints;
constructor(user: SqUser) {
this.id = user.id;
this.name = user.username;
this.handle = user.handle;
this.greenpoints = user.rankpoints;
this.joinedAt = user.joinedAt;
this.user = user;
}
/**
* Returns the number of posts the user created
*/
public async numberOfPosts(): Promise<number> {
const result = await queryHelper.first({
cache: true,
text: "SELECT COUNT(*) count FROM posts WHERE author = $1",
values: [this.id],
});
return result.count;
}
/**
* The date the user joined the platform
*/
public async joinedAt(): Promise<Date> {
await this.loadDataIfNotExists();
return new Date(this.$joinedAt);
return this.user.countPosts();
}
/**
* Returns all friends of the user.
*/
public async friends(): Promise<User[]> {
const result = await queryHelper.all({
cache: true,
text: "SELECT * FROM user_friends WHERE user_id = $1 OR friend_id = $1",
values: [this.id],
});
const result = await this.user.getFriends();
const userFriends = [];
for (const row of result) {
if (row.user_id === this.id) {
userFriends.push(new User(row.friend_id));
} else {
userFriends.push(new User(row.user_id));
}
for (const friend of result) {
userFriends.push(new User(friend));
}
return userFriends;
}
@ -86,43 +41,13 @@ export class User extends DataObject {
/**
* Returns all posts for a user.
*/
public async posts({first, offset}: {first: number, offset: number}): Promise<Post[]> {
first = first || 10;
offset = offset || 0;
const result = await queryHelper.all({
cache: true,
text: "SELECT * FROM posts WHERE author = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
values: [this.id, first, offset],
});
public async posts({first, offset}: { first: number, offset: number }): Promise<wrappers.Post[]> {
const postRes = await this.user.getPosts();
const posts = [];
for (const row of result) {
posts.push(new Post(row.id, row));
for (const post of postRes) {
posts.push(new wrappers.Post(post));
}
return posts;
}
/**
* Fetches the data for the user.
*/
protected async loadData(): Promise<void> {
let result: any;
if (this.row) {
result = this.row;
} else {
result = await queryHelper.first({
cache: true,
text: "SELECT * FROM users WHERE users.id = $1",
values: [this.id],
});
}
if (result) {
this.$name = result.name;
this.$handle = result.handle;
this.$email = result.email;
this.$greenpoints = result.greenpoints;
this.$joinedAt = result.joined_at;
this.dataLoaded = true;
}
}
}

@ -0,0 +1,12 @@
export {
init as datainit,
User as SqUser,
Post as SqPost,
Chat as SqChat,
Request as SqRequest,
PostVotes as SqPostVotes,
ChatMessage as SqChatMessage,
ChatMembers as SqChatMembers,
RequestType as SqRequestType,
UserFriends as SqUserFriends,
} from "./models";

@ -0,0 +1,279 @@
// tslint:disable:object-literal-sort-keys
import * as sqz from "sequelize";
import {
Association,
BelongsToGetAssociationMixin,
BelongsToManyAddAssociationMixin,
BelongsToManyCountAssociationsMixin,
BelongsToManyCreateAssociationMixin,
BelongsToManyGetAssociationsMixin,
BelongsToManyHasAssociationMixin,
DataTypes,
HasManyAddAssociationMixin,
HasManyCountAssociationsMixin,
HasManyCreateAssociationMixin,
HasManyGetAssociationsMixin,
HasManyHasAssociationMixin,
HasOneGetAssociationMixin,
Model,
Sequelize,
} from "sequelize";
import * as wrappers from "../wrappers";
const underscored = true;
enum VoteType {
UPVOTE = "UPVOTE",
DOWNVOTE = "DOWNVOTE",
}
export enum RequestType {
FRIENDREQUEST = "FRIENDREQUEST",
GROUPINVITE = "GROUPINVITE",
EVENTINVITE = "EVENTINVITE",
}
export class User extends Model {
public static associations: {
friends: Association<User, User>;
posts: Association<User, Post>;
votes: Association<User, PostVotes>;
requests: Association<User, Request>;
};
public id!: number;
public username!: string;
public handle!: string;
public email!: string;
public password!: string;
public rankpoints!: number;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public getFriends!: HasManyGetAssociationsMixin<User>;
public addFriend!: HasManyAddAssociationMixin<User, number>;
public hasFriend!: HasManyHasAssociationMixin<User, number>;
public countFriends!: HasManyCountAssociationsMixin;
public getPosts!: HasManyGetAssociationsMixin<Post>;
public addPost!: HasManyAddAssociationMixin<Post, number>;
public hasPost!: HasManyHasAssociationMixin<Post, number>;
public countPosts!: HasManyCountAssociationsMixin;
public createPost!: HasManyCreateAssociationMixin<Post>;
public getReceivedRequests!: HasManyGetAssociationsMixin<Request>;
public addReceivedRequest!: HasManyAddAssociationMixin<Request, number>;
public hasReceivedRequest!: HasManyHasAssociationMixin<Request, number>;
public countReceivedRequests!: HasManyCountAssociationsMixin;
public createReceivedRequest!: HasManyCreateAssociationMixin<Request>;
public getSentRequests!: HasManyGetAssociationsMixin<Request>;
public addSentRequest!: HasManyAddAssociationMixin<Request, number>;
public hasSentRequest!: HasManyHasAssociationMixin<Request, number>;
public countSentRequests!: HasManyCountAssociationsMixin;
public createSentRequest!: HasManyCreateAssociationMixin<Request>;
public getChats!: BelongsToManyGetAssociationsMixin<Chat>;
public addChat!: BelongsToManyAddAssociationMixin<Chat, number>;
public hasChat!: BelongsToManyHasAssociationMixin<Chat, number>;
public countChats!: BelongsToManyCountAssociationsMixin;
public createChat!: BelongsToManyCreateAssociationMixin<Chat>;
/**
* Getter for joined at as the date the entry was created.
*/
public get joinedAt(): Date {
// @ts-ignore
return this.getDataValue("createdAt");
}
/**
* Wraps itself into a user
*/
public get user(): wrappers.User {
return new wrappers.User(this);
}
/**
* returns the username.
*/
public get name(): string {
return this.getDataValue("username");
}
/**
* Wraps itself into a profile.
*/
public get profile(): wrappers.Profile {
return new wrappers.Profile(this);
}
}
export class UserFriends extends Model {
}
export class Post extends Model {
public static associations: {
author: Association<Post, User>,
votes: Association<Post, PostVotes>,
};
public id!: number;
public content!: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public getUser!: BelongsToGetAssociationMixin<User>;
public getVotes!: HasManyGetAssociationsMixin<PostVotes>;
public addVote!: HasManyAddAssociationMixin<PostVotes, number>;
public hasVote!: HasManyHasAssociationMixin<PostVotes, number>;
public countVotes!: HasManyCountAssociationsMixin;
public createVote!: HasManyCreateAssociationMixin<PostVotes>;
/**
* Wraps itself into a Post instance.
*/
public get post(): wrappers.Post {
return new wrappers.Post(this);
}
}
export class PostVotes extends Model {
public voteType: VoteType;
}
export class Request extends Model {
public id!: number;
public requestType!: RequestType;
public getSender!: HasOneGetAssociationMixin<User>;
public getReceiver!: HasOneGetAssociationMixin<User>;
}
export class Chat extends Model {
public static associations: {
members: Association<Chat, User>,
messages: Association<Chat, ChatMessage>,
};
public id!: number;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public getMembers!: BelongsToManyGetAssociationsMixin<User>;
public addMember!: BelongsToManyAddAssociationMixin<User, number>;
public hasMember!: BelongsToManyHasAssociationMixin<User, number>;
public countMembers!: BelongsToManyCountAssociationsMixin;
public getMessages!: HasManyGetAssociationsMixin<ChatMessage>;
public addMessage!: HasManyAddAssociationMixin<ChatMessage, number>;
public hasMessage!: HasManyHasAssociationMixin<ChatMessage, number>;
public countMessages!: HasManyCountAssociationsMixin;
public createMessage!: HasManyCreateAssociationMixin<ChatMessage>;
/**
* wraps itself into a chatroom.
*/
public get chatroom(): wrappers.Chatroom {
return new wrappers.Chatroom(this);
}
}
export class ChatMembers extends Model {
}
export class ChatMessage extends Model {
public id: number;
public content!: string;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
public getAuthor!: BelongsToGetAssociationMixin<User>;
public getChat!: BelongsToGetAssociationMixin<Chat>;
public get message(): wrappers.ChatMessage {
return new wrappers.ChatMessage(this);
}
}
export function init(sequelize: Sequelize) {
User.init({
username: {
allowNull: false,
type: sqz.STRING(128),
},
handle: {
allowNull: false,
type: sqz.STRING(128),
unique: true,
},
email: {
allowNull: false,
type: sqz.STRING(128),
unique: true,
},
password: {
allowNull: false,
type: sqz.STRING(128),
},
rankpoints: {
allowNull: false,
type: DataTypes.INTEGER,
defaultValue: 0,
},
}, {sequelize, underscored});
UserFriends.init({}, {sequelize, underscored});
Post.init({
content: DataTypes.TEXT,
}, {sequelize, underscored});
PostVotes.init({
voteType: {
type: DataTypes.ENUM,
values: ["UPVOTE", "DOWNVOTE"],
},
}, {sequelize, underscored});
Request.init({
requestType: {
type: DataTypes.ENUM,
values: ["FRIENDREQUEST", "GROUPINVITE", "EVENTINVITE"],
},
}, {sequelize, underscored});
Chat.init({}, {sequelize, underscored});
ChatMembers.init({}, {sequelize, underscored});
ChatMessage.init({
content: {
type: DataTypes.TEXT,
allowNull: false,
},
}, {sequelize, underscored});
User.belongsToMany(User, {through: UserFriends, as: "friends"});
Post.belongsTo(User, {foreignKey: "userId"});
User.hasMany(Post, {as: "posts", foreignKey: "userId"});
Post.belongsToMany(User, {through: PostVotes, as: "votes"});
User.belongsToMany(Post, {through: PostVotes, as: "votes"});
User.hasMany(Request, {as: "sentRequests"});
User.hasMany(Request, {as: "receivedRequests"});
User.belongsToMany(Chat, {through: ChatMembers});
Chat.belongsToMany(User, {through: ChatMembers, as: "members"});
Chat.hasMany(ChatMessage, {as: "messages"});
ChatMessage.belongsTo(Chat);
ChatMessage.belongsTo(User, {as: "author", foreignKey: "userId"});
User.hasMany(ChatMessage, {foreignKey: "userId"});
}

@ -1,30 +1,19 @@
import {Pool} from "pg";
import {Sequelize} from "sequelize";
import {ChatNotFoundError} from "../errors/ChatNotFoundError";
import {EmailAlreadyRegisteredError} from "../errors/EmailAlreadyRegisteredError";
import {UserNotFoundError} from "../errors/UserNotFoundError";
import globals from "../globals";
import {InternalEvents} from "../InternalEvents";
import {QueryHelper} from "../QueryHelper";
import {ChatMessage} from "./ChatMessage";
import {Chatroom} from "./Chatroom";
import * as models from "./datamodels";
import {Post} from "./Post";
import {Profile} from "./Profile";
import {Request} from "./Request";
import {User} from "./User";
const config = globals.config;
const tableCreationFile = __dirname + "/../../sql/create-tables.sql";
const tableUpdateFile = __dirname + "/../../sql/update-tables.sql";
const dbClient: Pool = new Pool({
database: config.database.database,
host: config.database.host,
password: config.database.password,
port: config.database.port,
user: config.database.user,
});
export const queryHelper = new QueryHelper(dbClient, tableCreationFile, tableUpdateFile);
/**
* Generates a new handle from the username and a base64 string of the current time.
* @param username
@ -38,14 +27,15 @@ function generateHandle(username: string) {
*/
namespace dataaccess {
export const pool: Pool = dbClient;
let sequelize: Sequelize;
/**
* Initializes everything that needs to be initialized asynchronous.
*/
export async function init() {
export async function init(seq: Sequelize) {
sequelize = seq;
try {
await queryHelper.init();
await models.datainit(sequelize);
} catch (err) {
globals.logger.error(err.message);
globals.logger.debug(err.stack);
@ -57,12 +47,9 @@ namespace dataaccess {
* @param userHandle
*/
export async function getUserByHandle(userHandle: string): Promise<User> {
const result = await queryHelper.first({
text: "SELECT * FROM users WHERE users.handle = $1",
values: [userHandle],
});
if (result) {
return new User(result.id, result);
const user = await models.SqUser.findOne({where: {handle: userHandle}});
if (user) {
return new User(user);
} else {
throw new UserNotFoundError(userHandle);
}
@ -74,12 +61,9 @@ namespace dataaccess {
* @param password
*/
export async function getUserByLogin(email: string, password: string): Promise<Profile> {
const result = await queryHelper.first({
text: "SELECT * FROM users WHERE email = $1 AND password = $2",
values: [email, password],
});
if (result) {
return new Profile(result.id, result);
const user = await models.SqUser.findOne({where: {email, password}});
if (user) {
return new Profile(user);
} else {
throw new UserNotFoundError(email);
}
@ -92,16 +76,11 @@ namespace dataaccess {
* @param password
*/
export async function registerUser(username: string, email: string, password: string) {
const existResult = await queryHelper.first({
text: "SELECT email FROM users WHERE email = $1;",
values: [email],
});
if (!existResult || !existResult.email) {
const result = await queryHelper.first({
text: "INSERT INTO users (name, handle, password, email) VALUES ($1, $2, $3, $4) RETURNING *",
values: [username, generateHandle(username), password, email],
});
return new Profile(result.id, result);
const existResult = !!(await models.SqUser.findOne({where: {username, email, password}}));
const handle = generateHandle(username);
if (!existResult) {
const user = await models.SqUser.create({username, email, password, handle});
return new Profile(user);
} else {
throw new EmailAlreadyRegisteredError(email);
}
@ -112,12 +91,9 @@ namespace dataaccess {
* @param postId
*/
export async function getPost(postId: number): Promise<Post> {
const result = await queryHelper.first({
text: "SELECT * FROM posts WHERE id = $1",
values: [postId],
});
if (result) {
return new Post(result.id, result);
const post = await models.SqPost.findByPk(postId);
if (post) {
return new Post(post);
} else {
return null;
}
@ -131,33 +107,18 @@ namespace dataaccess {
*/
export async function getPosts(first: number, offset: number, sort: SortType) {
if (sort === SortType.NEW) {
const results = await queryHelper.all({
cache: true,
text: "SELECT * FROM posts ORDER BY created_at DESC LIMIT $1 OFFSET $2",
values: [first, offset],
});
const posts = [];
for (const row of results) {
posts.push(new Post(row.id, row));
}
return posts;
const posts = await models.SqPost.findAll({order: [["createdAt", "DESC"]], limit: first, offset});
return posts.map((p) => new Post(p));
} else {
const results = await queryHelper.all({
cache: true,
text: `
SELECT * FROM (
const results: models.SqPost[] = await sequelize.query(
`SELECT id FROM (
SELECT *,
(SELECT count(*) FROM votes WHERE vote_type = 'UPVOTE' AND item_id = posts.id) AS upvotes ,
(SELECT count(*) FROM votes WHERE vote_type = 'DOWNVOTE' AND item_id = posts.id) AS downvotes
FROM posts) AS a ORDER BY (a.upvotes - a.downvotes) DESC LIMIT $1 OFFSET $2;
`,
values: [first, offset],
});
const posts = [];
for (const row of results) {
posts.push(new Post(row.id, row));
}
return posts;
FROM posts) AS a ORDER BY (a.upvotes - a.downvotes) DESC LIMIT ? OFFSET ?`,
{replacements: [first, offset], mapToModel: true, model: models.SqPost});
return results.map((p) => new Post(p));
}
}
@ -169,11 +130,8 @@ namespace dataaccess {
*/
export async function createPost(content: string, authorId: number, type?: string): Promise<Post> {
type = type || "MISC";
const result = await queryHelper.first({
text: "INSERT INTO posts (content, author, type) VALUES ($1, $2, $3) RETURNING *",
values: [content, authorId, type],
});
const post = new Post(result.id, result);
const sqPost = await models.SqPost.create({content, userId: authorId});
const post = new Post(sqPost);
globals.internalEmitter.emit(InternalEvents.POSTCREATE, post);
return post;
}
@ -183,10 +141,7 @@ namespace dataaccess {
* @param postId
*/
export async function deletePost(postId: number): Promise<boolean> {
const result = await queryHelper.first({
text: "DELETE FROM posts WHERE posts.id = $1",
values: [postId],
});
await (await models.SqPost.findByPk(postId)).destroy();
return true;
}
@ -195,31 +150,15 @@ namespace dataaccess {
* @param members
*/
export async function createChat(...members: number[]): Promise<Chatroom> {
const idResult = await queryHelper.first({
text: "INSERT INTO chats (id) values (default) RETURNING *;",
});
const id = idResult.id;
const transaction = await queryHelper.createTransaction();
try {
await transaction.begin();
return sequelize.transaction(async (t) => {
const chat = await models.SqChat.create({}, {transaction: t});
for (const member of members) {
await transaction.query({
name: "chat-member-insert",
text: "INSERT INTO chat_members (chat, member) VALUES ($1, $2);",
values: [id, member],
});
}
await transaction.commit();
} catch (err) {
globals.logger.warn(`Failed to insert chatmember into database: ${err.message}`);
globals.logger.debug(err.stack);
await transaction.rollback();
} finally {
transaction.release();
await chat.addMember(Number(member), {transaction: t});
}
const chat = new Chatroom(id);
globals.internalEmitter.emit(InternalEvents.CHATCREATE, chat);
return chat;
const chatroom = new Chatroom(chat);
globals.internalEmitter.emit(InternalEvents.CHATCREATE, chatroom);
return chatroom;
});
}
/**
@ -229,15 +168,11 @@ namespace dataaccess {
* @param content
*/
export async function sendChatMessage(authorId: number, chatId: number, content: string) {
const chat = new Chatroom(chatId);
if ((await chat.exists())) {
const result = await queryHelper.first({
text: "INSERT INTO chat_messages (chat, author, content) values ($1, $2, $3) RETURNING *",
values: [chatId, authorId, content],
});
const message = new ChatMessage(new User(result.author), chat, result.created_at, result.content);
globals.internalEmitter.emit(InternalEvents.CHATMESSAGE, message);
return message;
const chat = await models.SqChat.findByPk(chatId);
if (chat) {
const message = await chat.createMessage({content, userId: authorId});
globals.internalEmitter.emit(InternalEvents.CHATMESSAGE, message.message);
return message.message;
} else {
throw new ChatNotFoundError(chatId);
}
@ -247,30 +182,20 @@ namespace dataaccess {
* Returns all chats.
*/
export async function getAllChats(): Promise<Chatroom[]> {
const result = await queryHelper.all({
text: "SELECT id FROM chats;",
});
const chats = [];
for (const row of result) {
chats.push(new Chatroom(row.id));
}
return chats;
const chats = await models.SqChat.findAll();
return chats.map((c) => new Chatroom(c));
}
/**
* Sends a request to a user.
* @param sender
* @param receiver
* @param type
* @param requestType
*/
export async function createRequest(sender: number, receiver: number, type?: RequestType) {
type = type || RequestType.FRIENDREQUEST;
export async function createRequest(sender: number, receiver: number, requestType?: RequestType) {
requestType = requestType || RequestType.FRIENDREQUEST;
const result = await queryHelper.first({
text: "INSERT INTO requests (sender, receiver, type) VALUES ($1, $2, $3) RETURNING *",
values: [sender, receiver, type],
});
const request = new Request(new User(result.sender), new User(result.receiver), result.type);
const request = await models.SqRequest.create({senderId: sender, receiverId: receiver, requestType});
globals.internalEmitter.emit(InternalEvents.REQUESTCREATE, Request);
return request;
}

@ -0,0 +1,5 @@
export {User} from "./User";
export {Chatroom} from "./Chatroom";
export {Post} from "./Post";
export {Profile} from "./Profile";
export {ChatMessage} from "./ChatMessage";

@ -1,5 +1,4 @@
import {GraphQLError} from "graphql";
import {BaseError} from "./BaseError";
export class NotLoggedInGqlError extends GraphQLError {

@ -35,7 +35,7 @@ namespace globals {
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf(({ level, message, timestamp }) => {
winston.format.printf(({level, message, timestamp}) => {
return `${timestamp} ${level}: ${message}`;
}),
),

@ -3,8 +3,8 @@ import {Namespace, Server} from "socket.io";
import dataaccess from "../lib/dataaccess";
import {ChatMessage} from "../lib/dataaccess/ChatMessage";
import {Chatroom} from "../lib/dataaccess/Chatroom";
import {Request} from "../lib/dataaccess/datamodels/models";
import {Post} from "../lib/dataaccess/Post";
import {Request} from "../lib/dataaccess/Request";
import globals from "../lib/globals";
import {InternalEvents} from "../lib/InternalEvents";
import Route from "../lib/Route";
@ -37,18 +37,18 @@ class HomeRoute extends Route {
socket.on("postCreate", async (content) => {
if (socket.handshake.session.userId) {
const post = await dataaccess.createPost(content, socket.handshake.session.userId);
io.emit("post", await post.resolvedData());
io.emit("post", Object.assign(post, {htmlContent: post.htmlContent()}));
} else {
socket.emit("error", "Not logged in!");
}
});
globals.internalEmitter.on(InternalEvents.REQUESTCREATE, (request: Request) => {
if (request.receiver.id === socket.handshake.session.userId) {
socket.emit("request", request.resolvedData());
globals.internalEmitter.on(InternalEvents.REQUESTCREATE, async (request: Request) => {
if ((await request.getSender()).id === socket.handshake.session.userId) {
socket.emit("request", request);
}
});
globals.internalEmitter.on(InternalEvents.GQLPOSTCREATE, async (post: Post) => {
socket.emit("post", await post.resolvedData());
socket.emit("post", Object.assign(post, {htmlContent: post.htmlContent()}));
});
});
@ -82,15 +82,15 @@ class HomeRoute extends Route {
if (socket.handshake.session.userId) {
const userId = socket.handshake.session.userId;
const message = await dataaccess.sendChatMessage(userId, chatId, content);
socket.broadcast.emit("chatMessage", message.resolvedContent());
socket.emit("chatMessageSent", message.resolvedContent());
socket.broadcast.emit("chatMessage", Object.assign(message, {htmlContent: message.htmlContent()}));
socket.emit("chatMessageSent", Object.assign(message, {htmlContent: message.htmlContent()}));
} else {
socket.emit("error", "Not logged in!");
}
});
globals.internalEmitter.on(InternalEvents.GQLCHATMESSAGE, (message: ChatMessage) => {
if (message.chat.id === chatId) {
socket.emit("chatMessage", message.resolvedContent());
globals.internalEmitter.on(InternalEvents.GQLCHATMESSAGE, async (message: ChatMessage) => {
if ((await message.chat()).id === chatId) {
socket.emit("chatMessage", Object.assign(message, {htmlContent: message.htmlContent()}));
}
});
});

@ -4,6 +4,7 @@
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"allowSyntheticDefaultImports": true,
"outDir": "./dist",
"sourceMap": true,
"target": "es2018",

@ -21,7 +21,8 @@
},
"no-namespace": false,
"no-internal-module": false,
"max-classes-per-file": false
"max-classes-per-file": false,
"no-var-requires": false
},
"jsRules": {
"max-line-length": {

Loading…
Cancel
Save