Skip to content

Commit

Permalink
fix sync not deleting notes on server
Browse files Browse the repository at this point in the history
modified UI a little

add multiple select delete
  • Loading branch information
0xGingi committed Nov 15, 2024
1 parent 0b5d29b commit ebcd277
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 159 deletions.
62 changes: 36 additions & 26 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,48 +165,57 @@ async function processNotes(public_key, incoming_notes) {
conflicts: []
};

// Process incoming notes
for (const note of incoming_notes) {
const existing = await notesCollection.findOne({
public_key,
id: note.id,
});

if (!existing || existing.timestamp < note.timestamp) {
await notesCollection.updateOne(
{ public_key, id: note.id },
{
$set: {
id: note.id,
data: note.data,
nonce: note.nonce,
timestamp: note.timestamp,
signature: note.signature,
public_key,
deleted: note.deleted
}
},
{ upsert: true }
);

if (note.deleted) {
// If note is marked as deleted, remove it from the database
await notesCollection.deleteOne({ public_key, id: note.id });
} else {
// Otherwise update/insert the note
await notesCollection.updateOne(
{ public_key, id: note.id },
{ $set: { ...note, public_key } },
{ upsert: true }
);
await notesCollection.deleteOne({
public_key,
id: note.id
});
}

results.updated.push(note.id);
} else if (existing.timestamp === note.timestamp &&
existing.signature !== note.signature) {
results.conflicts.push(note.id);
}
}

// Fetch all non-deleted notes for this user
const allNotes = await notesCollection
.find({
public_key,
deleted: { $ne: true }
})
.toArray();

results.notes = allNotes.map(note => ({
id: note.id,
data: note.data,
nonce: note.nonce,
timestamp: note.timestamp,
signature: note.signature,
deleted: note.deleted
}));
.find({
public_key,
deleted: { $ne: true }
})
.toArray();

results.notes = allNotes.map(note => ({
id: note.id,
data: note.data,
nonce: note.nonce,
timestamp: note.timestamp,
signature: note.signature
}));

return results;
});
Expand All @@ -216,6 +225,7 @@ async function processNotes(public_key, incoming_notes) {
await session.endSession();
}
}

app.get('/api/health', async (req, res) => {
try {
await db.command({ ping: 1 });
Expand Down
Loading

0 comments on commit ebcd277

Please sign in to comment.