From fd8b9075273198280d6baa519f5aaf7a15f36382 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Wed, 18 Dec 2019 20:14:36 +0100 Subject: [PATCH] Settings addition to user profile - closes #50 - added the getSelf.settings field to get the user settings - added the setUserSettings mutation to set the settings for the user --- src/graphql/resolvers.ts | 17 +++++++++++++++++ src/graphql/schema.graphql | 6 ++++++ src/lib/models/User.ts | 11 +++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/graphql/resolvers.ts b/src/graphql/resolvers.ts index 4df7402..4851ed1 100644 --- a/src/graphql/resolvers.ts +++ b/src/graphql/resolvers.ts @@ -6,6 +6,7 @@ import globals from "../lib/globals"; import {InternalEvents} from "../lib/InternalEvents"; import * as models from "../lib/models"; import {is} from "../lib/regex"; +import * as yaml from "js-yaml"; /** * Returns the resolvers for the graphql api. @@ -121,6 +122,22 @@ export function resolver(req: any, res: any): any { return new GraphQLError("No username, email or password given."); } }, + async setUserSettings({settings}: {settings: string}) { + if (req.session.userId) { + const user = await models.User.findByPk(req.session.userId); + try { + user.frontendSettings = yaml.safeLoad(settings); + await user.save(); + return user.settings; + } catch (err) { + res.status(400); + return new GraphQLError("Invalid settings json."); + } + } else { + res.status(status.UNAUTHORIZED); + return new NotLoggedInGqlError(); + } + }, async vote({postId, type}: { postId: number, type: dataaccess.VoteType }) { if (postId && type) { if (req.session.userId) { diff --git a/src/graphql/schema.graphql b/src/graphql/schema.graphql index 1782d5d..e061f49 100644 --- a/src/graphql/schema.graphql +++ b/src/graphql/schema.graphql @@ -37,6 +37,9 @@ type Mutation { "Registers the user." register(username: String, email: String, passwordHash: String): Profile + "Sets the user settings to the specified settings string. The settings parameter should be a valid yaml." + setUserSettings(settings: String!): String! + "Logout of the user." logout: Boolean @@ -247,6 +250,9 @@ type Profile implements UserData { "the levels of the user depending on the points" level: Int! + + "the custom settings for the frontend" + settings: String! } "represents a single user post" diff --git a/src/lib/models/User.ts b/src/lib/models/User.ts index 4b57634..9b1026a 100644 --- a/src/lib/models/User.ts +++ b/src/lib/models/User.ts @@ -49,6 +49,10 @@ export class User extends Model { @Column({defaultValue: 0, allowNull: false}) public rankpoints: number; + @NotNull + @Column({defaultValue: {}, allowNull: false, type: sqz.JSON}) + public frontendSettings: any; + @BelongsToMany(() => User, () => Friendship, "userId") public rFriends: User[]; @@ -119,6 +123,13 @@ export class User extends Model { return Math.ceil(this.rankpoints / 100); } + /** + * returns the settings of the user as a jston string + */ + public get settings(): string { + return JSON.stringify(this.getDataValue("frontendSettings")); + } + /** * All friends of the user * @param first