-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_interface.js
100 lines (91 loc) · 3.65 KB
/
db_interface.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
// ep_sciencemesh - DB interface
// A plugin to integrate with CS3 storages powered by Reva and WOPI
//
// 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.
const db = require('ep_etherpad-lite/node/db/DB')
const mutex = require('async-mutex')
const log4js = require('ep_etherpad-lite/node_modules/log4js')
const smLogger = log4js.getLogger('ScienceMesh')
const dbMutex = new mutex.Mutex()
async function addMetadataToPad (padId, metadata) {
const release = await dbMutex.acquire()
try {
const md = await db.get(`efssmetadata:${padId}`)
if (md == null) {
// first metadata payload for this pad
await db.set(`efssmetadata:${padId}`, metadata)
smLogger.debug('addMetadataToPad: appended metadata')
} else {
// append another metadata payload
await db.set(`efssmetadata:${padId}`, md + '_' + metadata)
smLogger.debug('addMetadataToPad: set metadata')
}
} finally {
release()
}
}
async function setAuthorForPad (padId, authorId) {
const release = await dbMutex.acquire()
try {
const pendingmd = await db.get(`efssmetadata:${padId}`)
if (pendingmd == null) {
// this may currently happen if a pad was created directly with Etherpad
// TODO need to understand what happens with this uncaught error
smLogger.error('setAuthorForPad: no more pending metadata available')
throw new Error('No outstanding metadata available to set a new author')
}
if (pendingmd.indexOf('_') > 0) {
// get first metadata payload for this author and keep the rest
await db.set(`efssmetadata:${padId}:${authorId}`, pendingmd.substring(0, pendingmd.indexOf('_')))
await db.set(`efssmetadata:${padId}`, pendingmd.substring(pendingmd.indexOf('_') + 1))
smLogger.debug('setAuthorForPad: set metadata, more users may join')
} else {
// we found a single payload, use it and drop pending key
await db.set(`efssmetadata:${padId}:${authorId}`, pendingmd)
await db.remove(`efssmetadata:${padId}`)
smLogger.debug('setAuthorForPad: set metadata, no more pending metadata')
}
} finally {
release()
}
}
async function getMetadata (padId, authorId) {
// return required metadata, errors are thrown to the caller
const metadata = await db.get(`efssmetadata:${padId}:${authorId}`)
if (metadata == null) {
throw new Error(`Metadata not found for padId = ${padId} and authorId = ${authorId}`)
}
return metadata
}
async function removeAuthor (padId, authorId) {
// the mutex is not strictly needed here but for consistency we keep it
const release = await dbMutex.acquire()
try {
await db.remove(`efssmetadata:${padId}:${authorId}`)
smLogger.debug('removeAuthor: cleaned metadata')
} finally {
release()
}
}
module.exports.addMetadataToPad = addMetadataToPad
module.exports.setAuthorForPad = setAuthorForPad
module.exports.getMetadata = getMetadata
module.exports.removeAuthor = removeAuthor