-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
73 deletions.
There are no files selected for viewing
118 changes: 46 additions & 72 deletions
118
packages/api/src/resolvers/discover_feeds/articles/add.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,64 @@ | ||
import { v4 } from 'uuid' | ||
import { appDataSource } from '../../../data_source' | ||
import { | ||
InputMaybe, | ||
MutationSaveDiscoverArticleArgs, | ||
SaveDiscoverArticleError, | ||
SaveDiscoverArticleErrorCode, | ||
SaveDiscoverArticleSuccess, | ||
SaveSuccess, | ||
} from '../../../generated/graphql' | ||
import { userRepository } from '../../../repository/user' | ||
import { saveUrl } from '../../../services/save_url' | ||
import { libraryItemRepository } from '../../../repository/library_item' | ||
import { savePage } from '../../../services/save_page' | ||
import { findActiveUser } from '../../../services/user' | ||
import { authorized } from '../../../utils/gql-utils' | ||
|
||
export const saveDiscoverArticleResolver = authorized< | ||
SaveDiscoverArticleSuccess, | ||
SaveDiscoverArticleError, | ||
MutationSaveDiscoverArticleArgs | ||
>( | ||
async ( | ||
_, | ||
{ input: { discoverArticleId, timezone, locale } }, | ||
{ uid, log } | ||
) => { | ||
try { | ||
const user = await userRepository.findById(uid) | ||
if (!user) { | ||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.Unauthorized], | ||
} | ||
} | ||
|
||
const { rows: discoverArticles } = (await appDataSource.query( | ||
`SELECT url FROM omnivore.discover_feed_articles WHERE id=$1`, | ||
[discoverArticleId] | ||
)) as { | ||
rows: { | ||
url: string | ||
}[] | ||
} | ||
|
||
if (discoverArticles.length != 1) { | ||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.NotFound], | ||
} | ||
} | ||
|
||
const url = discoverArticles[0].url | ||
const savedArticle = await saveUrl( | ||
{ | ||
url, | ||
source: 'add-link', | ||
clientRequestId: v4(), | ||
locale: locale as InputMaybe<string>, | ||
timezone: timezone as InputMaybe<string>, | ||
}, | ||
user | ||
) | ||
|
||
if (savedArticle.__typename == 'SaveError') { | ||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.BadRequest], | ||
} | ||
} | ||
>(async (_, { input: { discoverArticleId } }, { authTrx, uid }) => { | ||
const user = await findActiveUser(uid) | ||
if (!user) { | ||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.Unauthorized], | ||
} | ||
} | ||
|
||
const saveSuccess = savedArticle as SaveSuccess | ||
const discoverItem = await authTrx( | ||
async (t) => | ||
await t.withRepository(libraryItemRepository).findById(discoverArticleId) | ||
) | ||
|
||
await appDataSource.query( | ||
`insert into omnivore.discover_feed_save_link (discover_article_id, user_id, article_save_id, article_save_url) VALUES ($1, $2, $3, $4) ON CONFLICT ON CONSTRAINT user_discover_feed_link DO UPDATE SET (article_save_id, article_save_url, deleted) = ($3, $4, false);`, | ||
[discoverArticleId, uid, saveSuccess.clientRequestId, saveSuccess.url] | ||
) | ||
if (!discoverItem) { | ||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.NotFound], | ||
} | ||
} | ||
|
||
return { | ||
__typename: 'SaveDiscoverArticleSuccess', | ||
url: saveSuccess.url, | ||
saveId: saveSuccess.clientRequestId, | ||
} | ||
} catch (error) { | ||
log.error('Error Saving Article', error) | ||
const saveResult = await savePage( | ||
{ | ||
clientRequestId: '', | ||
title: discoverItem.title, | ||
originalContent: discoverItem.originalContent || '', | ||
rssFeedUrl: discoverItem.subscription, | ||
url: discoverItem.originalUrl, | ||
source: 'discover', | ||
publishedAt: discoverItem.publishedAt, | ||
author: discoverItem.author || undefined, | ||
previewImage: discoverItem.thumbnail || undefined, | ||
}, | ||
user | ||
) | ||
|
||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.Unauthorized], | ||
} | ||
if (saveResult.__typename === 'SaveSuccess') { | ||
return { | ||
__typename: 'SaveDiscoverArticleSuccess', | ||
saveId: saveResult.clientRequestId, | ||
url: saveResult.url, | ||
} | ||
} | ||
) | ||
|
||
return { | ||
__typename: 'SaveDiscoverArticleError', | ||
errorCodes: [SaveDiscoverArticleErrorCode.Unauthorized], | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters