This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial (very bad) microsub support!
- Loading branch information
1 parent
6e7fb96
commit 6f6d6e7
Showing
24 changed files
with
1,029 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
const fetch = require('isomorphic-fetch'); | ||
const Micropub = require('micropub-helper'); | ||
const { URL } = require('url'); | ||
|
||
const defaultSettings = { | ||
me: '', | ||
scope: 'post create delete update', | ||
token: '', | ||
authEndpoint: '', | ||
tokenEndpoint: '', | ||
microsubEndpoint: '', | ||
}; | ||
|
||
class Microsub extends Micropub { | ||
constructor(userSettings = {}) { | ||
super(); | ||
this.options = Object.assign(defaultSettings, userSettings); | ||
|
||
// Bind all the things | ||
this.getChannels = this.getChannels.bind(this); | ||
this.search = this.search.bind(this); | ||
this.createChannel = this.createChannel.bind(this); | ||
this.follow = this.follow.bind(this); | ||
this.unfollow = this.unfollow.bind(this); | ||
this.preview = this.preview.bind(this); | ||
this.getFollowing = this.getFollowing.bind(this); | ||
this.getTimeline = this.getTimeline.bind(this); | ||
} | ||
|
||
getChannels() { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'channels'); | ||
fetch(url.toString(), { | ||
method: 'GET', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((channels) => { | ||
resolve(channels.channels); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
createChannel(channelName) { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'channels'); | ||
url.searchParams.append('name', channelName); | ||
fetch(url.toString(), { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((newChannel) => { | ||
resolve(newChannel); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
search(text) { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'search'); | ||
url.searchParams.append('query', text); | ||
fetch(url.toString(), { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((results) => { | ||
resolve(results.results); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
follow(feed, channel = 'default') { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'follow'); | ||
url.searchParams.append('url', feed); | ||
if (channel) { | ||
url.searchParams.append('channel', channel); | ||
} | ||
fetch(url.toString(), { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((results) => { | ||
resolve(results); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
unfollow(feed, channel = false) { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'unfollow'); | ||
url.searchParams.append('url', feed); | ||
if (channel) { | ||
url.searchParams.append('channel', channel); | ||
} | ||
fetch(url.toString(), { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((results) => { | ||
resolve(results); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
preview(feed) { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'preview'); | ||
url.searchParams.append('url', feed); | ||
fetch(url.toString(), { | ||
method: 'GET', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((results) => { | ||
resolve(results); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
getFollowing(channel) { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'follow'); | ||
url.searchParams.append('channel', channel); | ||
fetch(url.toString(), { | ||
method: 'GET', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((results) => { | ||
resolve(results); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
getTimeline(channel = 'default', after = false, before = false, limit = 20) { | ||
return new Promise((resolve, reject) => { | ||
const url = new URL(this.options.microsubEndpoint); | ||
url.searchParams.append('action', 'timeline'); | ||
if (channel) { | ||
url.searchParams.append('channel', channel); | ||
} | ||
if (limit) { | ||
url.searchParams.append('limit', limit); | ||
} | ||
if (after) { | ||
url.searchParams.append('after', after); | ||
} | ||
if (before) { | ||
url.searchParams.append('before', before); | ||
} | ||
fetch(url.toString(), { | ||
method: 'GET', | ||
headers: new Headers({ | ||
'Authorization': 'Bearer ' + this.options.token, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((results) => { | ||
resolve(results); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Microsub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const Micropub = require('micropub-helper'); | ||
|
||
module.exports = (req, res, next) => { | ||
req.body.clientId = 'http://together.com'; | ||
let micropub = new Micropub(req.body); | ||
micropub[req.params.method](req.body.param) | ||
.then((result) => { | ||
res.json({ | ||
result: result, | ||
options: micropub.options, | ||
}); | ||
}) | ||
.catch((err) => { | ||
let status = 500; | ||
if (err.status) { | ||
status = err.status; | ||
} | ||
res.status(status); | ||
res.json(err); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const Microsub = require('../microsub'); | ||
|
||
module.exports = (req, res, next) => { | ||
req.body.clientId = 'http://together.com'; | ||
let microsub = new Microsub(req.body); | ||
let params = req.body.params || []; | ||
microsub[req.params.method](...params) | ||
.then((result) => { | ||
res.json({ | ||
result: result, | ||
options: microsub.options, | ||
}); | ||
}) | ||
.catch((err) => { | ||
let status = 500; | ||
if (err.status) { | ||
status = err.status; | ||
} | ||
res.status(status); | ||
res.json(err); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module.exports = function (htmlString, url) { | ||
let rels = {}; | ||
let baseUrl = url; | ||
|
||
const doc = new DOMParser().parseFromString(htmlString, 'text/html'); | ||
const baseEl = doc.querySelector('base[href]'); | ||
const relEls = doc.querySelectorAll('[rel][href]'); | ||
|
||
if (baseEl) { | ||
const value = baseEl.getAttribute('href'); | ||
const url = new URL(value, url); | ||
baseUrl = url.toString(); | ||
} | ||
|
||
if (relEls.length) { | ||
relEls.forEach((relEl) => { | ||
const names = relEl.getAttribute('rel').toLowerCase().split("\\s+"); | ||
const value = relEl.getAttribute('href'); | ||
if (names.length && value !== null) { | ||
names.forEach((name) => { | ||
if (!rels[name]) { | ||
rels[name] = []; | ||
} | ||
const url = new URL(value, baseUrl).toString(); | ||
if (rels[name].indexOf(url) === -1) { | ||
rels[name].push(url); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return rels; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.