diff options
Diffstat (limited to 'lib/app.js')
-rw-r--r-- | lib/app.js | 109 |
1 files changed, 98 insertions, 11 deletions
@@ -4,14 +4,14 @@ var lru = require('hashlru') var pkg = require('../package') var u = require('./util') var pull = require('pull-stream') -var ssbAvatar = require('ssb-avatar') var hasher = require('pull-hash/ext/ssb') var multicb = require('multicb') var paramap = require('pull-paramap') var Contacts = require('ssb-contact') - +var About = require('./about') var Serve = require('./serve') var Render = require('./render') +var Git = require('./git') module.exports = App @@ -32,15 +32,17 @@ function App(sbot, config) { } sbot.get = memo({cache: lru(100)}, sbot.get) + this.about = new About(this, sbot.id) this.getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot) this.getAbout = memo({cache: this.aboutCache = lru(500)}, - getAbout.bind(this), sbot, sbot.id) + this._getAbout.bind(this)) this.unboxContent = memo({cache: lru(100)}, sbot.private.unbox) - this.reverseNameCache = lru(100) + this.reverseNameCache = lru(500) this.unboxMsg = this.unboxMsg.bind(this) this.render = new Render(this, this.opts) + this.git = new Git(this) } App.prototype.go = function () { @@ -175,10 +177,7 @@ App.prototype.addBlob = function (cb) { done(function (err, hash, add) { cb(err, hash) }) - return pull( - hasher(hashCb), - this.sbot.blobs.add(addCb) - ) + return sink } App.prototype.pushBlob = function (id, cb) { @@ -186,11 +185,56 @@ App.prototype.pushBlob = function (id, cb) { this.sbot.blobs.push(id, cb) } +App.prototype.readBlob = function (link) { + link = u.toLink(link) + return this.sbot.blobs.get({ + hash: link.link, + size: link.size, + }) +} + +App.prototype.readBlobSlice = function (link, opts) { + if (this.sbot.blobs.getSlice) return this.sbot.blobs.getSlice({ + hash: link.link, + size: link.size, + start: opts.start, + end: opts.end, + }) + return pull( + this.readBlob(link), + u.pullSlice(opts.start, opts.end) + ) +} + +App.prototype.ensureHasBlobs = function (links, cb) { + var self = this + var done = multicb({pluck: 1}) + links.forEach(function (link) { + var cb = done() + self.sbot.blobs.size(link.link, function (err, size) { + if (err) cb(err) + else if (size == null) cb(null, link) + else cb() + }) + }) + done(function (err, missingLinks) { + if (err) console.trace(err) + missingLinks = missingLinks.filter(Boolean) + if (missingLinks.length == 0) return cb() + return cb({name: 'BlobNotFoundError', links: missingLinks}) + }) +} + App.prototype.getReverseNameSync = function (name) { var id = this.reverseNameCache.get(name) return id } +App.prototype.getNameSync = function (name) { + var about = this.aboutCache.get(name) + return about && about.name +} + function getMsgWithValue(sbot, id, cb) { if (!id) return cb() sbot.get(id, function (err, value) { @@ -199,11 +243,12 @@ function getMsgWithValue(sbot, id, cb) { }) } -function getAbout(sbot, src, id, cb) { +App.prototype._getAbout = function (id, cb) { var self = this - ssbAvatar(sbot, src, id, function (err, about) { + if (!u.isRef(id)) return cb(null, {}) + self.about.get(id, function (err, about) { if (err) return cb(err) - var sigil = id && id[0] || '@' + var sigil = id[0] || '@' if (about.name && about.name[0] !== sigil) { about.name = sigil + about.name } @@ -212,6 +257,10 @@ function getAbout(sbot, src, id, cb) { }) } +App.prototype.pullGetMsg = function (id) { + return pull.asyncMap(this.getMsg)(pull.once(id)) +} + App.prototype.createLogStream = function (opts) { opts = opts || {} return opts.sortByTimestamp @@ -282,6 +331,40 @@ App.prototype.streamChannels = function (opts) { ) } +App.prototype.streamMyChannels = function (id, opts) { + // use ssb-query plugin if it is available, since it has an index for + // author + type + if (this.sbot.query) return pull( + this.sbot.query.read({ + reverse: true, + query: [ + {$filter: { + value: { + author: id, + content: {type: 'channel', subscribed: true} + } + }}, + {$map: ['value', 'content', 'channel']} + ] + }), + pull.unique() + ) + + return pull( + this.sbot.createUserStream({id: id, reverse: true}), + this.unboxMessages(), + pull.filter(function (msg) { + if (msg.value.content.type == 'channel') { + return msg.value.content.subscribed + } + }), + pull.map(function (msg) { + return msg.value.content.channel + }), + pull.unique() + ) +} + App.prototype.createContactStreams = function (id) { return new Contacts(this.sbot).createContactStreams(id) } @@ -345,3 +428,7 @@ App.prototype.getVoted = function (_opts, cb) { }) ) } + +App.prototype.createAboutStreams = function (id) { + return this.about.createAboutStreams(id) +} |