Skip to content

Commit 643ee79

Browse files
committed
update firebase to always show posts from all featured users
sort is shuffled too
1 parent b9e7c73 commit 643ee79

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

lib/Backend/firebase.dart

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:io';
22

3+
import 'package:collection/collection.dart';
34
import 'package:firebase_app_installations/firebase_app_installations.dart';
45
import 'package:firebase_core/firebase_core.dart';
56
import 'package:firebase_messaging/firebase_messaging.dart';
@@ -26,14 +27,7 @@ Future<void> initCosHubFirebase(Ref ref) async {
2627

2728
@freezed
2829
abstract class CosHubPost with _$CosHubPost {
29-
const factory CosHubPost({
30-
required String id,
31-
required String url,
32-
String? character,
33-
required String thumbnailUrl,
34-
String? profileThumbnailUrl,
35-
required String username,
36-
}) = _CosHubPost;
30+
const factory CosHubPost({required String id, required String url, String? character, required String thumbnailUrl, String? profileThumbnailUrl, required String username, required DateTime timestamp}) = _CosHubPost;
3731

3832
factory CosHubPost.fromJson(Map<String, dynamic> json) => _$CosHubPostFromJson(json);
3933
}
@@ -46,38 +40,44 @@ Future<List<CosHubPost>> getCosHubPosts(Ref ref) async {
4640
final appConstants = firestore.collection("appConstants");
4741
DocumentSnapshot<Map<String, dynamic>> featuredCosplayersUserIdsQuery = await appConstants.doc("featured_cosplayers").get();
4842
List<dynamic> featuredCosplayersUserIds = featuredCosplayersUserIdsQuery.data()!["user"] as List<dynamic>;
49-
final QuerySnapshot<Map<String, dynamic>> postsQuery = await firestore.collection("posts").where("userId", whereIn: featuredCosplayersUserIds).orderBy("createdAt", descending: true).limit(10).get();
5043
final QuerySnapshot<Map<String, dynamic>> usersQuery = await firestore.collection("users").where("id", whereIn: featuredCosplayersUserIds).get();
5144

45+
List<CosHubPost> mappedPosts = [];
5246

5347
final String cosHubUrl = (await getDynamicConfigInfo()).urls.coshubUrl;
54-
List<CosHubPost> mappedPosts = postsQuery.docs
55-
.map(
56-
(e) => e.data(),
57-
)
58-
.where(
59-
(element) {
60-
List<dynamic>? postImageUrls = element["postImageUrls"];
61-
return postImageUrls != null && postImageUrls.isNotEmpty;
62-
},
63-
).map(
64-
(postData) {
65-
Map<String, dynamic> userData = usersQuery.docs
66-
.firstWhere(
67-
(element) => element.data()["id"] == postData["userId"],
68-
)
69-
.data();
70-
CosHubPost cosHubPost = CosHubPost(
71-
id: postData["id"],
72-
url: cosHubUrl,
73-
thumbnailUrl: postData["postImageUrls"][0],
74-
profileThumbnailUrl: userData["profilePicture"],
75-
username: userData["username"],
76-
character: postData["character"],
77-
);
78-
return cosHubPost;
79-
},
80-
).toList();
48+
49+
// We want to grab a few posts from each featured user
50+
for (String featuredCosplayersUserId in featuredCosplayersUserIds) {
51+
final QuerySnapshot<Map<String, dynamic>> postsQuery = await firestore
52+
.collection("posts")
53+
.where("userId", isEqualTo: featuredCosplayersUserId)
54+
.orderBy("createdAt", descending: true)
55+
.limit(5)
56+
.get();
57+
mappedPosts.addAll(
58+
postsQuery.docs
59+
.map((e) => e.data())
60+
.where((element) {
61+
List<dynamic>? postImageUrls = element["postImageUrls"];
62+
return postImageUrls != null && postImageUrls.isNotEmpty;
63+
})
64+
.map((postData) {
65+
Map<String, dynamic> userData = usersQuery.docs.firstWhere((element) => element.data()["id"] == postData["userId"]).data();
66+
CosHubPost cosHubPost = CosHubPost(
67+
id: postData["id"],
68+
url: cosHubUrl,
69+
thumbnailUrl: postData["postImageUrls"][0],
70+
profileThumbnailUrl: userData["profilePicture"],
71+
username: userData["username"],
72+
character: postData["character"],
73+
timestamp: (postData['createdAt'] as Timestamp).toDate()
74+
);
75+
return cosHubPost;
76+
})
77+
.sortedBy((element) => element.timestamp,).toList(),
78+
);
79+
}
80+
8181
return mappedPosts;
8282
}
8383

@@ -86,9 +86,7 @@ bool didInitFirebase = false;
8686
Future<void> configurePushNotifications() async {
8787
if (!didInitFirebase) {
8888
// Required before doing anything with firebase
89-
await Firebase.initializeApp(
90-
options: DefaultFirebaseOptions.currentPlatform,
91-
);
89+
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
9290
didInitFirebase = true;
9391
}
9492

0 commit comments

Comments
 (0)