-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Delete message endpoint (#186)
* Stub the DELETE /m/{message_id} route * Extract the message ID data from the request path * Implement DbClient::delete_message * Implement the delete_notification_route handler * Fix errors after rebase Closes #175
- Loading branch information
1 parent
6df3e36
commit 6a7fa49
Showing
7 changed files
with
197 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use crate::error::{ApiError, ApiErrorKind, ApiResult}; | ||
use crate::server::ServerState; | ||
use actix_web::dev::{Payload, PayloadStream}; | ||
use actix_web::web::Data; | ||
use actix_web::{FromRequest, HttpRequest}; | ||
use fernet::MultiFernet; | ||
use futures::future; | ||
use uuid::Uuid; | ||
|
||
/// Holds information about a notification. The information is encoded and | ||
/// encrypted into a "message ID" which is presented to the user. Later, the | ||
/// user can send us the message ID to perform operations on the associated | ||
/// notification (e.g. delete it). | ||
#[derive(Debug)] | ||
pub enum MessageId { | ||
WithTopic { | ||
uaid: Uuid, | ||
channel_id: Uuid, | ||
topic: String, | ||
}, | ||
WithoutTopic { | ||
uaid: Uuid, | ||
channel_id: Uuid, | ||
timestamp: u64, | ||
}, | ||
} | ||
|
||
impl FromRequest for MessageId { | ||
type Error = ApiError; | ||
type Future = future::Ready<Result<Self, Self::Error>>; | ||
type Config = (); | ||
|
||
fn from_request(req: &HttpRequest, _: &mut Payload<PayloadStream>) -> Self::Future { | ||
let message_id_param = req | ||
.match_info() | ||
.get("message_id") | ||
.expect("{message_id} must be part of the path"); | ||
let state: Data<ServerState> = Data::extract(req) | ||
.into_inner() | ||
.expect("No server state found"); | ||
|
||
future::ready(MessageId::decrypt(&state.fernet, message_id_param)) | ||
} | ||
} | ||
|
||
impl MessageId { | ||
/// Encode and encrypt the message ID | ||
pub fn encrypt(&self, fernet: &MultiFernet) -> String { | ||
let id_str = match self { | ||
MessageId::WithTopic { | ||
uaid, | ||
channel_id, | ||
topic, | ||
} => format!( | ||
"01:{}:{}:{}", | ||
uaid.to_simple_ref(), | ||
channel_id.to_simple_ref(), | ||
topic | ||
), | ||
MessageId::WithoutTopic { | ||
uaid, | ||
channel_id, | ||
timestamp, | ||
} => format!( | ||
"02:{}:{}:{}", | ||
uaid.to_simple_ref(), | ||
channel_id.to_simple_ref(), | ||
timestamp | ||
), | ||
}; | ||
|
||
fernet.encrypt(id_str.as_bytes()) | ||
} | ||
|
||
/// Decrypt and decode the message ID | ||
pub fn decrypt(fernet: &MultiFernet, message_id: &str) -> ApiResult<Self> { | ||
let decrypted_bytes = fernet | ||
.decrypt(message_id) | ||
.map_err(|_| ApiErrorKind::InvalidMessageId)?; | ||
let decrypted_str = String::from_utf8_lossy(&decrypted_bytes); | ||
let segments: Vec<_> = decrypted_str.split(':').collect(); | ||
|
||
if segments.len() != 4 { | ||
return Err(ApiErrorKind::InvalidMessageId.into()); | ||
} | ||
|
||
let (version, uaid, chid, topic_or_timestamp) = | ||
(segments[0], segments[1], segments[2], segments[3]); | ||
|
||
match version { | ||
"01" => Ok(MessageId::WithTopic { | ||
uaid: Uuid::parse_str(uaid).map_err(|_| ApiErrorKind::InvalidMessageId)?, | ||
channel_id: Uuid::parse_str(chid).map_err(|_| ApiErrorKind::InvalidMessageId)?, | ||
topic: topic_or_timestamp.to_string(), | ||
}), | ||
"02" => Ok(MessageId::WithoutTopic { | ||
uaid: Uuid::parse_str(uaid).map_err(|_| ApiErrorKind::InvalidMessageId)?, | ||
channel_id: Uuid::parse_str(chid).map_err(|_| ApiErrorKind::InvalidMessageId)?, | ||
timestamp: topic_or_timestamp | ||
.parse() | ||
.map_err(|_| ApiErrorKind::InvalidMessageId)?, | ||
}), | ||
_ => Err(ApiErrorKind::InvalidMessageId.into()), | ||
} | ||
} | ||
|
||
/// Get the UAID of the associated notification | ||
pub fn uaid(&self) -> Uuid { | ||
match self { | ||
MessageId::WithTopic { uaid, .. } => *uaid, | ||
MessageId::WithoutTopic { uaid, .. } => *uaid, | ||
} | ||
} | ||
|
||
/// Get the sort-key for the associated notification | ||
pub fn sort_key(&self) -> String { | ||
match self { | ||
MessageId::WithTopic { | ||
channel_id, topic, .. | ||
} => format!("01:{}:{}", channel_id.to_hyphenated(), topic), | ||
MessageId::WithoutTopic { | ||
channel_id, | ||
timestamp, | ||
.. | ||
} => format!("02:{}:{}", timestamp, channel_id.to_hyphenated()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters