Skip to content

Commit

Permalink
feat: remove syncing website post data using a JF2 Feed
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jan 28, 2021
1 parent 069eac4 commit 6f39349
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 83 deletions.
1 change: 0 additions & 1 deletion indiekit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const twitter = new TwitterSyndicator({
indiekit.set('application.mongodbUrl', process.env.MONGODB_URL);

// Publication settings
indiekit.set('publication.jf2Feed', process.env.PUBLICATION_JF2);
indiekit.set('publication.me', process.env.PUBLICATION_URL);
indiekit.set('publication.preset', jekyll);
indiekit.set('publication.store', github);
Expand Down
1 change: 0 additions & 1 deletion packages/endpoint-syndicate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {syndicateController} from './lib/controllers/syndicate.js';
export const __dirname = path.dirname(fileURLToPath(import.meta.url));

const defaults = {
jf2Feed: false,
mountpath: '/syndicate'
};

Expand Down
36 changes: 1 addition & 35 deletions packages/endpoint-syndicate/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,48 +22,14 @@ export const getMicropubEndpoint = (publication, request) => {
* @returns {object} Post data for given url else recently published post
*/
export const getPostData = async (publication, url) => {
const {posts, jf2Feed} = publication;
const {posts} = publication;
let postData = {};

if (url) {
// Get item in database which matching URL
postData = await posts.findOne({
'properties.url': url
});
} else if (jf2Feed) {
// Fetch JF2 Feed and return first child
try {
const {body} = await got(jf2Feed, {responseType: 'json'});
const {children} = body;

if (children.length > 0) {
const properties = children[0];
properties['mp-slug'] = path.basename(properties.url);

// Check if feed item already exists in database
const storedPostData = await posts.findOne({
'properties.url': properties.url
});

// Create new post data object
postData.date = new Date();
postData.lastAction = 'import';
postData.properties = properties;

// Update existing or insert new post data
await (storedPostData ?
posts.updateOne(postData, {checkKeys: false}) :
posts.insertOne(postData, {checkKeys: false})
);
} else {
throw new Error('JF2 Feed does not contain any posts');
}
} catch (error) {
const errorMessage = error.response ?
error.response.body.message :
error.message;
throw new Error(errorMessage);
}
} else {
// Get items in database and return first item
const items = await posts.find().toArray();
Expand Down
4 changes: 2 additions & 2 deletions packages/endpoint-syndicate/tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.beforeEach(async t => {
.set('Accept', 'application/json');
});

test.skip('Syndicates a URL', async t => {
test('Syndicates a URL', async t => {
const authScope = nock('https://tokens.indieauth.com')
.get('/token')
.reply(200, {
Expand All @@ -54,7 +54,7 @@ test.skip('Syndicates a URL', async t => {
storeScope.done();
});

test.skip('Throws error syndicating a URL', async t => {
test('Throws error syndicating a URL', async t => {
const authScope = nock('https://tokens.indieauth.com')
.get('/token')
.reply(200, {
Expand Down
43 changes: 0 additions & 43 deletions packages/endpoint-syndicate/tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,6 @@ test('Gets post for given URL from database', async t => {
t.is(result.properties['mp-syndicate-to'], 'https://social.example/');
});

test('Adds post data from JF2 Feed', async t => {
const scope = nock('https://website.example')
.get('/feed.jf2')
.reply(200, getFixture('jf2/feed.jf2'));
const publication = {
jf2Feed: 'https://website.example/feed.jf2',
posts: {
findOne: async () => false,
insertOne: async () => {}
}
};
const result = await getPostData(publication);
t.is(result.properties.name, 'Second item in feed');
scope.done();
});

test('Updates post data from JF2 Feed to database', async t => {
const scope = nock('https://website.example')
.get('/feed.jf2')
.reply(200, getFixture('jf2/feed.jf2'));
const {publication} = t.context;
publication.jf2Feed = 'https://website.example/feed.jf2';
const result = await getPostData(publication);
t.is(result.properties.name, 'Second item in feed');
scope.done();
});

test('Throws error if no posts in JF2 Feed', async t => {
const scope = nock('https://website.example')
.get('/feed.jf2')
.reply(200, getFixture('jf2/feed-empty.jf2'));
const publication = {
jf2Feed: 'https://website.example/feed.jf2',
posts: {
findOne: async () => false,
insertOne: async () => {}
}
};
const error = await t.throwsAsync(getPostData(publication));
t.is(error.message, 'JF2 Feed does not contain any posts');
scope.done();
});

test('Gets post data from database', async t => {
const result = await getPostData(t.context.publication);
t.is(result.properties['mp-syndicate-to'], 'https://social.example/');
Expand Down
1 change: 0 additions & 1 deletion packages/indiekit/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const application = {
const publication = {
categories: [],
locale: 'en',
jf2Feed: false,
me: null,
postTemplate: null,
postTypes: [],
Expand Down

0 comments on commit 6f39349

Please sign in to comment.