Commit
•
2898d65
1
Parent(s):
827f345
add array shuffle
Browse files
src/app/server/actions/community.ts
CHANGED
@@ -4,6 +4,7 @@ import { v4 as uuidv4 } from "uuid"
|
|
4 |
|
5 |
import { CreatePostResponse, GetAppPostResponse, GetAppPostsResponse, Post, PostVisibility } from "@/types"
|
6 |
import { filterOutBadWords } from "./censorship"
|
|
|
7 |
|
8 |
const apiUrl = `${process.env.COMMUNITY_API_URL || ""}`
|
9 |
const apiToken = `${process.env.COMMUNITY_API_TOKEN || ""}`
|
@@ -96,10 +97,12 @@ export async function postToCommunity({
|
|
96 |
|
97 |
export async function getLatestPosts({
|
98 |
visibility,
|
99 |
-
maxNbPosts = 1000
|
|
|
100 |
}: {
|
101 |
visibility?: PostVisibility
|
102 |
maxNbPosts?: number
|
|
|
103 |
}): Promise<Post[]> {
|
104 |
|
105 |
let posts: Post[] = []
|
@@ -141,7 +144,13 @@ export async function getLatestPosts({
|
|
141 |
// console.log("response:", response)
|
142 |
|
143 |
const posts: Post[] = Array.isArray(response?.posts) ? response?.posts : []
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
return posts.slice(0, maxNbPosts)
|
146 |
} catch (err) {
|
147 |
// const error = `failed to get posts: ${err}`
|
|
|
4 |
|
5 |
import { CreatePostResponse, GetAppPostResponse, GetAppPostsResponse, Post, PostVisibility } from "@/types"
|
6 |
import { filterOutBadWords } from "./censorship"
|
7 |
+
import { shuffleArray } from "../utils/shuffleArray"
|
8 |
|
9 |
const apiUrl = `${process.env.COMMUNITY_API_URL || ""}`
|
10 |
const apiToken = `${process.env.COMMUNITY_API_TOKEN || ""}`
|
|
|
97 |
|
98 |
export async function getLatestPosts({
|
99 |
visibility,
|
100 |
+
maxNbPosts = 1000,
|
101 |
+
shuffle = true,
|
102 |
}: {
|
103 |
visibility?: PostVisibility
|
104 |
maxNbPosts?: number
|
105 |
+
shuffle?: boolean
|
106 |
}): Promise<Post[]> {
|
107 |
|
108 |
let posts: Post[] = []
|
|
|
144 |
// console.log("response:", response)
|
145 |
|
146 |
const posts: Post[] = Array.isArray(response?.posts) ? response?.posts : []
|
147 |
+
|
148 |
+
if (shuffle) {
|
149 |
+
shuffleArray(posts)
|
150 |
+
} else {
|
151 |
+
posts.sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt))
|
152 |
+
}
|
153 |
+
|
154 |
return posts.slice(0, maxNbPosts)
|
155 |
} catch (err) {
|
156 |
// const error = `failed to get posts: ${err}`
|
src/app/server/utils/shuffleArray.ts
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export const shuffleArray = (items: any[]) => {
|
2 |
+
for (let i = items.length - 1; i > 0; i--) {
|
3 |
+
const j = Math.floor(Math.random() * (i + 1));
|
4 |
+
const temp = items[i];
|
5 |
+
items[i] = items[j];
|
6 |
+
items[j] = temp;
|
7 |
+
}
|
8 |
+
}
|