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