-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (162 loc) · 6.25 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// ep_sciencemesh - main module
// A plugin to integrate with CS3 storages powered by Reva and WOPI
//
// Initial contribution: Mohammad Warid @waridrox
// Maintainer: Giuseppe Lo Presti @glpatcern
//
// Copyright 2018-2023 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
'use strict'
const fs = require('fs')
const URL = require('url')
const debounce = require('lodash')
const axios = require('axios')
const log4js = require('ep_etherpad-lite/node_modules/log4js')
const api = require('ep_etherpad-lite/node/db/API')
const absolutePaths = require('ep_etherpad-lite/node/utils/AbsolutePaths')
const eejs = require('ep_etherpad-lite/node/eejs')
const padMessageHandler = require('ep_etherpad-lite/node/handler/PadMessageHandler')
const argv = require('ep_etherpad-lite/node/utils/Cli').argv
const smLogger = log4js.getLogger('ScienceMesh')
const dbInterface = require('./db_interface.js')
// This is taken from ep_etherpad-lite/node/handler/APIHandler.js
let apikey = null
const apikeyFn = absolutePaths.makeAbsolute(argv.apikey || './APIKEY.txt')
try {
apikey = fs.readFileSync(apikeyFn, 'utf8').trim()
} catch (err) {
smLogger.error(`Unable to read the API key from ${apikeyFn}:`, JSON.stringify(err))
}
exports.eejsBlock_modals = (hookName, args, cb) => {
args.content += eejs.require('ep_sciencemesh/templates/notify.ejs')
cb()
}
const notifyUser = (padID, message) => {
const padId = padID
const msg = {
type: 'COLLABROOM',
data: {
type: 'CUSTOM',
payload: {
action: 'userNotification',
padId,
message
}
}
}
padMessageHandler.handleCustomObjectMessage(msg, false)
}
const getMetadata = async (context) => {
const metadata = await dbInterface.getMetadata(`${context.pad.id}`, `${context.author}`)
// returns a (wopiHost, wopiSrc, accessToken) tuple
const params = metadata.split(':')
const wopiSrc = decodeURIComponent(params[0])
smLogger.debug(`WOPI server URL: ${wopiSrc}`)
return [new URL(wopiSrc).origin, wopiSrc, params[1]]
}
const wopiCall = async (wopiHost, wopiSrc, accessToken, padID, close = false) => {
axios.post(`${wopiHost}/wopi/bridge/${padID}`, null, {
params: {
WOPISrc: `${wopiSrc}`,
access_token: `${accessToken}`,
close: `${close}`
},
headers: {
'X-EFSS-Bridged-App': 'Etherpad'
}
})
.then((response) => {
if (response.status === 202) {
smLogger.debug('wopiCall: enqueued action')
} else {
smLogger.debug('wopiCall: saved')
notifyUser(padID, response.data)
}
})
.catch((error) => {
smLogger.warn(`wopiCall: error ${error.statusText}: ${error.data.message}`)
notifyUser(padID, error.data)
if (error.status === 400 || error.status === 500) {
// TODO block further edit
}
})
}
// ### Exposed API ###
exports.setEFSSMetadata = async (hookName, context) => {
context.app.post('/setEFSSMetadata', async (req, res) => {
const query = req.query
if (query.apikey !== apikey) {
smLogger.warn('setEFSSMetadata: invalid API key')
res.status(401).send(JSON.stringify({ code: 1, message: 'Invalid API key' }))
return
}
if (!query.wopiSrc || !query.accessToken || !query.padID) {
smLogger.warn('setEFSSMetadata: missing arguments in query parameters', JSON.stringify(query))
res.status(400).send(JSON.stringify({ code: 1, message: 'Missing arguments' }))
return
}
const revisionCount = await api.getRevisionsCount(query.padID).catch((err) => {
if (err.name === 'apierror') return null
})
if (revisionCount) {
try {
dbInterface.addMetadataToPad(`${query.padID}`, `${(query.wopiSrc)}:${query.accessToken}`)
smLogger.info(`setEFSSMetadata: successfully added token ${query.accessToken} to ${query.padID}`)
res.status(200).send(JSON.stringify({ code: 0, message: 'OK' }))
} catch (err) {
smLogger.error(`setEFSSMetadata: error setting metadata: ${err}`)
res.status(500).send(JSON.stringify({ code: 3, message: `Error setting metadata: ${err}` }))
}
} else {
smLogger.warn(`setEFSSMetadata: invalid pad ID '${query.padID}'`)
res.status(404).send(JSON.stringify({ code: 2, message: 'PadID invalid or not found' }))
}
})
}
exports.padUpdate = debounce((hookName, context) => {
smLogger.debug('Pad content was updated after 3000 ms')
const metadata = getMetadata(context).catch((err) => {
smLogger.error(`padUpdate: error getting metadata for pad ${context.pad.id}:`, JSON.stringify(err))
notifyUser(context.pad.id, 'Error getting metadata for pad: ' + err)
})
const [wopiHost, wopiSrc, accessToken] = metadata
wopiCall(wopiHost, wopiSrc, accessToken, context.pad.id)
}, 3000)
exports.userJoin = async (hookName, { authorId, displayName, padId }) => {
smLogger.info(`Author ${authorId} joined pad ${padId}, attempting to set metadata`)
dbInterface.setAuthorForPad(padId, authorId)
}
exports.userLeave = function (hookName, session, callback) {
const param = {
author: session.author,
pad: {
id: session.padId
}
}
callback(
async (resolve, reject) => {
const metadata = await getMetadata(param).catch((err) => {
reject(smLogger.error(`userLeave: error getting metadata for pad ${param.pad.id}:`, JSON.stringify(err)))
})
const [wopiHost, wopiSrc, accessToken] = metadata
await wopiCall(wopiHost, wopiSrc, accessToken, session.padId, true)
await dbInterface.removeAuthor(session.padId, session.author)
resolve(smLogger.info(`userLeave: author ${session.author} left pad ${session.padId}`))
}
)
}