2024-03-29 19:37:19 +00:00
|
|
|
use async_trait::async_trait;
|
2024-03-29 21:07:49 +00:00
|
|
|
use fluffer::{Client, GemBytes};
|
2024-03-29 19:37:19 +00:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
|
|
|
use crate::error::GementionError;
|
2024-03-29 21:07:49 +00:00
|
|
|
|
|
|
|
macro_rules! titan {
|
|
|
|
($self:expr) => {
|
|
|
|
$self.client.titan.as_ref().unwrap()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:52:26 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:07:49 +00:00
|
|
|
/// Wraps an incoming Titan request and provides handy methods for
|
|
|
|
/// extracting the expected information.
|
|
|
|
pub(crate) struct MentionUpload<'a> {
|
|
|
|
client: &'a Client,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> MentionUpload<'a> {
|
|
|
|
pub fn from_client(client: &'a Client) -> Result<MentionUpload, GementionError> {
|
|
|
|
if let Some(_) = client.titan {
|
|
|
|
Ok(Self { client: &client })
|
|
|
|
} else {
|
|
|
|
Err(GementionError::NotTitanResource)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:52:26 +00:00
|
|
|
pub fn raw_body(&self) -> &[u8] {
|
2024-03-29 21:07:49 +00:00
|
|
|
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 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")
|
|
|
|
}
|
2024-03-30 10:52:26 +00:00
|
|
|
|
|
|
|
pub fn content(&self) -> Result<&str, GementionError> {
|
|
|
|
parse_content_body(self.raw_body())
|
|
|
|
}
|
2024-03-29 21:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> TryFrom<&'a Client> for MentionUpload<'a> {
|
|
|
|
type Error = GementionError;
|
|
|
|
|
|
|
|
fn try_from(client: &'a Client) -> Result<Self, Self::Error> {
|
|
|
|
MentionUpload::from_client(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2024-03-29 19:37:19 +00:00
|
|
|
pub(crate) enum MentionType {
|
|
|
|
Reply,
|
|
|
|
Like,
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:07:49 +00:00
|
|
|
#[derive(Debug)]
|
2024-03-29 19:37:19 +00:00
|
|
|
pub(crate) struct Mention {
|
|
|
|
mention_type: MentionType,
|
2024-03-29 21:07:49 +00:00
|
|
|
target: String,
|
2024-03-29 19:37:19 +00:00
|
|
|
user: String,
|
|
|
|
content: Option<String>,
|
|
|
|
}
|
|
|
|
|
2024-03-30 10:52:26 +00:00
|
|
|
// This is for converting an incoming Titan request into a mention
|
|
|
|
// type. It will attempt to parse the first line to identify the type.
|
|
|
|
// If the type cannot be identified, or if the string is not valid
|
|
|
|
// UTF-8, an error is returned.
|
2024-03-29 21:07:49 +00:00
|
|
|
impl TryFrom<&[u8]> for MentionType {
|
2024-03-29 19:37:19 +00:00
|
|
|
type Error = GementionError;
|
|
|
|
|
2024-03-29 21:07:49 +00:00
|
|
|
fn try_from(content: &[u8]) -> Result<Self, Self::Error> {
|
2024-03-29 19:37:19 +00:00
|
|
|
let content = std::str::from_utf8(&content)?;
|
2024-03-29 21:07:49 +00:00
|
|
|
let content_type = content
|
|
|
|
.lines()
|
|
|
|
.next()
|
2024-03-30 10:52:26 +00:00
|
|
|
.map(|line| line.trim())
|
|
|
|
.unwrap_or("[none provided]");
|
2024-03-29 21:07:49 +00:00
|
|
|
|
2024-03-30 10:52:26 +00:00
|
|
|
match content_type.to_lowercase() {
|
|
|
|
value if value == "reply" => Ok(Self::Reply),
|
|
|
|
value if value == "like" => Ok(Self::Like),
|
|
|
|
_ => Err(GementionError::InvalidMentionType(content_type.to_owned())),
|
|
|
|
}
|
2024-03-29 19:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 21:07:49 +00:00
|
|
|
impl<'a> TryFrom<MentionUpload<'a>> for Mention {
|
|
|
|
type Error = GementionError;
|
|
|
|
|
|
|
|
fn try_from(resource: MentionUpload<'a>) -> Result<Self, Self::Error> {
|
|
|
|
if resource.mime() == "text/plain" {
|
2024-03-30 10:52:26 +00:00
|
|
|
// Be flexible on mention type: if first line isn't a
|
|
|
|
// mention type, just assume reply.
|
|
|
|
let mention_type = match MentionType::try_from(resource.raw_body()) {
|
|
|
|
Ok(mention_type) => mention_type,
|
|
|
|
Err(GementionError::InvalidMentionType(_)) => MentionType::Reply,
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
let target = resource
|
|
|
|
.target()
|
|
|
|
.ok_or(GementionError::TargetNotProvided)?
|
|
|
|
.to_owned();
|
|
|
|
|
|
|
|
let user = resource
|
|
|
|
.username()
|
|
|
|
.ok_or(GementionError::UsernameNotProvided)?;
|
|
|
|
|
|
|
|
let content = resource.content()?.to_owned();
|
|
|
|
let content = if !content.is_empty() {
|
|
|
|
Some(content)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-03-29 21:07:49 +00:00
|
|
|
|
|
|
|
Ok(Mention {
|
2024-03-30 10:52:26 +00:00
|
|
|
user,
|
2024-03-29 21:07:49 +00:00
|
|
|
mention_type,
|
2024-03-30 10:52:26 +00:00
|
|
|
content,
|
|
|
|
target,
|
2024-03-29 21:07:49 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(GementionError::InvalidBody)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-29 19:37:19 +00:00
|
|
|
impl Display for MentionType {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Reply => write!(f, "Reply"),
|
|
|
|
Self::Like => write!(f, "Like"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl GemBytes for Mention {
|
|
|
|
async fn gem_bytes(self) -> Vec<u8> {
|
|
|
|
// TODO change output based on mention type:
|
|
|
|
// Reply/comment = current format.
|
|
|
|
// Like = "<user> liked this"
|
|
|
|
|
|
|
|
let headline = format!("## {} from {}", self.mention_type, self.user);
|
|
|
|
let content = self.content.unwrap_or(String::from("[no content]"));
|
|
|
|
|
|
|
|
format!("20 text/gemini\r\n{}\n\n{}", headline, content).into_bytes()
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 21:07:49 +00:00
|
|
|
|
|
|
|
pub(crate) struct MentionResponse {
|
|
|
|
status: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MentionResponse {
|
|
|
|
pub fn not_titan() -> MentionResponse {
|
|
|
|
Self {
|
|
|
|
status: "not a titan request".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verified(url: &str) -> MentionResponse {
|
|
|
|
Self {
|
|
|
|
status: format!("verified: {}", url),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn failure(reason: &str) -> MentionResponse {
|
|
|
|
Self {
|
|
|
|
status: format!("invalid: {}", reason),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl GemBytes for MentionResponse {
|
|
|
|
async fn gem_bytes(self) -> Vec<u8> {
|
|
|
|
format!("20 text/gemini\r\n{}", self.status).into_bytes()
|
|
|
|
}
|
|
|
|
}
|
2024-03-30 10:52:26 +00:00
|
|
|
|
|
|
|
#[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(())
|
|
|
|
}
|
|
|
|
}
|