-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (65 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* hive.js
* Copyright (C) 2013-2016 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License version 2
* as published by the Mozilla Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Mozilla Public License
* along with this program. If not, see <https://www.mozilla.org/en-US/MPL/2.0/>.
*/
var path = require('path')
, co = require('co')
, through = require('through2')
, JSONParse = require('json-stream')
module.exports = setup
module.exports.consumes = ['ui', 'broadcast', 'auth', 'logger']
function setup(plugin, imports, register) {
var ui = imports.ui
, broadcast = imports.broadcast
, auth = imports.auth
, logger = imports.logger.getLogger('plugin-chat')
ui.registerModule(path.join(__dirname, 'client.js'))
ui.registerStylesheet(path.join(__dirname, 'css/index.css'))
ui.registerLocaleDir(path.join(__dirname, 'locales'))
broadcast.registerChannel(new Buffer('chat'), function(user, docId, clientStream, broadcastStream) {
// Authorize chat:write
clientStream.pipe(JSONParse()).pipe(through.obj(function(buf, enc, cb){
var that = this
co(function*() {
var authorized = yield auth.authorize(user, 'document/chat:write', {document: docId})
if(!authorized) {
logger.debug('Dropped chat message because of lack of authorization')
return cb()
}
if(buf.user != user.id) {
logger.debug('Dropped chat the author of the message is not the sender.')
return cb()
}
that.push(buf)
}).then(cb).catch(cb)
})).pipe(JSONStringify()).pipe(broadcastStream)
// Athorize chat:read
broadcastStream.pipe(through(function(buf, enc, cb) {
var that = this
co(function*() {
var authorized = yield auth.authorize(user, 'document/chat:read', {document: docId})
if(!authorized) return cb()
that.push(buf)
}).then(cb).catch(cb)
})).pipe(clientStream)
})
register()
}
function JSONStringify() {
return through.obj(function(buf, enc, cb) {
this.push(JSON.stringify(buf)+'\n')
cb()
})
}