Replacing URLs by user ids in origins

This commit is contained in:
vdv committed 2023-03-04 16:07:22 +01:00
1 parent f07bf07c3f
commit c8644c8817
3 files changed
+92 -43

No files matched your search

+52 -40
View File
@@ -4,6 +4,7 @@ import indexeddb from 'pouchdb-adapter-indexeddb';
import { sleep, until } from '../lib/async-wait';
import { isEqual } from 'lodash';
import { dbUrlToUserId } from '../lib/user-id';
PouchDB.plugin(indexeddb);
@@ -109,53 +110,64 @@ export const watchDbLegacy = async () => {
const findDocumentsWithoutOrigin = async () => {
const allDocs = await db.allDocs({ include_docs: true });
return allDocs.rows
.filter((row: any) => !row.doc.origin)
.filter(
(row: any) =>
!row.doc.origin || !row.doc.origin.match(/^@([^>]+)>(.+)$/)
)
.map((row: any) => row.doc._id);
};
const updateDocumentsWithoutOrigin = async () => {
const documentsWithoutOrigin = await findDocumentsWithoutOrigin();
console.log({
caller: 'watchDbLegacy / addOrigin',
remoteUrl: state().remoteUrl,
nbDocumentsWithoutOrigin: documentsWithoutOrigin.length,
});
for (const docId of documentsWithoutOrigin) {
while (true) {
await sleep(100);
const doc = await db.get(docId);
console.log({
caller: 'watchDbLegacy / addOrigin',
remoteUrl: state().remoteUrl,
docId,
doc,
});
try {
db.put({ ...doc, origin: dbUrlToUserId(state().remoteUrl) });
break;
} catch (error) {
if (error.name === 'conflict') {
console.log({
caller: 'watchDbLegacy / addOrigin',
docId,
doc,
error,
});
} else {
console.error({
caller: 'watchDbLegacy / addOrigin',
docId,
doc,
error,
});
break;
}
}
}
}
};
if (!!state().remoteUrl) {
await updateDocumentsWithoutOrigin();
}
createEffect(async () => {
if (!!state().remoteUrl && remoteUrl() !== state().remoteUrl) {
setRemoteUrl(state().remoteUrl);
// Check documents and update without origin
const documentsWithoutOrigin = await findDocumentsWithoutOrigin();
console.log({
caller: 'watchDbLegacy / addOrigin',
remoteUrl: remoteUrl(),
nbDocumentsWithoutOrigin: documentsWithoutOrigin.length,
});
for (const docId of documentsWithoutOrigin) {
while (true) {
await sleep(100);
const doc = await db.get(docId);
console.log({
caller: 'watchDbLegacy / addOrigin',
remoteUrl: remoteUrl(),
docId,
doc,
});
try {
db.put({ ...doc, origin: remoteUrl() });
break;
} catch (error) {
if (error.name === 'conflict') {
console.log({
caller: 'watchDbLegacy / addOrigin',
docId,
doc,
error,
});
} else {
console.error({
caller: 'watchDbLegacy / addOrigin',
docId,
doc,
error,
});
break;
}
}
}
}
await updateDocumentsWithoutOrigin();
}
});