Gemention/src/models/web.rs

85 lines
2.5 KiB
Rust

use super::{mentions::Mention, verification::VerificationStatus};
use fluffer::{async_trait, GemBytes};
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()
}
}