From 7f6aaa33650ac9b9e33b6b90bd131917cb448662 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Sun, 7 Jun 2020 13:44:17 +0300 Subject: [PATCH] add invite_link --- .../down.sql | 1 + .../up.sql | 1 + src/bot/api.rs | 4 + src/bot/logic.rs | 6 ++ src/db/feeds.rs | 1 + src/db/telegram.rs | 7 ++ src/models/telegram_chat.rs | 1 + src/schema.rs | 1 + src/sync/reader/fetcher.rs | 66 +++++++++++++- tests/support/rss_feed_example.xml | 86 +------------------ 10 files changed, 85 insertions(+), 89 deletions(-) create mode 100644 migrations/2020-06-06-173018_add_invite_link_to_chats/down.sql create mode 100644 migrations/2020-06-06-173018_add_invite_link_to_chats/up.sql diff --git a/migrations/2020-06-06-173018_add_invite_link_to_chats/down.sql b/migrations/2020-06-06-173018_add_invite_link_to_chats/down.sql new file mode 100644 index 00000000..cd0e60cb --- /dev/null +++ b/migrations/2020-06-06-173018_add_invite_link_to_chats/down.sql @@ -0,0 +1 @@ +ALTER TABLE telegram_chats DROP COLUMN invite_link; diff --git a/migrations/2020-06-06-173018_add_invite_link_to_chats/up.sql b/migrations/2020-06-06-173018_add_invite_link_to_chats/up.sql new file mode 100644 index 00000000..a383762c --- /dev/null +++ b/migrations/2020-06-06-173018_add_invite_link_to_chats/up.sql @@ -0,0 +1 @@ +ALTER TABLE telegram_chats ADD COLUMN invite_link TEXT; diff --git a/src/bot/api.rs b/src/bot/api.rs index 7eb1a495..90088571 100644 --- a/src/bot/api.rs +++ b/src/bot/api.rs @@ -25,6 +25,7 @@ impl From for NewTelegramChat { first_name: Some(chat.first_name), last_name: chat.last_name, title: None, + invite_link: None, }, MessageChat::Group(chat) => NewTelegramChat { id: chat.id.into(), @@ -33,6 +34,7 @@ impl From for NewTelegramChat { username: None, first_name: None, last_name: None, + invite_link: chat.invite_link, }, MessageChat::Supergroup(chat) => NewTelegramChat { id: chat.id.into(), @@ -41,6 +43,7 @@ impl From for NewTelegramChat { username: chat.username, first_name: None, last_name: None, + invite_link: chat.invite_link, }, MessageChat::Unknown(chat) => NewTelegramChat { id: chat.id.into(), @@ -49,6 +52,7 @@ impl From for NewTelegramChat { username: chat.username, first_name: chat.first_name, last_name: chat.last_name, + invite_link: chat.invite_link, }, } } diff --git a/src/bot/logic.rs b/src/bot/logic.rs index 64d07956..ed110987 100644 --- a/src/bot/logic.rs +++ b/src/bot/logic.rs @@ -202,6 +202,7 @@ mod tests { username: Some("Username".to_string()), first_name: Some("First".to_string()), last_name: Some("Last".to_string()), + invite_link: None, title: None, }; @@ -230,6 +231,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; db_connection.test_transaction::<(), super::SubscriptionError, _>(|| { @@ -251,6 +253,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; db_connection.test_transaction::<(), super::SubscriptionError, _>(|| { @@ -275,6 +278,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; db_connection.test_transaction::<(), super::SubscriptionError, _>(|| { @@ -313,6 +317,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; db_connection.test_transaction::<(), super::SubscriptionError, _>(|| { @@ -356,6 +361,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; db_connection.test_transaction::<(), super::SubscriptionError, _>(|| { diff --git a/src/db/feeds.rs b/src/db/feeds.rs index a87931dd..1edc1890 100644 --- a/src/db/feeds.rs +++ b/src/db/feeds.rs @@ -432,6 +432,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; let chat = telegram::create_chat(connection, new_chat).unwrap(); diff --git a/src/db/telegram.rs b/src/db/telegram.rs index e03c9273..cb94156f 100644 --- a/src/db/telegram.rs +++ b/src/db/telegram.rs @@ -21,6 +21,7 @@ pub struct NewTelegramChat { pub username: Option, pub first_name: Option, pub last_name: Option, + pub invite_link: Option, } #[derive(Insertable, Clone, Copy)] @@ -42,6 +43,7 @@ pub fn create_chat(conn: &PgConnection, new_chat: NewTelegramChat) -> Result(conn) } @@ -230,6 +232,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; let updated_chat = NewTelegramChat { id: 42, @@ -238,6 +241,7 @@ mod tests { first_name: Some("First1".to_string()), last_name: Some("Last1".to_string()), title: None, + invite_link: None, }; let connection = db::establish_connection(); @@ -271,6 +275,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, }; connection.test_transaction::<(), Error, _>(|| { @@ -581,6 +586,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, } } @@ -592,6 +598,7 @@ mod tests { first_name: Some("First".to_string()), last_name: Some("Last".to_string()), title: None, + invite_link: None, } } } diff --git a/src/models/telegram_chat.rs b/src/models/telegram_chat.rs index 171b7c2b..4c382b2c 100644 --- a/src/models/telegram_chat.rs +++ b/src/models/telegram_chat.rs @@ -14,4 +14,5 @@ pub struct TelegramChat { pub updated_at: DateTime, pub title: Option, pub utc_offset_minutes: Option, + pub invite_link: Option, } diff --git a/src/schema.rs b/src/schema.rs index 6766ef68..ac215088 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -37,6 +37,7 @@ table! { updated_at -> Timestamptz, title -> Nullable, utc_offset_minutes -> Nullable, + invite_link -> Nullable, } } diff --git a/src/sync/reader/fetcher.rs b/src/sync/reader/fetcher.rs index 3f4e0208..1bab140b 100644 --- a/src/sync/reader/fetcher.rs +++ b/src/sync/reader/fetcher.rs @@ -35,7 +35,7 @@ impl From for FetchedFeed { .into_iter() .filter(|item| !item.links.is_empty()) .map(|item| { - let pub_date: DateTime = parse_time(item.published); + let pub_date: DateTime = parse_time(item.published, item.updated); FetchedFeedItem { title: item.title.map_or_else(|| "".to_string(), |s| s.content), description: item.summary.map(|s| s.content), @@ -75,9 +75,12 @@ impl From for FetchedFeed { } } -fn parse_time(pub_date: Option>) -> DateTime { +fn parse_time(pub_date: Option>, updated: Option>) -> DateTime { match pub_date { - None => db::current_time(), + None => match updated { + Some(value) => value, + None => db::current_time(), + }, Some(value) => value, } } @@ -96,7 +99,62 @@ mod tests { let fetched_feed: FetchedFeed = feed.into(); - let expected_result = FetchedFeed { title: "World".to_string(), link: "".to_string(), description: "NPR world news, international art and culture, world business and financial markets, world economy, and global trends in health, science and technology. Subscribe to the World Story of the Day podcast and RSS feed.".to_string(), feed_type: "json".to_string(), items: vec![FetchedFeedItem { title: "Trump Says U.S. Will Withdraw From WHO. Does He Have The Authority To Do It?".to_string(), description: Some("

In a press conference on Friday, the president said he would immediately sever ties — and funding — to the World Health Organization because of its relationship with China.

".to_string()), link: "https://www.npr.org/sections/goatsandsoda/2020/05/29/865816855/trump-says-u-s-will-withdraw-from-who-does-he-have-the-authority-to-do-it?utm_medium=JSONFeed&utm_campaign=world".to_string(), author: Some("Pien Huang".to_string()), guid: Some("865816855".to_string()), publication_date: DateTime::parse_from_rfc3339("2020-05-29T23:30:03Z").unwrap().into() }, FetchedFeedItem { title: "France Eases Some Pandemic Restrictions And Will Reopen Restaurants, Bars And Parks".to_string(), description: Some("

\"It will be so nice to be able to go lie on the grass in a park and have a picnic or to sit at a sidewalk cafe again,\" says a Paris resident. Restaurants and bars will reopen with restrictions June 2.

".to_string()), link: "https://www.npr.org/sections/coronavirus-live-updates/2020/05/29/864892887/france-eases-some-pandemic-restrictions-and-will-reopen-restaurants-bars-and-par?utm_medium=JSONFeed&utm_campaign=world".to_string(), author: Some("Eleanor Beardsley".to_string()), guid: Some("864892887".to_string()), publication_date: DateTime::parse_from_rfc3339("2020-05-29T20:00:34Z").unwrap().into() }, FetchedFeedItem { title: "Moscow Doubles Last Month\'s Coronavirus Death Toll Amid Suspicions Of Undercounting".to_string(), description: Some("

Media reports and analysts have questioned the accuracy of Russia\'s mortality figures for the virus. Moscow\'s Health Department now says 1,561 people died in April due to the coronavirus.

".to_string()), link: "https://www.npr.org/sections/coronavirus-live-updates/2020/05/29/865044503/moscow-doubles-last-months-coronavirus-death-toll-amid-suspicions-of-undercounti?utm_medium=JSONFeed&utm_campaign=world".to_string(), author: Some("Jason Slotkin".to_string()), guid: Some("865044503".to_string()), publication_date: DateTime::parse_from_rfc3339("2020-05-29T19:35:00Z").unwrap().into() }] }; + let expected_result = FetchedFeed { + title: "World".to_string(), + link: "".to_string(), + description: "NPR world news, international art and culture, world business and financial markets, world economy, and global trends in health, science and technology. Subscribe to the World Story of the Day podcast and RSS feed.".into(), + feed_type: "json".to_string(), + items: vec![ + FetchedFeedItem { + title: "Trump Says U.S. Will Withdraw From WHO. Does He Have The Authority To Do It?".to_string(), + description: Some("In a press conference on Friday, the president said he would immediately sever ties — and funding — to the World Health Organization because of its relationship with China.".to_string()), + link: "https://www.npr.org/sections/goatsandsoda/2020/05/29/865816855/trump-says-u-s-will-withdraw-from-who-does-he-have-the-authority-to-do-it?utm_medium=JSONFeed&utm_campaign=world".to_string(), + author: Some("Pien Huang".to_string()), + guid: Some("865816855".to_string()), + publication_date: DateTime::parse_from_rfc3339("2020-05-29T23:30:03Z").unwrap().into() }, + FetchedFeedItem { + title: "France Eases Some Pandemic Restrictions And Will Reopen Restaurants, Bars And Parks".to_string(), + description: Some("\"It will be so nice to be able to go lie on the grass in a park and have a picnic or to sit at a sidewalk cafe again,\" says a Paris resident. Restaurants and bars will reopen with restrictions June 2.".to_string()), + link: "https://www.npr.org/sections/coronavirus-live-updates/2020/05/29/864892887/france-eases-some-pandemic-restrictions-and-will-reopen-restaurants-bars-and-par?utm_medium=JSONFeed&utm_campaign=world".to_string(), + author: Some("Eleanor Beardsley".to_string()), + guid: Some("864892887".to_string()), + publication_date: DateTime::parse_from_rfc3339("2020-05-29T20:00:34Z").unwrap().into() }, + FetchedFeedItem { + title: "Moscow Doubles Last Month\'s Coronavirus Death Toll Amid Suspicions Of Undercounting".to_string(), + description: Some("Media reports and analysts have questioned the accuracy of Russia\'s mortality figures for the virus. Moscow\'s Health Department now says 1,561 people died in April due to the coronavirus.".to_string()), + link: "https://www.npr.org/sections/coronavirus-live-updates/2020/05/29/865044503/moscow-doubles-last-months-coronavirus-death-toll-amid-suspicions-of-undercounti?utm_medium=JSONFeed&utm_campaign=world".to_string(), + author: Some("Jason Slotkin".to_string()), + guid: Some("865044503".to_string()), + publication_date: DateTime::parse_from_rfc3339("2020-05-29T19:35:00Z").unwrap().into() } + ] + }; + + assert_eq!(expected_result, fetched_feed); + } + + #[test] + fn it_converts_atom_feed_to_fetched_feed() { + let atom_feed = fs::read_to_string("./tests/support/atom_feed_example.xml").unwrap(); + let feed = parser::parse(atom_feed.as_bytes()).unwrap(); + + let fetched_feed: FetchedFeed = feed.into(); + + let expected_result = FetchedFeed { + title: "Example Feed".into(), + link: "".into(), + description: "".to_string(), + feed_type: "atom".to_string(), + items: vec![FetchedFeedItem { + title: "Atom-Powered Robots Run Amok".to_string(), + description: Some("Some text.".into()), + link: "http://example.org/2003/12/13/atom03".into(), + author: Some("".into()), + guid: Some("urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a".into()), + publication_date: DateTime::parse_from_rfc3339("2003-12-13T18:30:02Z") + .unwrap() + .into(), + }], + }; assert_eq!(expected_result, fetched_feed); } diff --git a/tests/support/rss_feed_example.xml b/tests/support/rss_feed_example.xml index 57ea10d5..5a1ce9c5 100644 --- a/tests/support/rss_feed_example.xml +++ b/tests/support/rss_feed_example.xml @@ -63,89 +63,5 @@ Job Postings </i></font> http://www.feedforall.com/forum Tue, 19 Oct 2004 11:09:07 -0400 - - RSS Solutions for Governments - FeedForAll helps Governments communicate with the general public about positions on various issues, and keep the community aware of changes in important legislative issues. <b><i><br> -</b></i><br> -RSS uses Include:<br> -<i><font color="#00FF00">Legislative Calendar<br> -Votes<br> -Bulletins</i></font> - http://www.feedforall.com/government.htm - Computers/Software/Internet/Site Management/Content Management - http://www.feedforall.com/forum - Tue, 19 Oct 2004 11:09:05 -0400 - - - RSS Solutions for Politicians - FeedForAll helps Politicians communicate with the general public about positions on various issues, and keep the community notified of their schedule. <br> -<br> -Uses Include:<br> -<i><font color="#FF0000">Blogs<br> -Speaking Engagements <br> -Statements<br> - </i></font> - http://www.feedforall.com/politics.htm - Computers/Software/Internet/Site Management/Content Management - http://www.feedforall.com/forum - Tue, 19 Oct 2004 11:09:03 -0400 - - - RSS Solutions for Meteorologists - FeedForAll helps Meteorologists communicate with the general public about storm warnings and weather alerts, in specific regions. Using RSS meteorologists are able to quickly disseminate urgent and life threatening weather warnings. <br> -<br> -Uses Include:<br> -<i><font color="#0000FF">Weather Alerts<br> -Plotting Storms<br> -School Cancellations </i></font> - http://www.feedforall.com/weather.htm - Computers/Software/Internet/Site Management/Content Management - http://www.feedforall.com/forum - Tue, 19 Oct 2004 11:09:01 -0400 - - - RSS Solutions for Realtors & Real Estate Firms - FeedForAll helps Realtors and Real Estate companies communicate with clients informing them of newly available properties, and open house announcements. RSS helps to reach a targeted audience and spread the word in an inexpensive, professional manner. <font color="#0000FF"><br> -</font><br> -Feeds can be used for:<br> -<i><font color="#FF0000">Open House Dates<br> -New Properties For Sale<br> -Mortgage Rates</i></font> - http://www.feedforall.com/real-estate.htm - Computers/Software/Internet/Site Management/Content Management - http://www.feedforall.com/forum - Tue, 19 Oct 2004 11:08:59 -0400 - - - RSS Solutions for Banks / Mortgage Companies - FeedForAll helps <b>Banks, Credit Unions and Mortgage companies</b> communicate with the general public about rate changes in a prompt and professional manner. <br> -<br> -Uses include:<br> -<i><font color="#0000FF">Mortgage Rates<br> -Foreign Exchange Rates <br> -Bank Rates<br> -Specials</i></font> - http://www.feedforall.com/banks.htm - Computers/Software/Internet/Site Management/Content Management - http://www.feedforall.com/forum - Tue, 19 Oct 2004 11:08:57 -0400 - - - RSS Solutions for Law Enforcement - <b>FeedForAll</b> helps Law Enforcement Professionals communicate with the general public and other agencies in a prompt and efficient manner. Using RSS police are able to quickly disseminate urgent and life threatening information. <br> -<br> -Uses include:<br> -<i><font color="#0000FF">Amber Alerts<br> -Sex Offender Community Notification <br> -Weather Alerts <br> -Scheduling <br> -Security Alerts <br> -Police Report <br> -Meetings</i></font> - http://www.feedforall.com/law-enforcement.htm - Computers/Software/Internet/Site Management/Content Management - http://www.feedforall.com/forum - Tue, 19 Oct 2004 11:08:56 -0400 - - \ No newline at end of file +