204 lines
5.5 KiB
Rust
204 lines
5.5 KiB
Rust
use super::{mentions::Mention, verification::VerificationStatus};
|
|
use crate::error::GementionError;
|
|
use url::Url;
|
|
use crate::models::mentions::MentionType;
|
|
use fluffer::{async_trait, Client, GemBytes};
|
|
|
|
macro_rules! titan {
|
|
($self:expr) => {
|
|
$self.client.titan.as_ref().unwrap()
|
|
};
|
|
}
|
|
|
|
fn parse_content_body(raw_body: &[u8]) -> Result<&str, GementionError> {
|
|
let content = std::str::from_utf8(raw_body)?;
|
|
|
|
// Drop 1st line if it is a mention type. Otherwise return whole thing.
|
|
let mention_type = MentionType::try_from(raw_body);
|
|
let content: &str = if let Ok(_) = mention_type {
|
|
let amt_to_chop = content.lines().next().map(|line| line.len()).unwrap();
|
|
&content[amt_to_chop..]
|
|
} else {
|
|
content
|
|
};
|
|
|
|
Ok(content.trim())
|
|
}
|
|
|
|
/// Wraps an incoming Titan request and provides handy methods for
|
|
/// extracting the expected information for a client-to-server
|
|
/// gemention.
|
|
pub(crate) struct C2SMentionRequest<'a> {
|
|
client: &'a Client,
|
|
accessed_url: &'a Url,
|
|
}
|
|
|
|
impl<'a> C2SMentionRequest<'a> {
|
|
pub fn from_client(client: &'a Client) -> Result<C2SMentionRequest, GementionError> {
|
|
if let Some(_) = client.titan {
|
|
Ok(Self {
|
|
client: &client,
|
|
accessed_url: &client.url,
|
|
})
|
|
} else {
|
|
Err(GementionError::NotTitanResource)
|
|
}
|
|
}
|
|
|
|
pub fn raw_body(&self) -> &[u8] {
|
|
titan!(self).content.as_slice()
|
|
}
|
|
|
|
pub fn mime(&self) -> &str {
|
|
titan!(self).mime.as_ref()
|
|
}
|
|
|
|
pub fn token(&self) -> Option<&str> {
|
|
titan!(self).token.as_deref()
|
|
}
|
|
|
|
pub fn size(&self) -> usize {
|
|
titan!(self).size
|
|
}
|
|
|
|
pub fn certificate(&self) -> Option<String> {
|
|
self.client.certificate()
|
|
}
|
|
|
|
pub fn fingerprint(&self) -> Option<String> {
|
|
self.client.fingerprint()
|
|
}
|
|
|
|
pub fn username(&self) -> Option<String> {
|
|
self.client.name()
|
|
}
|
|
|
|
pub fn target(&self) -> Option<&str> {
|
|
self.client.parameter("target")
|
|
}
|
|
|
|
pub fn content(&self) -> Result<&str, GementionError> {
|
|
parse_content_body(self.raw_body())
|
|
}
|
|
|
|
/// The full URL that was accessed for the incoming request.
|
|
pub fn accessed_url(&self) -> &Url {
|
|
self.accessed_url
|
|
}
|
|
}
|
|
|
|
impl<'a> TryFrom<&'a Client> for C2SMentionRequest<'a> {
|
|
type Error = GementionError;
|
|
|
|
fn try_from(client: &'a Client) -> Result<Self, Self::Error> {
|
|
C2SMentionRequest::from_client(client)
|
|
}
|
|
}
|
|
|
|
pub(crate) enum MentionResponse {
|
|
VerifiedMention(Mention),
|
|
InvalidMention(Mention),
|
|
NotTitanRequest,
|
|
}
|
|
|
|
impl From<Mention> for MentionResponse {
|
|
fn from(mention: Mention) -> Self {
|
|
match mention.verify_status() {
|
|
VerificationStatus::Verified { .. } => Self::VerifiedMention(mention),
|
|
VerificationStatus::Invalid(_) => Self::InvalidMention(mention),
|
|
VerificationStatus::NotYetVerified => Self::InvalidMention(mention),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl MentionResponse {
|
|
pub fn should_have_body(&self) -> bool {
|
|
match self {
|
|
Self::VerifiedMention(_) | Self::InvalidMention(_) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub fn status_line(&self) -> String {
|
|
match self {
|
|
Self::VerifiedMention(_) => "20 text/gemini",
|
|
Self::InvalidMention(_) => "20 text/gemini",
|
|
Self::NotTitanRequest => "59 text/gemini",
|
|
}
|
|
.to_string()
|
|
}
|
|
|
|
pub fn headline(&self) -> String {
|
|
match self {
|
|
Self::VerifiedMention(_) => "# Mention Submitted",
|
|
Self::InvalidMention(_) => "# Error Submitting Mention",
|
|
Self::NotTitanRequest => "# Invalid Protocol For Request",
|
|
}
|
|
.to_string()
|
|
}
|
|
|
|
pub fn description(&self) -> String {
|
|
match self {
|
|
Self::VerifiedMention(_) => {
|
|
"You have successfully submitted a gemention. It is shown below."
|
|
}
|
|
Self::InvalidMention(_) => {
|
|
"There was an error submitting the gemention. The error is detailed below."
|
|
}
|
|
Self::NotTitanRequest => "Your request was not a Titan protocol request.",
|
|
}
|
|
.to_string()
|
|
}
|
|
|
|
pub fn mention_body(&self) -> String {
|
|
match self {
|
|
Self::VerifiedMention(m) | Self::InvalidMention(m) => m.as_gemtext(),
|
|
_ => "".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl GemBytes for MentionResponse {
|
|
async fn gem_bytes(self) -> Vec<u8> {
|
|
let status_line = self.status_line();
|
|
let body = if self.should_have_body() {
|
|
format!(
|
|
"{}\n\n{}\n\n{}",
|
|
self.headline(),
|
|
self.description(),
|
|
self.mention_body()
|
|
)
|
|
} else {
|
|
String::from("")
|
|
};
|
|
|
|
format!("{}\r\n{}", status_line, body).into_bytes()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn parse_comment_body_with_mention_type() -> Result<(), GementionError> {
|
|
let body = "reply\nthis is my comment body, which is a reply.".as_bytes();
|
|
let expected = "this is my comment body, which is a reply.";
|
|
let parsed = parse_content_body(body)?;
|
|
|
|
assert_eq!(expected, parsed);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn parse_comment_body_without_mention_type() -> Result<(), GementionError> {
|
|
let expected = "this is my comment body, which has no mention type.";
|
|
let body = expected.as_bytes();
|
|
let parsed = parse_content_body(body)?;
|
|
|
|
assert_eq!(expected, parsed);
|
|
Ok(())
|
|
}
|
|
}
|