Skip to content

Commit

Permalink
remove label in cache, too, when deleting it in vikunja and reset now…
Browse files Browse the repository at this point in the history
… operates directly
  • Loading branch information
Heiss committed Jul 20, 2024
1 parent 56c8fc8 commit bed0ad6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "vikunja-sync",
"name": "Vikunja Sync",
"version": "1.0.10",
"version": "1.0.11",
"minAppVersion": "0.15.0",
"description": "Integrates Vikunja.",
"author": "Peter Heiss",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class Commands {

while (true) {
const tasks = await this.plugin.tasksApi.getAllTasks();
const labels = this.plugin.labelsApi.getLabels()
const labels = await this.plugin.labelsApi.labelsApi.labelsGet();

if (tasks.length === 0 && labels.length === 0) {
if (this.plugin.settings.debugging) console.log("No tasks and labels found in Vikunja");
Expand All @@ -123,8 +123,8 @@ export default class Commands {
await this.plugin.labelsApi.deleteLabels(labels);
}


if (this.plugin.settings.debugging) console.log("Resetting tasks in Vikunja done");
await this.plugin.labelsApi.loadLabels();
new Notice("Resetting tasks and labels in Vikunja done");
}
).open();
Expand Down
4 changes: 2 additions & 2 deletions src/processing/syncLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SyncLabels implements IAutomatonSteps {
const allLabels = this.plugin.labelsApi.getLabels();
const dedupAllLabels = allLabels.filter((label, index, self) => self.findIndex(l => l.title === label.title) === index);
// remove all duplicated labels
await Promise.all(allLabels.filter(label => dedupAllLabels.find(l => l.id === label.id) === undefined).map(label => label.id && this.plugin.labelsApi.deleteLabel(label.id)));
await Promise.all(allLabels.filter(label => dedupAllLabels.find(l => l.id === label.id) === undefined).map(label => label.id && this.plugin.labelsApi.deleteLabel(label)));

if (!this.plugin.settings.removeLabelsIfInVaultNotUsed) {
if (this.plugin.settings.debugging) console.log("Step SyncLabels: Not deleting labels in vikunja if ID not found in vault");
Expand All @@ -37,7 +37,7 @@ class SyncLabels implements IAutomatonSteps {

if (this.plugin.settings.debugging) console.log("Step SyncLabels: Deleting labels in Vikunja", labelsToDelete);
// remove all labels not used in vault
await Promise.all(labelsToDelete.map(label => label.id && this.plugin.labelsApi.deleteLabel(label.id)));
await Promise.all(labelsToDelete.map(label => label.id && this.plugin.labelsApi.deleteLabel(label)));
}

private async createLabels(localTasks: PluginTask[]): Promise<PluginTask[]> {
Expand Down
17 changes: 11 additions & 6 deletions src/vikunja/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ class Label {
return await this.createLabel(label);
}

async deleteLabel(labelId: number) {
if (this.plugin.settings.debugging) console.log("LabelsAPI: Deleting label", labelId);
async deleteLabel(label: ModelsLabel) {
if (!label.id) throw new Error("Label id is required to delete label");
if (!label.title) throw new Error("Label title is required to delete label");

if (this.plugin.settings.debugging) console.log("LabelsAPI: Deleting label", label);
const param: LabelsIdDeleteRequest = {
id: labelId,
id: label.id,
};
await this.labelsApi.labelsIdDelete(param);
if (this.plugin.settings.debugging) console.log("LabelsAPI: Deleted label", labelId);
if (this.plugin.settings.debugging) console.log("LabelsAPI: Deleted label", label);
this.labelsMap.delete(label.title);
}

async updateLabel(label: ModelsLabel): Promise<ModelsLabel> {
Expand All @@ -106,11 +110,12 @@ class Label {
async deleteLabels(labels: ModelsLabel[]) {
for (const label of labels) {
if (!label.id) throw new Error("Label id is required to delete label");
await this.deleteLabel(label.id);
await this.deleteLabel(label);
}
}

private async loadLabels(): Promise<ModelsLabel[]> {
//Use with caution!!! Only use it in a place, where no other operations are currently running
async loadLabels(): Promise<ModelsLabel[]> {
this.labelsMap.clear();
let allLabels: ModelsLabel[] = [];
try {
Expand Down

0 comments on commit bed0ad6

Please sign in to comment.