Switch syncing to simple sync method
This commit is contained in:
parent
32bf923c1a
commit
e3f1fafad9
3 changed files with 25 additions and 23 deletions
19
app-sync.js
19
app-sync.js
|
@ -8,7 +8,7 @@ let errorMiddleware = require('./util/error-middleware');
|
|||
let config = require('./load-config');
|
||||
let { getAccountDb } = require('./account-db');
|
||||
|
||||
let fullSync = require('./sync-full');
|
||||
let simpleSync = require('./sync-simple');
|
||||
|
||||
let actual = require('@actual-app/api');
|
||||
let SyncPb = actual.internal.SyncProtoBuf;
|
||||
|
@ -121,22 +121,12 @@ app.post('/sync', async (req, res) => {
|
|||
return false;
|
||||
}
|
||||
|
||||
// TODO: We also provide a "simple" sync method which currently isn't
|
||||
// used. This method just stores the messages locally and doesn't
|
||||
// load the whole app at all. If we want to support end-to-end
|
||||
// encryption, this method is required because we can't read the
|
||||
// messages. Using it looks like this:
|
||||
//
|
||||
// let simpleSync = require('./sync-simple');
|
||||
// let {trie, newMessages } = simpleSync.sync(messages, since, file_id);
|
||||
|
||||
let { trie, newMessages } = await fullSync.sync(messages, since, file_id);
|
||||
let { trie, newMessages } = simpleSync.sync(messages, since, group_id);
|
||||
|
||||
// encode it back...
|
||||
let responsePb = new SyncPb.SyncResponse();
|
||||
responsePb.setMerkle(JSON.stringify(trie));
|
||||
|
||||
newMessages.forEach((msg) => responsePb.addMessages(msg));
|
||||
newMessages.forEach(msg => responsePb.addMessages(msg));
|
||||
|
||||
res.set('Content-Type', 'application/actual-sync');
|
||||
res.send(Buffer.from(responsePb.serializeBinary()));
|
||||
|
@ -306,6 +296,7 @@ app.post('/upload-user-file', async (req, res) => {
|
|||
'UPDATE files SET sync_version = ?, encrypt_meta = ?, name = ? WHERE id = ?',
|
||||
[syncFormatVersion, encryptMeta, name, fileId]
|
||||
);
|
||||
|
||||
res.send(JSON.stringify({ status: 'ok', groupId }));
|
||||
}
|
||||
});
|
||||
|
@ -377,7 +368,7 @@ app.get('/list-user-files', (req, res) => {
|
|||
res.send(
|
||||
JSON.stringify({
|
||||
status: 'ok',
|
||||
data: rows.map((row) => ({
|
||||
data: rows.map(row => ({
|
||||
deleted: row.deleted,
|
||||
fileId: row.id,
|
||||
groupId: row.group_id,
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
|
||||
CREATE TABLE messages_binary
|
||||
(timestamp TEXT,
|
||||
(timestamp TEXT PRIMARY KEY,
|
||||
is_encrypted BOOLEAN,
|
||||
content bytea,
|
||||
PRIMARY KEY(timestamp, group_id));
|
||||
content bytea);
|
||||
|
||||
CREATE TABLE messages_merkles
|
||||
(id TEXT PRIMAREY KEY,
|
||||
(id INTEGER PRIMARY KEY,
|
||||
merkle TEXT);
|
||||
|
|
|
@ -4,7 +4,8 @@ let { openDatabase } = require('./db');
|
|||
|
||||
let actual = require('@actual-app/api');
|
||||
let merkle = actual.internal.merkle;
|
||||
let Timestamp = actual.internal.timestamp.Timestamp;
|
||||
let SyncPb = actual.internal.SyncProtoBuf;
|
||||
let Timestamp = actual.internal.timestamp.default;
|
||||
|
||||
function getGroupDb(groupId) {
|
||||
let path = join(__dirname, `user-files/${groupId}.sqlite`);
|
||||
|
@ -57,7 +58,7 @@ function addMessages(db, messages) {
|
|||
}
|
||||
|
||||
function getMerkle(db, group_id) {
|
||||
let rows = db.all('SELECT * FROM messages_merkles', [group_id]);
|
||||
let rows = db.all('SELECT * FROM messages_merkles');
|
||||
|
||||
if (rows.length > 0) {
|
||||
return JSON.parse(rows[0].merkle);
|
||||
|
@ -68,8 +69,8 @@ function getMerkle(db, group_id) {
|
|||
}
|
||||
}
|
||||
|
||||
function sync(messages, since, fileId) {
|
||||
let db = getGroupDb(fileId);
|
||||
function sync(messages, since, groupId) {
|
||||
let db = getGroupDb(groupId);
|
||||
let newMessages = db.all(
|
||||
`SELECT * FROM messages_binary
|
||||
WHERE timestamp > ?
|
||||
|
@ -79,7 +80,18 @@ function sync(messages, since, fileId) {
|
|||
|
||||
let trie = addMessages(db, messages);
|
||||
|
||||
return { trie, newMessages };
|
||||
db.close();
|
||||
|
||||
return {
|
||||
trie,
|
||||
newMessages: newMessages.map(msg => {
|
||||
const envelopePb = new SyncPb.MessageEnvelope();
|
||||
envelopePb.setTimestamp(msg.timestamp);
|
||||
envelopePb.setIsencrypted(msg.is_encrypted);
|
||||
envelopePb.setContent(msg.content);
|
||||
return envelopePb;
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { sync };
|
||||
|
|
Loading…
Reference in a new issue