zulip.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. use crate::db::notifications::add_metadata;
  2. use crate::db::notifications::{self, delete_ping, move_indices, record_ping, Identifier};
  3. use crate::github::{self, GithubClient};
  4. use crate::handlers::Context;
  5. use anyhow::Context as _;
  6. use std::convert::TryInto;
  7. use std::env;
  8. use std::io::Write as _;
  9. #[derive(Debug, serde::Deserialize)]
  10. pub struct Request {
  11. /// Markdown body of the sent message.
  12. data: String,
  13. /// Metadata about this request.
  14. message: Message,
  15. /// Authentication token. The same for all Zulip messages.
  16. token: String,
  17. }
  18. #[derive(Debug, serde::Deserialize)]
  19. struct Message {
  20. sender_id: u64,
  21. recipient_id: u64,
  22. sender_short_name: String,
  23. sender_full_name: String,
  24. stream_id: Option<u64>,
  25. topic: Option<String>,
  26. #[serde(rename = "type")]
  27. type_: String,
  28. }
  29. #[derive(serde::Serialize, serde::Deserialize)]
  30. struct Response<'a> {
  31. content: &'a str,
  32. }
  33. #[derive(serde::Serialize, serde::Deserialize)]
  34. struct ResponseOwned {
  35. content: String,
  36. }
  37. pub const BOT_EMAIL: &str = "triage-rust-lang-bot@zulipchat.com";
  38. pub async fn to_github_id(client: &GithubClient, zulip_id: usize) -> anyhow::Result<Option<i64>> {
  39. let map = crate::team_data::zulip_map(client).await?;
  40. Ok(map.users.get(&zulip_id).map(|v| *v as i64))
  41. }
  42. pub async fn to_zulip_id(client: &GithubClient, github_id: i64) -> anyhow::Result<Option<usize>> {
  43. let map = crate::team_data::zulip_map(client).await?;
  44. Ok(map
  45. .users
  46. .iter()
  47. .find(|(_, github)| **github == github_id as usize)
  48. .map(|v| *v.0))
  49. }
  50. pub async fn respond(ctx: &Context, req: Request) -> String {
  51. let expected_token = std::env::var("ZULIP_TOKEN").expect("`ZULIP_TOKEN` set for authorization");
  52. if !openssl::memcmp::eq(req.token.as_bytes(), expected_token.as_bytes()) {
  53. return serde_json::to_string(&Response {
  54. content: "Invalid authorization.",
  55. })
  56. .unwrap();
  57. }
  58. log::trace!("zulip hook: {:?}", req);
  59. let gh_id = match to_github_id(&ctx.github, req.message.sender_id as usize).await {
  60. Ok(Some(gh_id)) => Ok(gh_id),
  61. Ok(None) => Err(serde_json::to_string(&Response {
  62. content: &format!(
  63. "Unknown Zulip user. Please add `zulip-id = {}` to your file in rust-lang/team.",
  64. req.message.sender_id
  65. ),
  66. })
  67. .unwrap()),
  68. Err(e) => {
  69. return serde_json::to_string(&Response {
  70. content: &format!("Failed to query team API: {:?}", e),
  71. })
  72. .unwrap();
  73. }
  74. };
  75. handle_command(ctx, gh_id, &req.data, &req.message).await
  76. }
  77. fn handle_command<'a>(
  78. ctx: &'a Context,
  79. gh_id: Result<i64, String>,
  80. words: &'a str,
  81. message_data: &'a Message,
  82. ) -> std::pin::Pin<Box<dyn std::future::Future<Output = String> + Send + 'a>> {
  83. Box::pin(async move {
  84. let mut words = words.split_whitespace();
  85. let next = words.next();
  86. if let Some("as") = next {
  87. return match execute_for_other_user(&ctx, words, message_data).await {
  88. Ok(r) => r,
  89. Err(e) => serde_json::to_string(&Response {
  90. content: &format!(
  91. "Failed to parse; expected `as <username> <command...>`: {:?}.",
  92. e
  93. ),
  94. })
  95. .unwrap(),
  96. };
  97. }
  98. let gh_id = match gh_id {
  99. Ok(id) => id,
  100. Err(e) => return e,
  101. };
  102. match next {
  103. Some("acknowledge") | Some("ack") => match acknowledge(gh_id, words).await {
  104. Ok(r) => r,
  105. Err(e) => serde_json::to_string(&Response {
  106. content: &format!(
  107. "Failed to parse acknowledgement, expected `(acknowledge|ack) <identifier>`: {:?}.",
  108. e
  109. ),
  110. })
  111. .unwrap(),
  112. },
  113. Some("add") => match add_notification(&ctx, gh_id, words).await {
  114. Ok(r) => r,
  115. Err(e) => serde_json::to_string(&Response {
  116. content: &format!(
  117. "Failed to parse description addition, expected `add <url> <description (multiple words)>`: {:?}.",
  118. e
  119. ),
  120. })
  121. .unwrap(),
  122. },
  123. Some("move") => match move_notification(gh_id, words).await {
  124. Ok(r) => r,
  125. Err(e) => serde_json::to_string(&Response {
  126. content: &format!(
  127. "Failed to parse movement, expected `move <from> <to>`: {:?}.",
  128. e
  129. ),
  130. })
  131. .unwrap(),
  132. },
  133. Some("meta") => match add_meta_notification(gh_id, words).await {
  134. Ok(r) => r,
  135. Err(e) => serde_json::to_string(&Response {
  136. content: &format!(
  137. "Failed to parse movement, expected `move <idx> <meta...>`: {:?}.",
  138. e
  139. ),
  140. })
  141. .unwrap(),
  142. },
  143. _ => serde_json::to_string(&Response {
  144. content: "Unknown command.",
  145. })
  146. .unwrap(),
  147. }
  148. })
  149. }
  150. // This does two things:
  151. // * execute the command for the other user
  152. // * tell the user executed for that a command was run as them by the user
  153. // given.
  154. async fn execute_for_other_user(
  155. ctx: &Context,
  156. mut words: impl Iterator<Item = &str>,
  157. message_data: &Message,
  158. ) -> anyhow::Result<String> {
  159. // username is a GitHub username, not a Zulip username
  160. let username = match words.next() {
  161. Some(username) => username,
  162. None => anyhow::bail!("no username provided"),
  163. };
  164. let user_id = match (github::User {
  165. login: username.to_owned(),
  166. id: None,
  167. })
  168. .get_id(&ctx.github)
  169. .await
  170. .context("getting ID of github user")?
  171. {
  172. Some(id) => id.try_into().unwrap(),
  173. None => {
  174. return Ok(serde_json::to_string(&Response {
  175. content: "Can only authorize for other GitHub users.",
  176. })
  177. .unwrap());
  178. }
  179. };
  180. let mut command = words.fold(String::new(), |mut acc, piece| {
  181. acc.push_str(piece);
  182. acc.push(' ');
  183. acc
  184. });
  185. let command = if command.is_empty() {
  186. anyhow::bail!("no command provided")
  187. } else {
  188. assert_eq!(command.pop(), Some(' ')); // pop trailing space
  189. command
  190. };
  191. let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
  192. let members = ctx
  193. .github
  194. .raw()
  195. .get("https://rust-lang.zulipchat.com/api/v1/users")
  196. .basic_auth(BOT_EMAIL, Some(&bot_api_token))
  197. .send()
  198. .await;
  199. let members = match members {
  200. Ok(members) => members,
  201. Err(e) => {
  202. return Ok(serde_json::to_string(&Response {
  203. content: &format!("Failed to get list of zulip users: {:?}.", e),
  204. })
  205. .unwrap());
  206. }
  207. };
  208. let members = members.json::<MembersApiResponse>().await;
  209. let members = match members {
  210. Ok(members) => members.members,
  211. Err(e) => {
  212. return Ok(serde_json::to_string(&Response {
  213. content: &format!("Failed to get list of zulip users: {:?}.", e),
  214. })
  215. .unwrap());
  216. }
  217. };
  218. // Map GitHub `user_id` to `zulip_user_id`.
  219. let zulip_user_id = match to_zulip_id(&ctx.github, user_id).await {
  220. Ok(Some(id)) => id as u64,
  221. Ok(None) => {
  222. return Ok(serde_json::to_string(&Response {
  223. content: &format!("Could not find Zulip ID for GitHub ID: {}", user_id),
  224. })
  225. .unwrap());
  226. }
  227. Err(e) => {
  228. return Ok(serde_json::to_string(&Response {
  229. content: &format!("Could not find Zulip ID for GitHub id {}: {:?}", user_id, e),
  230. })
  231. .unwrap());
  232. }
  233. };
  234. let user = match members.iter().find(|m| m.user_id == zulip_user_id) {
  235. Some(m) => m,
  236. None => {
  237. return Ok(serde_json::to_string(&Response {
  238. content: &format!("Could not find Zulip user email."),
  239. })
  240. .unwrap());
  241. }
  242. };
  243. let output = handle_command(ctx, Ok(user_id as i64), &command, message_data).await;
  244. let output_msg: ResponseOwned =
  245. serde_json::from_str(&output).expect("result should always be JSON");
  246. let output_msg = output_msg.content;
  247. // At this point, the command has been run (FIXME: though it may have
  248. // errored, it's hard to determine that currently, so we'll just give the
  249. // output fromt he command as well as the command itself).
  250. let message = format!(
  251. "{} ({}) ran `{}` with output `{}` as you.",
  252. message_data.sender_full_name, message_data.sender_short_name, command, output_msg
  253. );
  254. let res = MessageApiRequest {
  255. recipient: Recipient::Private {
  256. id: user.user_id,
  257. email: &user.email,
  258. },
  259. content: &message,
  260. }
  261. .send(ctx.github.raw())
  262. .await;
  263. match res {
  264. Ok(resp) => {
  265. if !resp.status().is_success() {
  266. log::error!(
  267. "Failed to notify real user about command: response: {:?}",
  268. resp
  269. );
  270. }
  271. }
  272. Err(err) => {
  273. log::error!("Failed to notify real user about command: {:?}", err);
  274. }
  275. }
  276. Ok(output)
  277. }
  278. #[derive(serde::Deserialize)]
  279. struct MembersApiResponse {
  280. members: Vec<Member>,
  281. }
  282. #[derive(serde::Deserialize)]
  283. struct Member {
  284. email: String,
  285. user_id: u64,
  286. }
  287. #[derive(serde::Serialize)]
  288. #[serde(tag = "type")]
  289. #[serde(rename_all = "snake_case")]
  290. pub enum Recipient<'a> {
  291. Stream {
  292. #[serde(rename = "to")]
  293. id: u64,
  294. topic: &'a str,
  295. },
  296. Private {
  297. #[serde(skip)]
  298. id: u64,
  299. #[serde(rename = "to")]
  300. email: &'a str,
  301. },
  302. }
  303. impl Recipient<'_> {
  304. pub fn narrow(&self) -> String {
  305. match self {
  306. Recipient::Stream { id, topic } => {
  307. // See
  308. // https://github.com/zulip/zulip/blob/46247623fc279/zerver/lib/url_encoding.py#L9
  309. // ALWAYS_SAFE without `.` from
  310. // https://github.com/python/cpython/blob/113e2b0a07c/Lib/urllib/parse.py#L772-L775
  311. //
  312. // ALWAYS_SAFE doesn't contain `.` because Zulip actually encodes them to be able
  313. // to use `.` instead of `%` in the encoded strings
  314. const ALWAYS_SAFE: &str =
  315. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-~";
  316. let mut encoded_topic = Vec::new();
  317. for ch in topic.bytes() {
  318. if !(ALWAYS_SAFE.contains(ch as char)) {
  319. write!(encoded_topic, "%{:02X}", ch).unwrap();
  320. } else {
  321. encoded_topic.push(ch);
  322. }
  323. }
  324. let mut encoded_topic = String::from_utf8(encoded_topic).unwrap();
  325. encoded_topic = encoded_topic.replace("%", ".");
  326. format!("stream/{}-xxx/topic/{}", id, encoded_topic)
  327. }
  328. Recipient::Private { id, .. } => format!("pm-with/{}-xxx", id),
  329. }
  330. }
  331. }
  332. #[derive(serde::Serialize)]
  333. pub struct MessageApiRequest<'a> {
  334. pub recipient: Recipient<'a>,
  335. pub content: &'a str,
  336. }
  337. impl<'a> MessageApiRequest<'a> {
  338. pub fn url(&self) -> String {
  339. format!(
  340. "https://rust-lang.zulipchat.com/#narrow/{}",
  341. self.recipient.narrow()
  342. )
  343. }
  344. pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
  345. let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
  346. #[derive(serde::Serialize)]
  347. struct SerializedApi<'a> {
  348. #[serde(rename = "type")]
  349. type_: &'static str,
  350. to: String,
  351. #[serde(skip_serializing_if = "Option::is_none")]
  352. topic: Option<&'a str>,
  353. content: &'a str,
  354. }
  355. Ok(client
  356. .post("https://rust-lang.zulipchat.com/api/v1/messages")
  357. .basic_auth(BOT_EMAIL, Some(&bot_api_token))
  358. .form(&SerializedApi {
  359. type_: match self.recipient {
  360. Recipient::Stream { .. } => "stream",
  361. Recipient::Private { .. } => "private",
  362. },
  363. to: match self.recipient {
  364. Recipient::Stream { id, .. } => id.to_string(),
  365. Recipient::Private { email, .. } => email.to_string(),
  366. },
  367. topic: match self.recipient {
  368. Recipient::Stream { topic, .. } => Some(topic),
  369. Recipient::Private { .. } => None,
  370. },
  371. content: self.content,
  372. })
  373. .send()
  374. .await?)
  375. }
  376. }
  377. async fn acknowledge(gh_id: i64, mut words: impl Iterator<Item = &str>) -> anyhow::Result<String> {
  378. let url = match words.next() {
  379. Some(url) => {
  380. if words.next().is_some() {
  381. anyhow::bail!("too many words");
  382. }
  383. url
  384. }
  385. None => anyhow::bail!("not enough words"),
  386. };
  387. let ident = if let Ok(number) = url.parse::<usize>() {
  388. Identifier::Index(
  389. std::num::NonZeroUsize::new(number)
  390. .ok_or_else(|| anyhow::anyhow!("index must be at least 1"))?,
  391. )
  392. } else {
  393. Identifier::Url(url)
  394. };
  395. match delete_ping(&mut crate::db::make_client().await?, gh_id, ident).await {
  396. Ok(deleted) => {
  397. let mut resp = format!("Acknowledged:\n");
  398. for deleted in deleted {
  399. resp.push_str(&format!(
  400. " * [{}]({}){}\n",
  401. deleted
  402. .short_description
  403. .as_deref()
  404. .unwrap_or(&deleted.origin_url),
  405. deleted.origin_url,
  406. deleted
  407. .metadata
  408. .map_or(String::new(), |m| format!(" ({})", m)),
  409. ));
  410. }
  411. Ok(serde_json::to_string(&Response { content: &resp }).unwrap())
  412. }
  413. Err(e) => Ok(serde_json::to_string(&Response {
  414. content: &format!("Failed to acknowledge {}: {:?}.", url, e),
  415. })
  416. .unwrap()),
  417. }
  418. }
  419. async fn add_notification(
  420. ctx: &Context,
  421. gh_id: i64,
  422. mut words: impl Iterator<Item = &str>,
  423. ) -> anyhow::Result<String> {
  424. let url = match words.next() {
  425. Some(idx) => idx,
  426. None => anyhow::bail!("url not present"),
  427. };
  428. let mut description = words.fold(String::new(), |mut acc, piece| {
  429. acc.push_str(piece);
  430. acc.push(' ');
  431. acc
  432. });
  433. let description = if description.is_empty() {
  434. None
  435. } else {
  436. assert_eq!(description.pop(), Some(' ')); // pop trailing space
  437. Some(description)
  438. };
  439. match record_ping(
  440. &ctx.db,
  441. &notifications::Notification {
  442. user_id: gh_id,
  443. origin_url: url.to_owned(),
  444. origin_html: String::new(),
  445. short_description: description,
  446. time: chrono::Utc::now().into(),
  447. team_name: None,
  448. },
  449. )
  450. .await
  451. {
  452. Ok(()) => Ok(serde_json::to_string(&Response {
  453. content: "Created!",
  454. })
  455. .unwrap()),
  456. Err(e) => Ok(serde_json::to_string(&Response {
  457. content: &format!("Failed to create: {:?}", e),
  458. })
  459. .unwrap()),
  460. }
  461. }
  462. async fn add_meta_notification(
  463. gh_id: i64,
  464. mut words: impl Iterator<Item = &str>,
  465. ) -> anyhow::Result<String> {
  466. let idx = match words.next() {
  467. Some(idx) => idx,
  468. None => anyhow::bail!("idx not present"),
  469. };
  470. let idx = idx
  471. .parse::<usize>()
  472. .context("index")?
  473. .checked_sub(1)
  474. .ok_or_else(|| anyhow::anyhow!("1-based indexes"))?;
  475. let mut description = words.fold(String::new(), |mut acc, piece| {
  476. acc.push_str(piece);
  477. acc.push(' ');
  478. acc
  479. });
  480. let description = if description.is_empty() {
  481. None
  482. } else {
  483. assert_eq!(description.pop(), Some(' ')); // pop trailing space
  484. Some(description)
  485. };
  486. match add_metadata(
  487. &mut crate::db::make_client().await?,
  488. gh_id,
  489. idx,
  490. description.as_deref(),
  491. )
  492. .await
  493. {
  494. Ok(()) => Ok(serde_json::to_string(&Response {
  495. content: "Added metadata!",
  496. })
  497. .unwrap()),
  498. Err(e) => Ok(serde_json::to_string(&Response {
  499. content: &format!("Failed to add: {:?}", e),
  500. })
  501. .unwrap()),
  502. }
  503. }
  504. async fn move_notification(
  505. gh_id: i64,
  506. mut words: impl Iterator<Item = &str>,
  507. ) -> anyhow::Result<String> {
  508. let from = match words.next() {
  509. Some(idx) => idx,
  510. None => anyhow::bail!("from idx not present"),
  511. };
  512. let to = match words.next() {
  513. Some(idx) => idx,
  514. None => anyhow::bail!("from idx not present"),
  515. };
  516. let from = from
  517. .parse::<usize>()
  518. .context("from index")?
  519. .checked_sub(1)
  520. .ok_or_else(|| anyhow::anyhow!("1-based indexes"))?;
  521. let to = to
  522. .parse::<usize>()
  523. .context("to index")?
  524. .checked_sub(1)
  525. .ok_or_else(|| anyhow::anyhow!("1-based indexes"))?;
  526. match move_indices(&mut crate::db::make_client().await?, gh_id, from, to).await {
  527. Ok(()) => Ok(serde_json::to_string(&Response {
  528. // to 1-base indices
  529. content: &format!("Moved {} to {}.", from + 1, to + 1),
  530. })
  531. .unwrap()),
  532. Err(e) => Ok(serde_json::to_string(&Response {
  533. content: &format!("Failed to move: {:?}.", e),
  534. })
  535. .unwrap()),
  536. }
  537. }