Code cleanup

pull/2/head
Trivernis 5 years ago
parent 580f15da3d
commit 7785a118f2

@ -1,4 +1,3 @@
import {AggregateError} from "bluebird";
import {GraphQLError} from "graphql"; import {GraphQLError} from "graphql";
import * as status from "http-status"; import * as status from "http-status";
import dataaccess from "../lib/dataaccess"; import dataaccess from "../lib/dataaccess";
@ -49,7 +48,7 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No chatId given."); return new GraphQLError("No chatId given.");
} }
}, },
async getGroup({groupId}: {groupId: number}) { async getGroup({groupId}: { groupId: number }) {
if (groupId) { if (groupId) {
return models.Group.findByPk(groupId); return models.Group.findByPk(groupId);
} else { } else {
@ -57,7 +56,7 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No group id given."); return new GraphQLError("No group id given.");
} }
}, },
async getRequest({requestId}: {requestId: number}) { async getRequest({requestId}: { requestId: number }) {
if (requestId) { if (requestId) {
return models.Request.findByPk(requestId); return models.Request.findByPk(requestId);
} else { } else {
@ -253,7 +252,7 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No sender or type given."); return new GraphQLError("No sender or type given.");
} }
}, },
async removeFriend({friendId}: {friendId: number}) { async removeFriend({friendId}: { friendId: number }) {
if (req.session.userId) { if (req.session.userId) {
const self = await models.User.findByPk(req.session.userId); const self = await models.User.findByPk(req.session.userId);
return await self.removeFriend(friendId); return await self.removeFriend(friendId);
@ -262,17 +261,17 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); 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); 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) { if (req.session.userId) {
return await dataaccess.createGroup(name, req.session.userId, members); return await dataaccess.createGroup(name, req.session.userId, members);
} else { } else {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async joinGroup({id}: {id: number}) { async joinGroup({id}: { id: number }) {
if (req.session.userId) { if (req.session.userId) {
try { try {
return await dataaccess return await dataaccess
@ -286,7 +285,7 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async leaveGroup({id}: {id: number}) { async leaveGroup({id}: { id: number }) {
if (req.session.userId) { if (req.session.userId) {
try { try {
return await dataaccess return await dataaccess
@ -300,7 +299,7 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async addGroupAdmin({groupId, userId}: {groupId: number, userId: number}) { async addGroupAdmin({groupId, userId}: { groupId: number, userId: number }) {
if (req.session.userId) { if (req.session.userId) {
const group = await models.Group.findByPk(groupId); const group = await models.Group.findByPk(groupId);
const self = await models.User.findByPk(req.session.userId); const self = await models.User.findByPk(req.session.userId);
@ -321,11 +320,11 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async removeGroupAdmin({groupId, userId}: {groupId: number, userId: number}) { async removeGroupAdmin({groupId, userId}: { groupId: number, userId: number }) {
if (req.session.userId) { if (req.session.userId) {
const group = await models.Group.findByPk(groupId); const group = await models.Group.findByPk(groupId);
const isCreator = Number(group.creatorId) === Number(req.session.userId); 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)) { if (group && !isCreator && Number(userId) !== Number(req.session.userId)) {
res.status(status.FORBIDDEN); res.status(status.FORBIDDEN);
return new GraphQLError("You are not the group creator!"); return new GraphQLError("You are not the group creator!");
@ -345,7 +344,7 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); 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) { if (req.session.userId) {
const date = new Date(dueDate); const date = new Date(dueDate);
const group = await models.Group.findByPk(groupId); const group = await models.Group.findByPk(groupId);
@ -355,7 +354,7 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async joinEvent({eventId}: {eventId: number}) { async joinEvent({eventId}: { eventId: number }) {
if (req.session.userId) { if (req.session.userId) {
const event = await models.Event.findByPk(eventId); const event = await models.Event.findByPk(eventId);
const self = await models.User.findByPk(req.session.userId); const self = await models.User.findByPk(req.session.userId);
@ -366,7 +365,7 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError(); return new NotLoggedInGqlError();
} }
}, },
async leaveEvent({eventId}: {eventId: number}) { async leaveEvent({eventId}: { eventId: number }) {
if (req.session.userId) { if (req.session.userId) {
const event = await models.Event.findByPk(eventId); const event = await models.Event.findByPk(eventId);
const self = await models.User.findByPk(req.session.userId); const self = await models.User.findByPk(req.session.userId);

@ -1,3 +1,6 @@
/**
* Events that are emitted and processsed internally on the server
*/
export enum InternalEvents { export enum InternalEvents {
CHATCREATE = "chatCreate", CHATCREATE = "chatCreate",
CHATMESSAGE = "chatMessage", CHATMESSAGE = "chatMessage",

@ -1,10 +1,3 @@
/**
* @author Trivernis
* @remarks
*
* Taken from {@link https://github.com/Trivernis/whooshy}
*/
import {Router} from "express"; import {Router} from "express";
import {Namespace, Server} from "socket.io"; import {Namespace, Server} from "socket.io";

@ -1,10 +1,8 @@
import * as crypto from "crypto"; import * as crypto from "crypto";
import * as status from "http-status";
import * as sqz from "sequelize"; import * as sqz from "sequelize";
import {Sequelize} from "sequelize-typescript"; import {Sequelize} from "sequelize-typescript";
import {ChatNotFoundError} from "./errors/ChatNotFoundError"; import {ChatNotFoundError} from "./errors/ChatNotFoundError";
import {EmailAlreadyRegisteredError} from "./errors/EmailAlreadyRegisteredError"; import {EmailAlreadyRegisteredError} from "./errors/EmailAlreadyRegisteredError";
import {GroupNotFoundGqlError, NotLoggedInGqlError} from "./errors/graphqlErrors";
import {GroupNotFoundError} from "./errors/GroupNotFoundError"; import {GroupNotFoundError} from "./errors/GroupNotFoundError";
import {InvalidLoginError} from "./errors/InvalidLoginError"; import {InvalidLoginError} from "./errors/InvalidLoginError";
import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError"; import {NoActionSpecifiedError} from "./errors/NoActionSpecifiedError";

@ -1,14 +1,8 @@
/**
* @author Trivernis
* @remarks
*
* Partly taken from {@link https://github.com/Trivernis/whooshy}
*/
import {EventEmitter} from "events"; import {EventEmitter} from "events";
import * as fsx from "fs-extra"; import * as fsx from "fs-extra";
import * as yaml from "js-yaml"; import * as yaml from "js-yaml";
import * as winston from "winston"; import * as winston from "winston";
require("winston-daily-rotate-file"); require("winston-daily-rotate-file");
const configPath = "config.yaml"; const configPath = "config.yaml";

@ -11,8 +11,12 @@ export enum RequestType {
@Table({underscored: true}) @Table({underscored: true})
export class Request extends Model<Request> { export class Request extends Model<Request> {
@NotNull @NotNull
@Column({type: sqz.ENUM, values: ["FRIENDREQUEST", "GROUPINVITE", "EVENTINVITE"], @Column({
defaultValue: "FRIENDREQUEST", allowNull: false}) allowNull: false,
defaultValue: "FRIENDREQUEST",
type: sqz.ENUM,
values: ["FRIENDREQUEST", "GROUPINVITE", "EVENTINVITE"],
})
public requestType: RequestType; public requestType: RequestType;
@ForeignKey(() => User) @ForeignKey(() => User)

@ -1,6 +1,5 @@
import * as sqz from "sequelize"; import * as sqz from "sequelize";
import { import {
BelongsTo,
BelongsToMany, BelongsToMany,
Column, Column,
CreatedAt, CreatedAt,
@ -57,7 +56,7 @@ export class User extends Model<User> {
public rFriendOf: User[]; public rFriendOf: User[];
@BelongsToMany(() => Post, () => PostVote) @BelongsToMany(() => Post, () => PostVote)
public votes: Array<Post & {PostVote: PostVote}>; public votes: Array<Post & { PostVote: PostVote }>;
@BelongsToMany(() => ChatRoom, () => ChatMember) @BelongsToMany(() => ChatRoom, () => ChatMember)
public rChats: ChatRoom[]; public rChats: ChatRoom[];
@ -125,7 +124,7 @@ export class User extends Model<User> {
* @param first * @param first
* @param offset * @param offset
*/ */
public async friends({first, offset}: {first: number, offset: number}): Promise<User[]> { public async friends({first, offset}: { first: number, offset: number }): Promise<User[]> {
const limit = first || 10; const limit = first || 10;
offset = offset || 0; offset = offset || 0;
return await this.$get("rFriendOf", {limit, offset}) as User[]; return await this.$get("rFriendOf", {limit, offset}) as User[];
@ -143,7 +142,7 @@ export class User extends Model<User> {
* @param first * @param first
* @param offset * @param offset
*/ */
public async chats({first, offset}: {first: number, offset: number}): Promise<ChatRoom[]> { public async chats({first, offset}: { first: number, offset: number }): Promise<ChatRoom[]> {
const limit = first || 10; const limit = first || 10;
offset = offset || 0; offset = offset || 0;
return await this.$get("rChats", {limit, offset}) as ChatRoom[]; return await this.$get("rChats", {limit, offset}) as ChatRoom[];
@ -170,7 +169,7 @@ export class User extends Model<User> {
return await this.$get("rReceivedRequests") as Request[]; return await this.$get("rReceivedRequests") as Request[];
} }
public async posts({first, offset}: {first: number, offset: number}): Promise<Post[]> { public async posts({first, offset}: { first: number, offset: number }): Promise<Post[]> {
const limit = first || 10; const limit = first || 10;
offset = offset || 0; offset = offset || 0;
return await this.$get("rPosts", {limit, offset}) as Post[]; return await this.$get("rPosts", {limit, offset}) as Post[];
@ -210,7 +209,7 @@ export class User extends Model<User> {
* @param first * @param first
* @param offset * @param offset
*/ */
public async groups({first, offset}: {first: number, offset: number}): Promise<Group[]> { public async groups({first, offset}: { first: number, offset: number }): Promise<Group[]> {
const limit = first || 10; const limit = first || 10;
offset = offset || 0; offset = offset || 0;
return await this.$get("rGroups", {limit, offset}) as Group[]; return await this.$get("rGroups", {limit, offset}) as Group[];

Loading…
Cancel
Save