fix: select readable_content from library_item table if content is requested by update since api

This commit is contained in:
Hongbo Wu
2024-08-19 10:00:38 +08:00
parent fc00c607e6
commit 626c456364
2 changed files with 85 additions and 61 deletions

View File

@ -99,7 +99,7 @@ import { Merge } from '../../util'
import { analytics } from '../../utils/analytics'
import { isSiteBlockedForParse } from '../../utils/blocked'
import { enqueueBulkAction } from '../../utils/createTask'
import { authorized } from '../../utils/gql-utils'
import { authorized, isFieldInSelectionSet } from '../../utils/gql-utils'
import {
cleanUrl,
errorHandler,
@ -677,69 +677,80 @@ export const updatesSinceResolver = authorized<
>,
UpdatesSinceError,
QueryUpdatesSinceArgs
>(async (_obj, { since, first, after, sort: sortParams, folder }, { uid }) => {
const startCursor = after || ''
const size = Math.min(first || 10, 100) // limit to 100 items
let startDate = new Date(since)
if (isNaN(startDate.getTime())) {
// for android app compatibility
startDate = new Date(0)
}
const sort = sortParamsToSort(sortParams)
// create a search query
const query = `in:${
folder || 'all'
} updated:${startDate.toISOString()} sort:${sort.by}-${sort.order}`
const searchLibraryItemArgs = {
includeDeleted: true,
query,
includeContent: false,
}
const libraryItems = await searchLibraryItems(
{
...searchLibraryItemArgs,
from: Number(startCursor),
size: size + 1, // fetch one more item to get next cursor
useFolders: true,
},
uid
)
const start =
startCursor && !isNaN(Number(startCursor)) ? Number(startCursor) : 0
const hasNextPage = libraryItems.length > size
const endCursor = String(start + libraryItems.length - (hasNextPage ? 1 : 0))
//TODO: refactor so that the lastCursor included
if (hasNextPage) {
// remove an extra if exists
libraryItems.pop()
}
const edges = libraryItems.map((item) => {
const updateReason = getUpdateReason(item, startDate)
return {
node: item,
cursor: endCursor,
itemID: item.id,
updateReason,
>(
async (
_obj,
{ since, first, after, sort: sortParams, folder },
{ uid },
info
) => {
const startCursor = after || ''
const size = Math.min(first || 10, 100) // limit to 100 items
let startDate = new Date(since)
if (isNaN(startDate.getTime())) {
// for android app compatibility
startDate = new Date(0)
}
})
const sort = sortParamsToSort(sortParams)
return {
edges,
pageInfo: {
hasPreviousPage: false,
startCursor,
hasNextPage,
endCursor,
searchLibraryItemArgs,
},
// create a search query
const query = `in:${
folder || 'all'
} updated:${startDate.toISOString()} sort:${sort.by}-${sort.order}`
const isContentRequested = isFieldInSelectionSet(info, 'content')
const searchLibraryItemArgs = {
includeDeleted: true,
query,
includeContent: isContentRequested,
}
const libraryItems = await searchLibraryItems(
{
...searchLibraryItemArgs,
from: Number(startCursor),
size: size + 1, // fetch one more item to get next cursor
useFolders: true,
},
uid
)
const start =
startCursor && !isNaN(Number(startCursor)) ? Number(startCursor) : 0
const hasNextPage = libraryItems.length > size
const endCursor = String(
start + libraryItems.length - (hasNextPage ? 1 : 0)
)
//TODO: refactor so that the lastCursor included
if (hasNextPage) {
// remove an extra if exists
libraryItems.pop()
}
const edges = libraryItems.map((item) => {
const updateReason = getUpdateReason(item, startDate)
return {
node: item,
cursor: endCursor,
itemID: item.id,
updateReason,
}
})
return {
edges,
pageInfo: {
hasPreviousPage: false,
startCursor,
hasNextPage,
endCursor,
searchLibraryItemArgs,
},
}
}
})
)
export const bulkActionResolver = authorized<
BulkActionSuccess,

View File

@ -1,3 +1,4 @@
import { GraphQLResolveInfo } from 'graphql'
import { ResolverFn } from '../generated/graphql'
import { Claims, ResolverContext } from '../resolvers/types'
@ -24,3 +25,15 @@ export function authorized<
return { errorCodes: ['UNAUTHORIZED'] } as TError
}
}
export const isFieldInSelectionSet = (
info: GraphQLResolveInfo,
fieldName: string
): boolean => {
return info.fieldNodes.some((node) => {
return node.selectionSet?.selections.some(
(selection) =>
selection.kind === 'Field' && selection.name.value === fieldName
)
})
}