Skip to content

Commit c2bf818

Browse files
committed
readme and also remove requirement for user id
1 parent 159fb2e commit c2bf818

File tree

3 files changed

+89
-34
lines changed

3 files changed

+89
-34
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
11
# gatsby-source-mastodon
2-
A Gatsby source plugin for fetching user toots from Mastodon
2+
3+
A Gatsby source plugin for fetching user toots from Mastodon. Currently only support a user's own toots.
4+
5+
## Usage
6+
7+
You will need to create an `Application` from your Mastodon instance (e.g. https://aus.social/settings/applications). At the very minimum you will need to grant `read` access.
8+
9+
Add the following block of configuration into your `gatsby-config.js`. Replace fields surrounded by `***` with the correct details.
10+
11+
```javascript
12+
module.exports = {
13+
plugins: [
14+
{
15+
resolve: 'gatsby-source-mastodon',
16+
options: {
17+
api_url: '***MASTODON_INSTANCE_API_URL***',
18+
limit: 25,
19+
access_token: '***MASTODON_ACCESS_TOKEN***',
20+
},
21+
],
22+
}
23+
```
24+
25+
### Field Definition
26+
27+
`api_url` - the api url of your mastodon instance (e.g https://aus.social/api/v1)
28+
`limit` - number of toots to return
29+
`access_token` - the access token for your mastodon application
30+
31+
## Query Mastodon Data
32+
33+
A sample of the query is included below, for all available field please check the graphl browser.
34+
35+
```graphql
36+
{
37+
allToot {
38+
edges {
39+
node {
40+
id
41+
url
42+
reblogs_count
43+
favourites_count
44+
account {
45+
username
46+
}
47+
}
48+
}
49+
}
50+
}
51+
```

gatsby-node.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const Masto = require('mastodon');
1+
const Masto = require("mastodon");
22

3-
const { tootType } = require('./schema');
3+
const { tootType } = require("./schema");
44

55
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
66
const { createNode } = actions;
@@ -34,11 +34,14 @@ exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOpt
3434
});
3535
};
3636

37-
return M.get(`accounts/${configOptions.user_id}/statuses`, {
38-
exclude_replies: true,
39-
limit: configOptions.limit
40-
}).then(resp => {
41-
createNodes(resp.data);
37+
return M.get("accounts/verify_credentials").then(resp1 => {
38+
const { id } = resp1.data;
39+
return M.get(`accounts/${id}/statuses`, {
40+
exclude_replies: true,
41+
limit: configOptions.limit
42+
}).then(resp2 => {
43+
createNodes(resp2.data);
44+
});
4245
});
4346
};
4447

src/gatsby-node.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
const Masto = require('mastodon')
1+
const Masto = require("mastodon");
22

3-
const { tootType } = require('./schema')
3+
const { tootType } = require("./schema");
44

55
exports.sourceNodes = (
66
{ actions, createNodeId, createContentDigest },
77
configOptions
88
) => {
9-
const { createNode } = actions
9+
const { createNode } = actions;
1010

1111
// Gatsby adds a configOption that's not needed for this plugin, delete it
12-
delete configOptions.plugins
12+
delete configOptions.plugins;
1313

1414
var M = new Masto({
1515
access_token: configOptions.access_token,
1616
timeout_ms: 60 * 1000,
17-
api_url: configOptions.api_url,
18-
})
17+
api_url: configOptions.api_url
18+
});
1919

2020
const createNodes = toots => {
2121
toots.forEach(toot => {
22-
const nodeId = createNodeId(`toot-${toot.id}`)
23-
const nodeContent = JSON.stringify(toot)
22+
const nodeId = createNodeId(`toot-${toot.id}`);
23+
const nodeContent = JSON.stringify(toot);
2424

2525
const nodeData = Object.assign({}, toot, {
2626
id: nodeId,
@@ -29,25 +29,28 @@ exports.sourceNodes = (
2929
internal: {
3030
type: `Toot`,
3131
content: nodeContent,
32-
contentDigest: createContentDigest(toot),
33-
},
34-
})
35-
36-
createNode(nodeData)
37-
})
38-
}
39-
40-
return M.get(`accounts/${configOptions.user_id}/statuses`, {
41-
exclude_replies: true,
42-
limit: configOptions.limit,
43-
}).then(resp => {
44-
createNodes(resp.data)
45-
})
46-
}
32+
contentDigest: createContentDigest(toot)
33+
}
34+
});
35+
36+
createNode(nodeData);
37+
});
38+
};
39+
40+
return M.get("accounts/verify_credentials").then(resp1 => {
41+
const { id } = resp1.data;
42+
return M.get(`accounts/${id}/statuses`, {
43+
exclude_replies: true,
44+
limit: configOptions.limit
45+
}).then(resp2 => {
46+
createNodes(resp2.data);
47+
});
48+
});
49+
};
4750

4851
exports.setFieldsOnGraphQLNodeType = ({ type }) => {
4952
if (type.name !== `Toot`) {
50-
return {}
53+
return {};
5154
}
52-
return tootType
53-
}
55+
return tootType;
56+
};

0 commit comments

Comments
 (0)