Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tag permission #263

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions server/src/services/tag.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
import Elysia from "elysia";
import type { DB } from "../_worker";
import { feedHashtags, hashtags } from "../db/schema";
import { getDB } from "../utils/di";
import { setup } from "../setup";

export function TagService() {
const db: DB = getDB();
return new Elysia({ aot: false })
.use(setup())
.group('/tag', (group) =>
group
.get('/', async () => {
Expand All @@ -24,15 +26,19 @@ export function TagService() {
}
})
})
.get('/:name', async ({ set, params: { name } }) => {
.get('/:name', async ({ admin, set, params: { name } }) => {
const nameDecoded = decodeURI(name)
const tag = await db.query.hashtags.findFirst({
where: eq(hashtags.name, nameDecoded),
with: {
feeds: {
with: {
feed: {
columns: { id: true, title: true, summary: true, content: true, createdAt: true, updatedAt: true },
columns: {
id: true, title: true, summary: true, content: true, createdAt: true, updatedAt: true,
draft: false,
listed: false
},
with: {
user: {
columns: { id: true, username: true, avatar: true }
Expand All @@ -45,18 +51,22 @@ export function TagService() {
}
}
}
}
}
},
where: (feeds: any) => admin ? undefined : and(eq(feeds.draft, 0), eq(feeds.listed, 1)),
} as any
}
}
}
});
const tagFeeds = tag?.feeds.map((tag) => {
const tagFeeds = tag?.feeds.map((tag: any) => {
if (!tag.feed) {
return null;
}
return {
...tag.feed,
hashtags: tag.feed.hashtags.map((tag) => tag.hashtag)
hashtags: tag.feed.hashtags.map((tag: any) => tag.hashtag)
}
})
}).filter((feed: any) => feed !== null);
if (!tag) {
set.status = 404;
return 'Not found';
Expand Down