rewrite postervice with await

This commit is contained in:
Benedek László 2024-05-10 01:22:53 +02:00
parent 7df9c61763
commit a67e4a43e4

View File

@ -5,7 +5,6 @@ import { AngularFirestore, Reference } from '@angular/fire/compat/firestore';
import { User } from '../../../model/User'; import { User } from '../../../model/User';
import { UserService } from './user.service'; import { UserService } from './user.service';
import { firstValueFrom } from 'rxjs'; import { firstValueFrom } from 'rxjs';
import { DocumentReference } from '@angular/fire/firestore';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -14,44 +13,30 @@ export class PostService {
constructor(private auth: AuthService, private afs: AngularFirestore, private usersvc: UserService) { } constructor(private auth: AuthService, private afs: AngularFirestore, private usersvc: UserService) { }
getFeed(length: Number, start: Number): Promise<Array<Post>> { getFeed(length: Number, start: Number): Promise<Array<Post>> {
return new Promise<Array<Post>>((resolve, reject) => { return new Promise<Array<Post>>(async (resolve, reject) => {
let result = Array<Post>(); if (!this.auth.checkAllowed())
reject();
let result: Array<Post> = new Array<Post>();
let currentUser = await this.usersvc.getCurrentUser();
let posts = await firstValueFrom(this.afs.collection('post').valueChanges({ idField: 'id' }));
posts.forEach(async post=>{
let user = await firstValueFrom(this.afs.doc((post as any).user).valueChanges()) as User;
let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges())
if (this.auth.checkAllowed()) {
this.usersvc.getCurrentUser()
.then(currentUser => {
// get posts
this.afs.collection<Post>('post').valueChanges({ idField: 'id' }).subscribe(posts => {
posts.forEach(post => {
// get the user who made this post
this.afs.doc((post as any).user).valueChanges().subscribe(user => {
// get how many likes it has
this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges().subscribe(likes => {
// check if we liked this post
let liked: boolean = likes.filter(like => (like as any).user.id == currentUser.username).length != 0;
result.push({ result.push({
user: { user: user,
username: (user as User).username, content: (post as any).content,
email: (user as User).email, image: (post as any).image,
picture: (user as User).picture,
followed: (user as User).followed
},
content: post.content,
image: post.image,
likes: likes.length, likes: likes.length,
liked: liked, liked: likes.filter(like => (like as any).user.id == currentUser.username).length != 0,
id: post.id id: post.id
}) })
})
});
});
resolve(result); resolve(result);
});
}) })
} else { })
reject()
}
});
} }
getPosts(username: String | undefined): Promise<Array<Post>> { getPosts(username: String | undefined): Promise<Array<Post>> {
@ -66,7 +51,7 @@ export class PostService {
// /user -> currentUser // /user -> currentUser
let user = await this.usersvc.getCurrentUser(); let user = await this.usersvc.getCurrentUser();
let userRef = this.afs.doc('/user/' + user.username).ref; let userRef = this.afs.doc('/user/' + user.username).ref;
let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef)).valueChanges())); let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef)).valueChanges({ idField: 'id' })));
posts.forEach(async (post) => { posts.forEach(async (post) => {
let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges()); let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges());
result.push({ result.push({
@ -85,7 +70,7 @@ export class PostService {
let user = await this.usersvc.getUser(username); let user = await this.usersvc.getUser(username);
let userRef = this.afs.doc('/user/' + user.username).ref; let userRef = this.afs.doc('/user/' + user.username).ref;
let currentUser = await this.usersvc.getCurrentUser(); let currentUser = await this.usersvc.getCurrentUser();
let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef)).valueChanges())); let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef)).valueChanges({ idField: 'id' })));
posts.forEach(async (post) => { posts.forEach(async (post) => {
let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges()); let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges());
result.push({ result.push({
@ -102,25 +87,19 @@ export class PostService {
}); });
} }
// FIX: duplicate posts newPost(content: String, image: String): Promise<void> {
newPost(content: String, image: String) { return new Promise<void>(async (resolve, reject) => {
return new Promise<void>((resolve, reject) => { if (!this.auth.checkAllowed())
console.log('new post') reject();
if (this.auth.checkAllowed()) {
this.auth.getUser().subscribe(data => { let user = await this.usersvc.getCurrentUser();
this.afs.collection<User>('user', ref => ref.where('id', '==', data?.uid)).valueChanges().subscribe(user => {
this.afs.collection('post').add({ this.afs.collection('post').add({
user: this.afs.doc('/user/' + user[0].username).ref, user: this.afs.doc('/user/' + user.username).ref,
content: content, content: content,
image: image, image: image
}) })
.then(() => resolve()) .then(() => resolve())
.catch(() => reject()); .catch(() => reject())
});
});
} else {
reject();
}
}); });
} }
@ -129,33 +108,30 @@ export class PostService {
} }
toggleLike(postId: String): Promise<boolean> { toggleLike(postId: String): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => { return new Promise<boolean>(async (resolve, reject) => {
if (this.auth.checkAllowed()) { if (!this.auth.checkAllowed())
this.usersvc.getCurrentUser() reject();
.then(user => {
let user = await this.usersvc.getCurrentUser();
let userRef = this.afs.doc('/user/' + user.username).ref; let userRef = this.afs.doc('/user/' + user.username).ref;
let postRef = this.afs.doc('/post/' + postId).ref; let postRef = this.afs.doc('/post/' + postId).ref;
this.afs.collection('like', ref => ref.where('user', '==', userRef).where('post', '==', postRef)).get().subscribe(likes => {
if (likes.docs.length == 0) { firstValueFrom(this.afs.collection('like', ref => ref.where('user', '==', userRef).where('post', '==', postRef)).valueChanges({ idField: 'id' }))
// like .then(async likes => {
this.afs.collection('like').add({ if (likes.length == 0) {
await this.afs.collection('like').add({
user: userRef, user: userRef,
post: postRef post: postRef
}) })
resolve(true); resolve(true);
} else { } else {
// dislike likes.forEach(async like => {
likes.docs.forEach(likes => { await this.afs.doc('/like/' + like.id).delete();
likes.ref.delete()
})
resolve(false); resolve(false);
})
} }
console.log(likes)
}) })
}) })
.catch(() => reject())
} else {
reject();
}
});
} }
} }