Move more code into modules

This commit is contained in:
yflory 2025-04-24 13:03:48 +02:00
parent fb75fd4824
commit f81b35241e
13 changed files with 103 additions and 133 deletions

View File

@ -37,7 +37,9 @@ getApi('config', ApiConfig => {
Broadcast
}).then((store) => {
console.log('API added to global "CP"');
globalThis.CP = store.api;
const api = globalThis.CP = store.api;
const cfg = { channel: '0123456789abcdef0123456789abcedf' };
api.pad.join(cfg, console.log);
});
});
});

View File

@ -4,7 +4,7 @@
const factory = (Sortify, UserObject, ProxyManager,
Migrate, Hash, Util, Constants, Feedback,
Realtime, Messaging, Pinpad, Rpc, Merge, Cache,
Realtime, Messaging, Pinpad, Rpc, Cache,
SF, AccountTS, DriveTS, PadTS, Cursor,
Support, Integration, OnlyOffice,
Mailbox, Profile, Team, Messenger, History,
@ -66,6 +66,9 @@ const factory = (Sortify, UserObject, ProxyManager,
Store.pad = Pad.init({
Store, store, postMessage, broadcast
});
Store.drive = Drive.initAPI({
Store, store, postMessage, broadcast
});
// Drive clients
var driveEventClients = [];
@ -140,37 +143,6 @@ const factory = (Sortify, UserObject, ProxyManager,
onSync(data.teamId, cb);
};
const copyObject = (src, target) => {
Object.keys(src).forEach(k => {
delete src[k];
});
Object.keys(target).forEach(k => {
src[k] = Util.clone(target[k]);
});
};
const getProxy = teamId => {
let proxy;
if (!teamId) {
proxy = store.drive?.proxy;
} else {
const s = getStore(teamId);
proxy = s?.drive?.proxy;
}
return proxy;
};
Store.drive = {
get: (clientId, data, cb) => {
let proxy = getProxy(data.teamId);
if (!proxy) { return void cb({error: 'ENOTFOUND'}); }
cb(proxy);
},
set: (clientId, data, cb) => {
let proxy = getProxy(data.teamId);
if (!proxy) { return void cb({error: 'ENOTFOUND'}); }
copyObject(proxy, data.value);
onSync(data.teamId, cb);
}
};
Store.getSharedFolder = function (clientId, data, cb) {
var s = getStore(data.teamId);
@ -355,17 +327,6 @@ const factory = (Sortify, UserObject, ProxyManager,
});
};
// Update for all users from accounts and return current user limits
Store.updatePinLimit = function (clientId, data, cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
store.rpc.updatePinLimits(function (e, limit, plan, note) {
if (e) { return void cb({error: e}); }
account.limit = limit;
account.plan = plan;
account.note = note;
cb(account);
});
};
// Get current user limits
Store.getPinLimit = function (clientId, data, cb) {
var s = getStore(data && data.teamId);
@ -387,15 +348,6 @@ const factory = (Sortify, UserObject, ProxyManager,
cb(account);
};
// clearOwnedChannel is only used for private chat and forms
Store.clearOwnedChannel = function (clientId, data, cb) {
var s = getStore(data && data.teamId);
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
s.rpc.clearOwnedChannel(data.channel, function (err) {
cb({error:err});
});
};
var arePinsSynced = function (cb) {
if (!store.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
@ -941,15 +893,6 @@ const factory = (Sortify, UserObject, ProxyManager,
});
};
/**
* Merge the anonymous drive into the user drive at registration
* data
* - anonHash
*/
Store.migrateAnonDrive = function (clientId, data, cb) {
var hash = data.anonHash;
Merge.anonDriveIntoUser(store, hash, cb);
};
// Set the display name (username) in the proxy
Store.setDisplayName = function (clientId, value, cb) {
@ -2652,13 +2595,6 @@ const factory = (Sortify, UserObject, ProxyManager,
*/
var initialized = false;
// Are we still in noDrive mode?
Store.hasDrive = function (clientId, data, cb) {
cb({
state: Boolean(store.proxy)
});
};
const loadHK = (cb) => {
if (store?.network?.historyKeeper) {
return setTimeout(cb);
@ -2805,7 +2741,7 @@ const factory = (Sortify, UserObject, ProxyManager,
startModules(clientId, ret, onInit);
});
});
Store.pad.onJoined.reg(next);
Store.pad.onJoined(next);
onPadRejectedEvt.reg(next);
});
}
@ -3004,7 +2940,6 @@ module.exports = factory(
require('./components/messaging'),
require('../common/pinpad'),
require('../common/rpc'),
require('./components/merge-drive'),
require('../common/cache-store'),
require('./components/sharedfolder'),
require('./components/account'), // .ts

View File

@ -3,7 +3,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import nacl from 'tweetnacl/nacl-fast';
import { Drive } from '../types'
import { Drive } from '../types';
import * as Merge from './merge-drive';
// node modules
import * as Listmap from 'chainpad-listmap';
@ -21,10 +22,59 @@ const onReadyEvt: any = Util.mkEvent(true);
const onDisconnectEvt: any = Util.mkEvent();
const onReconnectEvt: any = Util.mkEvent();
const init = (config) => {
const { broadcast, store, account } = config;
const copyObject = (src, target) => {
Object.keys(src).forEach(k => {
delete src[k];
});
Object.keys(target).forEach(k => {
src[k] = Util.clone(target[k]);
});
};
const getProxy = (ctx, teamId) => {
const store = ctx.store;
const Store = ctx.Store;
let proxy;
if (!teamId) {
proxy = store.drive?.proxy;
} else {
const s = Store.getStore(teamId);
proxy = s?.proxy?.drive;
}
return proxy;
};
const drive = store.drive = store.drive || {};
const initAPI = (config) => {
const { broadcast, store, Store, account } = config;
const ctx = {
store,
Store
};
return {
exists: (clientId, data, cb) => {
cb({ state: Boolean(store.proxy) });
},
get: (clientId, data, cb) => {
let proxy = getProxy(ctx, data.teamId);
if (!proxy) { return void cb({error: 'ENOTFOUND'}); }
cb({drive: proxy});
},
set: (clientId, data, cb) => {
let proxy = getProxy(ctx, data.teamId);
if (!proxy) { return void cb({error: 'ENOTFOUND'}); }
copyObject(proxy, data.value);
Store.onSync(data.teamId, cb);
},
migrateAnon: (clientId, data, cb) => {
Merge.anonDriveIntoUser(store, data.anonHash, cb);
}
};
};
const init = (config) => {
const { broadcast, store, Store, account } = config;
const drive = store.drive ||= {};
let data = store.proxy?.drive;
const hash:string = data?.hash || Hash.createRandomHash('drive');
@ -112,7 +162,8 @@ const init = (config) => {
};
const Drive: Drive = {
init: init,
init,
initAPI
};
export { Drive }

View File

@ -373,6 +373,16 @@ const _getLastHash: Callback = (ctx, clientId, data, cb) => {
});
};
// clearOwnedChannel is only used for private chat and forms
const _clear: Callback = (ctx, clientId, data, cb) => {
const { Store } = ctx;
const s = Store.getStore(data && data.teamId);
if (!s.rpc) { return void cb({error: 'RPC_NOT_READY'}); }
s.rpc.clearOwnedChannel(data.channel, err => {
cb({error:err});
});
};
const _destroy: Callback = (ctx, clientId, data, cb) => {
const { store, Store, channels, myDeletions } = ctx;
@ -464,6 +474,9 @@ const init = (config) => {
const destroy: RpcCall = (clientId, data, cb) => {
_destroy(ctx, clientId, data, cb);
};
const clear: RpcCall = (clientId, data, cb) => {
_clear(ctx, clientId, data, cb);
};
const setMetadata: RpcCall = (clientId, data, cb) => {
_setMetadata(ctx, clientId, data, cb);
};
@ -505,6 +518,7 @@ const init = (config) => {
return {
join,
destroy,
clear,
setMetadata,
getMetadata,
sendMessage,

View File

@ -91,6 +91,7 @@ const factory = (SRpc, Channel, Util) => {
cb(data);
});
});
// XXX allow multiple pads in same tab
chan.on('JOIN_PAD', function (data, cb) {
client.channelId = data.channel;
try {

View File

@ -12,22 +12,18 @@ const factory = AStore => {
// Ready
CONNECT: Store.init,
DISCONNECT: Store.disconnect,
MIGRATE_ANON_DRIVE: Store.migrateAnonDrive,
PING: function (cId, data, cb) { cb(); },
CACHE_DISABLE: Store.disableCache,
HAS_DRIVE: Store.hasDrive,
// RPC
UPDATE_PIN_LIMIT: Store.updatePinLimit,
GET_PIN_LIMIT: Store.getPinLimit,
CLEAR_OWNED_CHANNEL: Store.clearOwnedChannel,
PIN_PADS: Store.pinPads,
UNPIN_PADS: Store.unpinPads,
GET_PINNED_USAGE: Store.getPinnedUsage,
GET_DELETED_PADS: Store.getDeletedPads,
UPLOAD_CHUNK: Store.uploadChunk,
UPLOAD_COMPLETE: Store.uploadComplete,
UPLOAD_STATUS: Store.uploadStatus,
UPLOAD_CANCEL: Store.uploadCancel,
PIN_PADS: Store.pinPads,
UNPIN_PADS: Store.unpinPads,
GET_DELETED_PADS: Store.getDeletedPads,
GET_PINNED_USAGE: Store.getPinnedUsage,
// ANON RPC
ANON_RPC_MESSAGE: Store.anonRpcMsg,
GET_FILE_SIZE: Store.getFileSize,
@ -35,8 +31,6 @@ const factory = AStore => {
// Store
GET: Store.get,
SET: Store.set,
GET_DRIVE: Store.drive.get,
SET_DRIVE: Store.drive.set,
ADD_PAD: Store.addPad,
SET_PAD_TITLE: Store.setPadTitle,
MOVE_TO_TRASH: Store.moveToTrash,
@ -76,6 +70,7 @@ const factory = AStore => {
JOIN_PAD: Store.pad.join,
LEAVE_PAD: Store.pad.leave,
REMOVE_OWNED_CHANNEL: Store.pad.destroy,
CLEAR_OWNED_CHANNEL: Store.pad.clear,
CORRUPTED_CACHE: Store.pad.onCorruptedCache,
GET_LAST_HASH: Store.pad.getLastHash,
GET_FULL_HISTORY: Store.getFullHistory,
@ -92,6 +87,10 @@ const factory = AStore => {
DELETE_MAILBOX_MESSAGE: Store.deleteMailboxMessage,
// Drive
DRIVE_USEROBJECT: Store.userObjectCommand,
GET_DRIVE: Store.drive.get,
SET_DRIVE: Store.drive.set,
MIGRATE_ANON_DRIVE: Store.drive.migrateAnon,
HAS_DRIVE: Store.drive.exists,
// Settings,
DELETE_ACCOUNT: Store.deleteAccount,
REMOVE_OWNED_PADS: Store.removeOwnedPads,

View File

@ -54,6 +54,7 @@ export interface Account {
export type DriveConfig = {
store: any,
Store: any,
broadcast: (exclude: object, cmd: string, data?: any, cb?: any) => void,
postMessage: (clientId: string, cmd: string, data?: any, cb?: any) => void
}
@ -65,6 +66,7 @@ export interface DriveObject {
onReconnect: any
}
export interface Drive {
initAPI: (config: DriveConfig) => any
init: (config: DriveConfig) => DriveObject
}
@ -77,6 +79,7 @@ export type PadConfig = {
export interface PadObject {
join: RpcCall,
destroy: RpcCall,
clear: RpcCall,
setMetadata: RpcCall,
getMetadata: RpcCall,
leave: RpcCall,

View File

@ -734,20 +734,6 @@ define([
return tableObj.table;
};
// Msg.admin_updateLimitHint, .admin_updateLimitTitle, .admin_updateLimitButton
sidebar.addItem('update-limit', function (cb) {
var button = blocks.activeButton('primary', '',
Messages.admin_updateLimitButton, done => {
sFrameChan.query('Q_ADMIN_RPC', {
cmd: 'Q_UPDATE_LIMIT',
}, function (e, data) {
done(!!data);
UI.alert(data ? Messages.admin_updateLimitDone || 'done' : 'error' + e);
});
});
cb(button);
});
// Msg.admin_enableembedsHint, .admin_enableembedsTitle
sidebar.addCheckboxItem({
key: 'enableembeds',

View File

@ -24,11 +24,6 @@ define([
sframeChan.on('Q_ADMIN_RPC', function (data, cb) {
Cryptpad.adminRpc(data, cb);
});
sframeChan.on('Q_UPDATE_LIMIT', function (data, cb) {
Cryptpad.updatePinLimit(function (e) {
cb({error: e});
});
});
};
var category;
if (window.location.hash) {

View File

@ -408,19 +408,19 @@ define([
};
// Settings and drive and auth
common.getUserObject = function (teamId, cb) {
/*
postMessage("GET", {
teamId: teamId,
key: []
}, function (obj) {
cb(obj);
});
/*
*/
postMessage("GET_DRIVE", {
teamId: teamId,
}, function (obj) {
cb(obj);
});
*/
};
common.getSharedFolder = function (data, cb) {
postMessage("GET_SHARED_FOLDER", data, function (obj) {
@ -458,7 +458,6 @@ define([
});
return;
}
/*
postMessage("SET_DRIVE", {
teamId: data.teamId,
value: data.drive
@ -467,7 +466,7 @@ define([
}, {
timeout: 5 * 60 * 1000
});
*/
/*
postMessage("SET", {
teamId: data.teamId,
key: ['drive'],
@ -477,6 +476,7 @@ define([
}, {
timeout: 5 * 60 * 1000
});
*/
};
common.addSharedFolder = function (teamId, secret, cb) {
var href = (secret.keys && secret.keys.editKeyStr) ? '/drive/#' + Hash.getEditHashFromKeys(secret) : undefined;
@ -562,13 +562,6 @@ define([
});
};
common.updatePinLimit = function (cb) {
postMessage("UPDATE_PIN_LIMIT", null, function (obj) {
if (obj.error) { return void cb(obj.error); }
cb(undefined, obj.limit, obj.plan, obj.note);
});
};
common.getPinLimit = function (data, cb) {
postMessage("GET_PIN_LIMIT", data, function (obj) {
if (obj.error) { return void cb(obj.error); }

View File

@ -5552,22 +5552,6 @@ define([
UI.removeLoadingScreen();
}, {init:true});
/*
if (!APP.team) {
sframeChan.query('Q_DRIVE_GETDELETED', null, function (err, data) {
var ids = manager.findChannels(data);
var titles = [];
ids.forEach(function (id) {
var title = manager.getTitle(id);
titles.push(title);
var paths = manager.findFile(id);
manager.delete(paths, refresh);
});
if (!titles.length) { return; }
UI.log(Messages._getKey('fm_deletedPads', [titles.join(', ')]));
});
}
*/
APP.passwordModal = function (fId, data, cb) {
var content = [];

View File

@ -11,11 +11,18 @@ const factory = function (Channel, NodeWS) {
const commands = {
account: {
load: 'CONNECT'
},
drive: {
migrateAnon: 'MIGRATE_ANON_DRIVE'
},
pad: {
join: 'JOIN_PAD',
leave: 'LEAVE_PAD',
sendMsg: 'SEND_PAD_MSG',
destroy: 'REMOVE_OWNED_CHANNEL',
clear: 'CLEAR_OWNED_CHANNEL',
setMetadata: 'SET_PAD_METADATA',
getMetadata: 'GET_PAD_METADATA'
}

File diff suppressed because one or more lines are too long