| 1 | use serde_json::{Value, json}; |
| 2 | use session::{ApiResponse, Method, Request}; |
| 3 | |
| 4 | use super::Pins; |
| 5 | use super::chats::{api_error, ensure_success}; |
| 6 | use super::state::{PinnedChannels, error_text, parse_pinned_channels}; |
| 7 | use super::transport::CsaTransport; |
| 8 | use crate::error::{Error, Result}; |
| 9 | |
| 10 | #[derive(Debug, Clone, PartialEq)] |
| 11 | pub(super) enum ChannelAction { |
| 12 | Pin { channel_id: String }, |
| 13 | Unpin { channel_id: String }, |
| 14 | Reorder { channel_ids: Vec<String> }, |
| 15 | } |
| 16 | |
| 17 | impl ChannelAction { |
| 18 | pub(super) fn method(&self) -> Method { |
| 19 | match self { |
| 20 | ChannelAction::Unpin { .. } => Method::Delete, |
| 21 | _ => Method::Post, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub(super) fn body(&self) -> Value { |
| 26 | match self { |
| 27 | ChannelAction::Pin { channel_id } => json!({"newlyPinnedChannels": [channel_id]}), |
| 28 | ChannelAction::Unpin { channel_id } => json!({"channelIdsToUnpin": [channel_id]}), |
| 29 | ChannelAction::Reorder { channel_ids } => json!({"pinnedChannelOrder": channel_ids}), |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn is_version_mismatch(answer: &ApiResponse) -> bool { |
| 35 | answer.status == 412 |
| 36 | && error_text(&answer.body) |
| 37 | .to_ascii_lowercase() |
| 38 | .contains("versionmismatch") |
| 39 | } |
| 40 | |
| 41 | fn is_missing_from_pin_list(answer: &ApiResponse) -> bool { |
| 42 | answer.status == 412 && error_text(&answer.body).contains("does not include all requested ids") |
| 43 | } |
| 44 | |
| 45 | impl<T: CsaTransport> Pins<T> { |
| 46 | pub async fn pinned_channels(&self) -> Result<PinnedChannels> { |
| 47 | let answer = self |
| 48 | .transport |
| 49 | .send(Request::get(format!("{}/pinnedChannels", self.base_url))) |
| 50 | .await?; |
| 51 | ensure_success(&answer)?; |
| 52 | parse_pinned_channels(&answer.body) |
| 53 | } |
| 54 | |
| 55 | pub async fn pin_channel(&self, channel_id: &str) -> Result<PinnedChannels> { |
| 56 | let action = ChannelAction::Pin { |
| 57 | channel_id: channel_id.to_owned(), |
| 58 | }; |
| 59 | self.write_channels(action).await |
| 60 | } |
| 61 | |
| 62 | pub async fn unpin_channel(&self, channel_id: &str) -> Result<PinnedChannels> { |
| 63 | let action = ChannelAction::Unpin { |
| 64 | channel_id: channel_id.to_owned(), |
| 65 | }; |
| 66 | self.write_channels(action).await |
| 67 | } |
| 68 | |
| 69 | pub async fn reorder_channels(&self, channel_ids: &[String]) -> Result<PinnedChannels> { |
| 70 | let action = ChannelAction::Reorder { |
| 71 | channel_ids: channel_ids.to_vec(), |
| 72 | }; |
| 73 | self.write_channels(action).await |
| 74 | } |
| 75 | |
| 76 | async fn write_channels(&self, action: ChannelAction) -> Result<PinnedChannels> { |
| 77 | let mut state = self.pinned_channels().await?; |
| 78 | for attempt in 0..2 { |
| 79 | let mut request = Request::with_body( |
| 80 | action.method(), |
| 81 | format!("{}/pinnedChannels", self.base_url), |
| 82 | action.body(), |
| 83 | ); |
| 84 | request |
| 85 | .headers |
| 86 | .push(("if-match".into(), state.order_version.clone())); |
| 87 | let answer = self.transport.send(request).await?; |
| 88 | if answer.is_success() { |
| 89 | return self.pinned_channels().await; |
| 90 | } |
| 91 | if is_version_mismatch(&answer) && attempt == 0 { |
| 92 | state = self.pinned_channels().await?; |
| 93 | continue; |
| 94 | } |
| 95 | if is_version_mismatch(&answer) { |
| 96 | return Err(Error::VersionConflict); |
| 97 | } |
| 98 | if matches!(action, ChannelAction::Unpin { .. }) && is_missing_from_pin_list(&answer) { |
| 99 | return self.pinned_channels().await; |
| 100 | } |
| 101 | return Err(api_error(&answer)); |
| 102 | } |
| 103 | Err(Error::VersionConflict) |
| 104 | } |
| 105 | } |