git ssb

10+

Matt McKegg / patchwork



Commit 2a20acf33d2e0b90142c3b19c1a1fa74d59d32da

rank channel suggestions by most used

Matt McKegg committed on 2/12/2018, 8:02:06 PM
Parent: fa25bf8726402e36587f74731ea2243dbcff3c89

Files changed

modules/channel/obs/suggest.jschanged
plugs/channel/obs/recent.jsdeleted
plugs/channel/obs.jsadded
modules/channel/obs/suggest.jsView
@@ -3,8 +3,9 @@
33
44 exports.needs = nest({
55 'channel.obs.recent': 'first',
66 'channel.obs.subscribed': 'first',
7+ 'channel.obs.mostActive': 'first',
78 'keys.sync.id': 'first'
89 })
910
1011 exports.gives = nest('channel.async.suggest')
@@ -29,13 +30,13 @@
2930 function loadSuggestions () {
3031 if (!suggestions) {
3132 var id = api.keys.sync.id()
3233 subscribed = api.channel.obs.subscribed(id)
33- var recentlyUpdated = api.channel.obs.recent()
34- var contacts = computed([subscribed, recentlyUpdated], function (a, b) {
34+ var mostActive = api.channel.obs.mostActive()
35+ var contacts = computed([subscribed, mostActive], function (a, b) {
3536 var result = Array.from(a)
3637 b.forEach((item, i) => {
37- if (!result.includes(item)) {
38+ if (!result.includes(item[0])) {
3839 result.push(item)
3940 }
4041 })
4142 return result
@@ -46,18 +47,29 @@
4647 }
4748 }
4849
4950 function suggestion (id) {
50- return Struct({
51- title: id,
52- id: `#${id}`,
53- subtitle: computed([id, subscribed], subscribedCaption),
54- value: `#${id}`
55- })
51+ if (Array.isArray(id)) {
52+ return Struct({
53+ title: id[0],
54+ id: `#${id[0]}`,
55+ subtitle: computed([id[0], subscribed, `(${id[1]})`], subscribedCaption),
56+ value: `#${id}`
57+ })
58+ } else {
59+ return Struct({
60+ title: id,
61+ id: `#${id}`,
62+ subtitle: computed([id, subscribed], subscribedCaption),
63+ value: `#${id}`
64+ })
65+ }
5666 }
5767 }
5868
59-function subscribedCaption (id, subscribed) {
69+function subscribedCaption (id, subscribed, fallback) {
6070 if (subscribed.has(id)) {
6171 return 'subscribed'
72+ } else {
73+ return fallback || ''
6274 }
6375 }
plugs/channel/obs/recent.jsView
@@ -1,68 +1,0 @@
1-// uses lib/flumeview-channels
2-
3-var nest = require('depnest')
4-var pull = require('pull-stream')
5-
6-var { Value, Dict, Struct, computed, throttle } = require('mutant')
7-
8-exports.needs = nest({
9- 'sbot.pull.stream': 'first'
10-})
11-
12-exports.gives = nest({
13- 'channel.obs.recent': true
14-})
15-
16-exports.create = function (api) {
17- var recentChannels = null
18- var channelsLookup = null
19-
20- return nest({
21- 'channel.obs.recent': function () {
22- load()
23- return recentChannels
24- }
25- })
26-
27- function load () {
28- if (!recentChannels) {
29- var sync = Value(false)
30- channelsLookup = Dict()
31-
32- pull(
33- api.sbot.pull.stream(sbot => sbot.patchwork.channels({live: true})),
34- pull.drain(data => {
35- channelsLookup.transaction(() => {
36- for (var channel in data) {
37- var obs = channelsLookup.get(channel)
38- if (!obs) {
39- obs = ChannelRef(channel)
40- channelsLookup.put(channel, obs)
41- }
42- var count = data[channel].count != null ? data[channel].count : obs.count() + 1
43- var updatedAt = data[channel].timestamp
44- obs.set({ id: channel, updatedAt, count })
45- }
46- })
47- if (!sync()) {
48- sync.set(true)
49- }
50- })
51- )
52-
53- recentChannels = computed(throttle(channelsLookup, 1000), (lookup) => {
54- var values = Object.keys(lookup).map(x => lookup[x]).sort((a, b) => b.updatedAt - a.updatedAt).map(x => x.id)
55- return values
56- })
57- recentChannels.sync = sync
58- }
59- }
60-}
61-
62-function ChannelRef (id) {
63- return Struct({
64- id,
65- updatedAt: Value(0),
66- count: Value(0)
67- }, {merge: true})
68-}
plugs/channel/obs.jsView
@@ -1,0 +1,80 @@
1+// uses lib/flumeview-channels
2+
3+var nest = require('depnest')
4+var pull = require('pull-stream')
5+
6+var { Value, Dict, Struct, computed, throttle } = require('mutant')
7+
8+exports.needs = nest({
9+ 'sbot.pull.stream': 'first'
10+})
11+
12+exports.gives = nest({
13+ 'channel.obs.recent': true,
14+ 'channel.obs.mostActive': true
15+})
16+
17+exports.create = function (api) {
18+ var recentChannels = null
19+ var mostActiveChannels = null
20+ var channelsLookup = null
21+
22+ return nest({
23+ 'channel.obs.recent': function () {
24+ load()
25+ return recentChannels
26+ },
27+ 'channel.obs.mostActive': function () {
28+ load()
29+ return mostActiveChannels
30+ }
31+ })
32+
33+ function load () {
34+ if (!recentChannels) {
35+ var sync = Value(false)
36+ channelsLookup = Dict()
37+
38+ pull(
39+ api.sbot.pull.stream(sbot => sbot.patchwork.channels({live: true})),
40+ pull.drain(data => {
41+ channelsLookup.transaction(() => {
42+ for (var channel in data) {
43+ var obs = channelsLookup.get(channel)
44+ if (!obs) {
45+ obs = ChannelRef(channel)
46+ channelsLookup.put(channel, obs)
47+ }
48+ var count = data[channel].count != null ? data[channel].count : obs.count() + 1
49+ var updatedAt = data[channel].timestamp
50+ obs.set({ id: channel, updatedAt, count })
51+ }
52+ })
53+ if (!sync()) {
54+ sync.set(true)
55+ }
56+ })
57+ )
58+
59+ recentChannels = computed(throttle(channelsLookup, 1000), (lookup) => {
60+ var values = Object.keys(lookup).map(x => lookup[x]).sort((a, b) => b.updatedAt - a.updatedAt).map(x => x.id)
61+ return values
62+ })
63+
64+ mostActiveChannels = computed(throttle(channelsLookup, 1000), (lookup) => {
65+ var values = Object.keys(lookup).map(x => lookup[x]).sort((a, b) => b.count - a.count).map(x => [x.id, x.count])
66+ return values
67+ })
68+
69+ recentChannels.sync = sync
70+ }
71+ }
72+}
73+
74+function ChannelRef (id) {
75+ return Struct({
76+ id,
77+ updatedAt: Value(0),
78+ count: Value(0)
79+ }, {merge: true})
80+}

Built with git-ssb-web