Merge branch 'develop' of Software_Engineering_I/greenvironment-server into master

pull/5/head
Trivernis 4 years ago committed by Gitea
commit 6f3f0a0d65

@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- routine to cleanup orphaned media entries (not referenced by post, user, group)
- delete handler for media to delete the corresponding file
- type for create post to know if it is a media or text post (media posts are invisible until a media file is uploaded)
- reports and mutations to report posts and create reasons to report
### Removed
@ -49,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- only group admins can create group events
- config behaviour to use all files that reside in the ./config directory with the .toml format
- default response timeout from 2 minutes to 30 seconds
- cluster api to start workers with a 2 second delay each to avoid race conditions
### Fixed

@ -5,6 +5,16 @@ import App from "./app";
const numCPUs = require("os").cpus().length;
/**
* An asynchronous delay
* @param millis
*/
async function delay(millis: number) {
return new Promise(((resolve) => {
setTimeout(resolve, millis);
}));
}
if (cluster.isMaster) {
console.log(`[CLUSTER-M] Master ${process.pid} is running`);
@ -21,9 +31,12 @@ if (cluster.isMaster) {
});
});
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
(async () => {
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
await delay(1000);
}
})();
} else {
/**

@ -65,6 +65,8 @@ namespace dataaccess {
models.Activity,
models.BlacklistedPhrase,
models.Media,
models.Report,
models.ReportReason,
]);
} catch (err) {
globals.logger.error(err.message);

@ -0,0 +1,10 @@
import {BaseError} from "./BaseError";
/**
* An error that is thrown when the user changes the handle to a value that is already used by a different user
*/
export class HandleInUseError extends BaseError {
constructor(handle: string) {
super(`A different user is already using the handle '${handle}'!`);
}
}

@ -0,0 +1,10 @@
import {BaseError} from "./BaseError";
/**
* An error that is thrown when a user tries to report the same post twice
*/
export class ReportAlreadyExistsError extends BaseError {
constructor() {
super("You've already reported this post for that reason.");
}
}

@ -0,0 +1,10 @@
import {BaseError} from "./BaseError";
/**
* An error that is thrown when one tries to create a request with a name that already exists.
*/
export class ReportReasonNameAlreadyExistsError extends BaseError {
constructor(name: string) {
super(`A report reason with the name '${name}' already exists!`);
}
}

@ -0,0 +1,14 @@
import * as httpStatus from "http-status";
import {BaseError} from "./BaseError";
/**
* An error that is thrown when a report reason could not be found
*/
export class ReportReasonNotFoundError extends BaseError {
public readonly statusCode = httpStatus.NOT_FOUND;
constructor(reasonId: number) {
super(`A reason with the id '${reasonId}' could not be found`);
}
}

@ -38,7 +38,8 @@ export class Media extends Model<Media> {
/**
* The type of media
*/
@Column({type: sqz.ENUM, values: ["IMAGE", "VIDEO"]})
@NotNull
@Column({type: sqz.ENUM, values: ["IMAGE", "VIDEO"], allowNull: false})
public type: MediaType;
/**

@ -1,9 +1,20 @@
import * as sqz from "sequelize";
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
import {
BelongsTo,
BelongsToMany,
Column,
CreatedAt,
ForeignKey,
HasMany,
Model,
NotNull,
Table,
} from "sequelize-typescript";
import markdown from "../markdown";
import {Activity} from "./Activity";
import {Media} from "./Media";
import {PostVote, VoteType} from "./PostVote";
import {Report} from "./Report";
import {User} from "./User";
/**
@ -73,6 +84,12 @@ export class Post extends Model<Post> {
// tslint:disable-next-line:completed-docs
public rVotes: Array<User & { PostVote: PostVote }>;
/**
* The reports on the post
*/
@HasMany(() => Report, "postId")
public rReports: Report[];
/**
* The date the post was created at
*/
@ -100,6 +117,13 @@ export class Post extends Model<Post> {
return await this.$get("rVotes") as Array<User & { PostVote: PostVote }>;
}
/**
* Returns the reports on the post
*/
public async reports({first, offset}: {first: number, offset: number}): Promise<Report[]> {
return await this.$get("rReports", {limit: first, offset}) as Report[];
}
/**
* Returns the markdown-rendered html content of the post
*/

@ -0,0 +1,73 @@
import {BelongsTo, Column, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
import {Post, User} from "./index";
import {ReportReason} from "./ReportReason";
/**
* A report on a post
*/
@Table({underscored: true})
export class Report extends Model<Report> {
/**
* The id of the post that was reported
*/
@ForeignKey(() => Post)
@NotNull
@Column({allowNull: false, onDelete: "cascade", unique: "compositeIndex"})
public postId: number;
/**
* The id of the user who issued the report
*/
@ForeignKey(() => User)
@NotNull
@Column({allowNull: false, onDelete: "cascade", unique: "compositeIndex"})
public userId: number;
/**
* The reason for which the post was reported
*/
@ForeignKey(() => ReportReason)
@NotNull
@Column({allowNull: false, onDelete: "cascade", unique: "compositeIndex"})
public reasonId: number;
/**
* The user that reported the post
*/
@BelongsTo(() => User, "userId")
public rUser: User;
/**
* The post that was reported
*/
@BelongsTo(() => Post, "postId")
public rPost: Post;
/**
* The reason why the post was reported
*/
@BelongsTo(() => ReportReason, "reasonId")
public rReason: ReportReason;
/**
* Returns the user that reported the post
*/
public async user(): Promise<User> {
return await this.$get("rUser") as User;
}
/**
* Returns the post that was reported
*/
public async post(): Promise<Post> {
return await this.$get("rPost") as Post;
}
/**
* Returns the reason why the post was reported
*/
public async reason(): Promise<ReportReason> {
return await this.$get("rReason") as ReportReason;
}
}

@ -0,0 +1,24 @@
import * as sqz from "sequelize";
import {Column, Model, NotNull, Table, Unique} from "sequelize-typescript";
/**
* A reason for why a post was reported
*/
@Table({underscored: true})
export class ReportReason extends Model<ReportReason> {
/**
* The name of the reason (short and precise)
*/
@NotNull
@Unique
@Column({unique: true, allowNull: false, type: sqz.STRING(64)})
public name: string;
/**
* A longer descripion of the reason
*/
@NotNull
@Column({allowNull: false, type: sqz.STRING(512)})
public description: string;
}

@ -14,3 +14,5 @@ export {EventParticipant} from "./EventParticipant";
export {Activity} from "./Activity";
export {BlacklistedPhrase} from "./BlacklistedPhrase";
export {Media} from "./Media";
export {Report} from "./Report";
export {ReportReason} from "./ReportReason";

@ -46,8 +46,8 @@ export class GraphqlRoute extends Route {
} else {
response.status(400);
}
logger.debug(err.message);
logger.silly(err.stack);
logger.verbose(err.message);
logger.debug(err.stack);
return err.graphqlError ?? err;
},
graphiql: config.get<boolean>("api.graphiql"),

@ -33,22 +33,11 @@ interface IUploadConfirmation {
success: boolean;
}
type ImageFit = "cover" | "contain" | "fill" | "inside" | "outside";
/**
* Represents an upload handler.
*/
export class UploadRoute extends Route {
/**
* Returns the hash of the current time to be used as a filename.
*/
private static getFileName() {
const hash = crypto.createHash("md5");
hash.update(Number(Date.now()).toString());
return hash.digest("hex");
}
/**
* The directory where the uploaded data will be saved in
*/
@ -95,6 +84,9 @@ export class UploadRoute extends Route {
success: false,
};
}
if (uploadConfirmation.error) {
res.status(httpStatus.BAD_REQUEST);
}
res.json(uploadConfirmation);
});
}

