Overview
Comment: | fix messages, store edit string |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
10c25017bb7bd7d57ec7e66aa7bc09e8 |
User & Date: | arcade on 2021-11-13 20:37:12.717 |
Other Links: | manifest | tags |
Context
2021-11-13
| ||
21:50 | 0.2.2: finally regex works check-in: fb629f170b user: arcade tags: trunk | |
20:37 | fix messages, store edit string check-in: 10c25017bb user: arcade tags: trunk | |
10:50 | 0.2.1: even more errors now can reach users check-in: 4632d20d39 user: arcade tags: trunk | |
Changes
Modified Cargo.lock
from [22e8372ea2]
to [42d6ae8928].
︙ | ︙ | |||
2044 2045 2046 2047 2048 2049 2050 | "derive_builder 0.9.0", "quick-xml 0.17.2", "reqwest 0.9.24", ] [[package]] name = "rsstg" | | | 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 | "derive_builder 0.9.0", "quick-xml 0.17.2", "reqwest 0.9.24", ] [[package]] name = "rsstg" version = "0.2.1" dependencies = [ "anyhow", "atom_syndication", "chrono", "config", "futures 0.3.17", "futures-util", |
︙ | ︙ |
Modified src/command.rs
from [86f4f1e85f]
to [1ce7bd4b1f].
︙ | ︙ | |||
65 66 67 68 69 70 71 | } let iv_hash = match iv_hash { Some(hash) => { if ! RE_IV_HASH.is_match(hash) { bail!("IV hash should be 14 hex digits.\nNot {:?}", hash); }; Some(*hash) | | > | | > > > | | 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | } let iv_hash = match iv_hash { Some(hash) => { if ! RE_IV_HASH.is_match(hash) { bail!("IV hash should be 14 hex digits.\nNot {:?}", hash); }; Some(*hash) }, None => None, }; let url_re = match url_re { Some(re) => { let _url_rex = ReplaceCommand::new(re).context("Regexp parsing error:")?; Some(*re) }, None => None, }; let channel_id = i64::from(core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await?.id()); let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await .context("Sorry, I have no access to that chat.")?; let (mut me, mut user) = (false, false); for admin in chan_adm { if admin.user.id == core.my.id { me = true; }; if admin.user.id == sender { user = true; }; }; if ! me { bail!("I need to be admin on that channel."); }; if ! user { bail!("You should be admin on that channel."); }; core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None)?; Ok(()) } |
Modified src/core.rs
from [7064860d7e]
to [a89bbcb26a].
1 2 3 4 | use anyhow::{anyhow, bail, Context, Result}; use atom_syndication; use chrono::DateTime; use config; | < | 1 2 3 4 5 6 7 8 9 10 11 | use anyhow::{anyhow, bail, Context, Result}; use atom_syndication; use chrono::DateTime; use config; use reqwest; use sqlx::{ postgres::PgPoolOptions, Row, }; use rss; use std::{ |
︙ | ︙ | |||
99 100 101 102 103 104 105 106 | .bind(*id) .bind(owner) .fetch_one(&mut conn).await .with_context(|| format!("Query source:\n{:?}", &self.pool))?; drop(conn); let channel_id: i64 = row.try_get("channel_id")?; let url: &str = row.try_get("url")?; let iv_hash: Option<&str> = row.try_get("iv_hash")?; | > > > > > | | | | | | | < | | < < | | | | 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | .bind(*id) .bind(owner) .fetch_one(&mut conn).await .with_context(|| format!("Query source:\n{:?}", &self.pool))?; drop(conn); let channel_id: i64 = row.try_get("channel_id")?; let url: &str = row.try_get("url")?; /*let url: Cow<'a, str> = match row.try_get("url") { Ok(x) => String::from(x).to_owned().into(), Err(err) => bail!("Test"), };*/ let iv_hash: Option<&str> = row.try_get("iv_hash")?; //let url_re: Option<sedregex::ReplaceCommand> = match row.try_get("url_re")? { let url_re = match row.try_get("url_re")? { Some(x) => Some(sedregex::ReplaceCommand::new(x)?), None => None, }; let destination = match real { true => telegram_bot::UserId::new(channel_id), false => telegram_bot::UserId::new(row.try_get("owner")?), }; let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None; let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new(); let content = reqwest::get(url).await?.bytes().await?; match rss::Channel::read_from(&content[..]) { Ok(feed) => { for item in feed.items() { match item.link() { Some(link) => { let date = match item.pub_date() { Some(feed_date) => DateTime::parse_from_rfc2822(feed_date), None => DateTime::parse_from_rfc3339(&item.dublin_core_ext().unwrap().dates()[0]), }?; let url = link; posts.insert(date.clone(), url.into()); }, None => {} } }; }, Err(err) => match err { rss::Error::InvalidStartTag => { let feed = atom_syndication::Feed::read_from(&content[..]) .with_context(|| format!("Problem opening feed url:\n{}", &url))?; for item in feed.entries() { let date = item.published().unwrap(); let url = item.links()[0].href(); posts.insert(date.clone(), url.into()); }; }, rss::Error::Eof => (), _ => bail!("Unsupported or mangled content:\n{:?}\n{:#?}\n", &url, err) } }; for (date, url) in posts.iter() { let mut conn = self.pool.acquire().await .with_context(|| format!("Check post fetch conn:\n{:?}", &self.pool))?; let row = sqlx::query("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;") .bind(url) .bind(*id) .fetch_one(&mut conn).await .with_context(|| format!("Check post:\n{:?}", &conn))?; let exists: bool = row.try_get("exists")?; if ! exists { if this_fetch == None || *date > this_fetch.unwrap() { this_fetch = Some(*date); }; self.tg.send( match iv_hash { Some(hash) => telegram_bot::SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", match url_re { Some(x) => { bail!("Regex hit, result:\n{:#?}", x.execute(url)); url }, None => url, }, hash)), None => telegram_bot::SendMessage::new(destination, format!("{}", url)), }.parse_mode(telegram_bot::types::ParseMode::Html)).await .context("Can't post message:")?; sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);") .bind(*id) .bind(date) .bind(url) |
︙ | ︙ | |||
201 202 203 204 205 206 207 | .with_context(|| format!("Delete fetch conn:\n{:?}", &self.pool))?; match sqlx::query("delete from rsstg_source where source_id = $1 and owner = $2;") .bind(source_id) .bind(owner) .execute(&mut conn).await .with_context(|| format!("Delete source rule:\n{:?}", &self.pool))? .rows_affected() { | | | | | | | | | | | | | | | | 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | .with_context(|| format!("Delete fetch conn:\n{:?}", &self.pool))?; match sqlx::query("delete from rsstg_source where source_id = $1 and owner = $2;") .bind(source_id) .bind(owner) .execute(&mut conn).await .with_context(|| format!("Delete source rule:\n{:?}", &self.pool))? .rows_affected() { 0 => { Ok("No data found found.".to_string()) }, x => { Ok(format!("{} sources removed.", x)) }, } } pub async fn clean<S>(&self, source_id: &i32, owner: S) -> Result<String> where S: Into<i64> { let owner: i64 = owner.into(); let mut conn = self.pool.acquire().await .with_context(|| format!("Clean fetch conn:\n{:?}", &self.pool))?; match sqlx::query("delete from rsstg_post p using rsstg_source s where p.source_id = $1 and owner = $2 and p.source_id = s.source_id;") .bind(source_id) .bind(owner) .execute(&mut conn).await .with_context(|| format!("Clean seen posts:\n{:?}", &self.pool))? .rows_affected() { 0 => { Ok("No data found found.".to_string()) }, x => { Ok(format!("{} posts purged.", x)) }, } } pub async fn enable<S>(&self, source_id: &i32, owner: S) -> Result<&str> where S: Into<i64> { let owner: i64 = owner.into(); let mut conn = self.pool.acquire().await .with_context(|| format!("Enable fetch conn:\n{:?}", &self.pool))?; match sqlx::query("update rsstg_source set enabled = true where source_id = $1 and owner = $2") .bind(source_id) .bind(owner) .execute(&mut conn).await .with_context(|| format!("Enable source:\n{:?}", &self.pool))? .rows_affected() { 1 => { Ok("Source enabled.") }, 0 => { Ok("Source not found.") }, _ => { Err(anyhow!("Database error.")) }, } } pub async fn disable<S>(&self, source_id: &i32, owner: S) -> Result<&str> where S: Into<i64> { let owner: i64 = owner.into(); let mut conn = self.pool.acquire().await .with_context(|| format!("Disable fetch conn:\n{:?}", &self.pool))?; match sqlx::query("update rsstg_source set enabled = false where source_id = $1 and owner = $2") .bind(source_id) .bind(owner) .execute(&mut conn).await .with_context(|| format!("Disable source:\n{:?}", &self.pool))? .rows_affected() { 1 => { Ok("Source disabled.") }, 0 => { Ok("Source not found.") }, _ => { Err(anyhow!("Database error.")) }, } } pub async fn update<S>(&self, update: Option<i32>, channel: &str, channel_id: i64, url: &str, iv_hash: Option<&str>, url_re: Option<&str>, owner: S) -> Result<String> where S: Into<i64> { let owner: i64 = owner.into(); let mut conn = self.pool.acquire().await .with_context(|| format!("Update fetch conn:\n{:?}", &self.pool))?; match match update { Some(id) => { sqlx::query("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $5, channel = $6, url_re = $7 where source_id = $1").bind(id) }, None => { sqlx::query("insert into rsstg_source (channel_id, url, iv_hash, owner, channel, url_re) values ($1, $2, $3, $4, $5, $6)") }, } .bind(channel_id) .bind(url) .bind(iv_hash) .bind(owner) .bind(channel) .bind(url_re) .execute(&mut conn).await { Ok(_) => return Ok(String::from(match update { Some(_) => "Channel updated.", None => "Channel added.", })), Err(sqlx::Error::Database(err)) => { match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() { Some("_bt_check_unique", ) => { return Ok("Duplicate key.".to_string()) }, Some(_) => { return Ok("Database error.".to_string()) }, None => { return Ok("No database error extracted.".to_string()) }, }; }, Err(err) => { bail!("Sorry, unknown error:\n{:#?}\n", err); }, }; |
︙ | ︙ |