Skip to content

Commit

Permalink
feat: save posts to pocketbase
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Mar 21, 2024
1 parent 8f1afc3 commit e2e5499
Show file tree
Hide file tree
Showing 18 changed files with 477 additions and 170 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ APP_DOMAIN=r34.app

API_URL=https://api.r34.app

POCKETBASE_URL=https://pocketbase.r34.app

MATOMO_API_KEY=

SENTRY_DSN=
Expand Down
118 changes: 118 additions & 0 deletions assets/js/pocketbase.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import type Post from "./post.dto"

export interface IPocketbasePost {

id?: string

// Relations
user_id: string

// Default fields
created?: string
updated?: string


// Fields
original_id: number
original_domain: string

high_res_file: string
high_res_file_width?: number
high_res_file_height?: number
low_res_file?: string
low_res_file_width?: number
low_res_file_height?: number
preview_file: string
preview_file_width?: number
preview_file_height?: number

tags_artist?: string[]
tags_character?: string[]
tags_copyright?: string[]
tags_general?: string[]
tags_meta?: string[]

score?: number

sources: string[]

rating: string

media_type: 'image' | 'video'
}

export class PocketbasePostDTO implements IPocketbasePost {
id: IPocketbasePost['id'] = undefined
user_id: IPocketbasePost['user_id'] = ''
created: IPocketbasePost['created'] = undefined
updated: IPocketbasePost['updated'] = undefined
original_id: IPocketbasePost['original_id'] = 0
original_domain: IPocketbasePost['original_domain'] = ''
high_res_file: IPocketbasePost['high_res_file'] = ''
high_res_file_width: IPocketbasePost['high_res_file_width'] = undefined
high_res_file_height: IPocketbasePost['high_res_file_height'] = undefined
low_res_file: IPocketbasePost['low_res_file'] = undefined
low_res_file_width: IPocketbasePost['low_res_file_width'] = undefined
low_res_file_height: IPocketbasePost['low_res_file_height'] = undefined
preview_file: IPocketbasePost['preview_file'] = ''
preview_file_width: IPocketbasePost['preview_file_width'] = undefined
preview_file_height: IPocketbasePost['preview_file_height'] = undefined
tags_artist: IPocketbasePost['tags_artist'] = []
tags_character: IPocketbasePost['tags_character'] = []
tags_copyright: IPocketbasePost['tags_copyright'] = []
tags_general: IPocketbasePost['tags_general'] = []
tags_meta: IPocketbasePost['tags_meta'] = []
score: IPocketbasePost['score'] = undefined
sources: IPocketbasePost['sources'] = []
rating: IPocketbasePost['rating'] = 'unknown'
media_type: IPocketbasePost['media_type'] = 'image'
}

export class PocketbasePost extends PocketbasePostDTO {
constructor(dto: PocketbasePostDTO) {
super()
Object.assign(this, dto)
}

static fromPost(post: Post, user_id: string): PocketbasePost {

return new PocketbasePost({
original_id: post.id,

original_domain: post.domain,

high_res_file: post.high_res_file.url,
high_res_file_width: post.high_res_file.width,
high_res_file_height: post.high_res_file.height,

low_res_file: post.low_res_file.url,
low_res_file_width: post.low_res_file.width,
low_res_file_height: post.low_res_file.height,

preview_file: post.preview_file.url,
preview_file_width: post.preview_file.width,
preview_file_height: post.preview_file.height,

tags_artist: post.tags.artist.length > 0 ? post.tags.artist : undefined,
tags_character: post.tags.character.length > 0 ? post.tags.character : undefined,
tags_copyright: post.tags.copyright.length > 0 ? post.tags.copyright : undefined,
tags_general: post.tags.general.length > 0 ? post.tags.general : undefined,
tags_meta: post.tags.meta.length > 0 ? post.tags.meta : undefined,

tags: [
...post.tags.artist.map((tag) => ({ name: tag, type: 'artist' })),
...post.tags.character.map((tag) => ({ name: tag, type: 'character' })),
...post.tags.copyright.map((tag) => ({ name: tag, type: 'copyright' })),
...post.tags.general.map((tag) => ({ name: tag, type: 'general' })),
...post.tags.meta.map((tag) => ({ name: tag, type: 'meta' }))
],

score: post.score,
sources: post.sources,
rating: post.rating,
media_type: post.media_type,

user_id
})
}
}
65 changes: 0 additions & 65 deletions assets/js/post.d.ts

