import { ELogicError , IFeed } from '../schemas' ; import LogicError from './error' ; import { parseFeed } from './feedparser' ; import { articleDB , feedDB } from './pouchdb' ; const Logic = { createFeed : async ( feedUrl ) => { const isExists = await feedDB . isFeedExists ( feedUrl ) ; if ( isExists && ! isExists . deleteTime ) { return null ; } const newFeed = await parseFeed ( feedUrl , '' ) ; if ( ! newFeed ) { throw new LogicError ( ELogicError . FEEDPARSER_NOT_FOUND ) ; } const response = await feedDB . insertFeed ( newFeed ) ; if ( response && response . ok ) { newFeed . _id = response . id ; newFeed . _rev = response . rev ; const articles = newFeed . articles ; if ( articles ) { articles . forEach ( article => ( article . feedId = newFeed . _id ) ) ; await articleDB . batchInsertArticles ( articles ) ; } } return newFeed ; } , deleteFeeds : async ( feedIds ) => { let changes = 0 ; for ( const feedId of feedIds ) { const response = await feedDB . deleteFeed ( feedId ) ; changes += response . ok ? 1 : 0 ; } return changes ; } , getAllFeeds : async ( ) => { const feeds = await feedDB . getAllFeeds ( ) ; return feeds ; } , getArticle : async ( articleId ) => { const article = await articleDB . get ( articleId ) ; return article ; } , getArticleContent : async ( articleId ) => { const articleContent = await articleDB . getArticleContent ( articleId ) ; return articleContent ; } , getArticles : async ( selector = { } , limit = 9999 , skip = 0 ) => { const articles = await articleDB . queryArticles ( selector , limit , skip ) ; return articles ; } , setAriclesIsRead : async ( articleIds ) => { const changes = await articleDB . batchReadArticles ( articleIds ) ; return changes ; } , setArticleIsRead : async ( articleId ) => { await articleDB . readArticle ( articleId ) ; } , setArticleIsStarred : async ( articleId , isStarred ) => { await articleDB . starArticle ( articleId , isStarred ) ; } , updateFeedArticles : async ( feed ) => { const newFeed = await parseFeed ( feed . url , feed . etag || '' ) ; if ( ! newFeed || newFeed . publishTime <= feed . publishTime ) { return 0 ; } newFeed . createTime = feed . createTime ; newFeed . url = feed . url ; newFeed . _id = feed . _id ; const articles = newFeed . articles ; const response = await feedDB . updateFeed ( newFeed ) ; if ( response && response . ok && articles && articles . length ) { articles . forEach ( article => ( article . feedId = feed . _id ) ) ; await articleDB . batchInsertArticles ( articles ) ; } return 1 ; } , } ; export default Logic ;