Skip to content

Commit

Permalink
Save Mastodon URL as "statusUrl" on a posted note
Browse files Browse the repository at this point in the history
  • Loading branch information
brookback committed Oct 22, 2024
1 parent bad6ef6 commit 5e1eb09
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,17 @@ jobs:
todo_file: ${{ env.MASTODON_TODO }}
log_file: ${{ env.MASTODON_LOG_FILE }}

- name: Commit and push posted notes
- name: Commit and push changed files
# Pull first, since something could've been pushed in-between.
run: |
git pull
git config user.name "Automated"
git config user.email "[email protected]"
git add $log_file
# The note that was posted now as an "url" in the frontmatter
git add $log_file $dir
timestamp=$(date -u)
git commit -m "Latest post to Mastodon: $timestamp [skip-ci]" || exit 0
git push
env:
log_file: ${{ env.MASTODON_LOG_FILE }}
dir: ${{ env.NOTES_DIR }}
43 changes: 41 additions & 2 deletions _syndicate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { idOf, notePermalinkOf } from './src/_includes/permalinks.ts';
import { extract } from 'std/front_matter/any.ts';
import * as Yaml from 'std/yaml/mod.ts';

export interface Todo {
id: string;
Expand Down Expand Up @@ -61,6 +63,7 @@ export const postStatus = async (todo: Todo, accessToken: string, dryRun = false
console.log('\n' + indent(`${statusBody}`, 3) + '\n');

if (dryRun) {
await persistStatusUrl(todo, 'https://fake-url.com', true);
return;
}

Expand All @@ -78,13 +81,49 @@ export const postStatus = async (todo: Todo, accessToken: string, dryRun = false
body: form,
});

const json = await res.json();
interface Status {
id: string;
url: string;
}

interface Error {
error: string;
}

if (!res.ok) {
throw new Error(`Posting failed with ${res.status}: ${json.error}`);
const err = await res.json() as Error;
throw new Error(`Posting failed with ${res.status}: ${err.error}`);
}

const json = await res.json() as Status;

console.log(`> Status posted to ${json.url}`);

await persistStatusUrl(todo, json.url);
};

export const persistStatusUrl = async (todo: Todo, url: string, dryRun = false) => {
const data = {
statusUrl: url,
};

const filePath = './src' + todo.sourcePath;

const post = await Deno.readTextFile(filePath);
const res = extract(post);
const newData = {
...res.attrs,
...data,
};

const content = `---\n${Yaml.stringify(newData)}---\n${res.body}`;

if (dryRun) {
console.log(`> Save to ${filePath}`);
console.log(content);
} else {
await Deno.writeTextFile(filePath, content);
}
};

const indent = (str: string, n: number) => {
Expand Down

0 comments on commit 5e1eb09

Please sign in to comment.