Commit 9f6842d31239574b9aae7935976aeef22e7a67fa
move actions so they can see sbot
Dominic Tarr committed on 2/18/2019, 9:12:40 AMParent: 6454209172a781e000df19c353e8c35ef42b5b84
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -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 | |
3 | 7 | 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') | |
8 | 11 | var toHTML = u.toHTML |
9 | -var pull = require('pull-stream') | |
10 | -var toPull = require('stream-to-pull-stream') | |
11 | -var ref = require('ssb-ref') | |
12 | 12 | |
13 | 13 | // middleware |
14 | 14 | var Logger = require('morgan') |
15 | 15 | var Emoji = require('emoji-server') |
@@ -17,38 +17,10 @@ | ||
17 | 17 | var BodyParser = require('urlencoded-request-parser') |
18 | 18 | var FavIcon = require('serve-favicon') |
19 | 19 | var Coherence = require('coherence-framework') |
20 | 20 | |
21 | -var doctype = '<!DOCTYPE html \n PUBLIC "-//W3C//DTD HTML 4.01//EN"\n "http://www.w3.org/TR/html4/strict.dtd">' | |
22 | - | |
23 | 21 | //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 | - } | |
40 | 22 | |
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 | - | |
51 | 23 | require('ssb-client')(function (err, sbot) { |
52 | 24 | if(err) throw err |
53 | 25 | |
54 | 26 | var coherence = Coherence(require('./layout')) |
@@ -69,8 +41,47 @@ | ||
69 | 41 | .use('friends', require('./apis/friends')(sbot)) |
70 | 42 | .use('search', require('./apis/search')(sbot)) |
71 | 43 | .use('mentions', require('./apis/mentions')(sbot)) |
72 | 44 | |
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 | + | |
73 | 84 | require('http').createServer(Stack( |
74 | 85 | Logger(), |
75 | 86 | //everything breaks if blobs isn't first, but not sure why? |
76 | 87 | require('ssb-ws/blobs')(sbot, {prefix: '/blobs'}), |
@@ -96,49 +107,54 @@ | ||
96 | 107 | function (req, res, next) { |
97 | 108 | if(req.method == 'GET') return next() |
98 | 109 | var id = req.cookies.id || sbot.id |
99 | 110 | 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 | + | |
109 | 124 | if(opts.type === 'preview') { |
110 | 125 | // TODO: pass opts.id in, and wether this message |
111 | 126 | // preview should allow recipient selection, or changing id. |
112 | 127 | // api.preview can set the shape of the message if it likes. |
113 | 128 | |
114 | 129 | req.url = '/preview?'+QS.stringify(opts) |
115 | - coherence(req, res, next) | |
130 | + return coherence(req, res, next) | |
116 | 131 | |
117 | 132 | //XXX this isn't working |
118 | 133 | |
119 | 134 | // toHTML(layout.call(self, callApi(['preview'], opts))) (function (err, result) { |
120 | 135 | // if(err) next(err) |
121 | 136 | // else res.end('<!DOCTYPE html>'+result.outerHTML) |
122 | 137 | // }) |
123 | - return | |
138 | +// return | |
124 | 139 | } |
125 | - actions[opts.type](opts, null, req, function (err, _opts, context) { | |
140 | + actions[opts.type](opts, req, function (err, _opts, context) { | |
126 | 141 | if(err) return next(err) |
127 | 142 | if(context) { |
128 | 143 | req.cookies = context |
129 | 144 | res.setHeader('set-Cookie', QS.stringify(context)) |
130 | 145 | } |
131 | 146 | /* |
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 | |
135 | 149 | and the browser wants to resubmit the POST. |
136 | 150 | |
137 | 151 | I think we want to do this for most types, |
138 | 152 | exception is for preview - in which we return |
139 | 153 | the same data rendered differently and don't write |
140 | 154 | to DB at all. |
155 | + | |
156 | + Should preview be implemented like this too? | |
141 | 157 | */ |
142 | 158 | res.setHeader('location', req.url) |
143 | 159 | res.writeHead(303) |
144 | 160 | res.end() |
@@ -153,4 +169,6 @@ | ||
153 | 169 | )).listen(8005) |
154 | 170 | }) |
155 | 171 | |
156 | 172 | |
173 | + | |
174 | + |
Built with git-ssb-web