Skip to main content

fedi_post/
lib.rs

1//! A library for posting to fediverse services like Lemmy and PieFed.
2
3#![warn(missing_docs)]
4
5use thiserror::Error;
6
7/// Code for posting to [Lemmy](https://join-lemmy.org/).
8pub mod lemmy;
9/// Code for posting to [PieFed](https://join.piefed.social/).
10pub mod piefed;
11
12/// A post that can be submitted to a fediverse service.
13#[derive(Debug, Clone)]
14pub struct ServerPost {
15    /// The community to post to.
16    pub community: String,
17    /// The title of the post.
18    pub title: String,
19    /// An url to attach to the post.
20    /// This can be a link to a website or an image.
21    pub url: Option<String>,
22    /// The body text of the post.
23    pub body: Option<String>,
24    /// The language of the post as a short language code, e.g. "en" for English.
25    pub language: Option<String>,
26    /// The alt text of the attached url, if it is an image.
27    pub alt_text: Option<String>,
28    /// Whether the post should be marked as NSFW (not safe for work).
29    pub nsfw: Option<bool>,
30    /// Whether the post should be marked as NSFL (not safe for life).
31    pub nsfl: Option<bool>,
32    /// Whether the post is generated by A"I" (artificial "intelligence").
33    pub ai_generated: Option<bool>,
34    /// The thumbnail for the post.
35    /// If not set, the service may fetch or generate one.
36    pub custom_thumbnail: Option<String>,
37}
38
39/// The credentials required to log in to a fediverse service and post.
40#[derive(Debug, Clone)]
41pub struct ServerCredentials {
42    /// The domain of the fediverse service, e.g. `sopuli.xyz`.
43    pub domain: String,
44    /// The username to log in with.
45    pub username: String,
46    /// The password to log in with.
47    pub password: String,
48    /// The kind of fediverse service to post to.
49    pub fediverse_service_kind: FediverseServiceKind,
50}
51
52/// The kind of a fediverse service.
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub enum FediverseServiceKind {
55    /// [Lemmy](https://join-lemmy.org/)
56    Lemmy,
57    /// [PieFed](https://join.piefed.social/)
58    PieFed,
59}
60
61/// The error type for this library.
62#[derive(Debug, Error)]
63pub enum Error {
64    /// Errors that can occur when posting to Lemmy.
65    #[error("An error occurred while posting to Lemmy: {0}")]
66    Lemmy(#[from] lemmy::LemmyError),
67    /// Errors that can occur when posting to PieFed.
68    #[error("An error occurred while posting to PieFed: {0}")]
69    PieFed(#[from] piefed::PieFedError),
70}
71
72/// Create the post `post` on a fediverse service specified by `credentials`.
73pub async fn post_to_server(
74    credentials: &ServerCredentials,
75    post: ServerPost,
76) -> Result<(), Error> {
77    match credentials.fediverse_service_kind {
78        FediverseServiceKind::Lemmy => lemmy::post_to_lemmy(credentials, post).await?,
79        FediverseServiceKind::PieFed => piefed::post_to_piefed(credentials, post).await?,
80    }
81    Ok(())
82}