Skip to content

Commit

Permalink
feat: remove posts with unknown media
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Feb 19, 2024
1 parent 6a6181e commit e00fc62
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 70 deletions.
17 changes: 8 additions & 9 deletions components/pages/posts/post/Post.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script lang="ts" setup>
import { useUserSettings } from '~/composables/useUserSettings'
import { ChevronDownIcon } from '@heroicons/vue/24/outline'
import {useUserSettings} from '~/composables/useUserSettings'
import {ChevronDownIcon} from '@heroicons/vue/24/outline'
import { vOnLongPress } from '@vueuse/components'
import Tag from '~/assets/js/tag.dto'
import type { IPost } from '~/assets/js/post'
import { toast } from 'vue-sonner'
import { useAppStatistics } from '~/composables/useAppStatistics'
import {vOnLongPress} from '@vueuse/components'
import Tag from '~/assets/js/tag.dto'
import type {IPost} from '~/assets/js/post'
import {toast} from 'vue-sonner'
import {useAppStatistics} from '~/composables/useAppStatistics'
const props = defineProps<{
const props = defineProps<{
domain: string
post: IPost
Expand Down Expand Up @@ -138,7 +138,6 @@
/>

<PostSource
v-if="post.media_type !== 'unknown'"
:post-file-url="post.media_type === 'image' ? mediaFile.file : mediaFile.posterFile"
:post-sources="post.sources"
/>
Expand Down
13 changes: 4 additions & 9 deletions components/pages/posts/post/PostMedia.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts" setup>
import { vIntersectionObserver } from '@vueuse/components'
import type { IPost } from 'assets/js/post'
import {vIntersectionObserver} from '@vueuse/components'
import type {IPost} from 'assets/js/post'
const { isPremium } = useUserData()
const { isPremium } = useUserData()
export interface PostMediaProps {
mediaSrc: string | null
Expand All @@ -27,10 +27,6 @@
const triedToLoadWithProxy = shallowRef(false)
if (props.mediaType === 'unknown') {
error.value = new Error('Unknown media type')
}
function onMediaError(event: Event) {
if (hasError.value) {
return
Expand Down Expand Up @@ -122,7 +118,6 @@
</span>

<button
v-if="error?.message !== 'Unknown media type'"
class="focus-visible:focus-outline-util hover:hover-bg-util hover:hover-text-util mx-auto inline-flex items-center justify-center rounded-md px-2 py-1 text-sm ring-1 ring-base-0/20"
type="button"
@click="manuallyReloadMedia"
Expand All @@ -134,7 +129,7 @@
<!-- Premium promotion -->
<!-- TODO: Improve style -->
<div
v-if="!isPremium && error?.message !== 'Unknown media type'"
v-if="!isPremium"
class="text-xs text-base-content"
>
<NuxtLink
Expand Down
44 changes: 28 additions & 16 deletions pages/posts/[domain].vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<script lang="ts" setup>
import { useBooruList } from '~/composables/useBooruList'
import { ArrowPathIcon, ExclamationCircleIcon, QuestionMarkCircleIcon } from '@heroicons/vue/24/solid'
import { MagnifyingGlassIcon } from '@heroicons/vue/24/outline'
import { toast } from 'vue-sonner'
import Tag from '~/assets/js/tag.dto'
import type { Ref } from 'vue'
import { generatePostsRoute } from '~/assets/js/RouterHelper'
import { tagArrayToTitle } from '~/assets/js/SeoHelper'
import { capitalize } from 'lodash-es'
import type { Domain } from '~/assets/js/domain'
import type { IPostPage } from '~/assets/js/post'
import { useInfiniteQuery } from '@tanstack/vue-query'
import { FetchError } from 'ofetch'
import * as Sentry from '@sentry/vue'
const router = useRouter()
import {useBooruList} from '~/composables/useBooruList'
import {ArrowPathIcon, ExclamationCircleIcon, QuestionMarkCircleIcon} from '@heroicons/vue/24/solid'
import {MagnifyingGlassIcon} from '@heroicons/vue/24/outline'
import {toast} from 'vue-sonner'
import Tag from '~/assets/js/tag.dto'
import type {Ref} from 'vue'
import {generatePostsRoute} from '~/assets/js/RouterHelper'
import {tagArrayToTitle} from '~/assets/js/SeoHelper'
import {capitalize} from 'lodash-es'
import type {Domain} from '~/assets/js/domain'
import type {IPostPage} from '~/assets/js/post'
import {useInfiniteQuery} from '@tanstack/vue-query'
import {FetchError} from 'ofetch'
import * as Sentry from '@sentry/vue'
const router = useRouter()
const route = useRoute()
const config = useRuntimeConfig()
const $authState = useState('auth-internal')
Expand Down Expand Up @@ -149,6 +149,18 @@
} = useInfiniteQuery({
queryKey: ['posts', selectedBooru, selectedTags, selectedFilters],
queryFn: fetchPosts,
select: (data) => {
// Delete all posts that have `media_type: 'unknown'`
data.pages.forEach((page) => {
page.data = page.data.filter((post) => post.media_type !== 'unknown')
})
return {
pages: data.pages,
pageParams: data.pageParams,
}
},
initialPageParam: '',
getNextPageParam: (lastPage, allPages, lastPageParam) => {
if (lastPage.links.next == null) {
Expand Down
41 changes: 5 additions & 36 deletions test/pages/posts.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { describe, expect, it } from 'vitest'
import { createPage, setup, url } from '@nuxt/test-utils'
import {describe, expect, it} from 'vitest'
import {createPage, setup, url} from '@nuxt/test-utils'
import {
mockPostsPage0,
mockPostsPage1,
mockPostsPageWithOfflineMedia,
mockPostsPageWithoutResults,
mockPostsPageWithUnknownMedia
mockPostsPageWithoutResults
} from './posts.mock-data'
import { defaultSetupConfig } from '../helper'
import {defaultSetupConfig} from '../helper'

describe('/', async () => {
await setup(defaultSetupConfig)
Expand Down Expand Up @@ -122,37 +121,7 @@ describe('/', async () => {
).toBe(mockPostsPage0.data[0].tags.general.length)
})

it('renders warning when unknown media is loaded', async () => {
// Arrange
const page = await createPage()

await page.route(
'**/posts?baseEndpoint=*',
(route) =>
route.fulfill({
status: 200,
json: mockPostsPageWithUnknownMedia
}),
{ times: 1 }
)

// Act
await Promise.all([page.goto(url('/posts/safebooru.org')), page.waitForResponse('**/posts?baseEndpoint=*')])

// Assert

// Expect 1 post
expect(
//
await page.getByTestId('posts-list').locator('li').count()
).toBe(1)

// Expect post to have warning
expect(
//
await page.getByTestId('posts-list').locator('li').first().textContent()
).toContain('Unknown media')
})
// TODO: Test that verifies if a post with 'unknown' media type is not rendered

it.skip('proxies media when media failed to load', async () => {
throw new Error('Not implemented')
Expand Down

0 comments on commit e00fc62

Please sign in to comment.