Pārlūkot izejas kodu

feat: Add user schema

Owen Diffey 1 gadu atpakaļ
vecāks
revīzija
e537231cde

+ 1 - 0
backend/src/modules/DataModule.ts

@@ -223,6 +223,7 @@ export default class DataModule extends BaseModule {
 			news: await this.loadModel("news"),
 			session: await this.loadModel("session"),
 			station: await this.loadModel("station"),
+			user: await this.loadModel("user")
 		};
 	}
 

+ 202 - 0
backend/src/schemas/user.ts

@@ -0,0 +1,202 @@
+import { Model, Schema, SchemaTypes, Types } from "mongoose";
+import { BaseSchema } from "../types/Schemas";
+
+export enum UserRole {
+	ADMIN = "admin",
+	MODERATOR = "moderator",
+	USER = "user"
+}
+
+export enum UserAvatarType {
+	GRAVATAR = "gravatar",
+	INITIALS = "initials"
+}
+
+export enum UserAvatarColor {
+	BLUE = "blue",
+	GREEN = "green",
+	ORANGE = "orange",
+	PURPLE = "purple",
+	RED = "red",
+	TEAL = "teal"
+}
+
+export interface UserSchema extends BaseSchema {
+	username: string;
+	role: UserRole;
+	email: {
+		address: string;
+		verified: boolean;
+		verificationToken?: string;
+	};
+	avatar: {
+		type: UserAvatarType;
+		url?: string;
+		color?: UserAvatarColor;
+	};
+	services: {
+		password?: {
+			password: string;
+			reset: {
+				code: string;
+				expires: number;
+			};
+			set: {
+				code: string;
+				expires: number;
+			};
+		};
+		github?: {
+			id: number;
+			access_token: string;
+		};
+	};
+	statistics: {
+		songsRequested: number;
+	};
+	likedSongsPlaylist: Types.ObjectId;
+	dislikedSongsPlaylist: Types.ObjectId;
+	favoriteStations: Types.ObjectId[];
+	name: string;
+	location?: string;
+	bio?: string;
+	preferences: {
+		orderOfPlaylists: Types.ObjectId[];
+		nightmode: boolean;
+		autoSkipDisliked: boolean;
+		activityLogPublic: boolean;
+		anonymousSongRequests: boolean;
+		activityWatch: boolean;
+	};
+}
+
+export type UserModel = Model<UserSchema>;
+
+export const schema = new Schema<UserSchema, UserModel>(
+	{
+		username: {
+			type: SchemaTypes.String,
+			required: true
+		},
+		role: {
+			type: SchemaTypes.String,
+			enum: Object.values(UserRole),
+			required: true
+		},
+		email: {
+			address: {
+				type: SchemaTypes.String,
+				required: true
+			},
+			verified: {
+				type: SchemaTypes.Boolean,
+				default: false,
+				required: true
+			},
+			verificationToken: {
+				type: SchemaTypes.String,
+				required: false
+			}
+		},
+		avatar: {
+			type: {
+				type: SchemaTypes.String,
+				enum: Object.values(UserAvatarType),
+				required: true
+			},
+			url: {
+				type: SchemaTypes.String,
+				required: false
+			},
+			color: {
+				type: SchemaTypes.String,
+				enum: Object.values(UserAvatarColor),
+				required: false
+			}
+		},
+		services: {
+			password: {
+				password: SchemaTypes.String,
+				reset: {
+					code: {
+						type: SchemaTypes.String,
+						minLength: 8,
+						maxLength: 8
+					},
+					expires: { type: SchemaTypes.Date }
+				},
+				set: {
+					code: {
+						type: SchemaTypes.String,
+						minLength: 8,
+						maxLength: 8
+					},
+					expires: { type: SchemaTypes.Date }
+				}
+			},
+			github: {
+				id: SchemaTypes.Number,
+				access_token: SchemaTypes.String
+			}
+		},
+		statistics: {
+			songsRequested: {
+				type: SchemaTypes.Number,
+				default: 0,
+				required: true
+			}
+		},
+		likedSongsPlaylist: {
+			type: SchemaTypes.ObjectId,
+			required: true
+		},
+		dislikedSongsPlaylist: {
+			type: SchemaTypes.ObjectId,
+			required: true
+		},
+		favoriteStations: [SchemaTypes.ObjectId],
+		name: {
+			type: SchemaTypes.String,
+			required: true
+		},
+		location: {
+			type: SchemaTypes.String,
+			required: false
+		},
+		bio: {
+			type: SchemaTypes.String,
+			required: false
+		},
+		preferences: {
+			orderOfPlaylists: [SchemaTypes.ObjectId],
+			nightmode: {
+				type: SchemaTypes.Boolean,
+				default: false,
+				required: true
+			},
+			autoSkipDisliked: {
+				type: SchemaTypes.Boolean,
+				default: true,
+				required: true
+			},
+			activityLogPublic: {
+				type: SchemaTypes.Boolean,
+				default: false,
+				required: true
+			},
+			anonymousSongRequests: {
+				type: SchemaTypes.Boolean,
+				default: false,
+				required: true
+			},
+			activityWatch: {
+				type: SchemaTypes.Boolean,
+				default: false,
+				required: true
+			}
+		}
+	},
+	{ documentVersion: 4 }
+);
+
+export type UserSchemaType = typeof schema;

+ 2 - 0
backend/src/types/Models.ts

@@ -2,10 +2,12 @@ import { AbcModel } from "../schemas/abc";
 import { NewsModel } from "../schemas/news";
 import { SessionModel } from "../schemas/session";
 import { StationModel } from "../schemas/station";
+import { UserModel } from "../schemas/user";
 
 export type Models = {
 	abc: AbcModel;
 	news: NewsModel;
 	session: SessionModel;
 	station: StationModel;
+	user: UserModel;
 };

+ 2 - 0
backend/src/types/Schemas.ts

@@ -4,6 +4,7 @@ import { AbcSchemaType } from "../schemas/abc";
 import { NewsSchemaType } from "../schemas/news";
 import { SessionSchemaType } from "../schemas/session";
 import { StationSchemaType } from "../schemas/station";
+import { UserSchemaType } from "../schemas/user";
 
 // eslint-disable-next-line
 export interface BaseSchema extends DocumentVersion, TimestampsSchema {
@@ -20,4 +21,5 @@ export type Schemas = {
 	news: NewsSchemaType;
 	session: SessionSchemaType;
 	station: StationSchemaType;
+	user: UserSchemaType;
 };