gatsby-source-pleroma
Version:
gatsby-source plugin for Pleroma federated social network feed
108 lines (89 loc) • 4.72 kB
JavaScript
;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var axios = require('axios');
var crypto = require('crypto');
var fs = require('fs');
var Path = require('path');
exports.sourceNodes = function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref, configOptions) {
var boundActionCreators = _ref.boundActionCreators,
createNodeId = _ref.createNodeId;
var createNode, instance, userId, pages, apiUrl, processPost, nextPage;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
createNode = boundActionCreators.createNode;
// Gatsby adds a configOption that's not needed for this plugin, delete it
delete configOptions.plugins;
// URL to the pleroma instance (no trailing slash) and your userId are **required**
instance = configOptions.instance, userId = configOptions.userId;
pages = configOptions.pages || 1;
// Note: as of 7/28/18, the `count` param doesn't actually do anything: it's ALWAYS 20.
//const count = configOptions.count || 20
apiUrl = instance + '/api/qvitter/statuses/user_timeline.json?user_id=' + userId;
processPost = function processPost(post) {
var nodeId = createNodeId('pleroma-post-' + post.id);
var nodeContent = post.text;
var nodeContentDigest = crypto.createHash('md5').update(nodeContent).digest('hex');
// If there are not any attachments in your dataset, declarative calls to the attachment node will fail
// We add a placeholder here with an empty string and explicit empty boolean to make sure it stays in the schema
// Note: id is often null on REAL attachments, and is not a reliable field on which to filter empty attachments.
var attachmentPlaceholder = { id: null, url: '', empty: true, mimetype: '', oembed: null };
var nodeData = _extends({}, post, {
attachments: post.attachments && post.attachments.length ? post.attachments : [attachmentPlaceholder],
in_reply_to_status_id: post.in_reply_to_status_id || 0,
id: nodeId,
parent: null,
children: [],
internal: {
type: 'PleromaPost',
content: nodeContent,
contentDigest: nodeContentDigest
}
});
return nodeData;
};
nextPage = function nextPage(data, pages) {
var maxId = data[data.length - 1].id;
var nextUrl = apiUrl + ('&max_id=' + maxId);
axios.get(nextUrl).then(function (_ref3) {
var data = _ref3.data;
if (!data || !data.length) {
return;
}
data.forEach(function (post) {
var nodeData = processPost(post);
createNode(nodeData);
});
if (pages > 0) {
nextPage(data, pages - 1);
}
}).catch(function (err) {
return console.log(err);
});
};
return _context.abrupt('return', axios.get(apiUrl).then(function (_ref4) {
var data = _ref4.data;
data.forEach(function (post) {
var nodeData = processPost(post);
createNode(nodeData);
});
if (pages > 1) {
nextPage(data, pages);
}
}).catch(function (err) {
console.log('Error fetching from Pleroma Source!', err);
}));
case 8:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function (_x, _x2) {
return _ref2.apply(this, arguments);
};
}();