jbilcke-hf HF staff commited on
Commit
955e8e9
·
1 Parent(s): 478b897

add a visibility filtering system

Browse files
Files changed (3) hide show
  1. src/core/getAppPosts.mts +7 -3
  2. src/index.mts +6 -4
  3. src/types.mts +5 -0
src/core/getAppPosts.mts CHANGED
@@ -1,10 +1,14 @@
1
  import { postDirFilePath } from "../config.mts"
2
- import { Post } from "../types.mts"
3
 
4
  import { readPostFiles } from "./readPostFiles.mts"
5
 
6
- export const getAppPosts = async (appId: string): Promise<Post[]> => {
7
  const posts = await readPostFiles(postDirFilePath, appId)
8
 
9
- return posts
 
 
 
 
10
  }
 
1
  import { postDirFilePath } from "../config.mts"
2
+ import { Post, PostVisibility } from "../types.mts"
3
 
4
  import { readPostFiles } from "./readPostFiles.mts"
5
 
6
+ export const getAppPosts = async (appId: string, visibility?: PostVisibility): Promise<Post[]> => {
7
  const posts = await readPostFiles(postDirFilePath, appId)
8
 
9
+ if (visibility) {
10
+ return posts.filter(post => post.visibility === visibility)
11
+ } else {
12
+ return posts
13
+ }
14
  }
src/index.mts CHANGED
@@ -5,7 +5,7 @@ import { v4 as uuidv4 } from "uuid"
5
  import { hasValidAuthorization } from "./utils/hasValidAuthorization.mts"
6
  import { initFolders } from "./initFolders.mts"
7
  import { getValidNumber } from "./utils/getValidNumber.mts"
8
- import { CreatePostResponse, GetAppPostsResponse, Post } from "./types.mts"
9
  import { savePost } from "./core/savePost.mts"
10
  import { getAppPosts } from "./core/getAppPosts.mts"
11
  import { deletePost } from "./core/deletePost.mts"
@@ -38,6 +38,7 @@ app.post("/post", async (req, res) => {
38
  const assetUrl = `${req.body.assetUrl || ""}`
39
  const previewUrl = `${req.body.previewUrl || assetUrl}`
40
  const createdAt = `${req.body.createdAt || new Date().toISOString()}`
 
41
  const upvotes = getValidNumber(req.body.upvotes, 0, 1e15, 0)
42
  const downvotes = getValidNumber(req.body.upvotes, 0, 1e15, 0)
43
 
@@ -88,6 +89,7 @@ app.post("/post", async (req, res) => {
88
  previewUrl,
89
  assetUrl,
90
  createdAt,
 
91
  upvotes,
92
  downvotes,
93
  }
@@ -108,7 +110,7 @@ app.post("/post", async (req, res) => {
108
  res.end()
109
  })
110
 
111
- app.get("/posts/:appId", async (req, res) => {
112
 
113
  const appId = `${req.params.appId}`
114
 
@@ -121,7 +123,7 @@ app.get("/posts/:appId", async (req, res) => {
121
  }
122
 
123
  try {
124
- const posts = await getAppPosts(appId)
125
  res.status(200)
126
  console.log(`returning ${posts.length} community posts for app ${appId}`)
127
  res.write(JSON.stringify({ posts } as GetAppPostsResponse))
@@ -137,7 +139,7 @@ app.get("/posts/:appId", async (req, res) => {
137
  }
138
  })
139
 
140
- // get metadata (json)
141
  app.delete("/posts/:appId/:postId", async (req, res) => {
142
 
143
  if (!hasValidAuthorization(req.headers)) {
 
5
  import { hasValidAuthorization } from "./utils/hasValidAuthorization.mts"
6
  import { initFolders } from "./initFolders.mts"
7
  import { getValidNumber } from "./utils/getValidNumber.mts"
8
+ import { CreatePostResponse, GetAppPostsResponse, Post, PostVisibility } from "./types.mts"
9
  import { savePost } from "./core/savePost.mts"
10
  import { getAppPosts } from "./core/getAppPosts.mts"
11
  import { deletePost } from "./core/deletePost.mts"
 
38
  const assetUrl = `${req.body.assetUrl || ""}`
39
  const previewUrl = `${req.body.previewUrl || assetUrl}`
40
  const createdAt = `${req.body.createdAt || new Date().toISOString()}`
41
+ const visibility = `${req.body.visibility || "normal"}` as PostVisibility
42
  const upvotes = getValidNumber(req.body.upvotes, 0, 1e15, 0)
43
  const downvotes = getValidNumber(req.body.upvotes, 0, 1e15, 0)
44
 
 
89
  previewUrl,
90
  assetUrl,
91
  createdAt,
92
+ visibility,
93
  upvotes,
94
  downvotes,
95
  }
 
110
  res.end()
111
  })
112
 
113
+ app.get("/posts/:appId/:visibility", async (req, res) => {
114
 
115
  const appId = `${req.params.appId}`
116
 
 
123
  }
124
 
125
  try {
126
+ const posts = await getAppPosts(appId, visibility)
127
  res.status(200)
128
  console.log(`returning ${posts.length} community posts for app ${appId}`)
129
  res.write(JSON.stringify({ posts } as GetAppPostsResponse))
 
139
  }
140
  })
141
 
142
+ // delete a post
143
  app.delete("/posts/:appId/:postId", async (req, res) => {
144
 
145
  if (!hasValidAuthorization(req.headers)) {
src/types.mts CHANGED
@@ -1,4 +1,8 @@
1
 
 
 
 
 
2
 
3
  export type Post = {
4
  postId: string
@@ -7,6 +11,7 @@ export type Post = {
7
  previewUrl: string
8
  assetUrl: string
9
  createdAt: string
 
10
  upvotes: number
11
  downvotes: number
12
  }
 
1
 
2
+ export type PostVisibility =
3
+ | "featured" // featured by admins
4
+ | "trending" // top trending / received more than 10 upvotes
5
+ | "normal" // default visibility
6
 
7
  export type Post = {
8
  postId: string
 
11
  previewUrl: string
12
  assetUrl: string
13
  createdAt: string
14
+ visibility: PostVisibility
15
  upvotes: number
16
  downvotes: number
17
  }