traxxx/assets/js/tags/actions.js

359 lines
7.7 KiB
JavaScript
Executable File

import { graphql, get } from '../api';
import {
releaseFields,
batchFragment,
} 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, scenesConnection, batches: [lastBatch] } = await graphql(`
query Tag(
$tagSlug: String!
$offset: Int = 0,
$limit:Int = 1000,
$after:Datetime = "1900-01-01",
$before:Datetime = "2100-01-01",
$orderBy: [ReleasesSummariesOrderBy!],
$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: releasesSummariesConnection(
first: $limit
offset: $offset
orderBy: $orderBy
filter: {
or: [
{
not: { tags: { overlaps: $exclude } }
}
{
tags: { isNull: true }
}
]
tags: { anyEqualTo: $tagSlug }
effectiveDate: { lessThan: $before, greaterThan: $after }
showcased: { equalTo: true }
}
) {
releases: nodes {
release {
${releaseFields}
}
}
totalCount
}
${batchFragment}
}
`, {
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: scenesConnection.releases.map(({ release }) => curateRelease(release, 'scene', { lastBatch: lastBatch.id })),
totalCount: scenesConnection.totalCount,
};
}
async function fetchTags({ _commit }, {
limit = 100,
slugs = [],
_group,
_priority,
}) {
const { tags } = await graphql(`
query Tags(
$slugs: [String!] = [],
$limit: Int = 100
$exclude: [String!]
) {
tags(
filter: {
slug: {
in: $slugs
notIn: $exclude
}
},
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,
exclude: store.state.ui.tagFilter,
});
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: 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
}
}
}
`, {
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;