diff --git a/src-tauri/src/feed/channel.rs b/src-tauri/src/feed/channel.rs index 6c59f4c17..e4794c05c 100644 --- a/src-tauri/src/feed/channel.rs +++ b/src-tauri/src/feed/channel.rs @@ -667,10 +667,12 @@ pub async fn fetch_site_favicon(url: &str) -> Option { favicon_url } -pub async fn sync_articles(uuid: String) -> Vec<(usize, String, String)> { +pub async fn sync_articles(uuid: String) -> HashMap { + let mut result = HashMap::new(); + let channel = match feed::channel::get_feed_by_uuid(&uuid) { Some(channel) => channel, - None => return vec![(0, uuid, "feed not found".to_string())], + None => return HashMap::new(), }; let res = match feed::parse_feed(&channel.feed_url).await { @@ -680,34 +682,48 @@ pub async fn sync_articles(uuid: String) -> Vec<(usize, String, String)> { } Err(err) => { feed::channel::update_health_status(&uuid, 1, err.to_string()); - return vec![(0, uuid, err.to_string())]; + result.insert(uuid, (0, err.to_string())); + + return result; } }; let articles = create_article_models(&channel.uuid, &channel.feed_url, &res); - let result = feed::article::Article::add_articles(channel.uuid, articles); + let record = feed::article::Article::add_articles(channel.uuid, articles); + + result.insert(uuid, (record, "".to_string())); + + return result; - vec![(result, uuid, "".to_string())] } -pub async fn sync_article_in_folder(uuid: String) -> Vec<(usize, String, String)> { +pub async fn sync_article_in_folder(uuid: String) -> HashMap { let connection = db::establish_connection(); - let feeds = feed::folder::get_channels_in_folders(connection, vec![uuid]); + let feeds = feed::folder::get_channels_in_folders(connection, vec![uuid.clone()]); + let mut result: HashMap = HashMap::new(); + let mut count = 0; - println!("{:?}", feeds); - let mut res = vec![]; + + log::debug!("sync_article_in_folder: feeds {:?}", feeds); for feed in feeds { - res.extend(sync_articles(feed.uuid).await); + let record = sync_articles(feed.uuid.clone()).await; + let (num, _message) = record.get(&String::from(feed.uuid)).unwrap(); + result.extend(record.clone()); + count += num; } - res + result.insert(uuid, (count, String::from(""))); + + result } -pub async fn sync_feed(uuid: String, feed_type: String) -> Vec<(usize, String, String)> { +// pub struct SyncFeedResult {} +pub async fn sync_feed(uuid: String, feed_type: String) -> HashMap { if feed_type == "folder" { return feed::channel::sync_article_in_folder(uuid.to_string()).await; } else { + log::debug!("start sync feed ===>"); return feed::channel::sync_articles(uuid.to_string()).await; } } @@ -755,10 +771,12 @@ mod tests { #[tokio::test] async fn test_sync_feed() { - sync_feed( - "1e2dbbcd-5f44-4811-a907-0b6320b3ee9e".to_string(), + let a = sync_feed( + "efc718b5-14a8-45ce-8d0b-975013198ac1".to_string(), "feed".to_string(), ) .await; + + println!("a {:?}", a); } } diff --git a/src-tauri/src/feed/folder.rs b/src-tauri/src/feed/folder.rs index db8ce7faf..e2e927c27 100644 --- a/src-tauri/src/feed/folder.rs +++ b/src-tauri/src/feed/folder.rs @@ -11,7 +11,7 @@ pub fn get_channels_in_folders( uuids: Vec, ) -> Vec { let result = schema::feed_metas::dsl::feed_metas - .filter(schema::feed_metas::uuid.eq_any(&uuids)) + .filter(schema::feed_metas::folder_uuid.eq_any(&uuids)) .load::(&mut connection) .expect("Expect get feed meta"); diff --git a/src-tauri/src/server/handlers/article.rs b/src-tauri/src/server/handlers/article.rs index 1d6f0119d..8333d39c5 100644 --- a/src-tauri/src/server/handlers/article.rs +++ b/src-tauri/src/server/handlers/article.rs @@ -71,8 +71,6 @@ pub async fn handle_mark_as_read( is_all: body.is_all, }); - println!("=========> {:?}", body); - Ok(web::Json(res)) } diff --git a/src/components/Subscribes/ItemView.tsx b/src/components/Subscribes/ItemView.tsx index 0d6a2902d..efba784c5 100644 --- a/src/components/Subscribes/ItemView.tsx +++ b/src/components/Subscribes/ItemView.tsx @@ -65,7 +65,7 @@ export const ItemView: FC = ({ >
{ @@ -49,7 +50,7 @@ const ChannelList = (): JSX.Element => { setRefreshing, done, setDone, - startFresh, + startRefresh, ] = useRefresh(); const store = useBearStore((state) => ({ feed: state.feed, @@ -64,7 +65,9 @@ const ChannelList = (): JSX.Element => { setViewMeta: state.setViewMeta, collectionMeta: state.collectionMeta, initCollectionMetas: state.initCollectionMetas, + syncArticles: state.syncArticles, })); + const [, , channelUuid] = useQuery(); useEffect(() => { @@ -91,6 +94,29 @@ const ChannelList = (): JSX.Element => { }; const reloadFeedData = (feed: FeedResItem | null) => { + if (feed) { + store.syncArticles(feed?.uuid, feed?.item_type) + .then(([,, message ]) => { + if (message) { + toast({ + title: "Something wrong!", + variant: "destructive", + description: message, + action: ( + Close + ), + }); + } else { + toast({ + title: "Success", + description: message, + action: ( + Close + ), + }); + } + }); + } console.log("TODO"); }; @@ -210,7 +236,7 @@ const ChannelList = (): JSX.Element => { } /> - + {

@@ -353,8 +379,8 @@ const ChannelList = (): JSX.Element => { - store.feedContextMenuTarget?.link && - copyText(store.feedContextMenuTarget?.link).then(() => + store.feedContextMenuTarget?.feed_url && + copyText(store.feedContextMenuTarget?.feed_url).then(() => toast({ title: "Current URL copied to clipboard", description: "Paste it wherever you like", diff --git a/src/components/Subscribes/useRefresh.tsx b/src/components/Subscribes/useRefresh.tsx index b0ec23667..c6bad644f 100644 --- a/src/components/Subscribes/useRefresh.tsx +++ b/src/components/Subscribes/useRefresh.tsx @@ -59,7 +59,7 @@ export const useRefresh = () => { setRefreshing(true); - dataAgent.getUserConfig().then((config) => { + dataAgent.getUserConfig().then(({ data: config }) => { const { threads = 5 } = config; const limit = pLimit(threads); const fns = (store.feedList || []).map((channel: any) => { diff --git a/src/stores/createFeedSlice.ts b/src/stores/createFeedSlice.ts index df57933c4..f4e3a6b3a 100644 --- a/src/stores/createFeedSlice.ts +++ b/src/stores/createFeedSlice.ts @@ -40,6 +40,8 @@ export interface FeedSlice { openFolder: (uuid: string) => void; closeFolder: (uuid: string) => void; + + syncArticles: (uuid: string, type: string) => Promise<[number, string, string]>; } export const createFeedSlice: StateCreator = ( @@ -192,10 +194,7 @@ export const createFeedSlice: StateCreator = ( }; return Promise.all([dataAgent.getFeeds(), dataAgent.getUnreadTotal()]).then( ([{ data: feedList }, { data: unreadTotal }]) => { - console.log("%c Line:188 🍡 unreadTotal", "color:#4fff4B", unreadTotal); - console.log("%c Line:185 🍺 feedList", "color:#7f2b82", feedList); feedList = initUnreadCount(feedList, unreadTotal); - console.log("%c Line:191 🥐 feedList", "color:#2eafb0", feedList); set(() => ({ feedList: feedList || [], })); @@ -242,5 +241,19 @@ export const createFeedSlice: StateCreator = ( set(() => ({ feedList: [...list] })) + }, + + syncArticles(uuid: string, type: string) { + return dataAgent.syncFeed(type, uuid) + .then(({ data }) => { + const [num, , message] = data[0]; + + if (num > 0) { + get().initCollectionMetas(); + get().getFeedList(); + } + + return data[0]; + }) } });