Skip to content

Commit

Permalink
Merge pull request #84 from Snaacky/dev
Browse files Browse the repository at this point in the history
Display view count in admin panel
  • Loading branch information
Yasamato authored Dec 30, 2023
2 parents 689c18b + 23975ad commit 6b20189
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Website](https://img.shields.io/website?down_message=offline&label=theindex.moe&up_message=online&url=https%3A%2F%2Ftheindex.moe)](https://theindex.moe)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Snaacky/theindex/Docker?logo=github)](https://github.com/Snaacky/theindex)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/Snaacky/theindex/publish-docker-image.yml?logo=github)](https://github.com/Snaacky/theindex)
[![CodeFactor](https://www.codefactor.io/repository/github/snaacky/theindex/badge)](https://www.codefactor.io/repository/github/snaacky/theindex)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/a91c8d3ec420474cbac1a524073c83ee)](https://www.codacy.com/gh/Snaacky/theindex/dashboard)
[![Discord](https://img.shields.io/discord/974468300304171038?label=Discord&logo=discord)](https://discord.gg/snackbox)
Expand Down Expand Up @@ -94,7 +94,6 @@ Here is a collection of the possible environment variables with their default va
| `CACHE_URL` | Connection string for the redis cache database | `"redis://redis:6379"` |
| `CHROME_URL` | WebSocket URL to a running chrome instance | `"ws://chrome:3300"` |
| `AUDIT_WEBHOOK` | WebHook-URL for audit-log, leave empty to disable support | `""` |
| `SOCKS_PROXY` | Connection string for SOCKS5 proxy to use for pinging | `""` |
| `DISCORD_CLIENT_ID` | Discord OAuth2 client ID | `"your_discord_oauth_client_id"` |
| `DISCORD_CLIENT_SECRET` | Discord OAuth2 client secret | `"your_discord_oauth_client_secret"` |
| `DISCORD_BOT_TOKEN` | Required to access BOT resources | `"your_discord_bot_token"` |
Expand Down Expand Up @@ -265,7 +264,6 @@ our ideas, and we find some time, we will certainly implement your requested fea
- We bake everything into a [docker image](https://www.docker.com/)
- Running on [node js](https://nodejs.org/)
- User-authentication via [NextAuth.js](https://next-auth.js.org/)
- PWA-support with [next-pwa](https://www.npmjs.com/package/next-pwa)
- [mongodb](https://www.mongodb.com) as database backend
- [redis](https://redis.io) as cache
- [puppeteer](https://github.com/puppeteer/puppeteer) to create screenshots of websites
Expand Down
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 })
}
3 changes: 3 additions & 0 deletions pages/admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ const Admin = ({
<div>
{popular.map((item, i) => <div key={i + '_' + item.name}>
{item.name} <DataBadge name={'#' + i} />
<div>
{item.views} Views
</div>
</div>)}
</div>
</>
Expand Down
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 6b20189

Please sign in to comment.