import { graphql, get } from '../api'; import { releaseFields, } from '../fragments'; import { curateTag, curateRelease } from '../curate'; import getDateRange from '../get-date-range'; function initTagsActions(store, _router) { async function fetchTagBySlug({ _commit }, { tagSlug, pageNumber = 1, limit = 100, range = 'latest', }) { const { before, after, orderBy } = getDateRange(range); const { tagBySlug } = await graphql(` query Tag( $tagSlug:String! $offset: Int = 0, $limit:Int = 1000, $after:Datetime = "1900-01-01", $before:Datetime = "2100-01-01", $orderBy: [ReleasesOrderBy!], $exclude: [String!] $hasAuth: Boolean! $userId: Int ) { tagBySlug(slug:$tagSlug) { id name slug description group { name slug } poster: tagsPoster { media { id thumbnail lazy path width height thumbnailWidth thumbnailHeight comment entity { id name slug type independent parent { id name slug type independent } } sfw: sfwMedia { id thumbnail lazy path comment } } } photos: tagsPhotos(orderBy: MEDIA_BY_MEDIA_ID__INDEX_ASC) { media { id thumbnail lazy path width height thumbnailWidth thumbnailHeight comment entity { id name slug type independent parent { id name slug type independent } } sfw: sfwMedia { id thumbnail lazy path comment } } } banners: bannersTags(filter: { banner: { bannersTagsConnection: { none: { tag: { slug: { in: $exclude } } } } } }) { banner { id width height type ratio entity { id type name slug independent parent { id type name slug independent } } campaigns { id url entity { id type name slug parent { id type name slug } } } } } scenesConnection( filter: { date: { lessThan: $before, greaterThan: $after, }, releasesTagsConnection: { none: { tag: { slug: { in: $exclude } } } } }, first: $limit, orderBy: $orderBy, offset: $offset ) { releases: nodes { ${releaseFields} } totalCount } } } `, { tagSlug, limit, after, before, orderBy, offset: Math.max(0, (pageNumber - 1)) * limit, exclude: store.state.ui.tagFilter.filter((tagFilter) => tagFilter !== tagSlug), hasAuth: !!store.state.auth.user, userId: store.state.auth.user?.id, }); return { tag: curateTag(tagBySlug, null, curateRelease), releases: tagBySlug.scenesConnection.releases.map((release) => curateRelease(release)), totalCount: tagBySlug.scenesConnection.totalCount, }; } async function fetchTags({ _commit }, { limit = 100, slugs = [], _group, _priority, }) { const { tags } = await graphql(` query Tags( $slugs: [String!] = [], $limit: Int = 100 ) { tags( filter: { slug: { in: $slugs } }, first: $limit ) { id name slug poster: tagsPoster { media { thumbnail comment lazy width height thumbnailWidth thumbnailHeight entity { id name slug type independent parent { id name slug type independent } } sfw: sfwMedia { thumbnail comment lazy } } } group { name slug } } } `, { slugs, limit, }); return tags.map((tag) => curateTag(tag, store.state.ui.sfw)); } async function searchTags({ _commit }, { limit = 100, minLength = 2, query, _group, _priority, }) { const { tags } = await graphql(` query SearchTags( $query: String!, $limit: Int = 100 $minLength: Int = 2 ) { tags: searchTags( query: $query, first: $limit minLength: $minLength ) { id name slug poster: tagsPosterByTagId { media { thumbnail comment lazy width height thumbnailWidth thumbnailHeight entity { id name slug type independent parent { id name slug type independent } } sfw: sfwMedia { thumbnail comment lazy } } } group { name slug } } } `, { query, limit, minLength, }); return tags.map((tag) => curateTag(tag, store.state.ui.sfw)); } async function fetchTagReleases({ _commit }, tagId) { const releases = await get(`/tags/${tagId}/releases`, { filter: store.state.ui.filter, after: store.getters.after, before: store.getters.before, }); return releases; } return { fetchTagBySlug, fetchTags, fetchTagReleases, searchTags, }; } export default initTagsActions;