From 7785a118f2ce4f9e6726c3c7f57af641f2cb24c1 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Tue, 29 Oct 2019 18:41:20 +0100 Subject: [PATCH] Code cleanup --- src/graphql/resolvers.ts | 27 +++++++++++++-------------- src/lib/InternalEvents.ts | 3 +++ src/lib/Route.ts | 7 ------- src/lib/dataaccess.ts | 2 -- src/lib/globals.ts | 8 +------- src/lib/models/Request.ts | 8 ++++++-- src/lib/models/User.ts | 11 +++++------ 7 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts index fcd2eb2..ecdc504 100644 --- a/src/graphql/resolvers.ts +++ b/src/graphql/resolvers.ts @@ -1,4 +1,3 @@ -import {AggregateError} from "bluebird"; import {GraphQLError} from "graphql"; import * as status from "http-status"; import dataaccess from "../lib/dataaccess"; @@ -49,7 +48,7 @@ export function resolver(req: any, res: any): any { return new GraphQLError("No chatId given."); } }, - async getGroup({groupId}: {groupId: number}) { + async getGroup({groupId}: { groupId: number }) { if (groupId) { return models.Group.findByPk(groupId); } else { @@ -57,7 +56,7 @@ export function resolver(req: any, res: any): any { return new GraphQLError("No group id given."); } }, - async getRequest({requestId}: {requestId: number}) { + async getRequest({requestId}: { requestId: number }) { if (requestId) { return models.Request.findByPk(requestId); } else { @@ -253,7 +252,7 @@ export function resolver(req: any, res: any): any { return new GraphQLError("No sender or type given."); } }, - async removeFriend({friendId}: {friendId: number}) { + async removeFriend({friendId}: { friendId: number }) { if (req.session.userId) { const self = await models.User.findByPk(req.session.userId); return await self.removeFriend(friendId); @@ -262,17 +261,17 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async getPosts({first, offset, sort}: {first: number, offset: number, sort: dataaccess.SortType}) { + async getPosts({first, offset, sort}: { first: number, offset: number, sort: dataaccess.SortType }) { return await dataaccess.getPosts(first, offset, sort); }, - async createGroup({name, members}: {name: string, members: number[]}) { + async createGroup({name, members}: { name: string, members: number[] }) { if (req.session.userId) { return await dataaccess.createGroup(name, req.session.userId, members); } else { return new NotLoggedInGqlError(); } }, - async joinGroup({id}: {id: number}) { + async joinGroup({id}: { id: number }) { if (req.session.userId) { try { return await dataaccess @@ -286,7 +285,7 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async leaveGroup({id}: {id: number}) { + async leaveGroup({id}: { id: number }) { if (req.session.userId) { try { return await dataaccess @@ -300,7 +299,7 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async addGroupAdmin({groupId, userId}: {groupId: number, userId: number}) { + async addGroupAdmin({groupId, userId}: { groupId: number, userId: number }) { if (req.session.userId) { const group = await models.Group.findByPk(groupId); const self = await models.User.findByPk(req.session.userId); @@ -321,11 +320,11 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async removeGroupAdmin({groupId, userId}: {groupId: number, userId: number}) { + async removeGroupAdmin({groupId, userId}: { groupId: number, userId: number }) { if (req.session.userId) { const group = await models.Group.findByPk(groupId); const isCreator = Number(group.creatorId) === Number(req.session.userId); - const userIsCreator = Number(group.creatorId) === Number(userId) ; + const userIsCreator = Number(group.creatorId) === Number(userId); if (group && !isCreator && Number(userId) !== Number(req.session.userId)) { res.status(status.FORBIDDEN); return new GraphQLError("You are not the group creator!"); @@ -345,7 +344,7 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async createEvent({name, dueDate, groupId}: {name: string, dueDate: string, groupId: number}) { + async createEvent({name, dueDate, groupId}: { name: string, dueDate: string, groupId: number }) { if (req.session.userId) { const date = new Date(dueDate); const group = await models.Group.findByPk(groupId); @@ -355,7 +354,7 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async joinEvent({eventId}: {eventId: number}) { + async joinEvent({eventId}: { eventId: number }) { if (req.session.userId) { const event = await models.Event.findByPk(eventId); const self = await models.User.findByPk(req.session.userId); @@ -366,7 +365,7 @@ export function resolver(req: any, res: any): any { return new NotLoggedInGqlError(); } }, - async leaveEvent({eventId}: {eventId: number}) { + async leaveEvent({eventId}: { eventId: number }) { if (req.session.userId) { const event = await models.Event.findByPk(eventId); const self = await models.User.findByPk(req.session.userId); diff --git a/src/lib/InternalEvents.ts b/src/lib/InternalEvents.ts index d26947d..feb56c3 100644 --- a/src/lib/InternalEvents.ts +++ b/src/lib/InternalEvents.ts @@ -1,3 +1,6 @@ +/** + * Events that are emitted and processsed internally on the server + */ export enum InternalEvents { CHATCREATE = "chatCreate", CHATMESSAGE = "chatMessage", diff --git a/src/lib/Route.ts b/src/lib/Route.ts index 6c3426e..b0ad0ef 100644 --- a/src/lib/Route.ts +++ b/src/lib/Route.ts @@ -1,10 +1,3 @@ -/** - * @author Trivernis - * @remarks - * - * Taken from {@link https://github.com/Trivernis/whooshy} - */ - import {Router} from "express"; import {Namespace, Server} from "socket.io"; diff --git a/src/lib/dataaccess.ts b/src/lib/dataaccess.ts index b11897a..7dc3f0f 100644 --- a/src/lib/dataaccess.ts +++ b/src/lib/dataaccess.ts @@ -1,10 +1,8 @@ import * as crypto from "crypto"; -import * as status from "http-status"; import * as sqz from "sequelize"; import {Sequelize} from "sequelize-typescript"; import {ChatNotFoundError} from "./errors/ChatNotFoundError"; import {EmailAlreadyRegisteredError} from "./errors/EmailAlreadyRegisteredError"; -import {GroupNotFoundGqlError, NotLoggedInGqlError} from "./errors/graphqlErrors"; import {GroupNotFoundError} from "./errors/GroupNotFoundError"; import {InvalidLoginError} from "./errors/InvalidLoginError"; import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError"; diff --git a/src/lib/globals.ts b/src/lib/globals.ts index 9b13f38..2f682b5 100644 --- a/src/lib/globals.ts +++ b/src/lib/globals.ts @@ -1,14 +1,8 @@ -/** - * @author Trivernis - * @remarks - * - * Partly taken from {@link https://github.com/Trivernis/whooshy} - */ - import {EventEmitter} from "events"; import * as fsx from "fs-extra"; import * as yaml from "js-yaml"; import * as winston from "winston"; + require("winston-daily-rotate-file"); const configPath = "config.yaml"; diff --git a/src/lib/models/Request.ts b/src/lib/models/Request.ts index ef8e434..97643ad 100644 --- a/src/lib/models/Request.ts +++ b/src/lib/models/Request.ts @@ -11,8 +11,12 @@ export enum RequestType { @Table({underscored: true}) export class Request extends Model { @NotNull - @Column({type: sqz.ENUM, values: ["FRIENDREQUEST", "GROUPINVITE", "EVENTINVITE"], - defaultValue: "FRIENDREQUEST", allowNull: false}) + @Column({ + allowNull: false, + defaultValue: "FRIENDREQUEST", + type: sqz.ENUM, + values: ["FRIENDREQUEST", "GROUPINVITE", "EVENTINVITE"], + }) public requestType: RequestType; @ForeignKey(() => User) diff --git a/src/lib/models/User.ts b/src/lib/models/User.ts index d194379..7667d38 100644 --- a/src/lib/models/User.ts +++ b/src/lib/models/User.ts @@ -1,6 +1,5 @@ import * as sqz from "sequelize"; import { - BelongsTo, BelongsToMany, Column, CreatedAt, @@ -57,7 +56,7 @@ export class User extends Model { public rFriendOf: User[]; @BelongsToMany(() => Post, () => PostVote) - public votes: Array; + public votes: Array; @BelongsToMany(() => ChatRoom, () => ChatMember) public rChats: ChatRoom[]; @@ -125,7 +124,7 @@ export class User extends Model { * @param first * @param offset */ - public async friends({first, offset}: {first: number, offset: number}): Promise { + public async friends({first, offset}: { first: number, offset: number }): Promise { const limit = first || 10; offset = offset || 0; return await this.$get("rFriendOf", {limit, offset}) as User[]; @@ -143,7 +142,7 @@ export class User extends Model { * @param first * @param offset */ - public async chats({first, offset}: {first: number, offset: number}): Promise { + public async chats({first, offset}: { first: number, offset: number }): Promise { const limit = first || 10; offset = offset || 0; return await this.$get("rChats", {limit, offset}) as ChatRoom[]; @@ -170,7 +169,7 @@ export class User extends Model { return await this.$get("rReceivedRequests") as Request[]; } - public async posts({first, offset}: {first: number, offset: number}): Promise { + public async posts({first, offset}: { first: number, offset: number }): Promise { const limit = first || 10; offset = offset || 0; return await this.$get("rPosts", {limit, offset}) as Post[]; @@ -210,7 +209,7 @@ export class User extends Model { * @param first * @param offset */ - public async groups({first, offset}: {first: number, offset: number}): Promise { + public async groups({first, offset}: { first: number, offset: number }): Promise { const limit = first || 10; offset = offset || 0; return await this.$get("rGroups", {limit, offset}) as Group[];