Added visibility configuration
This commit is contained in:
		
							parent
							
								
									52cb0c5ea2
								
							
						
					
					
						commit
						4fcb8a9771
					
				
							
								
								
									
										31
									
								
								README.md
								
								
								
								
							
							
						
						
									
										31
									
								
								README.md
								
								
								
								
							| 
						 | 
					@ -40,16 +40,43 @@ 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:
 | 
					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                                                                                                      |
 | 
					| Name         | Value                                                                                                      |
 | 
				
			||||||
|------------|------------------------------------------------------------------------------------------------------------|
 | 
					|--------------|------------------------------------------------------------------------------------------------------------|
 | 
				
			||||||
| `base_url`   | The base URL of the instance                                                                               |
 | 
					| `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 |
 | 
					| `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:
 | 
					An example `Misskey` publisher entry looks like this:
 | 
				
			||||||
```json
 | 
					```json
 | 
				
			||||||
	"publisher": {
 | 
						"publisher": {
 | 
				
			||||||
		"Misskey": {
 | 
							"Misskey": {
 | 
				
			||||||
			"base_url": "",
 | 
								"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"
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,14 +25,15 @@ pub struct MinMax {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
					#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
				
			||||||
pub enum Publisher {
 | 
					pub enum Publisher {
 | 
				
			||||||
    Misskey(FediverseConfig<String>),
 | 
					    Misskey(FediverseConfig<String, misskey::model::note::Visibility>),
 | 
				
			||||||
    Mastodon(FediverseConfig<Option<mammut::Data>>),
 | 
					    Mastodon(FediverseConfig<Option<mammut::Data>, mammut::status_builder::Visibility>),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
					#[derive(Serialize, Deserialize, Debug, Clone)]
 | 
				
			||||||
pub struct FediverseConfig<T> {
 | 
					pub struct FediverseConfig<T, V> {
 | 
				
			||||||
    pub base_url: String,
 | 
					    pub base_url: String,
 | 
				
			||||||
    pub token: T,
 | 
					    pub token: T,
 | 
				
			||||||
 | 
						pub visibility: V,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Default for Config {
 | 
					impl Default for Config {
 | 
				
			||||||
| 
						 | 
					@ -53,6 +54,7 @@ impl Default for Config {
 | 
				
			||||||
            publisher: Publisher::Misskey(FediverseConfig {
 | 
					            publisher: Publisher::Misskey(FediverseConfig {
 | 
				
			||||||
                base_url: "".to_string(),
 | 
					                base_url: "".to_string(),
 | 
				
			||||||
                token: "".to_string(),
 | 
					                token: "".to_string(),
 | 
				
			||||||
 | 
									visibility: misskey::model::note::Visibility::Public,
 | 
				
			||||||
            }),
 | 
					            }),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -146,7 +146,7 @@ async fn resolve_publisher(
 | 
				
			||||||
) -> Result<Either<MisskeyPublisher, MastodonPublisher>, Box<dyn Error>> {
 | 
					) -> Result<Either<MisskeyPublisher, MastodonPublisher>, Box<dyn Error>> {
 | 
				
			||||||
    let publisher = match &config.publisher {
 | 
					    let publisher = match &config.publisher {
 | 
				
			||||||
        config::Publisher::Misskey(cfg) => {
 | 
					        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) => {
 | 
					        config::Publisher::Mastodon(cfg) => {
 | 
				
			||||||
            let app = AppBuilder {
 | 
					            let app = AppBuilder {
 | 
				
			||||||
| 
						 | 
					@ -158,6 +158,7 @@ async fn resolve_publisher(
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let mut registration = Registration::new(cfg.base_url.clone());
 | 
					            let mut registration = Registration::new(cfg.base_url.clone());
 | 
				
			||||||
            registration.register(app)?;
 | 
					            registration.register(app)?;
 | 
				
			||||||
 | 
								let vis = cfg.visibility;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let mastodon = if let Some(data) = cfg.token.clone() {
 | 
					            let mastodon = if let Some(data) = cfg.token.clone() {
 | 
				
			||||||
                Mastodon::from_data(data.clone())
 | 
					                Mastodon::from_data(data.clone())
 | 
				
			||||||
| 
						 | 
					@ -173,13 +174,14 @@ async fn resolve_publisher(
 | 
				
			||||||
                config.publisher = Publisher::Mastodon(FediverseConfig {
 | 
					                config.publisher = Publisher::Mastodon(FediverseConfig {
 | 
				
			||||||
                    base_url: cfg.base_url.clone(),
 | 
					                    base_url: cfg.base_url.clone(),
 | 
				
			||||||
                    token: Some(fedi.data.clone()),
 | 
					                    token: Some(fedi.data.clone()),
 | 
				
			||||||
 | 
										visibility: vis.clone(),
 | 
				
			||||||
                });
 | 
					                });
 | 
				
			||||||
                config.save(CONFIG_PATH)?;
 | 
					                config.save(CONFIG_PATH)?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                fedi
 | 
					                fedi
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Either::Right(MastodonPublisher::new(mastodon))
 | 
					            Either::Right(MastodonPublisher::new(mastodon, vis))
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    Ok(publisher)
 | 
					    Ok(publisher)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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 futures::Sink;
 | 
				
			||||||
use mammut::{status_builder::Visibility, Mastodon, StatusBuilder};
 | 
					use mammut::{status_builder::Visibility, Mastodon, StatusBuilder};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct MastodonPublisher {
 | 
					pub struct MastodonPublisher {
 | 
				
			||||||
    mastodon: Mastodon,
 | 
					    mastodon: Mastodon,
 | 
				
			||||||
 | 
					    post_visibility: Visibility,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl MastodonPublisher {
 | 
					impl MastodonPublisher {
 | 
				
			||||||
    pub fn new(mastodon: Mastodon) -> Self {
 | 
					    pub fn new(mastodon: Mastodon, vis: Visibility) -> Self {
 | 
				
			||||||
        Self { mastodon }
 | 
					        Self {
 | 
				
			||||||
 | 
					            mastodon: mastodon,
 | 
				
			||||||
 | 
					            post_visibility: vis,
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -22,7 +30,7 @@ impl Sink<String> for MastodonPublisher {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn start_send(self: Pin<&mut Self>, item: String) -> Result<(), Self::Error> {
 | 
					    fn start_send(self: Pin<&mut Self>, item: String) -> Result<(), Self::Error> {
 | 
				
			||||||
        let mut post = StatusBuilder::new(item);
 | 
					        let mut post = StatusBuilder::new(item);
 | 
				
			||||||
        post.visibility = Some(Visibility::Public);
 | 
					        post.visibility = Some(self.post_visibility);
 | 
				
			||||||
        self.mastodon.new_status(post)?;
 | 
					        self.mastodon.new_status(post)?;
 | 
				
			||||||
        Ok(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,16 +1,18 @@
 | 
				
			||||||
use futures::Sink;
 | 
					use futures::Sink;
 | 
				
			||||||
use misskey::{ClientExt, HttpClient};
 | 
					use misskey::{Client, ClientExt, HttpClient, model::note::Visibility};
 | 
				
			||||||
use std::{error::Error, task::Poll};
 | 
					use std::{error::Error, task::Poll};
 | 
				
			||||||
use url::Url;
 | 
					use url::Url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct MisskeyPublisher {
 | 
					pub struct MisskeyPublisher {
 | 
				
			||||||
    client: HttpClient,
 | 
					    client: HttpClient,
 | 
				
			||||||
 | 
						post_visibility: Visibility,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl MisskeyPublisher {
 | 
					impl MisskeyPublisher {
 | 
				
			||||||
    pub fn new(url: &String, token: String) -> Result<Self, Box<dyn Error>> {
 | 
					    pub fn new(url: &String, token: String, vis: Visibility) -> Result<Self, Box<dyn Error>> {
 | 
				
			||||||
        Ok(Self {
 | 
					        Ok(Self {
 | 
				
			||||||
            client: HttpClient::with_token(Url::parse(url)?, token)?,
 | 
					            client: HttpClient::with_token(Url::parse(url)?, token)?,
 | 
				
			||||||
 | 
								post_visibility: vis,
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -26,7 +28,9 @@ impl Sink<String> for MisskeyPublisher {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn start_send(self: std::pin::Pin<&mut Self>, item: String) -> Result<(), Self::Error> {
 | 
					    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(())
 | 
					        Ok(())
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue