From e0dd9b270f39ccfa6328d9461d0414070aee4c9d Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Jan 2020 16:52:05 +0100 Subject: [PATCH] Make every userId in status fields optional - Make userId in joined on Group and Event optional - Make userId and deletable in userVote on Post optional --- src/graphql/schema.graphql | 8 ++++---- src/lib/models/Event.ts | 4 +++- src/lib/models/Group.ts | 4 +++- src/lib/models/Post.ts | 10 +++++++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/graphql/schema.graphql b/src/graphql/schema.graphql index 954ca8b..8c974d2 100644 --- a/src/graphql/schema.graphql +++ b/src/graphql/schema.graphql @@ -304,10 +304,10 @@ type Post { createdAt: String! "the type of vote the user performed on the post" - userVote(userId: ID!): VoteType + userVote(userId: ID): VoteType "if the post can be deleted by the specified user" - deletable(userId: ID!): Boolean! + deletable(userId: ID): Boolean! "the activity that belongs to the post" activity: Activity @@ -387,7 +387,7 @@ type Group { events(first: Int=10, offset: Int=0): [Event!]! "If the user with the specified id has joined the group" - joined(userId: Int!): Boolean! + joined(userId: Int): Boolean! } type Event { @@ -407,7 +407,7 @@ type Event { participants(first: Int=10, offset: Int=0): [User!]! "Returns if the user with the specified id has joined the event" - joined(userId: Int!): Boolean + joined(userId: Int): Boolean } "respresents an access token entry with the value as the acutal token and expires as the date the token expires." diff --git a/src/lib/models/Event.ts b/src/lib/models/Event.ts index cd98388..c52e9a1 100644 --- a/src/lib/models/Event.ts +++ b/src/lib/models/Event.ts @@ -64,8 +64,10 @@ export class Event extends Model { /** * Returns if the specified user has joined the event * @param userId + * @param request */ - public async joined({userId}: {userId: number}): Promise { + public async joined({userId}: {userId: number}, request: any): Promise { + userId = userId ?? request.session.userId; const participants = await this.$get("rParticipants", {where: {id: userId}}) as User[]; return participants.length !== 0; } diff --git a/src/lib/models/Group.ts b/src/lib/models/Group.ts index 2b6b282..f7f7ce0 100644 --- a/src/lib/models/Group.ts +++ b/src/lib/models/Group.ts @@ -125,8 +125,10 @@ export class Group extends Model { /** * Returns if a user has joined the group * @param userId + * @param request */ - public async joined({userId}: {userId: number}): Promise { + public async joined({userId}: {userId: number}, request: any): Promise { + userId = userId ?? request.session.userId; const members = await this.$get("rMembers", {where: {id: userId}}) as User[]; return members.length !== 0; } diff --git a/src/lib/models/Post.ts b/src/lib/models/Post.ts index ed41e56..59ee1de 100644 --- a/src/lib/models/Post.ts +++ b/src/lib/models/Post.ts @@ -1,5 +1,6 @@ import * as sqz from "sequelize"; import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; +import globals from "../globals"; import markdown from "../markdown"; import {Activity} from "./Activity"; import {PostVote, VoteType} from "./PostVote"; @@ -132,8 +133,10 @@ export class Post extends Model { /** * Returns the type of vote that was performed on the post by the user specified by the user id. * @param userId + * @param request */ - public async userVote({userId}: {userId: number}): Promise { + public async userVote({userId}: {userId: number}, request: any): Promise { + userId = userId ?? request.session.userId; const votes = await this.$get("rVotes", {where: {id: userId}}) as Array; return votes[0]?.PostVote?.voteType; } @@ -141,9 +144,10 @@ export class Post extends Model { /** * Returns if the post can be deleted by the user with the given id. * @param userId + * @param request */ - public async deletable({userId}: {userId: number}): Promise { - + public async deletable({userId}: {userId: number}, request: any): Promise { + userId = userId ?? request.session.userId; const isAuthor = Number(userId) === Number(this.authorId); if (!isAuthor) { return (await User.findOne({where: {id: userId}})).isAdmin;