Fixed friendships

- fixed problem with accepting friendships
- added removeFriend mutation
pull/2/head
Trivernis 5 years ago
parent 5c6dd6b24f
commit 82d0e1bc59

@ -1,3 +1,4 @@
import {AggregateError} from "bluebird";
import {GraphQLError} from "graphql"; import {GraphQLError} from "graphql";
import * as status from "http-status"; import * as status from "http-status";
import dataaccess from "../lib/dataaccess"; import dataaccess from "../lib/dataaccess";
@ -226,6 +227,15 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No sender or type given."); return new GraphQLError("No sender or type given.");
} }
}, },
async removeFriend({friendId}: {friendId: number}) {
if (req.session.userId) {
const self = await models.User.findByPk(req.session.userId);
return await self.removeFriend(friendId);
} else {
res.status(status.UNAUTHORIZED);
return new NotLoggedInGqlError();
}
},
async getPosts({first, offset, sort}: {first: number, offset: number, sort: dataaccess.SortType}) { async getPosts({first, offset, sort}: {first: number, offset: number, sort: dataaccess.SortType}) {
return await dataaccess.getPosts(first, offset, sort); return await dataaccess.getPosts(first, offset, sort);
}, },

@ -49,6 +49,9 @@ type Mutation {
"lets you deny a request for a given request id" "lets you deny a request for a given request id"
denyRequest(requestId: ID!): Boolean denyRequest(requestId: ID!): Boolean
"removes a friend"
removeFriend(friendId: ID!): Boolean
"send a message in a Chatroom" "send a message in a Chatroom"
sendMessage(chatId: ID!, content: String!): ChatMessage sendMessage(chatId: ID!, content: String!): ChatMessage
@ -138,6 +141,9 @@ type User implements UserData{
"the points of the user" "the points of the user"
points: Int points: Int
"the groups the user has joined"
groups: [Group]
"the levels of the user depending on the points" "the levels of the user depending on the points"
level: Int level: Int
} }

@ -213,7 +213,7 @@ namespace dataaccess {
requestType = requestType || RequestType.FRIENDREQUEST; requestType = requestType || RequestType.FRIENDREQUEST;
const request = await models.Request.create({senderId: sender, receiverId: receiver, requestType}); const request = await models.Request.create({senderId: sender, receiverId: receiver, requestType});
globals.internalEmitter.emit(InternalEvents.REQUESTCREATE, Request); globals.internalEmitter.emit(InternalEvents.REQUESTCREATE, request);
return request; return request;
} }

@ -1,15 +1,17 @@
import {Column, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; import {Column, ForeignKey, Model, NotNull, PrimaryKey, Table} from "sequelize-typescript";
import {User} from "./User"; import {User} from "./User";
@Table({underscored: true}) @Table({underscored: true})
export class Friendship extends Model<Friendship> { export class Friendship extends Model<Friendship> {
@ForeignKey(() => User) @ForeignKey(() => User)
@PrimaryKey
@NotNull @NotNull
@Column({allowNull: false}) @Column({allowNull: false})
public userId: number; public userId: number;
@ForeignKey(() => User) @ForeignKey(() => User)
@PrimaryKey
@NotNull @NotNull
@Column({allowNull: false}) @Column({allowNull: false})
public friendId: number; public friendId: number;

@ -1,5 +1,6 @@
import * as sqz from "sequelize"; import * as sqz from "sequelize";
import { import {
BelongsTo,
BelongsToMany, BelongsToMany,
Column, Column,
CreatedAt, CreatedAt,
@ -11,6 +12,7 @@ import {
UpdatedAt, UpdatedAt,
} from "sequelize-typescript"; } from "sequelize-typescript";
import {RequestNotFoundError} from "../errors/RequestNotFoundError"; import {RequestNotFoundError} from "../errors/RequestNotFoundError";
import {UserNotFoundError} from "../errors/UserNotFoundError";
import {ChatMember} from "./ChatMember"; import {ChatMember} from "./ChatMember";
import {ChatMessage} from "./ChatMessage"; import {ChatMessage} from "./ChatMessage";
import {ChatRoom} from "./ChatRoom"; import {ChatRoom} from "./ChatRoom";
@ -48,9 +50,12 @@ export class User extends Model<User> {
@Column({defaultValue: 0, allowNull: false}) @Column({defaultValue: 0, allowNull: false})
public rankpoints: number; public rankpoints: number;
@BelongsToMany(() => User, () => Friendship) @BelongsToMany(() => User, () => Friendship, "userId")
public rFriends: User[]; public rFriends: User[];
@BelongsToMany(() => User, () => Friendship, "friendId")
public rFriendOf: User[];
@BelongsToMany(() => Post, () => PostVote) @BelongsToMany(() => Post, () => PostVote)
public votes: Array<Post & {PostVote: PostVote}>; public votes: Array<Post & {PostVote: PostVote}>;
@ -69,7 +74,7 @@ export class User extends Model<User> {
@HasMany(() => Post, "authorId") @HasMany(() => Post, "authorId")
public rPosts: Post[]; public rPosts: Post[];
@HasMany(() => Request, "receiverId") @HasMany(() => Request, "senderId")
public rSentRequests: Request[]; public rSentRequests: Request[];
@HasMany(() => Request, "receiverId") @HasMany(() => Request, "receiverId")
@ -104,7 +109,7 @@ export class User extends Model<User> {
} }
public async friends(): Promise<User[]> { public async friends(): Promise<User[]> {
return await this.$get("rFriends") as User[]; return await this.$get("rFriendOf") as User[];
} }
public async chats(): Promise<ChatRoom[]> { public async chats(): Promise<ChatRoom[]> {
@ -157,11 +162,25 @@ export class User extends Model<User> {
if (requests.length > 0) { if (requests.length > 0) {
const request = requests[0]; const request = requests[0];
if (request.requestType === RequestType.FRIENDREQUEST) { if (request.requestType === RequestType.FRIENDREQUEST) {
await this.$add("friends", sender); await Friendship.bulkCreate([
{userId: this.id, friendId: sender},
{userId: sender, friendId: this.id},
], {ignoreDuplicates: true});
await request.destroy(); await request.destroy();
} }
} else { } else {
throw new RequestNotFoundError(sender, this.id, type); throw new RequestNotFoundError(sender, this.id, type);
} }
} }
public async removeFriend(friendId: number) {
const friend = await User.findByPk(friendId);
if (friend) {
await this.$remove("rFriends", friend);
await this.$remove("rFriendOf", friend);
return true;
} else {
throw new UserNotFoundError(friendId);
}
}
} }

Loading…
Cancel
Save