git ssb

0+

Dominic / yap-tags



Commit 4cafd1d101a0adaea7624b382b997d6581f6ba02

initial

Dominic Tarr committed on 8/31/2019, 4:44:25 PM

Files changed

LICENSEadded
README.mdadded
get.jsadded
index.jsadded
package.jsonadded
reduce.jsadded
yarn-error.logadded
yarn.lockadded
LICENSEView
@@ -1,0 +1,22 @@
1 +Copyright (c) 2019 Dominic Tarr
2 +
3 +Permission is hereby granted, free of charge,
4 +to any person obtaining a copy of this software and
5 +associated documentation files (the "Software"), to
6 +deal in the Software without restriction, including
7 +without limitation the rights to use, copy, modify,
8 +merge, publish, distribute, sublicense, and/or sell
9 +copies of the Software, and to permit persons to whom
10 +the Software is furnished to do so,
11 +subject to the following conditions:
12 +
13 +The above copyright notice and this permission notice
14 +shall be included in all copies or substantial portions of the Software.
15 +
16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
20 +ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
README.mdView
@@ -1,0 +1,6 @@
1 +# yap-tags
2 +
3 +
4 +## License
5 +
6 +MIT
get.jsView
@@ -1,0 +1,23 @@
1 +var Sort = require('ssb-sort')
2 +var Reduce = require('./reduce')
3 +var pull = require('pull-stream')
4 +
5 +module.exports = function (sbot, id, cb) {
6 + sbot.get(id, function (err, msg) {
7 + pull(
8 + sbot.links({
9 + dest: id, values: true
10 + }),
11 + pull.collect(function (err, ary) {
12 + ary = ary.filter(function (e) {
13 + return e.value.content.type == 'tag' || e.value.content.type == 'about'
14 + })
15 + ary.unshift({key: id, value:msg})
16 + ary = Sort(ary)
17 + var tag = ary.reduce(Reduce, null)
18 + tag.branch = Sort.heads(ary)
19 + cb(null, tag)
20 + })
21 + )
22 + })
23 +}
index.jsView
@@ -1,0 +1,140 @@
1 +var pull = require('pull-stream')
2 +var u = require('yap-util')
3 +var Get = require('./get')
4 +var Paramap = require('pull-paramap')
5 +var Many = require('pull-many')
6 +//adding tags
7 +/*
8 + type a name for the tag,
9 + if it's a new name, create a new tag.
10 + if it's an old name, add to that tag.
11 + if there is more than one tag with that name,
12 + dropdown to disambiguate, defaulting to the one you created.
13 +*/
14 +
15 +module.exports = function (sbot) {
16 + return function (use) {
17 + use('tag', function (opts, apply, req) {
18 + return function (cb) {
19 + Get(sbot, opts.id, function (err, tag) {
20 + console.log(tag)
21 + cb(null,
22 + apply('messageLayout', {
23 + id: tag.root,
24 + author: tag.creator,
25 + ts: tag.created,
26 + content: ['div.tag',
27 + ['h2', ['a', {href: u.toUrl('tags/tag', {id: tag.root})}, tag.name]],
28 + ['ol'].concat(tag.contents.map(function (e) {
29 + return ['li',
30 + apply('/patch/messageLink', {id: e})
31 + ]
32 + }))
33 + ]
34 + })
35 + )
36 + })
37 + }
38 + })
39 +
40 + use('link', function (opts, apply) {
41 + return function (cb) {
42 + var name, author
43 + //the linking message
44 + sbot.get(opts.id, function (err, msg) {
45 + author = msg.author
46 + if(msg.content.root) next(msg.content.root)
47 + else
48 + next(opts.id)
49 + })
50 + function next (id) {
51 + pull(
52 + sbot.links({dest: id, rel: 'about', values: true}),
53 + pull.drain(function (data) {
54 + if(data.value.content.name) {
55 + name = data.value.content.name
56 + author = data.value.author
57 + }
58 + }, function () {
59 + cb(null, ['div.Tag', apply('avatar', {id: author}), ['a', {href: u.toUrl('tags/tag', {id: id})}, '#'+name]])
60 + })
61 + )
62 + }
63 + }
64 + })
65 +
66 + use('tagged', function (opts, apply) {
67 + return ['ol.Tagged', pull(
68 + sbot.links({dest: opts.id, rel: 'message', values: true}),
69 + pull.map(function (data) {
70 + return ['li', apply('tags/link', {id: data.key})]
71 +// return ['li', ['a', {href: u.toUrl('message', {id: data.key})}, data.key]]
72 + return //apply('messageLink', data)
73 + })
74 + )]
75 + })
76 + use('addTag', function (opts, apply) {
77 + return ['ol.Tagged',
78 + ['a', {href: '#'}, 'new'],
79 + ['select',
80 + ['option', 'new'],
81 + ['option', 'old...']
82 + ],
83 + ]
84 + })
85 + use.list('extra', 'tagged')
86 + use.list('extra', 'addTag')
87 + use('raw', function (opts, apply, req) {
88 + return function (cb) {
89 + Get(opts, function (err, tag) {
90 + cb(null, ['pre', JSON.stringify(tag, null, 2)])
91 + })
92 + }
93 + })
94 +
95 + function Query (path) {
96 + return function (opts, apply, req) {
97 + var query = u.createQuery(Object.assign({ type: 'tag', limit: 100}, opts))
98 + query.query[0].$filter.value.content.root = {$is: 'undefined'}
99 + if(opts.author)
100 + query.query[0].$filter.value.author = opts.author
101 + if(opts.name)
102 + query.query[0].$filter.value.name = opts.name
103 +
104 + var source = sbot.query.read(query)
105 + var render = pull.map(function (e) { return apply(path, {id: e.key}) })
106 + if(!opts.name) return pull(source, render)
107 +
108 + var query = u.createQuery({
109 + type: 'about', name: opts.name
110 + })
111 +
112 + //merge the stream of tags with names on them,
113 + //with tags + about
114 + return pull(
115 + Many([
116 + source,
117 + pull(
118 + sbot.query.read(query),
119 + pull.asyncMap(function (data, cb) {
120 + sbot.get({id:data.value.content.about, meta: true}, function (err, _data) {
121 + if(err || _data.value.content.type !== 'tag' || _data.value.content.root) cb()
122 + else cb(null, _data)
123 + })
124 + }),
125 + pull.filter(Boolean),
126 + ),
127 + ]),
128 + render
129 + )
130 + }
131 + }
132 +
133 + use('all', Query('tags/tag'))
134 + use('all-raw', Query('tags/raw'))
135 + use('menu', function (opts, apply, req) {
136 + return ['a', {href: '/tags/all'}, 'Tags']
137 + })
138 + use.list('menu', 'menu')
139 + }
140 +}