|
|
@ -1,5 +1,5 @@
|
|
|
|
import * as sqz from "sequelize";
|
|
|
|
import * as sqz from "sequelize";
|
|
|
|
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table,} from "sequelize-typescript";
|
|
|
|
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
|
|
|
|
import markdown from "../markdown";
|
|
|
|
import markdown from "../markdown";
|
|
|
|
import {PostVote, VoteType} from "./PostVote";
|
|
|
|
import {PostVote, VoteType} from "./PostVote";
|
|
|
|
import {User} from "./User";
|
|
|
|
import {User} from "./User";
|
|
|
@ -44,6 +44,11 @@ export class Post extends Model<Post> {
|
|
|
|
return (await this.votes()).filter((v) => v.PostVote.voteType === VoteType.DOWNVOTE).length;
|
|
|
|
return (await this.votes()).filter((v) => v.PostVote.voteType === VoteType.DOWNVOTE).length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Toggles the vote of the user.
|
|
|
|
|
|
|
|
* @param userId
|
|
|
|
|
|
|
|
* @param type
|
|
|
|
|
|
|
|
*/
|
|
|
|
public async vote(userId: number, type: VoteType): Promise<VoteType> {
|
|
|
|
public async vote(userId: number, type: VoteType): Promise<VoteType> {
|
|
|
|
type = type ?? VoteType.UPVOTE;
|
|
|
|
type = type ?? VoteType.UPVOTE;
|
|
|
|
let votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
|
|
|
|
let votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
|
|
|
@ -67,4 +72,13 @@ export class Post extends Model<Post> {
|
|
|
|
|
|
|
|
|
|
|
|
return vote.PostVote.voteType;
|
|
|
|
return vote.PostVote.voteType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns the type of vote that was performend on the post by the user specified by the user id.
|
|
|
|
|
|
|
|
* @param userId
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public async userVote({userId}: {userId: number}): Promise<VoteType> {
|
|
|
|
|
|
|
|
const votes = await this.$get("rVotes", {where: {id: userId}}) as Array<User & {PostVote: PostVote}>;
|
|
|
|
|
|
|
|
return votes[0]?.PostVote?.voteType;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|