Compare commits

..

1 Commits

Author SHA1 Message Date
Benedek László 7f97e300ff search 2024-05-09 23:30:31 +02:00
5 changed files with 58 additions and 55 deletions

View File

@ -7,9 +7,8 @@ export const routes: Routes = [
{ path: 'feed', loadChildren: () => import('./feed/feed.module').then(m => m.FeedModule), canActivate: [CheckTokenCanActivate] }, { path: 'feed', loadChildren: () => import('./feed/feed.module').then(m => m.FeedModule), canActivate: [CheckTokenCanActivate] },
{ path: 'user', loadChildren: () => import('./user/user.module').then(m => m.UserModule), canActivate: [CheckTokenCanActivate] }, { path: 'user', loadChildren: () => import('./user/user.module').then(m => m.UserModule), canActivate: [CheckTokenCanActivate] },
{ path: 'new-post', loadChildren: () => import('./new-post/new-post.module').then(m => m.NewPostModule), canActivate: [CheckTokenCanActivate] }, { path: 'new-post', loadChildren: () => import('./new-post/new-post.module').then(m => m.NewPostModule), canActivate: [CheckTokenCanActivate] },
{ path: 'search', loadChildren: () => import('./search/search.module').then(m => m.SearchModule), canActivate: [CheckTokenCanActivate] }, { path: '', redirectTo: 'feed', pathMatch: 'full' },
{ path: '', redirectTo: 'auth/login', pathMatch: 'full' }, { path: 'search', loadChildren: () => import('./search/search.module').then(m => m.SearchModule) },
{ path: '**', redirectTo: 'feed', pathMatch: 'full' },
]; ];
@NgModule({ @NgModule({

View File

@ -4,7 +4,6 @@ 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({
@ -54,50 +53,50 @@ export class PostService {
}); });
} }
getPosts(username: String | undefined): Promise<Array<Post>> { getPosts(username: String, lenght: Number, start: Number) {
return new Promise<Array<Post>>(async (resolve, reject) => { return new Promise<Array<Post>>((resolve, reject) => {
if (this.auth.checkAllowed()) {
if (!this.auth.checkAllowed())
reject()
let result: Array<Post> = new Array<Post>;
if (username == undefined) { if (username == undefined) {
// /user -> currentUser this.auth.getUser().subscribe(cred => {
let user = await this.usersvc.getCurrentUser(); // get the user by id
let userRef = this.afs.doc('/user/' + user.username).ref; this.afs.collection<User>('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => {
let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef)).valueChanges())); // get their posts
posts.forEach(async (post) => { this.afs.collection<Post>('post', ref => ref.where('user', '==', this.afs.doc('/user/' + user[0].username).ref)).valueChanges({ idField: 'id' }).subscribe(posts => {
let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges()); posts.forEach(post => {
result.push({ // get how many likes it has
user: user, this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges().subscribe(likes => {
content: post.content, // check if we liked this post
image: post.image, post.liked = likes.filter(like => (like as any).user.id == user[0].username).length != 0;
likes: likes.length, post.user = user[0] as User;
liked: likes.filter(like => (like as any).user.id == user.username).length != 0, post.likes = likes.length;
id: post.id
}) })
resolve(posts)
}) })
resolve(result); });
})
});
} else { } else {
// /user/name this.usersvc.getCurrentUser()
let user = await this.usersvc.getUser(username); .then(currentUser => {
let userRef = this.afs.doc('/user/' + user.username).ref; // get the user by username
let currentUser = await this.usersvc.getCurrentUser(); this.afs.collection<User>('user').doc(username as string).valueChanges().subscribe(user => {
let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef)).valueChanges())); // get their posts
posts.forEach(async (post) => { this.afs.collection<Post>('post', ref => ref.where('user', '==', this.afs.doc('/user/' + username).ref)).valueChanges({ idField: 'id' }).subscribe(posts => {
let likes = await firstValueFrom(this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges()); posts.forEach(post => {
result.push({ // get how many likes it has
user: user, this.afs.collection('like', ref => ref.where('post', '==', this.afs.doc('/post/' + post.id).ref)).valueChanges().subscribe(likes => {
content: post.content, post.liked = likes.filter(like => (like as any).user.id == currentUser.username).length != 0;
image: post.image, post.user = user as User;
likes: likes.length, post.likes = likes.length;
liked: likes.filter(like => (like as any).user.id == currentUser.username).length != 0,
id: post.id
}) })
resolve(posts)
}) })
resolve(result); });
});
})
}
} else {
reject()
} }
}); });
} }

View File

@ -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').doc(username as string).valueChanges().subscribe(data => { resolve(data as User) }); this.afs.collection<User>('user', ref => ref.where('username', '==', username)).valueChanges().subscribe(data => { resolve(data[0] 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]) })

View File

@ -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 userSv: UserService, private postSv: PostService) { } constructor(private auth: AuthService, private postSv: PostService) { }
ngOnInit(): void { ngOnInit(): void {
this.userSv.getCurrentUser().then(user => { this.auth.getUser().subscribe(user => {
this.isOwned = user?.username == this.post?.user.username; this.isOwned = user?.email == this.post?.user.email;
}); });
} }

View File

@ -16,8 +16,13 @@ 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) { }
async ngOnInit(): Promise<void> { ngOnInit(): void {
this.user = await this.userService.getUser(this.route.snapshot.params['username']); this.userService.getUser(this.route.snapshot.params['username'])
this.posts = await this.postService.getPosts(this.route.snapshot.params['username']); .then(user => { this.user = user })
.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'); });
} }
} }