fix getPosts
This commit is contained in:
parent
1f7ee78d6a
commit
7df9c61763
@ -4,6 +4,7 @@ import { AuthService } from '../auth/auth.service';
|
|||||||
import { AngularFirestore, Reference } from '@angular/fire/compat/firestore';
|
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 { DocumentReference } from '@angular/fire/firestore';
|
import { DocumentReference } from '@angular/fire/firestore';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
@ -53,50 +54,50 @@ export class PostService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getPosts(username: String, lenght: Number, start: Number) {
|
getPosts(username: String | undefined): Promise<Array<Post>> {
|
||||||
return new Promise<Array<Post>>((resolve, reject) => {
|
return new Promise<Array<Post>>(async (resolve, reject) => {
|
||||||
if (this.auth.checkAllowed()) {
|
|
||||||
if (username == undefined) {
|
if (!this.auth.checkAllowed())
|
||||||
this.auth.getUser().subscribe(cred => {
|
|
||||||
// get the user by id
|
|
||||||
this.afs.collection<User>('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => {
|
|
||||||
// get their posts
|
|
||||||
this.afs.collection<Post>('post', ref => ref.where('user', '==', this.afs.doc('/user/' + user[0].username).ref)).valueChanges({ idField: 'id' }).subscribe(posts => {
|
|
||||||
posts.forEach(post => {
|
|
||||||
// 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
|
|
||||||
post.liked = likes.filter(like => (like as any).user.id == user[0].username).length != 0;
|
|
||||||
post.user = user[0] as User;
|
|
||||||
post.likes = likes.length;
|
|
||||||
})
|
|
||||||
resolve(posts)
|
|
||||||
})
|
|
||||||
});
|
|
||||||
})
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.usersvc.getCurrentUser()
|
|
||||||
.then(currentUser => {
|
|
||||||
// get the user by username
|
|
||||||
this.afs.collection<User>('user').doc(username as string).valueChanges().subscribe(user => {
|
|
||||||
// get their posts
|
|
||||||
this.afs.collection<Post>('post', ref => ref.where('user', '==', this.afs.doc('/user/' + username).ref)).valueChanges({ idField: 'id' }).subscribe(posts => {
|
|
||||||
posts.forEach(post => {
|
|
||||||
// get how many likes it has
|
|
||||||
this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges().subscribe(likes => {
|
|
||||||
post.liked = likes.filter(like => (like as any).user.id == currentUser.username).length != 0;
|
|
||||||
post.user = user as User;
|
|
||||||
post.likes = likes.length;
|
|
||||||
})
|
|
||||||
resolve(posts)
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
reject()
|
reject()
|
||||||
|
|
||||||
|
let result: Array<Post> = new Array<Post>;
|
||||||
|
|
||||||
|
if (username == undefined) {
|
||||||
|
// /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()));
|
||||||
|
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({
|
||||||
|
user: user,
|
||||||
|
content: post.content,
|
||||||
|
image: post.image,
|
||||||
|
likes: likes.length,
|
||||||
|
liked: likes.filter(like => (like as any).user.id == user.username).length != 0,
|
||||||
|
id: post.id
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
resolve(result);
|
||||||
|
} else {
|
||||||
|
// /user/name
|
||||||
|
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()));
|
||||||
|
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({
|
||||||
|
user: user,
|
||||||
|
content: post.content,
|
||||||
|
image: post.image,
|
||||||
|
likes: likes.length,
|
||||||
|
liked: likes.filter(like => (like as any).user.id == currentUser.username).length != 0,
|
||||||
|
id: post.id
|
||||||
|
})
|
||||||
|
})
|
||||||
|
resolve(result);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ export class UserService {
|
|||||||
return new Promise<User>((resolve, reject) => {
|
return new Promise<User>((resolve, reject) => {
|
||||||
if (this.auth.checkAllowed()) {
|
if (this.auth.checkAllowed()) {
|
||||||
if (username != undefined) {
|
if (username != undefined) {
|
||||||
this.afs.collection<User>('user', ref => ref.where('username', '==', username)).valueChanges().subscribe(data => { resolve(data[0] as User) });
|
this.afs.collection<User>('user').doc(username as string).valueChanges().subscribe(data => { resolve(data as User) });
|
||||||
} else {
|
} else {
|
||||||
this.auth.getUser().subscribe(cred => {
|
this.auth.getUser().subscribe(cred => {
|
||||||
this.afs.collection<User>('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { resolve(user[0]) })
|
this.afs.collection<User>('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { resolve(user[0]) })
|
||||||
|
@ -3,8 +3,8 @@ import { CommonModule } from '@angular/common';
|
|||||||
import { SmallUserComponent } from '../small-user/small-user.component';
|
import { SmallUserComponent } from '../small-user/small-user.component';
|
||||||
import { Post } from '../../../../model/Post';
|
import { Post } from '../../../../model/Post';
|
||||||
import { NgIf } from '@angular/common';
|
import { NgIf } from '@angular/common';
|
||||||
import { AuthService } from '../../../services/auth/auth.service';
|
|
||||||
import { PostService } from '../../../services/data/post.service';
|
import { PostService } from '../../../services/data/post.service';
|
||||||
|
import { UserService } from '../../../services/data/user.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-post',
|
selector: 'app-post',
|
||||||
@ -19,11 +19,11 @@ export class PostComponent implements OnInit {
|
|||||||
isOwned: boolean = false;
|
isOwned: boolean = false;
|
||||||
deleted: boolean = false;
|
deleted: boolean = false;
|
||||||
|
|
||||||
constructor(private auth: AuthService, private postSv: PostService) { }
|
constructor(private userSv: UserService, private postSv: PostService) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.auth.getUser().subscribe(user => {
|
this.userSv.getCurrentUser().then(user => {
|
||||||
this.isOwned = user?.email == this.post?.user.email;
|
this.isOwned = user?.username == this.post?.user.username;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,13 +16,8 @@ export class UserComponent implements OnInit{
|
|||||||
|
|
||||||
constructor(private postService: PostService, private userService: UserService, private route: ActivatedRoute) { }
|
constructor(private postService: PostService, private userService: UserService, private route: ActivatedRoute) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
async ngOnInit(): Promise<void> {
|
||||||
this.userService.getUser(this.route.snapshot.params['username'])
|
this.user = await this.userService.getUser(this.route.snapshot.params['username']);
|
||||||
.then(user => { this.user = user })
|
this.posts = await this.postService.getPosts(this.route.snapshot.params['username']);
|
||||||
.catch(() => alert('failed to get user'));
|
|
||||||
|
|
||||||
this.postService.getPosts(this.route.snapshot.params['username'], 10, 0)
|
|
||||||
.then(posts => { this.posts = posts; })
|
|
||||||
.catch(()=> { alert('failed to fetch feed'); });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user