Skip to content

Commit

Permalink
add fetchContent api
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Dec 19, 2023
1 parent 07b96c4 commit 92aa11b
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
48 changes: 48 additions & 0 deletions packages/api/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,23 @@ export type FeedsSuccess = {
pageInfo: PageInfo;
};

export type FetchContentError = {
__typename?: 'FetchContentError';
errorCodes: Array<FetchContentErrorCode>;
};

export enum FetchContentErrorCode {
BadRequest = 'BAD_REQUEST',
Unauthorized = 'UNAUTHORIZED'
}

export type FetchContentResult = FetchContentError | FetchContentSuccess;

export type FetchContentSuccess = {
__typename?: 'FetchContentSuccess';
success: Scalars['Boolean'];
};

export type Filter = {
__typename?: 'Filter';
category?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -1335,6 +1352,7 @@ export type Mutation = {
deleteNewsletterEmail: DeleteNewsletterEmailResult;
deleteRule: DeleteRuleResult;
deleteWebhook: DeleteWebhookResult;
fetchContent: FetchContentResult;
generateApiKey: GenerateApiKeyResult;
googleLogin: LoginResult;
googleSignup: GoogleSignupResult;
Expand Down Expand Up @@ -1467,6 +1485,11 @@ export type MutationDeleteWebhookArgs = {
};


export type MutationFetchContentArgs = {
id: Scalars['ID'];
};


export type MutationGenerateApiKeyArgs = {
input: GenerateApiKeyInput;
};
Expand Down Expand Up @@ -3624,6 +3647,10 @@ export type ResolversTypes = {
FeedsInput: FeedsInput;
FeedsResult: ResolversTypes['FeedsError'] | ResolversTypes['FeedsSuccess'];
FeedsSuccess: ResolverTypeWrapper<FeedsSuccess>;
FetchContentError: ResolverTypeWrapper<FetchContentError>;
FetchContentErrorCode: FetchContentErrorCode;
FetchContentResult: ResolversTypes['FetchContentError'] | ResolversTypes['FetchContentSuccess'];
FetchContentSuccess: ResolverTypeWrapper<FetchContentSuccess>;
Filter: ResolverTypeWrapper<Filter>;
FiltersError: ResolverTypeWrapper<FiltersError>;
FiltersErrorCode: FiltersErrorCode;
Expand Down Expand Up @@ -4119,6 +4146,9 @@ export type ResolversParentTypes = {
FeedsInput: FeedsInput;
FeedsResult: ResolversParentTypes['FeedsError'] | ResolversParentTypes['FeedsSuccess'];
FeedsSuccess: FeedsSuccess;
FetchContentError: FetchContentError;
FetchContentResult: ResolversParentTypes['FetchContentError'] | ResolversParentTypes['FetchContentSuccess'];
FetchContentSuccess: FetchContentSuccess;
Filter: Filter;
FiltersError: FiltersError;
FiltersResult: ResolversParentTypes['FiltersError'] | ResolversParentTypes['FiltersSuccess'];
Expand Down Expand Up @@ -4987,6 +5017,20 @@ export type FeedsSuccessResolvers<ContextType = ResolverContext, ParentType exte
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type FetchContentErrorResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['FetchContentError'] = ResolversParentTypes['FetchContentError']> = {
errorCodes?: Resolver<Array<ResolversTypes['FetchContentErrorCode']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type FetchContentResultResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['FetchContentResult'] = ResolversParentTypes['FetchContentResult']> = {
__resolveType: TypeResolveFn<'FetchContentError' | 'FetchContentSuccess', ParentType, ContextType>;
};

export type FetchContentSuccessResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['FetchContentSuccess'] = ResolversParentTypes['FetchContentSuccess']> = {
success?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};

export type FilterResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['Filter'] = ResolversParentTypes['Filter']> = {
category?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
createdAt?: Resolver<ResolversTypes['Date'], ParentType, ContextType>;
Expand Down Expand Up @@ -5377,6 +5421,7 @@ export type MutationResolvers<ContextType = ResolverContext, ParentType extends
deleteNewsletterEmail?: Resolver<ResolversTypes['DeleteNewsletterEmailResult'], ParentType, ContextType, RequireFields<MutationDeleteNewsletterEmailArgs, 'newsletterEmailId'>>;
deleteRule?: Resolver<ResolversTypes['DeleteRuleResult'], ParentType, ContextType, RequireFields<MutationDeleteRuleArgs, 'id'>>;
deleteWebhook?: Resolver<ResolversTypes['DeleteWebhookResult'], ParentType, ContextType, RequireFields<MutationDeleteWebhookArgs, 'id'>>;
fetchContent?: Resolver<ResolversTypes['FetchContentResult'], ParentType, ContextType, RequireFields<MutationFetchContentArgs, 'id'>>;
generateApiKey?: Resolver<ResolversTypes['GenerateApiKeyResult'], ParentType, ContextType, RequireFields<MutationGenerateApiKeyArgs, 'input'>>;
googleLogin?: Resolver<ResolversTypes['LoginResult'], ParentType, ContextType, RequireFields<MutationGoogleLoginArgs, 'input'>>;
googleSignup?: Resolver<ResolversTypes['GoogleSignupResult'], ParentType, ContextType, RequireFields<MutationGoogleSignupArgs, 'input'>>;
Expand Down Expand Up @@ -6559,6 +6604,9 @@ export type Resolvers<ContextType = ResolverContext> = {
FeedsError?: FeedsErrorResolvers<ContextType>;
FeedsResult?: FeedsResultResolvers<ContextType>;
FeedsSuccess?: FeedsSuccessResolvers<ContextType>;
FetchContentError?: FetchContentErrorResolvers<ContextType>;
FetchContentResult?: FetchContentResultResolvers<ContextType>;
FetchContentSuccess?: FetchContentSuccessResolvers<ContextType>;
Filter?: FilterResolvers<ContextType>;
FiltersError?: FiltersErrorResolvers<ContextType>;
FiltersResult?: FiltersResultResolvers<ContextType>;
Expand Down
16 changes: 16 additions & 0 deletions packages/api/src/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,21 @@ type FeedsSuccess {
pageInfo: PageInfo!
}

type FetchContentError {
errorCodes: [FetchContentErrorCode!]!
}

enum FetchContentErrorCode {
BAD_REQUEST
UNAUTHORIZED
}

union FetchContentResult = FetchContentError | FetchContentSuccess

type FetchContentSuccess {
success: Boolean!
}

type Filter {
category: String
createdAt: Date!
Expand Down Expand Up @@ -1198,6 +1213,7 @@ type Mutation {
deleteNewsletterEmail(newsletterEmailId: ID!): DeleteNewsletterEmailResult!
deleteRule(id: ID!): DeleteRuleResult!
deleteWebhook(id: ID!): DeleteWebhookResult!
fetchContent(id: ID!): FetchContentResult!
generateApiKey(input: GenerateApiKeyInput!): GenerateApiKeyResult!
googleLogin(input: GoogleLoginInput!): LoginResult!
googleSignup(input: GoogleSignupInput!): GoogleSignupResult!
Expand Down
49 changes: 49 additions & 0 deletions packages/api/src/resolvers/article/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ import {
CreateArticleError,
CreateArticleErrorCode,
CreateArticleSuccess,
FetchContentError,
FetchContentErrorCode,
FetchContentSuccess,
MoveToFolderError,
MoveToFolderErrorCode,
MoveToFolderSuccess,
MutationBulkActionArgs,
MutationCreateArticleArgs,
MutationFetchContentArgs,
MutationMoveToFolderArgs,
MutationSaveArticleReadingProgressArgs,
MutationSetBookmarkArticleArgs,
Expand Down Expand Up @@ -67,6 +71,7 @@ import {
} from '../../services/labels'
import {
createLibraryItem,
findLibraryItemById,
findLibraryItemByUrl,
findLibraryItemsByPrefix,
searchLibraryItems,
Expand Down Expand Up @@ -981,6 +986,50 @@ export const moveToFolderResolver = authorized<
}
})

export const fetchContentResolver = authorized<
FetchContentSuccess,
FetchContentError,
MutationFetchContentArgs
>(async (_, { id }, { uid, log, pubsub }) => {
analytics.track({
userId: uid,
event: 'fetch_content',
properties: {
id,
},
})

const item = await findLibraryItemById(id, uid)
if (!item) {
return {
errorCodes: [FetchContentErrorCode.Unauthorized],
}
}

// if the content is not fetched yet, create a page save request
if (item.state === LibraryItemState.ContentNotFetched) {
try {
await createPageSaveRequest({
userId: uid,
url: item.originalUrl,
articleSavingRequestId: id,
priority: 'high',
pubsub,
})
} catch (error) {
log.error('fetchContentResolver error', error)

return {
errorCodes: [FetchContentErrorCode.BadRequest],
}
}
}

return {
success: true,
}
})

const getUpdateReason = (libraryItem: LibraryItem, since: Date) => {
if (libraryItem.deletedAt) {
return UpdateReason.Deleted
Expand Down
16 changes: 16 additions & 0 deletions packages/api/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,21 @@ const schema = gql`
BAD_REQUEST
}
union FetchContentResult = FetchContentSuccess | FetchContentError
type FetchContentSuccess {
success: Boolean!
}
type FetchContentError {
errorCodes: [FetchContentErrorCode!]!
}
enum FetchContentErrorCode {
UNAUTHORIZED
BAD_REQUEST
}
# Mutations
type Mutation {
googleLogin(input: GoogleLoginInput!): LoginResult!
Expand Down Expand Up @@ -2829,6 +2844,7 @@ const schema = gql`
input: UpdateSubscriptionInput!
): UpdateSubscriptionResult!
moveToFolder(id: ID!, folder: String!): MoveToFolderResult!
fetchContent(id: ID!): FetchContentResult!
}
# FIXME: remove sort from feedArticles after all cached tabs are closed
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/services/create_page_save_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const createPageSaveRequest = async ({
pubsub
)
}
// reset state to processing if not in following
// reset state to processing
if (libraryItem.state !== LibraryItemState.Processing) {
libraryItem = await updateLibraryItem(
libraryItem.id,
Expand Down

0 comments on commit 92aa11b

Please sign in to comment.