@ -4,14 +4,20 @@ import isEmail from "validator/lib/isEmail";
import dataAccess from "../../lib/dataAccess";
import {BlacklistedError} from "../../lib/errors/BlacklistedError";
import {GroupNotFoundError} from "../../lib/errors/GroupNotFoundError";
import {HandleInUseError} from "../../lib/errors/HandleInUseError";
import {InvalidEmailError} from "../../lib/errors/InvalidEmailError";
import {NotAGroupAdminError} from "../../lib/errors/NotAGroupAdminError";
import {NotAnAdminError} from "../../lib/errors/NotAnAdminError";
import {NotTheGroupCreatorError} from "../../lib/errors/NotTheGroupCreatorError";
import {PostNotFoundError} from "../../lib/errors/PostNotFoundError";
import {ReportAlreadyExistsError} from "../../lib/errors/ReportAlreadyExistsError";
import {ReportReasonNameAlreadyExistsError} from "../../lib/errors/ReportReasonNameAlreadyExistsError";
import {ReportReasonNotFoundError} from "../../lib/errors/ReportReasonNotFoundError";
import globals from "../../lib/globals";
import {InternalEvents} from "../../lib/InternalEvents";
import {Activity, BlacklistedPhrase, ChatMessage, ChatRoom, Event, Group, Post, Request, User} from "../../lib/models";
import {Report} from "../../lib/models";
import {ReportReason} from "../../lib/models";
import {UploadManager} from "../../lib/UploadManager";
import {BaseResolver} from "./BaseResolver";
@ -98,6 +104,36 @@ export class MutationResolver extends BaseResolver {
return user;
}
/**
* Sets a new username for a user
* @param username
* @param request
*/
public async setUsername({username}: {username: string}, request: any): Promise<User> {
this.ensureLoggedIn(request);
const user = await User.findByPk(request.session.userId);
user.username = username;
await user.save();
return user;
}
/**
* Sets a new handle for the user. If the handle is alredy used by a different user, an error is returned
* @param handle
* @param request
*/
public async setHandle({handle}: {handle: string}, request: any): Promise<User> {
this.ensureLoggedIn(request);
const user = await User.findByPk(request.session.userId);
const handleAvailable = !(await User.findOne({where: {handle}}));
if (!handleAvailable) {
throw new HandleInUseError(handle);
}
user.handle = handle;
await user.save();
return user;
}
/**
* Sets the frontend settings for the logged in user
* @param settings
@ -176,6 +212,29 @@ export class MutationResolver extends BaseResolver {
}
}
/**
* Reports a post
* @param postId
* @param reasonId
* @param request
*/
public async reportPost({postId, reasonId}: {postId: number, reasonId: number}, request: any): Promise<Report> {
this.ensureLoggedIn(request);
const post = await Post.findByPk(postId);
if (!post) {
throw new PostNotFoundError(postId);
}
const reason = await ReportReason.findByPk(reasonId);
if (!reason) {
throw new ReportReasonNotFoundError(reasonId);
}
const report = await Report.findOne({where: {postId, reasonId, userId: request.session.userId}});
if (report) {
throw new ReportAlreadyExistsError();
}
return Report.create({postId, reasonId, userId: request.session.userId}, {include: [ReportReason]});
}
/**
* Creates a chat with several members
* @param members
@ -478,4 +537,24 @@ export class MutationResolver extends BaseResolver {
return false;
}
}
/**
* Creates a new reason for reporting posts
* @param name
* @param description
* @param request
*/
public async createReportReason({name, description}: {name: string, description: string}, request: any):
Promise<ReportReason> {
this.ensureLoggedIn(request);
const user = await User.findByPk(request.session.userId);
if (!user.isAdmin) {
throw new NotAnAdminError();
}
const reasonExists = await ReportReason.findOne({where: {name}});
if (reasonExists) {
throw new ReportReasonNameAlreadyExistsError(name);
}
return ReportReason.create({name, description});
}
}

