Skip to content

Commit

Permalink
Add view count to popular
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasamato committed Dec 30, 2023
1 parent f8452dd commit d63e504
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/db/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function userExists(uid: string): Promise<boolean> {
)
}

async function gatherUserInfo(user: User): Promise<User> {
export async function gatherUserInfo(user: User): Promise<User> {
if (user === null || typeof user === 'undefined') {
console.warn(
'Well... gathering user data of',
Expand Down
22 changes: 12 additions & 10 deletions lib/db/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ export async function getLastViews(type: Types, n: number) {
// resolve content info
return (
await Promise.all(
sorted.map(
async (s) =>
await findOne(
singularToPlural(type),
type === 'user' ? { uid: s.contentId } : { _id: s.contentId }
)
)
sorted.map(async (s) => {
const data = await findOne(
singularToPlural(type),
type === Types.user ? { uid: s.contentId } : { _id: s.contentId }
)

data.views = s.count
return data
})
)
).filter((s) => s !== null && typeof s !== 'undefined')
}
Expand Down Expand Up @@ -89,14 +91,14 @@ export async function addView({ uid, type, contentId }) {
})
}

export async function deleteView(_id) {
export async function deleteView(_id: string) {
return await deleteOne('views', { _id })
}

export async function countViews() {
return await count('views')
}

export async function countViewsOfContent(type, contentId) {
return await count('users', { type, contentId })
export async function countViewsOfContent(type: Types, _id: string) {
return await count('views', { type, _id })
}
1 change: 1 addition & 0 deletions types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum AccountType {
admin = 'admin',
editor = 'editor',
user = 'user',
views: number
}

export interface UserUpdate {
Expand Down

0 comments on commit d63e504

Please sign in to comment.