This file was deleted.

151 changes: 151 additions & 0 deletions assets/js/post.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import type { IPocketbasePost } from "./pocketbase.dto"

export interface IPostPage {
data: IPost[]
meta: IPostPageMeta
links: IPostPageLinks
}

export interface IPost {

domain: string

id: number

score?: number | null

high_res_file: IPostHighResFile
low_res_file: IPostLowResFile
preview_file: IPostPreviewFile

tags: IPostTags

sources: string[]

rating: string

media_type: 'image' | 'video' | 'unknown'
}

export interface IPostHighResFile {
url: string
width: number
height: number
}

export interface IPostLowResFile {
url: string | null
width: number | null
height: number | null
}

export interface IPostPreviewFile {
url: string | null
width: number | null
height: number | null
}

export interface IPostTags {
artist: string[]
character: string[]
copyright: string[]
general: string[]
meta: string[]
}

export interface IPostPageMeta {
items_count: number
total_items: number
current_page: number
total_pages: number
items_per_page: number
}

export interface IPostPageLinks {
self: string
first: string
last: string
prev: string
next: string
}

export class PostDTO implements IPost {
domain: IPost['domain'] = ''

id: IPost['id'] = 0

score?: IPost['score']

high_res_file: IPost['high_res_file'] = {
url: '',
width: 0,
height: 0,
}
low_res_file: IPost['low_res_file'] = {
url: null,
width: null,
height: null,
}
preview_file: IPost['preview_file'] = {
url: null,
width: null,
height: null,
}

tags: IPost['tags'] = {
artist: [],
character: [],
copyright: [],
general: [],
meta: [],
}

sources: IPost['sources'] = []

rating: IPost['rating'] = 'unknown'

media_type: IPost['media_type'] = 'unknown'
}

export default class Post extends PostDTO {
constructor(dto: PostDTO) {
super()
Object.assign(this, dto)
}

static fromPocketbasePost(post: IPocketbasePost): Post {

return new Post({
id: post.original_id,

domain: post.original_domain,

high_res_file: {
url: post.high_res_file,
width: post.high_res_file_width,
height: post.high_res_file_height
},
low_res_file: {
url: post.low_res_file,
width: post.low_res_file_width,
height: post.low_res_file_height
},
preview_file: {
url: post.preview_file,
width: post.preview_file_width,
height: post.preview_file_height
},
tags: {
artist: post.tags_artist ?? [],
character: post.tags_character ?? [],
copyright: post.tags_copyright ?? [],
general: post.tags_general ?? [],
meta: post.tags_meta ?? []
},
score: post.score,
sources: post.sources,
rating: post.rating,
media_type: post.media_type
})
}
}
2 changes: 1 addition & 1 deletion assets/js/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IPostTags } from "./post"
import type { IPostTags } from "./post.dto"

export interface ITag {
name: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<script lang="ts" setup>
import { ChevronDownIcon } from '@heroicons/vue/24/outline'
import type { IPost } from '~/assets/js/post'
import type { IPost } from '~/assets/js/post.dto'
import Tag from '~/assets/js/tag.dto'
import { useUserSettings } from '~/composables/useUserSettings'
const props = defineProps<{
domain: string
post: IPost
selectedTags: Tag[]
Expand Down Expand Up @@ -91,13 +89,12 @@
<div class="flex items-center p-2">
<PostSave
v-if="mediaFile.file"
:domain="domain"
:post="post"
/>

<PostDownload
v-if="mediaFile.file"
:mediaName="`${domain}-${post.id}`"
:mediaName="`${post.domain}-${post.id}`"
:mediaUrl="mediaFile.file"
/>

Expand Down
Loading

0 comments on commit e2e5499

Please sign in to comment.