git ssb

0+

dangerousbeans / yap



forked from Dominic / yap

Commit 9f6842d31239574b9aae7935976aeef22e7a67fa

move actions so they can see sbot

Dominic Tarr committed on 2/18/2019, 9:12:40 AM
Parent: 6454209172a781e000df19c353e8c35ef42b5b84

Files changed

index.jschanged
index.jsView
@@ -1,15 +1,15 @@
1-var fs = require('fs')
2-var path = require('path')
1+var fs = require('fs')
2+var path = require('path')
3+var ref = require('ssb-ref')
4+var Stack = require('stack')
5+
6+//refactor to ditch these things
37 var nested = require('libnested')
4-var Stack = require('stack')
5-var URL = require('url')
6-var QS = require('qs')
7-var u = require('./util')
8+var URL = require('url')
9+var QS = require('qs')
10+var u = require('./util')
811 var toHTML = u.toHTML
9-var pull = require('pull-stream')
10-var toPull = require('stream-to-pull-stream')
11-var ref = require('ssb-ref')
1212
1313 // middleware
1414 var Logger = require('morgan')
1515 var Emoji = require('emoji-server')
@@ -17,38 +17,10 @@
1717 var BodyParser = require('urlencoded-request-parser')
1818 var FavIcon = require('serve-favicon')
1919 var Coherence = require('coherence-framework')
2020
21-var doctype = '<!DOCTYPE html \n PUBLIC "-//W3C//DTD HTML 4.01//EN"\n "http://www.w3.org/TR/html4/strict.dtd">'
22-
2321 //actions may make writes to sbot, or can set things
24-var actions = {
25- //note: opts is post body
26- identitySelect: function (opts, apply, req, cb) {
27- var context = req.cookies
28- context.id = opts.id
29- cb(null, opts, context)
30- },
31- preview: function (opts, apply, req, cb) {
32- cb(null, opts)
33- },
34- publish: function (opts, apply, req, cb) {
35- if(opts.content.recps === '')
36- delete opts.content.recps
37- else if('string' === typeof opts.content.recps) {
38- opts.content.recps = opts.content.recps.split(',')
39- }
4022
41- if(Array.isArray(opts.content.recps))
42- opts.private = true
43-
44- sbot.identities.publishAs(opts, function (err, msg) {
45- if(err) cb(err)
46- else cb()
47- })
48- }
49-}
50-
5123 require('ssb-client')(function (err, sbot) {
5224 if(err) throw err
5325
5426 var coherence = Coherence(require('./layout'))
@@ -69,8 +41,47 @@
6941 .use('friends', require('./apis/friends')(sbot))
7042 .use('search', require('./apis/search')(sbot))
7143 .use('mentions', require('./apis/mentions')(sbot))
7244
45+ var actions = {
46+ //note: opts is post body
47+
48+ //sets id in cookie
49+ identitySelect: function (opts, req, cb) {
50+ var context = req.cookies
51+ context.id = opts.id
52+ cb(null, null, context)
53+ },
54+
55+ //sets id in cookie
56+ languageSelect: function (opts, req, cb) {
57+ throw new Error('not implemented yet')
58+ },
59+
60+ //theme, in cookie
61+
62+ //renders immediately
63+// preview: function (opts, req, cb) {
64+// cb(null, opts)
65+// },
66+
67+ publish: function (opts, req, cb) {
68+ if(opts.content.recps === '')
69+ delete opts.content.recps
70+ else if('string' === typeof opts.content.recps) {
71+ opts.content.recps = opts.content.recps.split(',')
72+ }
73+
74+ if(Array.isArray(opts.content.recps))
75+ opts.private = true
76+
77+ sbot.identities.publishAs(opts, function (err, msg) {
78+ if(err) cb(err)
79+ else cb()
80+ })
81+ }
82+ }
83+
7384 require('http').createServer(Stack(
7485 Logger(),
7586 //everything breaks if blobs isn't first, but not sure why?
7687 require('ssb-ws/blobs')(sbot, {prefix: '/blobs'}),
@@ -96,49 +107,54 @@
96107 function (req, res, next) {
97108 if(req.method == 'GET') return next()
98109 var id = req.cookies.id || sbot.id
99110 var opts = req.body
100- function callApi (path, opts) {
101- try {
102- var fn = nested.get(apis, path)
103- if(!fn) return next()
104- return fn(opts, apply, req)
105- } catch(err) {
106- next(err)
107- }
108- }
111+// function callApi (path, opts) {
112+// try {
113+// var fn = nested.get(apis, path)
114+// if(!fn) return next()
115+// return fn(opts, apply, req)
116+// } catch(err) {
117+// next(err)
118+// }
119+// }
120+
121+ // handle preview specially, (to confirm a message)
122+ //
123+
109124 if(opts.type === 'preview') {
110125 // TODO: pass opts.id in, and wether this message
111126 // preview should allow recipient selection, or changing id.
112127 // api.preview can set the shape of the message if it likes.
113128
114129 req.url = '/preview?'+QS.stringify(opts)
115- coherence(req, res, next)
130+ return coherence(req, res, next)
116131
117132 //XXX this isn't working
118133
119134 // toHTML(layout.call(self, callApi(['preview'], opts))) (function (err, result) {
120135 // if(err) next(err)
121136 // else res.end('<!DOCTYPE html>'+result.outerHTML)
122137 // })
123- return
138+// return
124139 }
125- actions[opts.type](opts, null, req, function (err, _opts, context) {
140+ actions[opts.type](opts, req, function (err, _opts, context) {
126141 if(err) return next(err)
127142 if(context) {
128143 req.cookies = context
129144 res.setHeader('set-Cookie', QS.stringify(context))
130145 }
131146 /*
132- after handling the post,
133- redirect to a normal page.
134- this is a work around for if you hit refresh
147+ After handling the post, redirect to a normal page.
148+ This is a work around for if you hit refresh
135149 and the browser wants to resubmit the POST.
136150
137151 I think we want to do this for most types,
138152 exception is for preview - in which we return
139153 the same data rendered differently and don't write
140154 to DB at all.
155+
156+ Should preview be implemented like this too?
141157 */
142158 res.setHeader('location', req.url)
143159 res.writeHead(303)
144160 res.end()
@@ -153,4 +169,6 @@
153169 )).listen(8005)
154170 })
155171
156172
173+
174+

Built with git-ssb-web