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
pull/4/head
Trivernis 5 years ago
parent e798e3f059
commit fd8b907527

@ -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) {

@ -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"

@ -49,6 +49,10 @@ export class User extends Model<User> {
@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<User> {
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

Loading…
Cancel
Save