Implementing traits for accepting actual gemention requests
This commit is contained in:
parent
4a6379b9fa
commit
d34ca875ab
|
@ -189,6 +189,7 @@ name = "gemention"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"async-trait",
|
||||||
"fluffer",
|
"fluffer",
|
||||||
"germ",
|
"germ",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
|
|
@ -7,6 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.81"
|
anyhow = "1.0.81"
|
||||||
|
async-trait = "0.1.79"
|
||||||
fluffer = "4"
|
fluffer = "4"
|
||||||
germ = "0.4"
|
germ = "0.4"
|
||||||
thiserror = "1.0.58"
|
thiserror = "1.0.58"
|
||||||
|
|
|
@ -5,6 +5,7 @@ use fluffer::Client;
|
||||||
// - Make Gemini request to see if target page supports gemention.
|
// - Make Gemini request to see if target page supports gemention.
|
||||||
// - If so, store mention in DB.
|
// - If so, store mention in DB.
|
||||||
pub(crate) async fn receive_mention(client: Client) -> String {
|
pub(crate) async fn receive_mention(client: Client) -> String {
|
||||||
|
// TODO change to return MentionResponse or something like that.
|
||||||
let titan = match client.titan {
|
let titan = match client.titan {
|
||||||
Some(ref titan) => titan,
|
Some(ref titan) => titan,
|
||||||
_ => return "".to_string(),
|
_ => return "".to_string(),
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::str::Utf8Error;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
|
@ -8,6 +10,9 @@ pub(crate) enum GementionError {
|
||||||
#[error("url parsing error: {0}")]
|
#[error("url parsing error: {0}")]
|
||||||
UrlParsingError(#[from] url::ParseError),
|
UrlParsingError(#[from] url::ParseError),
|
||||||
|
|
||||||
|
#[error("value was not utf8: {0}")]
|
||||||
|
Utf8Error(#[from] Utf8Error),
|
||||||
|
|
||||||
#[error("generic error: {0}")]
|
#[error("generic error: {0}")]
|
||||||
UnclassifiedError(#[from] anyhow::Error),
|
UnclassifiedError(#[from] anyhow::Error),
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use fluffer::GemBytes;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
use crate::error::GementionError;
|
||||||
|
pub(crate) enum MentionType {
|
||||||
|
Reply,
|
||||||
|
Like,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Mention {
|
||||||
|
mention_type: MentionType,
|
||||||
|
user: String,
|
||||||
|
content: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is for converting an incoming Titan request into a mention. It
|
||||||
|
// is very flexible: If the incoming value matches a specific format,
|
||||||
|
// it'll accurately convert the stringi nto a proper mention.
|
||||||
|
// Otherwise, it just assumes comment. An error is returned only if
|
||||||
|
// input is not utf8.
|
||||||
|
impl TryFrom<Vec<u8>> for Mention {
|
||||||
|
type Error = GementionError;
|
||||||
|
|
||||||
|
fn try_from(content: Vec<u8>) -> Result<Self, Self::Error> {
|
||||||
|
let content = std::str::from_utf8(&content)?;
|
||||||
|
Ok(Mention {
|
||||||
|
mention_type: MentionType::Reply,
|
||||||
|
user: "".to_string(),
|
||||||
|
content: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub(crate) mod verification;
|
||||||
|
pub(crate) mod mentions;
|
|
@ -4,7 +4,7 @@ use germ::request::request as germ_request;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::error::GementionError;
|
use crate::error::GementionError;
|
||||||
use crate::models::*;
|
use crate::models::verification::*;
|
||||||
|
|
||||||
const OUR_ENDPOINT: &'static str = "titan://localhost/receive/";
|
const OUR_ENDPOINT: &'static str = "titan://localhost/receive/";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue