issue-bot/src/cron.rs

110 lines
4.2 KiB
Rust
Raw Normal View History

2020-09-10 22:33:34 +03:00
/* This file is part of issue-bot.
*
* issue-bot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* issue-bot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with issue-bot. If not, see <https://www.gnu.org/licenses/>.
*/
2019-09-20 12:59:13 +03:00
use super::*;
2020-09-10 22:33:34 +03:00
use melib::email::address::Address;
2019-09-20 12:59:13 +03:00
2020-09-10 22:33:34 +03:00
pub fn check(conn: Connection, conf: Configuration) -> Result<()> {
let mut stmt = conn.prepare("SELECT * FROM issue")?;
2019-09-20 12:59:13 +03:00
let mut results = stmt
.query_map(NO_PARAMS, |row| {
let submitter: String = row.get(1)?;
let password: Vec<u8> = row.get(2)?;
let last_update: Option<String> = row.get(7)?;
Ok(Issue {
id: row.get(0)?,
2020-09-10 22:33:34 +03:00
submitter: Address::new(None, submitter.as_str().to_string()),
2019-09-20 12:59:13 +03:00
password: Password::from_slice(password.as_slice()).unwrap(),
time_created: row.get(3)?,
anonymous: row.get(4)?,
subscribed: row.get(5)?,
title: row.get(6)?,
last_update: last_update.unwrap_or(String::new()),
})
2020-09-10 22:33:34 +03:00
})?
.collect::<std::result::Result<Vec<Issue>, _>>()?;
2019-09-20 12:59:13 +03:00
for issue in &mut results {
let mut update = false;
let mut comments = api::comments(issue.id, &issue.last_update, &conf);
let mut new_value = issue.last_update.clone();
comments.retain(|c| {
let created_at = c["created_at"].to_string();
if created_at > issue.last_update {
if created_at > new_value {
new_value = created_at;
update = true;
}
true
} else {
false
}
});
if update {
2020-09-10 22:33:34 +03:00
let mut stmt =
conn.prepare("UPDATE issue SET last_update = (:last_update) WHERE id = (:id)")?;
2019-09-20 12:59:13 +03:00
assert_eq!(
2020-09-10 22:33:34 +03:00
stmt.execute_named(&[(":last_update", &new_value), (":id", &issue.id),])?,
2019-09-20 12:59:13 +03:00
1
);
if issue.subscribed {
let comments = comments
.into_iter()
.map(|c| {
if c["user"]["login"].as_str().unwrap() == &conf.bot_username {
c["body"].as_str().unwrap().to_string()
} else {
format!(
"User {} replied:\n\n{}",
c["user"]["login"],
c["body"].as_str().unwrap()
)
}
})
.collect::<Vec<String>>();
let mut notice = melib::Draft::default();
notice.headers_mut().insert(
2020-09-10 22:33:34 +03:00
HeaderName::new_unchecked("From"),
Address::new(
None,
format!(
"{local_part}@{domain}",
local_part = &conf.local_part,
domain = &conf.domain
),
)
2019-09-20 12:59:13 +03:00
.to_string(),
);
notice.headers_mut().insert(
2020-09-10 22:33:34 +03:00
HeaderName::new_unchecked("Subject"),
2019-09-20 12:59:13 +03:00
format!(
"[{tag}] new replies in issue `{title}`",
tag = &conf.tag,
title = &issue.title
)
.to_string(),
);
notice
.headers_mut()
2020-09-10 22:33:34 +03:00
.insert(HeaderName::new_unchecked("To"), issue.submitter.to_string());
2019-09-20 12:59:13 +03:00
notice.set_body(templates::reply_update(&issue, &conf, comments));
send_mail(notice, &conf);
}
}
}
2020-09-10 22:33:34 +03:00
Ok(())
2019-09-20 12:59:13 +03:00
}