Skip to content

Commit

Permalink
fix: better default values, log version on start, better logs for Mas…
Browse files Browse the repository at this point in the history
…todon

changed `link_tag` default to something not Tera: `{$ emile_social $}`
changed `draft_template` to not `.html` to avoid being picked up by `zola` on build
  • Loading branch information
Geobert committed May 31, 2024
1 parent 5efeea6 commit 77b1b4a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "emile"
version = "0.5.0"
version = "0.5.1"
authors = ["Geobert Quach <geobert@proton.me>"]
edition = "2021"

Expand All @@ -14,7 +14,7 @@ serde = "1.0.203"
serde_derive = "1.0.203"
slug = "0.1.5"
notify = { version = "6.1.1", default-features = false }
clap = { version = "4.5.4", features = ["derive"] }
clap = { version = "4.5.4", features = ["derive", "cargo"] }
lazy_static = "1.4.0"
tokio = { version = "1.37.0", features = ["sync", "macros", "rt-multi-thread", "time"] }
notify-debouncer-mini = { version = "0.4.1", default-features = false }
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ drafts_year_shift = 0

# emile will take this file to create a draft post by adding `title`, `date` and
#`draft = true` in the frontmatter
draft_template = "templates/draft.html"
draft_template = "draft.txt"

# Destination for `publish` command.
publish_dest = "content/"
Expand Down Expand Up @@ -75,15 +75,15 @@ link_template = "social_link.txt"

# tag to put in the blog post, to be replaced by the `link_template` snippet to have link
# to social media post
link_tag = "{% emile_social %}"
link_tag = "{$ emile_social $}"

# if a tag match, use the associated lang (ex: [{ tag = "english", lang = "en" }])
tag_lang = []

# tag in the list will not be in the social post (ex: ["english", "misc"])
filtered_tag = []

# social instances to post to. One per `api` (accepled values are "mastodon" or "bluesky").
# social instances to post to. One per `api` (accepted values are "mastodon" or "bluesky").
#`*_var` are environment variable to read the needed value from. If `social` is present,
# it cannot be empty
# ex:
Expand Down Expand Up @@ -189,7 +189,8 @@ in `social_template`.
### Social media link

`emile` can add links to the social media posts it created so people can react on your
article.
article. The tag defined by `link_tag` will be replaced by the expanded template
`link_template`.

The file specified in `link_template` must be in the `/template` directory. It must
contains one `{links}` (plural) tag which will be expanded to a list of links to the
Expand Down
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl SiteConfigBuilder {
.unwrap_or_else(|| PathBuf::from("social_link.txt")),
link_tag: cfg_builder
.link_tag
.unwrap_or("{% emile_social %}".to_owned()),
.unwrap_or("{$ emile_social $}".to_owned()),
instances: cfg_builder.instances,
});

Expand All @@ -211,7 +211,7 @@ impl SiteConfigBuilder {
drafts_year_shift: cfg_builder.drafts_year_shift.unwrap_or(0),
draft_template: cfg_builder
.draft_template
.unwrap_or_else(|| "draft.html".to_string()),
.unwrap_or_else(|| "draft.txt".to_string()),
publish_dest: cfg_builder
.publish_dest
.unwrap_or_else(|| PathBuf::from("content/posts")),
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod watcher;

use opt::{Commands, Opt};
use regex::Regex;
use tracing::error;
use tracing::{error, info};
use tracing_subscriber::{fmt::time::UtcTime, prelude::*, EnvFilter};
use watcher::SiteWatcher;

Expand Down Expand Up @@ -55,6 +55,8 @@ async fn main() -> Result<()> {
None
};

info!("emile {}", clap::crate_version!());

match opt.command {
Commands::New { title } => {
let cfg = SiteConfigBuilder::get_config();
Expand Down
26 changes: 19 additions & 7 deletions src/social/mastodon.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use reqwest::Url;
use anyhow::{bail, Result};
use reqwest::{StatusCode, Url};
use serde_derive::{Deserialize, Serialize};
use tracing::{error, info};
use tracing::{error, info, warn};

use crate::config::SocialInstance;

Expand Down Expand Up @@ -42,18 +42,24 @@ pub async fn push_to_mastodon(
use sha2::{Digest, Sha256};
let hash = format!("{:x}", Sha256::digest(toot.status.as_bytes()));

let status = reqwest::Client::new()
let res = reqwest::Client::new()
.post(&format!("https://{}/api/v1/statuses", instance.server))
.bearer_auth(&token)
.header("Idempotency-Key", hash)
.json(&toot)
.send()
.await?
.json::<Status>()
.await?;

if res.status() != StatusCode::OK {
let status = res.status();
let text = res.text().await?;
bail!("Failed to push to Mastodon: {status}, {text}");
}

let status = res.json::<Status>().await?;

// bookmark it to avoid deletion and for easy retrieval
reqwest::Client::new()
let res = reqwest::Client::new()
.post(&format!(
"https://{}/api/v1/statuses/{}/bookmark",
instance.server, status.id
Expand All @@ -62,5 +68,11 @@ pub async fn push_to_mastodon(
.send()
.await?;

if res.status() != StatusCode::OK {
let status = res.status();
let text = res.text().await?;
warn!("Failed to bookmark toot: {status}, {text}");
}

Ok(Some(Url::parse(&status.uri)?))
}

0 comments on commit 77b1b4a

Please sign in to comment.