added timestamp for feed items
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
// extern crate bcrypt;
|
||||
|
||||
// use bcrypt::{hash, DEFAULT_COST};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::Insertable;
|
||||
// use uuid::Uuid;
|
||||
|
||||
use crate::schema::feed_item;
|
||||
|
||||
@@ -13,15 +10,23 @@ pub struct NewFeedItem {
|
||||
pub content: String,
|
||||
pub title: String,
|
||||
pub url: String,
|
||||
pub created_ts: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
impl NewFeedItem {
|
||||
pub fn new(feed_id: i32, content: String, title: String, url: String) -> Self {
|
||||
pub fn new(
|
||||
feed_id: i32,
|
||||
content: String,
|
||||
title: String,
|
||||
url: String,
|
||||
created_ts: Option<NaiveDateTime>,
|
||||
) -> Self {
|
||||
Self {
|
||||
feed_id,
|
||||
content,
|
||||
title,
|
||||
url,
|
||||
created_ts,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::models::feed::rss_feed::Feed;
|
||||
use diesel::{Associations, Identifiable, Queryable};
|
||||
|
||||
use crate::schema::feed_item;
|
||||
#[derive(Clone, Queryable, Identifiable, Associations)]
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{Associations, Identifiable, Queryable};
|
||||
#[derive(Clone, Identifiable, Queryable, Associations)]
|
||||
#[diesel(belongs_to(Feed))]
|
||||
#[diesel(table_name=feed_item)]
|
||||
pub struct FeedItem {
|
||||
@@ -12,6 +12,7 @@ pub struct FeedItem {
|
||||
pub read: bool,
|
||||
pub title: String,
|
||||
pub url: String,
|
||||
pub created_ts: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
impl FeedItem {
|
||||
@@ -22,6 +23,7 @@ impl FeedItem {
|
||||
url: String,
|
||||
content: String,
|
||||
read: bool,
|
||||
created_ts: Option<NaiveDateTime>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
@@ -30,6 +32,7 @@ impl FeedItem {
|
||||
url,
|
||||
content,
|
||||
read,
|
||||
created_ts,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-4
@@ -10,6 +10,7 @@ use crate::{
|
||||
schema::feed_item,
|
||||
};
|
||||
use actix_web::{web, HttpRequest, Responder};
|
||||
use chrono::Local;
|
||||
use diesel::prelude::*;
|
||||
|
||||
use super::structs::article::Article;
|
||||
@@ -41,10 +42,17 @@ pub async fn get(path: web::Path<JsonUser>, req: HttpRequest) -> impl Responder
|
||||
|
||||
let article_list: Vec<Article> = existing_item
|
||||
.into_iter()
|
||||
.map(|feed_item: FeedItem| Article {
|
||||
title: feed_item.title,
|
||||
content: feed_item.content,
|
||||
url: feed_item.url,
|
||||
.map(|feed_item: FeedItem| {
|
||||
let time: String = match feed_item.created_ts {
|
||||
Some(r) => r.to_string(),
|
||||
None => Local::now().naive_local().to_string(),
|
||||
};
|
||||
Article {
|
||||
title: feed_item.title,
|
||||
content: feed_item.content,
|
||||
url: feed_item.url,
|
||||
timestamp: time,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ pub struct Article {
|
||||
pub title: String,
|
||||
pub content: String,
|
||||
pub url: String,
|
||||
pub timestamp: String,
|
||||
}
|
||||
|
||||
// impl Article {
|
||||
|
||||
+11
-3
@@ -12,13 +12,14 @@ use crate::{
|
||||
},
|
||||
};
|
||||
use actix_web::{web, HttpRequest, HttpResponse, Responder};
|
||||
use chrono::{Local, NaiveDateTime};
|
||||
use diesel::prelude::*;
|
||||
use rss::Item;
|
||||
use scraper::{Html, Selector};
|
||||
|
||||
fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) {
|
||||
let item_title = item.title.unwrap();
|
||||
let frag = Html::parse_fragment(&item.content.unwrap());
|
||||
let item_title = item.title.clone().unwrap();
|
||||
let frag = Html::parse_fragment(&item.content.clone().unwrap());
|
||||
let mut content = "".to_string();
|
||||
let frag_clone = frag.clone();
|
||||
frag.tree.into_iter().for_each(|node| {
|
||||
@@ -40,13 +41,20 @@ fn create_feed_item(item: Item, feed: &Feed, connection: &mut PgConnection) {
|
||||
.filter(title.eq(&item_title))
|
||||
.load(connection)
|
||||
.unwrap();
|
||||
|
||||
// todo;
|
||||
if existing_item.is_empty() {
|
||||
log::info!("{:?}", item.pub_date());
|
||||
let mut time: NaiveDateTime = Local::now().naive_local();
|
||||
if item.pub_date().is_some() {
|
||||
let format_string = "%a, %d %b %Y %H:%M:%S %z";
|
||||
time = NaiveDateTime::parse_from_str(item.pub_date().unwrap(), format_string).unwrap();
|
||||
}
|
||||
let new_feed_item = NewFeedItem::new(
|
||||
feed.id,
|
||||
content.clone(),
|
||||
item_title.clone(),
|
||||
item.link.unwrap(),
|
||||
Some(time),
|
||||
);
|
||||
let insert_result = diesel::insert_into(feed_item::table)
|
||||
.values(&new_feed_item)
|
||||
|
||||
@@ -17,6 +17,7 @@ diesel::table! {
|
||||
read -> Bool,
|
||||
title -> Varchar,
|
||||
url -> Varchar,
|
||||
created_ts -> Nullable<Timestamp>,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user