diff --git a/packages/api/src/jobs/rss/refreshFeed.ts b/packages/api/src/jobs/rss/refreshFeed.ts index deda89220..dba8ed535 100644 --- a/packages/api/src/jobs/rss/refreshFeed.ts +++ b/packages/api/src/jobs/rss/refreshFeed.ts @@ -8,6 +8,7 @@ import { env } from '../../env' import { redisDataSource } from '../../redis_data_source' import createHttpTaskWithToken from '../../utils/createTask' import { RSSRefreshContext } from './refreshAllFeeds' +import { updateSubscription } from '../../services/update_subscription' type FolderType = 'following' | 'inbox' @@ -615,13 +616,12 @@ const processSubscription = async ( const nextScheduledAt = scheduledAt + updatePeriodInMs * updateFrequency // update subscription lastFetchedAt - const updatedSubscription = await sendUpdateSubscriptionMutation( - userId, - subscriptionId, - lastItemFetchedAt, - updatedLastFetchedChecksum, - new Date(nextScheduledAt) - ) + const updatedSubscription = await updateSubscription(userId, subscriptionId, { + id: subscriptionId, + lastFetchedAt: lastItemFetchedAt, + lastFetchedChecksum: updatedLastFetchedChecksum, + scheduledAt: new Date(nextScheduledAt), + }) console.log('Updated subscription', updatedSubscription) } diff --git a/packages/api/src/resolvers/subscriptions/index.ts b/packages/api/src/resolvers/subscriptions/index.ts index ab785b73a..2b0e0b12f 100644 --- a/packages/api/src/resolvers/subscriptions/index.ts +++ b/packages/api/src/resolvers/subscriptions/index.ts @@ -50,6 +50,7 @@ import { keysToCamelCase, } from '../../utils/helpers' import { parseFeed, parseOpml, RSS_PARSER_CONFIG } from '../../utils/parser' +import { updateSubscription } from '../../services/update_subscription' type PartialSubscription = Omit @@ -332,34 +333,7 @@ export const updateSubscriptionResolver = authorized< }, }) - const updatedSubscription = await authTrx(async (t) => { - const repo = t.getRepository(Subscription) - - // update subscription - await t.getRepository(Subscription).save({ - id: input.id, - name: input.name || undefined, - description: input.description || undefined, - lastFetchedAt: input.lastFetchedAt - ? new Date(input.lastFetchedAt) - : undefined, - lastFetchedChecksum: input.lastFetchedChecksum || undefined, - status: input.status || undefined, - scheduledAt: input.scheduledAt - ? new Date(input.scheduledAt) - : undefined, - autoAddToLibrary: input.autoAddToLibrary ?? undefined, - isPrivate: input.isPrivate ?? undefined, - fetchContent: input.fetchContent ?? undefined, - folder: input.folder ?? undefined, - }) - - return repo.findOneByOrFail({ - id: input.id, - user: { id: uid }, - }) - }) - + const updatedSubscription = await updateSubscription(uid, input.id, input) return { subscription: updatedSubscription, } diff --git a/packages/api/src/services/update_subscription.ts b/packages/api/src/services/update_subscription.ts new file mode 100644 index 000000000..33f9cdfb0 --- /dev/null +++ b/packages/api/src/services/update_subscription.ts @@ -0,0 +1,47 @@ +import { Subscription } from '../entity/subscription' +import { UpdateSubscriptionInput } from '../generated/graphql' +import { getRepository } from '../repository' + +const ensureOwns = async (userId: string, subscriptionId: string) => { + const repo = getRepository(Subscription) + + const existing = repo.findOneByOrFail({ + id: subscriptionId, + user: { id: userId }, + }) + if (!existing) { + throw new Error('Can not find subscription being updated.') + } +} + +export const updateSubscription = async ( + userId: string, + subscriptionId: string, + newData: UpdateSubscriptionInput +): Promise => { + ensureOwns(userId, subscriptionId) + + const repo = getRepository(Subscription) + await repo.save({ + id: subscriptionId, + name: newData.name || undefined, + description: newData.description || undefined, + lastFetchedAt: newData.lastFetchedAt + ? new Date(newData.lastFetchedAt) + : undefined, + lastFetchedChecksum: newData.lastFetchedChecksum || undefined, + status: newData.status || undefined, + scheduledAt: newData.scheduledAt + ? new Date(newData.scheduledAt) + : undefined, + autoAddToLibrary: newData.autoAddToLibrary ?? undefined, + isPrivate: newData.isPrivate ?? undefined, + fetchContent: newData.fetchContent ?? undefined, + folder: newData.folder ?? undefined, + }) + + return (await getRepository(Subscription).findOneByOrFail({ + id: subscriptionId, + user: { id: userId }, + })) as Subscription +}