From 4fcb8a9771e74dc051f4736f0aff237bedf014f3 Mon Sep 17 00:00:00 2001 From: emilis Date: Thu, 30 Sep 2021 18:53:33 +0200 Subject: [PATCH] Added visibility configuration --- README.md | 37 ++++++++++++++++++++++++++++++++----- src/config.rs | 8 +++++--- src/main.rs | 6 ++++-- src/publish/mastodon.rs | 16 ++++++++++++---- src/publish/misskey.rs | 10 +++++++--- 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d1ea45c..7047bbd 100644 --- a/README.md +++ b/README.md @@ -39,17 +39,44 @@ This default uses the `Misskey` publisher. If you want to publish to Mastodon, P The publisher can currently hold one of two JSON objects, named either `Mastodon` or `Misskey`, which determines which posting API it will use. Whether the object is `Misskey` or `Mastodon`, it has the following members: -| Name | Value | -|------------|------------------------------------------------------------------------------------------------------------| -| `base_url` | The base URL of the instance | -| `token` | The auth token for the account, leave empty for `Mastodon` as you will be prompted to log in and authorize | +| Name | Value | +|--------------|------------------------------------------------------------------------------------------------------------| +| `base_url` | The base URL of the instance | +| `token` | The auth token for the account, leave empty for `Mastodon` as you will be prompted to log in and authorize | +| `visibility` | The visibility scope of the statuses to be posted. These differ between publishers, see the list below | + +**Misskey visibility values** + +* `Public` (Global) +* `Home` +* `Followers` +* `Specified` (DMs) + +**Mastodon-comptaibles visibility values** + +* `public` (Global) +* `unlisted` (Home) +* `private` (Followers only) +* `direct` (DMs) An example `Misskey` publisher entry looks like this: ```json "publisher": { "Misskey": { "base_url": "", - "token": "" + "token": "", + "visibility": "Public" + } + } +``` + +And for mastodon/pleroma: +```json + "publisher": { + "Mastodon": { + // Token will be set by izzilis on authentication (requires interactive) + "base_url": "", + "visibility": "public" } } ``` diff --git a/src/config.rs b/src/config.rs index 15a0ab0..f3e78d7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,14 +25,15 @@ pub struct MinMax { #[derive(Serialize, Deserialize, Debug, Clone)] pub enum Publisher { - Misskey(FediverseConfig), - Mastodon(FediverseConfig>), + Misskey(FediverseConfig), + Mastodon(FediverseConfig, mammut::status_builder::Visibility>), } #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct FediverseConfig { +pub struct FediverseConfig { pub base_url: String, pub token: T, + pub visibility: V, } impl Default for Config { @@ -53,6 +54,7 @@ impl Default for Config { publisher: Publisher::Misskey(FediverseConfig { base_url: "".to_string(), token: "".to_string(), + visibility: misskey::model::note::Visibility::Public, }), } } diff --git a/src/main.rs b/src/main.rs index f16afb1..6107038 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,7 +146,7 @@ async fn resolve_publisher( ) -> Result, Box> { let publisher = match &config.publisher { config::Publisher::Misskey(cfg) => { - Either::Left(MisskeyPublisher::new(&cfg.base_url, cfg.token.clone())?) + Either::Left(MisskeyPublisher::new(&cfg.base_url, cfg.token.clone(), cfg.visibility)?) } config::Publisher::Mastodon(cfg) => { let app = AppBuilder { @@ -158,6 +158,7 @@ async fn resolve_publisher( let mut registration = Registration::new(cfg.base_url.clone()); registration.register(app)?; + let vis = cfg.visibility; let mastodon = if let Some(data) = cfg.token.clone() { Mastodon::from_data(data.clone()) @@ -173,13 +174,14 @@ async fn resolve_publisher( config.publisher = Publisher::Mastodon(FediverseConfig { base_url: cfg.base_url.clone(), token: Some(fedi.data.clone()), + visibility: vis.clone(), }); config.save(CONFIG_PATH)?; fedi }; - Either::Right(MastodonPublisher::new(mastodon)) + Either::Right(MastodonPublisher::new(mastodon, vis)) } }; Ok(publisher) diff --git a/src/publish/mastodon.rs b/src/publish/mastodon.rs index 78d8ba6..eeb3543 100644 --- a/src/publish/mastodon.rs +++ b/src/publish/mastodon.rs @@ -1,15 +1,23 @@ -use std::{error::Error, pin::Pin, task::{Context, Poll}}; +use std::{ + error::Error, + pin::Pin, + task::{Context, Poll}, +}; use futures::Sink; use mammut::{status_builder::Visibility, Mastodon, StatusBuilder}; pub struct MastodonPublisher { mastodon: Mastodon, + post_visibility: Visibility, } impl MastodonPublisher { - pub fn new(mastodon: Mastodon) -> Self { - Self { mastodon } + pub fn new(mastodon: Mastodon, vis: Visibility) -> Self { + Self { + mastodon: mastodon, + post_visibility: vis, + } } } @@ -22,7 +30,7 @@ impl Sink for MastodonPublisher { fn start_send(self: Pin<&mut Self>, item: String) -> Result<(), Self::Error> { let mut post = StatusBuilder::new(item); - post.visibility = Some(Visibility::Public); + post.visibility = Some(self.post_visibility); self.mastodon.new_status(post)?; Ok(()) } diff --git a/src/publish/misskey.rs b/src/publish/misskey.rs index e810f85..b7aa48a 100644 --- a/src/publish/misskey.rs +++ b/src/publish/misskey.rs @@ -1,16 +1,18 @@ use futures::Sink; -use misskey::{ClientExt, HttpClient}; +use misskey::{Client, ClientExt, HttpClient, model::note::Visibility}; use std::{error::Error, task::Poll}; use url::Url; pub struct MisskeyPublisher { client: HttpClient, + post_visibility: Visibility, } impl MisskeyPublisher { - pub fn new(url: &String, token: String) -> Result> { + pub fn new(url: &String, token: String, vis: Visibility) -> Result> { Ok(Self { client: HttpClient::with_token(Url::parse(url)?, token)?, + post_visibility: vis, }) } } @@ -26,7 +28,9 @@ impl Sink for MisskeyPublisher { } fn start_send(self: std::pin::Pin<&mut Self>, item: String) -> Result<(), Self::Error> { - smol::block_on(self.client.create_note(item))?; + let mut req = self.client.build_note(); + let req = req.text(item).visibility(self.post_visibility).as_request(); + smol::block_on(self.client.request(req))?; Ok(()) }