order posts by publishing date

This commit is contained in:
Benedek László 2024-05-10 12:40:59 +02:00
parent dfe4b1b00d
commit d3246d082c

View File

@ -20,7 +20,7 @@ export class PostService {
let result: Array<Post> = new Array<Post>(); let result: Array<Post> = new Array<Post>();
let currentUser = await this.usersvc.getCurrentUser(); let currentUser = await this.usersvc.getCurrentUser();
let posts = await firstValueFrom(this.afs.collection('post').valueChanges({ idField: 'id' })); let posts = await firstValueFrom(this.afs.collection('post', ref => ref.orderBy('date', 'desc')).valueChanges({ idField: 'id' }));
posts.forEach(async post=>{ posts.forEach(async post=>{
let user = await firstValueFrom(this.afs.doc((post as any).user).valueChanges()) as User; let user = await firstValueFrom(this.afs.doc((post as any).user).valueChanges()) as User;
@ -51,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({ idField: 'id' }))); let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef).orderBy('date', 'desc')).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({
@ -70,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({ idField: 'id' }))); let posts = (await firstValueFrom(this.afs.collection<Post>('post', ref => ref.where('user', '==', userRef).orderBy('date', 'desc')).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({
@ -96,7 +96,8 @@ export class PostService {
this.afs.collection('post').add({ this.afs.collection('post').add({
user: this.afs.doc('/user/' + user.username).ref, user: this.afs.doc('/user/' + user.username).ref,
content: content, content: content,
image: image image: image,
date: Date.now()
}) })
.then(() => resolve()) .then(() => resolve())
.catch(() => reject()) .catch(() => reject())