git ssb

10+

Matt McKegg / patchwork



Commit a5417350593eb41e7c3b56c8edf96aa895411fb2

allow undig, update digs in realtime

Matt McKegg committed on 10/29/2016, 10:05:33 AM
Parent: 03158ef331e75bc9f0565e3c8bc867e8168672e5

Files changed

api/index.jschanged
api/like-cache.jsadded
modules/like.jsadded
api/index.jsView
@@ -1,7 +1,8 @@
11 var pull = require('pull-stream')
22 var ssbKeys = require('ssb-keys')
33 var ref = require('ssb-ref')
4 +var LikeCache = require('./like-cache')
45
56 function Hash (onHash) {
67 var buffers = []
78 return pull.through(function (data) {
@@ -25,8 +26,9 @@
2526
2627 module.exports = function (sbot, opts) {
2728 var connection_status = []
2829 var keys = opts.keys
30 + var likeCache = LikeCache()
2931
3032 var internal = {
3133 getLatest: function (id, cb) {
3234 sbot.getLatest(id, cb)
@@ -46,8 +48,11 @@
4648 connection_status: connection_status,
4749 get_id: function () {
4850 return sbot.id
4951 },
52 + get_likes: function (id) {
53 + return likeCache.get(id)
54 + },
5055 sbot_blobs_add: function (cb) {
5156 return pull(
5257 Hash(function (err, id) {
5358 if(err) return cb(err)
@@ -79,8 +84,9 @@
7984 return pull(
8085 sbot.createLogStream(opts),
8186 pull.through(function (e) {
8287 CACHE[e.key] = CACHE[e.key] || e.value
88 + likeCache.updateFrom(e)
8389 })
8490 )
8591 },
8692 sbot_user_feed: function (opts) {
api/like-cache.jsView
@@ -1,0 +1,26 @@
1 +var Value = require('@mmckegg/mutant/value')
2 +
3 +module.exports = function LikeCache () {
4 + var lookup = {}
5 +
6 + return { get, updateFrom }
7 +
8 + function get (msgId) {
9 + if (!lookup[msgId]) {
10 + lookup[msgId] = Value({})
11 + }
12 + return lookup[msgId]
13 + }
14 +
15 + function updateFrom (msg) {
16 + if (msg.key && msg.value && msg.value.content && msg.value.content.type === 'vote') {
17 + if (msg.value.content.vote && msg.value.content.vote.link) {
18 + var likes = get(msg.value.content.vote.link)()
19 + if (!likes[msg.value.author] || likes[msg.value.author][1] < msg.timestamp) {
20 + likes[msg.value.author] = [msg.value.content.vote.value > 0, msg.timestamp]
21 + get(msg.value.content.vote.link).set(likes)
22 + }
23 + }
24 + }
25 + }
26 +}
modules/like.jsView
@@ -1,0 +1,71 @@
1 +var h = require('@mmckegg/mutant/html-element')
2 +var computed = require('@mmckegg/mutant/computed')
3 +var when = require('@mmckegg/mutant/when')
4 +var plugs = require('patchbay/plugs')
5 +var message_link = plugs.first(exports.message_link = [])
6 +var get_id = plugs.first(exports.get_id = [])
7 +var get_likes = plugs.first(exports.get_likes = [])
8 +var publish = plugs.first(exports.sbot_publish = [])
9 +
10 +exports.message_content = exports.message_content_mini = function (msg, sbot) {
11 + if (msg.value.content.type !== 'vote') return
12 + var link = msg.value.content.vote.link
13 + return [
14 + msg.value.content.vote.value > 0 ? 'dug' : 'undug',
15 + ' ', message_link(link)
16 + ]
17 +}
18 +
19 +exports.message_meta = function (msg, sbot) {
20 + return h('span.likes', [computed(get_likes(msg.key), likeCount)])
21 +}
22 +
23 +exports.message_action = function (msg, sbot) {
24 + var id = get_id()
25 + var dug = computed([get_likes(msg.key), id], doesLike)
26 +
27 + if (msg.value.content.type !== 'vote') {
28 + return h('a.dig', {
29 + href: '#',
30 + 'ev-click': function () {
31 + var dig = dug() ? {
32 + type: 'vote',
33 + vote: { link: msg.key, value: 0, expression: 'Undig' }
34 + } : {
35 + type: 'vote',
36 + vote: { link: msg.key, value: 1, expression: 'Dig' }
37 + }
38 + if (msg.value.content.recps) {
39 + dig.recps = msg.value.content.recps.map(function (e) {
40 + return e && typeof e !== 'string' ? e.link : e
41 + })
42 + dig.private = true
43 + }
44 + publish(dig)
45 + }
46 + }, when(dug, 'Undig', 'Dig'))
47 + }
48 +}
49 +
50 +function doesLike (likes, userId) {
51 + return likes && likes[userId] && likes[userId][0] || false
52 +}
53 +
54 +function likeCount (data) {
55 + var likes = getLikes(data)
56 + if (likes.length === 1) {
57 + return '1 Dig'
58 + }
59 + if (likes.length > 1) {
60 + return likes.length + ' Digs'
61 + }
62 +}
63 +
64 +function getLikes (likes) {
65 + return Object.keys(likes).reduce((result, id) => {
66 + if (likes[id][0]) {
67 + result.push(id)
68 + }
69 + return result
70 + }, [])
71 +}

Built with git-ssb-web