1

I am struggling with a basic Mastodon API call - how to delete a toot. I am using the node.js 'mastodon-api' package.

Here are the function declarations:

const Mastodon = require('mastodon-api');
const mastodonInstance = 'https://social.example.com';
const clientId = '...';
const clientSecret = '...';
const accessToken = '...';
const profileId = '...';
let mastodon;

// Initialize Mastodon API
try {
    console.log(`Opening API instance to ${mastodonInstance}`);
    mastodon = new Mastodon({
        client_id: clientId,
        client_secret: clientSecret,
        access_token: accessToken,
        api_url: `${mastodonInstance}/api/v1/`
    });
} catch (error) {
    console.error('Opening of the Mastodon API instance failed');
    process.exit(1);
}

// createToot: create a new toot with specified toot and optional media
async function createToot(text, mediaData = null) {
    try {
        const params = { status: text };
        if (mediaData) {
            params.media = {
                file: mediaData.buffer,
                mime_type: mediaData.mimeType
            };
        }
        await mastodon.post('statuses', params);
        console.log('createToot: Toot successfully created.');
        return(0);
    } catch (error) {
        console.error('createToot: Error creating toot:', error.message);
    }
}

// deleteToot: delete existing toot
async function deleteToot(existingToot) {
    console.log('deleteToot: Hello!'); 
    try {
        const response = await mastodon.delete(`statuses/${existingToot.id}`);
        console.log('deleteToot: Server response:', response.data); 
        return(0);
    } catch (error) {
        console.error('deleteToot: Error:', error.message);
    }
}

I cannot succesfully delete a toot with deleteToot() or create a new toot with createToot().

Here is how I am invoking both:

    // create a new toot with the cleanText and mediaObject
    try{
        let status = createToot(cleanText,mediaObject);
    } catch(error) {
        console.error('main: Error creating new toot:', error.message);
        process.exit(1);
    };   

    // delete an existing toot specified by the existingToot object
    try {
        let status = await deleteToot(existingToot);
        console.log(`modifyToot: deleteToot returned ${status}`);
    } catch (error) {
        console.error(`modifyToot: deleteToot call failed. Error: ${error.message}`);
    }  

When I invoke both functions, I receive no output to console.log, or console.error. All errors are trapped however. The existing toot remains untouched after deleteToot() is invoked. No new toots are created after createToot() is invoked.

A few other observations:

  • mastodon-api version 1.3.0 is being used; it was installed via npm.
  • I can read toots successfully via mastodon.get() calls.
  • The client credentials underneath the Mastodon server profile have both 'read' and 'write' top level scopes checked.
  • When I use alternative delete toot API mastodon.del() an error is trapped saying this methods does not exist.
  • I tried using mastodon.deleteStatus(). No error was thrown, but the toot was not deleted, either.
try {
  await mastodon.del(`statuses/${Toot.id}`);
  console.log('Success: The status has been deleted.');
} catch (error) {
  console.error('Error:', error);
}

try {
  await mastodon.deleteStatus(Toot.id);
  console.log('Success: The status has been deleted.');
} catch (error) {
  console.error('Error:', error);
}

I'm at a standstill. Am I calling the Mastodon API correctly?

Chris
  • 11
  • 1
  • One thing that stands out is that you're not awaiting `createToot` in one of your examples. – Evert Jul 22 '23 at 18:46
  • 1
    `mastodon-api` hasn't been updated in four years. I don't know if ActivityPub has changed since then, but you might have better luck with the `masto` package. It also looks like `mastodon-api` is built with callbacks in mind, not Promises, though the underlying `request` method does return a Promise. – Zac Anger Jul 22 '23 at 19:11
  • @Evert - nice catch! – Chris Jul 23 '23 at 00:05

0 Answers0