git ssb

1+

bencevans / ssb-news



Tree: bb1d879ac72e9462c12547ce70a4ccba104a1066

Files: bb1d879ac72e9462c12547ce70a4ccba104a1066 / src / index.js

918 bytesRaw
1const debug = require('debug')('ssb-news')
2const pull = require('pull-stream')
3const isUrl = require('is-url')
4
5module.exports = class SSBNews {
6 constructor (sbot) {
7 this.sbot = sbot
8 }
9
10 _isLink (link) {
11 return isUrl(link)
12 }
13
14 recent (cb) {
15 pull(
16 this.sbot.messagesByType({ type: 'news', reverse: true }),
17 pull.filter((msg) => this._isLink(msg.value.content.link)),
18 pull.collect(cb)
19 )
20 }
21
22 story (id, cb) {
23 debug('story', id)
24 this.sbot.get(id, cb)
25 }
26
27 publish (story, cb) {
28 if (typeof story.title !== 'string') {
29 return cb(new Error('missing story title'))
30 }
31 if (typeof story.link !== 'string') {
32 return cb(new Error('missing story link'))
33 }
34 if (isUrl(story.link) === false) {
35 throw new Error('invalid link')
36 }
37 this.sbot.publish({
38 type: 'news',
39 title: story.title,
40 link: story.link
41 }, cb)
42 }
43}
44

Built with git-ssb-web