A bot that allows users to post issues on a Gitea instance without registering simply by acting as a medium.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.4 KiB

2 years ago
  1. use super::*;
  2. pub fn check(conn: Connection, conf: Config) {
  3. let mut stmt = conn.prepare("SELECT * FROM issue").unwrap();
  4. let mut results = stmt
  5. .query_map(NO_PARAMS, |row| {
  6. let submitter: String = row.get(1)?;
  7. let password: Vec<u8> = row.get(2)?;
  8. let last_update: Option<String> = row.get(7)?;
  9. Ok(Issue {
  10. id: row.get(0)?,
  11. submitter: new_address(submitter.as_str()),
  12. password: Password::from_slice(password.as_slice()).unwrap(),
  13. time_created: row.get(3)?,
  14. anonymous: row.get(4)?,
  15. subscribed: row.get(5)?,
  16. title: row.get(6)?,
  17. last_update: last_update.unwrap_or(String::new()),
  18. })
  19. })
  20. .unwrap()
  21. .map(|r| r.unwrap())
  22. .collect::<Vec<Issue>>();
  23. for issue in &mut results {
  24. let mut update = false;
  25. let mut comments = api::comments(issue.id, &issue.last_update, &conf);
  26. let mut new_value = issue.last_update.clone();
  27. comments.retain(|c| {
  28. let created_at = c["created_at"].to_string();
  29. if created_at > issue.last_update {
  30. if created_at > new_value {
  31. new_value = created_at;
  32. update = true;
  33. }
  34. true
  35. } else {
  36. false
  37. }
  38. });
  39. if update {
  40. let mut stmt = conn
  41. .prepare("UPDATE issue SET last_update = (:last_update) WHERE id = (:id)")
  42. .unwrap();
  43. assert_eq!(
  44. stmt.execute_named(&[(":last_update", &new_value), (":id", &issue.id),])
  45. .unwrap(),
  46. 1
  47. );
  48. if issue.subscribed {
  49. let comments = comments
  50. .into_iter()
  51. .map(|c| {
  52. if c["user"]["login"].as_str().unwrap() == &conf.bot_username {
  53. c["body"].as_str().unwrap().to_string()
  54. } else {
  55. format!(
  56. "User {} replied:\n\n{}",
  57. c["user"]["login"],
  58. c["body"].as_str().unwrap()
  59. )
  60. }
  61. })
  62. .collect::<Vec<String>>();
  63. let mut notice = melib::Draft::default();
  64. notice.headers_mut().insert(
  65. "From".to_string(),
  66. new_address(&format!(
  67. "{local_part}@{domain}",
  68. local_part = &conf.local_part,
  69. domain = &conf.domain
  70. ))
  71. .to_string(),
  72. );
  73. notice.headers_mut().insert(
  74. "Subject".to_string(),
  75. format!(
  76. "[{tag}] new replies in issue `{title}`",
  77. tag = &conf.tag,
  78. title = &issue.title
  79. )
  80. .to_string(),
  81. );
  82. notice
  83. .headers_mut()
  84. .insert("To".to_string(), issue.submitter.to_string());
  85. notice.set_body(templates::reply_update(&issue, &conf, comments));
  86. send_mail(notice, &conf);
  87. }
  88. }
  89. }
  90. }