Files: 1c2359030ac279f1fdfef805d7938f3003372c24 / ssb-server-blog-stats.js
4011 bytesRaw
1 | const FlumeView = require('flumeview-level') |
2 | const get = require('lodash/get') |
3 | const pull = require('pull-stream') |
4 | const isBlog = require('scuttle-blog/isBlog') |
5 | const { isMsg: isMsgRef } = require('ssb-ref') |
6 | |
7 | const getType = (msg) => get(msg, 'value.content.type') |
8 | const getAuthor = (msg) => get(msg, 'value.author') |
9 | const getCommentRoot = (msg) => get(msg, 'value.content.root') |
10 | const getLikeRoot = (msg) => get(msg, 'value.content.vote.link') |
11 | const getTimestamp = (msg) => get(msg, 'value.timestamp') |
12 | |
13 | const FLUME_VIEW_VERSION = 5 |
14 | |
15 | module.exports = { |
16 | name: 'blogStats', |
17 | version: 1, |
18 | manifest: { |
19 | get: 'async', |
20 | read: 'source', |
21 | readBlogs: 'source', |
22 | getBlogs: 'async', |
23 | readComments: 'source', |
24 | readLikes: 'source' |
25 | }, |
26 | init: (server, config) => { |
27 | console.log('initialising blog-stats plugin') |
28 | const myKey = server.keys.id |
29 | |
30 | const view = server._flumeUse( |
31 | 'internalblogStats', |
32 | FlumeView(FLUME_VIEW_VERSION, map) |
33 | ) |
34 | |
35 | return { |
36 | get: view.get, |
37 | read: view.read, |
38 | readBlogs, |
39 | getBlogs, |
40 | readComments, |
41 | readLikes, |
42 | // getLikes |
43 | // getComments |
44 | } |
45 | |
46 | function map (msg, seq) { |
47 | var root |
48 | |
49 | switch (getType(msg)) { |
50 | case 'blog': |
51 | if (isBlog(msg) && myBlog(msg)) return [['B', msg.key, getTimestamp(msg)]] |
52 | else return [] |
53 | |
54 | case 'vote': |
55 | // process.stdout.write('L') |
56 | root = getLikeRoot(msg) |
57 | // TODO figure out how to only store likes I care about |
58 | if (root) return [['L', root, getTimestamp(msg)]] |
59 | else return [] |
60 | |
61 | // Note this catches: |
62 | // - all likes, on all things D: |
63 | // - likes AND unlikes |
64 | |
65 | case 'post': |
66 | // process.stdout.write('C') |
67 | root = getCommentRoot(msg) |
68 | // TODO figure out how to only store likes I care about |
69 | if (root) return [['C', root, getTimestamp(msg)]] |
70 | else return [] |
71 | |
72 | // Note this catches: |
73 | // - all comments, on all things D: |
74 | |
75 | default: |
76 | return [] |
77 | } |
78 | } |
79 | |
80 | function readBlogs (options = {}) { |
81 | const query = Object.assign({}, { |
82 | gte: ['B', null, null], |
83 | // null is the 'minimum' structure in bytewise ordering |
84 | lte: ['B~', undefined, undefined], |
85 | reverse: true, |
86 | values: true, |
87 | keys: false, |
88 | seqs: false |
89 | }, options) |
90 | |
91 | return view.read(query) |
92 | } |
93 | |
94 | function getBlogs (options, cb) { |
95 | pull( |
96 | readBlogs(options), |
97 | pull.collect(cb) |
98 | ) |
99 | } |
100 | |
101 | function readComments (options = {}) { |
102 | var key |
103 | if (!options.blog) key = null |
104 | else if (isMsgRef(options.blog)) key = options.blog |
105 | else if (isMsgRef(options.blog.key) && isBlog(options.blog)) key = options.blog.key |
106 | |
107 | const query = Object.assign({}, { |
108 | gt: ['C', key, null], |
109 | lt: ['C', key, undefined], |
110 | // undefined is the 'maximum' structure in bytewise ordering https://www.npmjs.com/package/bytewise#order-of-supported-structures |
111 | reverse: true, |
112 | values: true, |
113 | keys: false, |
114 | seqs: false |
115 | }, options) |
116 | |
117 | delete query.blog |
118 | |
119 | return view.read(query) |
120 | } |
121 | |
122 | function readLikes (options = {}) { |
123 | var key |
124 | if (!options.blog) key = null |
125 | else if (isMsgRef(options.blog)) key = options.blog |
126 | else if (isMsgRef(options.blog.key) && isBlog(options.blog)) key = options.blog.key |
127 | |
128 | const query = Object.assign({}, { |
129 | // gt: ['L', key, null], |
130 | // lt: ['L', key, undefined], // why doesn't this work? |
131 | gt: ['L', key, null], // null is minimum in bytewise ordering |
132 | lt: ['L', key + '~', undefined], // undefinted in maximum in bytewise ordering |
133 | reverse: true, |
134 | values: true, |
135 | keys: false, |
136 | seqs: false |
137 | }, options) |
138 | |
139 | delete query.blog |
140 | |
141 | return view.read(query) |
142 | } |
143 | |
144 | function myBlog (msg) { |
145 | return getAuthor(msg) === myKey |
146 | } |
147 | } |
148 | } |
149 |
Built with git-ssb-web