2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2011-07-08 17:42:07 +01:00
|
|
|
/**
|
|
|
|
* The ReadOnlyManager manages the database and rendering releated to read only pads
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2011-08-11 15:26:41 +01:00
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-07-08 17:42:07 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-02-28 21:19:10 +01:00
|
|
|
|
2023-06-22 22:54:02 +02:00
|
|
|
import {db} from './DB';
|
2023-06-23 18:57:36 +02:00
|
|
|
import {randomString} from '../utils/randomstring';
|
2012-01-28 13:24:58 +01:00
|
|
|
|
2020-09-16 14:57:27 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if the id pattern matches a read-only pad id
|
2023-06-22 22:54:02 +02:00
|
|
|
* @param {String} id the pad's id
|
2020-09-16 14:57:27 -03:00
|
|
|
*/
|
2023-06-22 22:54:02 +02:00
|
|
|
export const isReadOnlyId = (id: string) => id.startsWith('r.');
|
2020-09-16 14:57:27 -03:00
|
|
|
|
2011-07-08 17:42:07 +01:00
|
|
|
/**
|
|
|
|
* returns a read only id for a pad
|
|
|
|
* @param {String} padId the id of the pad
|
|
|
|
*/
|
2023-06-22 22:54:02 +02:00
|
|
|
export const getReadOnlyId = async (padId) => {
|
2019-01-30 16:19:51 +00:00
|
|
|
// check if there is a pad2readonly entry
|
2020-11-23 13:24:19 -05:00
|
|
|
let readOnlyId = await db.get(`pad2readonly:${padId}`);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 16:19:51 +00:00
|
|
|
// there is no readOnly Entry in the database, let's create one
|
|
|
|
if (readOnlyId == null) {
|
2020-11-23 13:24:19 -05:00
|
|
|
readOnlyId = `r.${randomString(16)}`;
|
2021-07-04 16:30:30 +02:00
|
|
|
await Promise.all([
|
|
|
|
db.set(`pad2readonly:${padId}`, readOnlyId),
|
|
|
|
db.set(`readonly2pad:${readOnlyId}`, padId),
|
|
|
|
]);
|
2019-01-30 16:19:51 +00:00
|
|
|
}
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 16:19:51 +00:00
|
|
|
return readOnlyId;
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2011-07-08 17:42:07 +01:00
|
|
|
|
2011-07-08 18:33:01 +01:00
|
|
|
/**
|
2019-02-08 23:20:57 +01:00
|
|
|
* returns the padId for a read only id
|
2011-07-08 18:33:01 +01:00
|
|
|
* @param {String} readOnlyId read only id
|
|
|
|
*/
|
2023-06-22 22:54:02 +02:00
|
|
|
export const getPadId = async (readOnlyId) => await db.get(`readonly2pad:${readOnlyId}`);
|
2012-04-23 16:18:14 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-08 23:20:57 +01:00
|
|
|
* returns the padId and readonlyPadId in an object for any id
|
2023-06-22 22:54:02 +02:00
|
|
|
* @param {String} id padIdOrReadonlyPadId read only id or real pad id
|
2012-04-23 16:18:14 +02:00
|
|
|
*/
|
2023-06-22 22:54:02 +02:00
|
|
|
export const getIds = async (id: string) => {
|
2023-06-23 20:53:55 +02:00
|
|
|
const readonly = isReadOnlyId(id);
|
2019-02-08 23:20:57 +01:00
|
|
|
|
2019-01-30 16:19:51 +00:00
|
|
|
// Might be null, if this is an unknown read-only id
|
2023-06-23 20:53:55 +02:00
|
|
|
const readOnlyPadId = readonly ? id : await getReadOnlyId(id);
|
|
|
|
const padId = readonly ? await getPadId(id) : id;
|
2019-01-30 16:19:51 +00:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
return {readOnlyPadId, padId, readonly};
|
|
|
|
};
|