@ -4,9 +4,10 @@ import dataaccess from "../../lib/dataAccess";
import {ChatNotFoundError} from "../../lib/errors/ChatNotFoundError";
import {PostNotFoundGqlError} from "../../lib/errors/graphqlErrors";
import {GroupNotFoundError} from "../../lib/errors/GroupNotFoundError";
import {NotAnAdminError} from "../../lib/errors/NotAnAdminError";
import {RequestNotFoundError} from "../../lib/errors/RequestNotFoundError";
import {UserNotFoundError} from "../../lib/errors/UserNotFoundError";
import {Activity, BlacklistedPhrase, ChatRoom, Event, Group, Post, Request, User} from "../../lib/models";
import {Activity, BlacklistedPhrase, ChatRoom, Event, Group, Post, Report, Request, User} from "../../lib/models";
import {BlacklistedResult} from "./BlacklistedResult";
import {MutationResolver} from "./MutationResolver";
import {SearchResult} from "./SearchResult";
@ -184,4 +185,19 @@ export class QueryResolver extends MutationResolver {
return (await BlacklistedPhrase.findAll({limit: first, offset}))
.map((p) => p.phrase);
}
/**
* Returns all reports with pagination if the user is an admin
* @param first
* @param offset
* @param request
*/
public async getReports({first, offset}: {first: number, offset: number}, request: any): Promise<Report[]> {
this.ensureLoggedIn(request);
const user = await User.findByPk(request.session.userId);
if (!user?.isAdmin) {
throw new NotAnAdminError();
}
return Report.findAll({limit: first, offset});
}
}

