Skip to content

Commit

Permalink
Add Mastodon client
Browse files Browse the repository at this point in the history
  • Loading branch information
misenhower committed Dec 11, 2022
1 parent d8fa3a7 commit b559813
Show file tree
Hide file tree
Showing 5 changed files with 611 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ TWITTER_CONSUMER_KEY=
TWITTER_CONSUMER_SECRET=
TWITTER_ACCESS_TOKEN_KEY=
TWITTER_ACCESS_TOKEN_SECRET=

# Mastodon API parameters
MASTODON_URL=
MASTODON_ACCESS_TOKEN=
48 changes: 48 additions & 0 deletions app/social/clients/MastodonClient.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { login } from 'masto/fetch';
import Client from "./Client.mjs";

export default class MastodonClient extends Client
{
key = 'mastodon';
name = 'Mastodon';

#url;
#accessToken;

constructor() {
super();

this.#url = process.env.MASTODON_URL;
this.#accessToken = process.env.MASTODON_ACCESS_TOKEN;
}

async send(status, generator) {
// Mastodon API
const masto = await login({
url: this.#url,
accessToken: this.#accessToken,
disableVersionCheck: true,
disableExperimentalWarning: true,
});

// Upload images
let mediaIds = await Promise.all(
status.media.map(async m => {
let request = { file: new Blob([m.file]) };
if (m.altText) {
request.description = m.altText;
}

let attachment = await masto.mediaAttachments.create(request);

return attachment.id;
}),
);

// Send status
await masto.statuses.create({
status: status.status,
mediaIds,
});
}
}
2 changes: 2 additions & 0 deletions app/social/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FileWriter from "./clients/FileWriter.mjs";
import MastodonClient from "./clients/MastodonClient.mjs";
import TwitterClient from "./clients/TwitterClient.mjs";
import DailyDropGearStatus from "./generators/DailyDropGearStatus.mjs";
import RegularGearStatus from "./generators/RegularGearStatus.mjs";
Expand All @@ -22,6 +23,7 @@ function defaultStatusGenerators() {
function defaultClients() {
return [
new TwitterClient,
new MastodonClient,
];
}

Expand Down
Loading

0 comments on commit b559813

Please sign in to comment.