git ssb


5+

dinosaur / patchcore



Commit ab512c6473ad4ed1ba9143dc61c51545cf6ce781

Merge branch 'master' of github.com:ssbc/patchcore

mix irving committed on 3/19/2017, 5:55:08 AM
Parent: 0b961bbde74c58a7bdc542e6385a99ebc902bc41
Parent: f526bdeefd8c4fe7cb1108421d647e74ab85ef24

Files changed

channel/obs/subscribed.jschanged
contact/async.jschanged
contact/obs.jschanged
feed/obs/thread.jschanged
feed/pull/channel.jschanged
feed/pull/mentions.jschanged
message/async/name.jschanged
message/obs/likes.jschanged
message/obs/name.jschanged
package.jsonchanged
channel/obs/subscribed.jsView
@@ -1,27 +1,38 @@
11 var pull = require('pull-stream')
22 var computed = require('mutant/computed')
33 var MutantPullReduce = require('mutant-pull-reduce')
44 var nest = require('depnest')
5+var ref = require('ssb-ref')
56
67 var throttle = require('mutant/throttle')
78
89 exports.needs = nest({
910 'sbot.pull.userFeed': 'first'
1011 })
1112
1213 exports.gives = nest({
13- 'channel.obs': ['subscribed']
14+ 'channel.obs.subscribed': true,
15+ 'sbot.hook.feed': true
1416 })
1517
1618 exports.create = function (api) {
1719 var cache = {}
20+ var reducers = {}
1821
1922 return nest({
20- 'channel.obs': {subscribed}
23+ 'channel.obs.subscribed': subscribed,
24+ 'sbot.hook.feed': function (msg) {
25+ if (isChannelSubscription(msg)) {
26+ if (msg.value.content.channel && reducers[msg.value.author]) {
27+ reducers[msg.value.author].push(msg)
28+ }
29+ }
30+ }
2131 })
2232
2333 function subscribed (userId) {
34+ if (!ref.isFeed(userId)) throw new Error('a feed id must be specified')
2435 if (cache[userId]) {
2536 return cache[userId]
2637 } else {
2738 var stream = pull(
@@ -30,18 +41,23 @@
3041 return !msg.value || msg.value.content.type === 'channel'
3142 })
3243 )
3344
45+ var latestTimestamp = 0
46+
3447 var result = MutantPullReduce(stream, (result, msg) => {
35- var c = msg.value.content
36- if (typeof c.channel === 'string' && c.channel) {
37- var channel = c.channel.trim()
38- if (channel) {
39- if (typeof c.subscribed === 'boolean') {
40- if (c.subscribed) {
41- result.add(channel)
42- } else {
43- result.delete(channel)
48+ if (msg.value.timestamp > latestTimestamp) {
49+ var c = msg.value.content
50+ if (typeof c.channel === 'string' && c.channel) {
51+ latestTimestamp = msg.value.timestamp
52+ var channel = c.channel.trim()
53+ if (channel) {
54+ if (typeof c.subscribed === 'boolean') {
55+ if (c.subscribed) {
56+ result.add(channel)
57+ } else {
58+ result.delete(channel)
59+ }
4460 }
4561 }
4662 }
4763 }
@@ -50,8 +66,10 @@
5066 startValue: new Set(),
5167 nextTick: true
5268 })
5369
70+ reducers[userId] = result
71+
5472 var instance = throttle(result, 2000)
5573 instance.sync = result.sync
5674
5775 instance.has = function (value) {
@@ -62,4 +80,8 @@
6280 return instance
6381 }
6482 }
6583 }
84+
85+function isChannelSubscription (msg) {
86+ return msg.value && msg.value.content && msg.value.content.type === 'channel'
87+}
contact/async.jsView
@@ -1,6 +1,7 @@
11 var nest = require('depnest')
22 var pull = require('pull-stream')
3+var ref = require('ssb-ref')
34
45 exports.needs = nest({
56 'sbot.pull.query': 'first',
67 'sbot.async.publish': 'first'
@@ -28,16 +29,18 @@
2829 )
2930 }
3031
3132 function follow (id, cb) {
33+ if (!ref.isFeed(id)) throw new Error('a feed id must be specified')
3234 api.sbot.async.publish({
3335 type: 'contact',
3436 contact: id,
3537 following: true
3638 }, cb)
3739 }
3840
3941 function unfollow (id, cb) {
42+ if (!ref.isFeed(id)) throw new Error('a feed id must be specified')
4043 api.sbot.async.publish({
4144 type: 'contact',
4245 contact: id,
4346 following: false
@@ -45,15 +48,15 @@
4548 }
4649 }
4750
4851 function makeQuery (a, b) {
49- return {"$filter": {
52+ return {'$filter': {
5053 value: {
5154 author: a,
5255 content: {
5356 type: 'contact',
5457 contact: b,
5558 following: true
5659 }
57- },
60+ }
5861 }}
5962 }
contact/obs.jsView
@@ -31,8 +31,9 @@
3131 }
3232 })
3333
3434 function following (id) {
35+ if (!ref.isFeed(id)) throw new Error('a feed id must be specified')
3536 if (!followingCache[id]) {
3637 followingCache[id] = reduce(api.sbot.pull.query({
3738 query: [
3839 makeQuery(id, { $prefix: '@' }),
@@ -48,8 +49,9 @@
4849 return followingCache[id]
4950 }
5051
5152 function followers (id) {
53+ if (!ref.isFeed(id)) throw new Error('a feed id must be specified')
5254 if (!followerCache[id]) {
5355 followerCache[id] = reduce(api.sbot.pull.query({
5456 query: [
5557 makeQuery({ $prefix: '@' }, id),
@@ -68,9 +70,9 @@
6870
6971 function reduce (stream) {
7072 var newestValues = {}
7173 return MutantPullReduce(stream, (result, item) => {
72- if (!ref.isFeed(item.id)) return result
74+ if (!ref.isFeed(item.id)) return result // invalid message, skip this item
7375 newestValues[item.id] = newestValues[item.id] || 0
7476 if (newestValues[item.id] < item.timestamp) {
7577 newestValues[item.id] = item.timestamp
7678 if (item.value != null) {
feed/obs/thread.jsView
@@ -1,8 +1,9 @@
11 var nest = require('depnest')
22 var pull = require('pull-stream')
33 var pullCat = require('pull-cat')
44 var sort = require('ssb-sort')
5+var ref = require('ssb-ref')
56 var { map, computed } = require('mutant')
67
78 exports.needs = nest({
89 'sbot.pull.links': 'first',
@@ -14,8 +15,10 @@
1415 exports.gives = nest('feed.obs.thread')
1516
1617 exports.create = function (api) {
1718 return nest('feed.obs.thread', function (rootId, {branch}) {
19+ if (!ref.isLink(rootId)) throw new Error('an id must be specified')
20+
1821 var rootMessageStream = pull(
1922 pull.values([rootId]),
2023 pull.asyncMap((key, cb) => {
2124 return api.sbot.async.get(key, (err, value) => cb(err, {key, value}))
feed/pull/channel.jsView
@@ -7,8 +7,10 @@
77 })
88
99 exports.create = function (api) {
1010 return nest('feed.pull.channel', function (channel) {
11+ if (typeof channel !== 'string') throw new Error('a channel name be specified')
12+
1113 return function (opts) {
1214 var filter = {value: {content: { channel }}}
1315 var query = {query: [
1416 {$filter: filter}
feed/pull/mentions.jsView
@@ -1,7 +1,8 @@
11 const nest = require('depnest')
22 const extend = require('xtend')
33 const pull = require('pull-stream')
4+const ref = require('ssb-ref')
45
56 exports.needs = nest({
67 'sbot.pull.log': 'first',
78 'message.sync.unbox': 'first',
@@ -11,8 +12,9 @@
1112 exports.gives = nest('feed.pull.mentions')
1213
1314 exports.create = function (api) {
1415 return nest('feed.pull.mentions', function (id) {
16+ if (!ref.isFeed(id)) throw new Error('a feed id must be specified')
1517 var cache = api.sbot.sync.cache()
1618
1719 return function getStream (opts) {
1820 opts = extend(opts)
message/async/name.jsView
@@ -1,6 +1,7 @@
11 const nest = require('depnest')
22 const getAvatar = require('ssb-avatar')
3+const ref = require('ssb-ref')
34
45 exports.needs = nest({
56 'sbot.async.get': 'first',
67 'sbot.pull.links': 'first',
@@ -11,8 +12,9 @@
1112 // needs an async version
1213
1314 exports.create = function (api) {
1415 return nest('message.async.name', function (id, cb) {
16+ if (!ref.isLink(id)) throw new Error('an id must be specified')
1517 var fallbackName = id.substring(0, 10) + '...'
1618 api.sbot.async.get(id, function (err, value) {
1719 if (err && err.name === 'NotFoundError') {
1820 return cb(null, fallbackName + '...(missing)')
message/obs/likes.jsView
@@ -1,5 +1,6 @@
11 var nest = require('depnest')
2+var ref = require('ssb-ref')
23 var { Value, computed } = require('mutant')
34
45 exports.needs = nest({
56 'message.sync.unbox': 'first'
@@ -30,8 +31,9 @@
3031 }
3132 }
3233 },
3334 'message.obs.likes': (id) => {
35+ if (!ref.isLink(id)) throw new Error('an id must be specified')
3436 return computed(get(id), getLikes)
3537 }
3638 })
3739
message/obs/name.jsView
@@ -7,9 +7,9 @@
77 exports.gives = nest('message.obs.name')
88
99 exports.create = function (api) {
1010 return nest('message.obs.name', function (id) {
11- if (typeof id !== 'string') { throw new Error('link must be to message id') }
11+ if (!ref.isLink(id)) throw new Error('an id must be specified')
1212 var value = Value(id.substring(0, 10) + '...')
1313
1414 if (ref.isMsg(id)) {
1515 api.message.async.name(id, function (err, name) {
package.jsonView
@@ -1,7 +1,7 @@
11 {
22 "name": "patchcore",
3- "version": "0.4.5",
3+ "version": "0.4.7",
44 "description": "minimal core for ssb clients",
55 "main": "index.js",
66 "scripts": {
77 "start": "electro example",

Built with git-ssb-web