@ -45,6 +45,9 @@ type Query {
"Returns the blacklist with pagination."
getBlacklistedPhrases(first: Int = 20, offset: Int = 0): [String!]! @complexity(value: 1, multipliers: ["first"])
"Returns all issued reports with pagination"
getReports(first: Int = 20, offset: Int = 0): [Report!]! @complexity(value: 1, multipliers: ["first"])
}
type Mutation {
@ -60,15 +63,18 @@ type Mutation {
"Sets the user settings to the specified settings string. The settings parameter should be a valid yaml."
setUserSettings(settings: String!): String!
"Sets a new username"
setUsername(username: String!): Profile!
"Sets a new handle"
setHandle(handle: String!): Profile!
"Logout of the user."
logout: Boolean
"Upvote/downvote a Post"
vote(postId: ID!, type: VoteType!): VoteResult
"Report the post"
report(postId: ID!): Boolean!
"send a request"
sendRequest(receiver: ID!, type: RequestType): Request
@ -90,6 +96,9 @@ type Mutation {
"delete the post for a given post id"
deletePost(postId: ID!): Boolean!
"reports the post for a specific report reason"
reportPost(postId: ID!, reasonId: ID!): Report
"Creates a chat between the user (and optional an other user)"
createChat(members: [ID!]): ChatRoom!
@ -131,6 +140,9 @@ type Mutation {
"Removes a phrase from the blacklist. Returns true if the phrase could be found and deleted."
removeFromBlacklist(phrase: String!, languageCode: String = "en"): Boolean!
"Creates a new report reason"
createReportReason(name: String!, description: String!): ReportReason
}
interface UserData {
@ -346,6 +358,9 @@ type Post {
"the uploaded file or video for the post"
media: Media
"returns all reports issued on the post"
reports(first: Int = 20, offset: Int = 0): [Report!]!
}
"represents a request of any type"
@ -517,6 +532,9 @@ type BlacklistedResult {
"a type of uploaded media"
type Media {
"the id of the media"
id: ID!
"the url pointing to the media in the data folder"
url: String!
@ -524,6 +542,35 @@ type Media {
type: MediaType
}
"a report on a post"
type Report {
"the id of the report"
id: ID!
"the post that was reported"
post: Post!
"the reason why the post was reported"
reason: ReportReason!
"the user who reported the post"
user: User!
}
"the reason for a report"
type ReportReason {
"the id of the reason"
id: ID!
"the name of the report reason"
name: String!
"the description of the reason"
description: String!
}
"represents the type of media"
enum MediaType {
VIDEO

Loading…
Cancel
Save