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 * as status from "http-status";
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.");
}
},
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}) {
return await dataaccess.getPosts(first, offset, sort);
},

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

@ -213,7 +213,7 @@ namespace dataaccess {
requestType = requestType || RequestType.FRIENDREQUEST;
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;
}

@ -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";
@Table({underscored: true})
export class Friendship extends Model<Friendship> {
@ForeignKey(() => User)
@PrimaryKey
@NotNull
@Column({allowNull: false})
public userId: number;
@ForeignKey(() => User)
@PrimaryKey
@NotNull
@Column({allowNull: false})
public friendId: number;

@ -1,5 +1,6 @@
import * as sqz from "sequelize";
import {
BelongsTo,
BelongsToMany,
Column,
CreatedAt,
@ -11,6 +12,7 @@ import {
UpdatedAt,
} from "sequelize-typescript";
import {RequestNotFoundError} from "../errors/RequestNotFoundError";
import {UserNotFoundError} from "../errors/UserNotFoundError";
import {ChatMember} from "./ChatMember";
import {ChatMessage} from "./ChatMessage";
import {ChatRoom} from "./ChatRoom";
@ -48,9 +50,12 @@ export class User extends Model<User> {
@Column({defaultValue: 0, allowNull: false})
public rankpoints: number;
@BelongsToMany(() => User, () => Friendship)
@BelongsToMany(() => User, () => Friendship, "userId")
public rFriends: User[];
@BelongsToMany(() => User, () => Friendship, "friendId")
public rFriendOf: User[];
@BelongsToMany(() => Post, () => PostVote)
public votes: Array<Post & {PostVote: PostVote}>;
@ -69,7 +74,7 @@ export class User extends Model<User> {
@HasMany(() => Post, "authorId")
public rPosts: Post[];
@HasMany(() => Request, "receiverId")
@HasMany(() => Request, "senderId")
public rSentRequests: Request[];
@HasMany(() => Request, "receiverId")
@ -104,7 +109,7 @@ export class User extends Model<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[]> {
@ -157,11 +162,25 @@ export class User extends Model<User> {
if (requests.length > 0) {
const request = requests[0];
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();
}
} else {
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