1- use crate :: post:: { Commit , ContentType , Post , QueryPost } ;
1+ use crate :: post:: { Commit , ContentType , Post } ;
22use anyhow:: { Context , Result } ;
33use r2d2:: Pool ;
44use r2d2_sqlite:: SqliteConnectionManager ;
@@ -16,7 +16,7 @@ impl PostService {
1616 Self { pool }
1717 }
1818
19- fn row_to_query_post ( row : & rusqlite:: Row ) -> rusqlite:: Result < QueryPost > {
19+ fn row_to_post ( row : & rusqlite:: Row ) -> rusqlite:: Result < Post > {
2020 let id: String = row. get ( "id" ) ?;
2121 let content_type_str: String = row. get ( "content_type" ) ?;
2222 let content_type = ContentType :: from ( content_type_str) ;
@@ -28,7 +28,7 @@ impl PostService {
2828 let content: String = row. get ( "content" ) ?;
2929 let commits: Option < String > = row. get ( "commits" ) ?;
3030
31- Ok ( QueryPost {
31+ Ok ( Post {
3232 id,
3333 content_type,
3434 title,
@@ -38,6 +38,7 @@ impl PostService {
3838 date,
3939 content,
4040 commits,
41+ real_commits : None ,
4142 } )
4243 }
4344
@@ -48,7 +49,7 @@ impl PostService {
4849 let mut stmt = conn. prepare (
4950 "SELECT * FROM posts WHERE content_type != 'special' ORDER BY date DESC LIMIT 5" ,
5051 ) ?;
51- let iter = stmt. query_map ( [ ] , Self :: row_to_query_post ) ?;
52+ let iter = stmt. query_map ( [ ] , Self :: row_to_post ) ?;
5253 let mut result = Vec :: new ( ) ;
5354 for post in iter {
5455 result. push ( post?) ;
@@ -71,7 +72,7 @@ impl PostService {
7172 let mut stmt = conn. prepare (
7273 "SELECT * FROM posts WHERE content_type != 'special' ORDER BY date DESC LIMIT ? OFFSET ?" ,
7374 ) ?;
74- let iter = stmt. query_map ( params ! [ POSTS_PER_PAGE , offset] , Self :: row_to_query_post ) ?;
75+ let iter = stmt. query_map ( params ! [ POSTS_PER_PAGE , offset] , Self :: row_to_post ) ?;
7576 let mut result = Vec :: new ( ) ;
7677 for post in iter {
7778 result. push ( post?) ;
@@ -110,7 +111,7 @@ impl PostService {
110111 let result = conn. query_row (
111112 "SELECT * FROM posts WHERE id = ? AND content_type != 'special'" ,
112113 [ & id_clone] ,
113- Self :: row_to_query_post ,
114+ Self :: row_to_post ,
114115 ) ?;
115116 anyhow:: Result :: < _ > :: Ok ( result)
116117 } )
@@ -129,7 +130,7 @@ impl PostService {
129130 let result = conn. query_row (
130131 "SELECT * FROM posts WHERE id = ? AND content_type = 'special'" ,
131132 [ & id_clone] ,
132- Self :: row_to_query_post ,
133+ Self :: row_to_post ,
133134 ) ?;
134135 anyhow:: Result :: < _ > :: Ok ( result)
135136 } )
@@ -140,14 +141,14 @@ impl PostService {
140141 self . convert_to_post ( query) . await
141142 }
142143
143- pub async fn get_rss_entries ( & self ) -> Result < Vec < QueryPost > > {
144+ pub async fn get_rss_entries ( & self ) -> Result < Vec < Post > > {
144145 let pool = self . pool . clone ( ) ;
145146 task:: spawn_blocking ( move || {
146147 let conn = pool. get ( ) ?;
147148 let mut stmt = conn. prepare (
148149 "SELECT * FROM posts WHERE content_type != 'special' ORDER BY date DESC LIMIT 20" ,
149150 ) ?;
150- let iter = stmt. query_map ( [ ] , Self :: row_to_query_post ) ?;
151+ let iter = stmt. query_map ( [ ] , Self :: row_to_post ) ?;
151152 let mut result = Vec :: new ( ) ;
152153 for post in iter {
153154 result. push ( post?) ;
@@ -159,14 +160,14 @@ impl PostService {
159160 . context ( "Failed to fetch RSS entries" )
160161 }
161162
162- async fn convert_to_post ( & self , query : QueryPost ) -> Result < Post > {
163- self . bulk_convert_to_posts ( vec ! [ query ] )
163+ async fn convert_to_post ( & self , post : Post ) -> Result < Post > {
164+ self . bulk_convert_to_posts ( vec ! [ post ] )
164165 . await
165166 . map ( |mut v| v. remove ( 0 ) )
166167 }
167168
168- async fn bulk_convert_to_posts ( & self , query_posts : Vec < QueryPost > ) -> Result < Vec < Post > > {
169- let all_commit_ids: Vec < _ > = query_posts
169+ async fn bulk_convert_to_posts ( & self , mut posts : Vec < Post > ) -> Result < Vec < Post > > {
170+ let all_commit_ids: Vec < _ > = posts
170171 . iter ( )
171172 . filter_map ( |post| post. commits . as_ref ( ) )
172173 . flat_map ( |commits| commits. split_whitespace ( ) )
@@ -212,27 +213,16 @@ impl PostService {
212213 HashMap :: new ( )
213214 } ;
214215
215- Ok ( query_posts
216- . into_iter ( )
217- . map ( |post| {
218- let real_commits = post. commits . as_ref ( ) . map ( |ids| {
219- ids. split_whitespace ( )
220- . filter_map ( |id| commits_map. get ( id) . cloned ( ) )
221- . collect ( )
222- } ) ;
223-
224- Post {
225- id : post. id ,
226- content_type : post. content_type ,
227- title : post. title ,
228- link : post. link ,
229- via : post. via ,
230- quote_author : post. quote_author ,
231- date : post. date ,
232- content : post. content ,
233- real_commits,
234- }
235- } )
236- . collect ( ) )
216+ for post in & mut posts {
217+ if let Some ( ids) = & post. commits {
218+ let real_commits: Vec < Commit > = ids
219+ . split_whitespace ( )
220+ . filter_map ( |id| commits_map. get ( id) . cloned ( ) )
221+ . collect ( ) ;
222+ post. real_commits = Some ( real_commits) ;
223+ }
224+ }
225+
226+ Ok ( posts)
237227 }
238228}
0 commit comments