diff --git a/backend/dao/user.js b/backend/dao/user.js index abfa6c645..86e4ebcef 100644 --- a/backend/dao/user.js +++ b/backend/dao/user.js @@ -1,8 +1,8 @@ const MonkeyError = require("../handlers/error"); const { mongoDB } = require("../init/mongodb"); +const { ObjectID } = require("mongodb"); const { checkAndUpdatePb } = require("../handlers/pb"); const { updateAuthEmail } = require("../handlers/auth"); -const uuid = require("uuid"); class UsersDAO { static async addUser(name, email, uid) { @@ -53,12 +53,12 @@ class UsersDAO { } static async addTag(uid, name) { - let id = uuid.v4(); + let _id = ObjectID(); await mongoDB() .collection("users") - .updateOne({ uid }, { $push: { tags: { id, name } } }); + .updateOne({ uid }, { $push: { tags: { _id, name } } }); return { - id, + _id, name, }; } @@ -69,12 +69,12 @@ class UsersDAO { return user.tags; } - static async editTag(uid, id, name) { + static async editTag(uid, _id, name) { const user = await mongoDB().collection("users").findOne({ uid }); if (!user) throw new MonkeyError(404, "User not found"); if ( user.tags === undefined || - user.tags.filter((t) => t.id === id).length === 0 + user.tags.filter((t) => t._id == _id).length === 0 ) throw new MonkeyError(404, "Tag not found"); return await mongoDB() @@ -82,18 +82,18 @@ class UsersDAO { .updateOne( { uid: uid, - "tags.id": id, + "tags._id": ObjectID(_id), }, { $set: { "tags.$.name": name } } ); } - static async removeTag(uid, id) { + static async removeTag(uid, _id) { const user = await mongoDB().collection("users").findOne({ uid }); if (!user) throw new MonkeyError(404, "User not found"); if ( user.tags === undefined || - user.tags.filter((t) => t.id === id).length === 0 + user.tags.filter((t) => t._id == _id).length === 0 ) throw new MonkeyError(404, "Tag not found"); return await mongoDB() @@ -101,18 +101,18 @@ class UsersDAO { .updateOne( { uid: uid, - "tags.id": id, + "tags._id": ObjectID(_id), }, - { $pull: { tags: { id } } } + { $pull: { tags: { _id: ObjectID(_id) } } } ); } - static async removeTagPb(uid, id) { + static async removeTagPb(uid, _id) { const user = await mongoDB().collection("users").findOne({ uid }); if (!user) throw new MonkeyError(404, "User not found"); if ( user.tags === undefined || - user.tags.filter((t) => t.id === id).length === 0 + user.tags.filter((t) => t._id == _id).length === 0 ) throw new MonkeyError(404, "Tag not found"); return await mongoDB() @@ -120,7 +120,7 @@ class UsersDAO { .updateOne( { uid: uid, - "tags.id": id, + "tags._id": ObjectID(_id), }, { $set: { "tags.$.personalBests": {} } } ); @@ -197,7 +197,7 @@ class UsersDAO { let tagsToCheck = []; user.tags.forEach((tag) => { - if (tags.includes(tag.id)) { + if (tags.includes(tag._id)) { tagsToCheck.push(tag); } }); @@ -218,11 +218,11 @@ class UsersDAO { wpm ); if (tagpb.isPb) { - ret.push(tag.id); + ret.push(tag._id); await mongoDB() .collection("users") .updateOne( - { uid, "tags.id": tag.id }, + { uid, "tags._id": ObjectID(tag._id) }, { $set: { "tags.$.personalBests": tagpb.obj } } ); } diff --git a/src/js/account.js b/src/js/account.js index b4d306f6f..f7e7e69f0 100644 --- a/src/js/account.js +++ b/src/js/account.js @@ -233,7 +233,7 @@ function loadMoreLines(lineIndex) { if (result.tags !== undefined && result.tags.length > 0) { result.tags.forEach((tag) => { DB.getSnapshot().tags.forEach((snaptag) => { - if (tag === snaptag.id) { + if (tag === snaptag._id) { tagNames += snaptag.name + ", "; } }); diff --git a/src/js/account/result-filters.js b/src/js/account/result-filters.js index b85a32bd5..03c87ab3f 100644 --- a/src/js/account/result-filters.js +++ b/src/js/account/result-filters.js @@ -99,7 +99,7 @@ export function getFilter(group, filter) { export function loadTags(tags) { tags.forEach((tag) => { - defaultResultFilters.tags[tag.id] = true; + defaultResultFilters.tags[tag._id] = true; }); } @@ -305,7 +305,7 @@ export function updateTags() { DB.getSnapshot().tags.forEach((tag) => { $( ".pageAccount .content .filterButtons .buttonsAndTitle.tags .buttons" - ).append(`
${tag.name}
`); + ).append(`
${tag.name}
`); }); } else { $(".pageAccount .content .filterButtons .buttonsAndTitle.tags").addClass( @@ -412,7 +412,7 @@ $(".pageAccount .topFilters .button.currentConfigFilter").click((e) => { DB.getSnapshot().tags.forEach((tag) => { if (tag.active === true) { filters["tags"]["none"] = false; - filters["tags"][tag.id] = true; + filters["tags"][tag._id] = true; } }); diff --git a/src/js/commandline-lists.js b/src/js/commandline-lists.js index 8294b1a44..ca34d6d0e 100644 --- a/src/js/commandline-lists.js +++ b/src/js/commandline-lists.js @@ -227,12 +227,12 @@ export function updateTagCommands() { } commandsTags.list.push({ - id: "toggleTag" + tag.id, + id: "toggleTag" + tag._id, noIcon: true, display: dis, sticky: true, exec: () => { - TagController.toggle(tag.id); + TagController.toggle(tag._id); TestUI.updateModesNotice(); let txt = tag.name; @@ -243,14 +243,14 @@ export function updateTagCommands() { } if (Commandline.isSingleListCommandLineActive()) { $( - `#commandLine .suggestions .entry[command='toggleTag${tag.id}']` + `#commandLine .suggestions .entry[command='toggleTag${tag._id}']` ).html( `
Tags > ` + txt ); } else { $( - `#commandLine .suggestions .entry[command='toggleTag${tag.id}']` + `#commandLine .suggestions .entry[command='toggleTag${tag._id}']` ).html(txt); } }, diff --git a/src/js/popups/edit-preset-popup.js b/src/js/popups/edit-preset-popup.js index ade00131a..57fbdbcb4 100644 --- a/src/js/popups/edit-preset-popup.js +++ b/src/js/popups/edit-preset-popup.js @@ -73,7 +73,7 @@ async function apply() { let activeTagIds = []; DB.getSnapshot().tags.forEach((tag) => { if (tag.active) { - activeTagIds.push(tag.id); + activeTagIds.push(tag._id); } }); configChanges.tags = activeTagIds; diff --git a/src/js/popups/edit-tags-popup.js b/src/js/popups/edit-tags-popup.js index 3e0385bd7..f2fde525c 100644 --- a/src/js/popups/edit-tags-popup.js +++ b/src/js/popups/edit-tags-popup.js @@ -90,7 +90,7 @@ async function apply() { Notifications.add("Tag added", 1); DB.getSnapshot().tags.push({ name: response.data.name, - id: response.data.id, + _id: response.data._id, }); ResultTagsPopup.updateButtons(); Settings.update(); @@ -116,7 +116,7 @@ async function apply() { } else { Notifications.add("Tag updated", 1); DB.getSnapshot().tags.forEach((tag) => { - if (tag.id === tagid) { + if (tag._id === tagid) { tag.name = inputVal; } }); @@ -141,7 +141,7 @@ async function apply() { } else { Notifications.add("Tag removed", 1); DB.getSnapshot().tags.forEach((tag, index) => { - if (tag.id === tagid) { + if (tag._id === tagid) { DB.getSnapshot().tags.splice(index, 1); } }); @@ -166,7 +166,7 @@ async function apply() { } else { Notifications.add("Tag PB cleared", 1); DB.getSnapshot().tags.forEach((tag, index) => { - if (tag.id === tagid) { + if (tag._id === tagid) { tag.personalBests = {}; } }); diff --git a/src/js/popups/result-tags-popup.js b/src/js/popups/result-tags-popup.js index 93826a793..43daa948a 100644 --- a/src/js/popups/result-tags-popup.js +++ b/src/js/popups/result-tags-popup.js @@ -34,7 +34,7 @@ export function updateButtons() { $("#resultEditTagsPanel .buttons").empty(); DB.getSnapshot().tags.forEach((tag) => { $("#resultEditTagsPanel .buttons").append( - `
${tag.name}
` + `
${tag.name}
` ); }); } @@ -105,7 +105,7 @@ $("#resultEditTagsPanel .confirmButton").click((e) => { if (newtags.length > 0) { newtags.forEach((tag) => { DB.getSnapshot().tags.forEach((snaptag) => { - if (tag === snaptag.id) { + if (tag === snaptag._id) { tagNames += snaptag.name + ", "; } }); diff --git a/src/js/settings.js b/src/js/settings.js index 058f223bc..509458ff2 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -422,7 +422,7 @@ function refreshTagsSettingsSection() { } tagsEl.append(` -
+
diff --git a/src/js/tag-controller.js b/src/js/tag-controller.js index 8327338d8..21cc36a36 100644 --- a/src/js/tag-controller.js +++ b/src/js/tag-controller.js @@ -7,7 +7,7 @@ export function saveActiveToLocalStorage() { try { DB.getSnapshot().tags.forEach((tag) => { if (tag.active === true) { - tags.push(tag.id); + tags.push(tag._id); } }); // let d = new Date(); @@ -31,7 +31,7 @@ export function clear(nosave = false) { export function set(tagid, state, nosave = false) { DB.getSnapshot().tags.forEach((tag) => { - if (tag.id === tagid) { + if (tag._id === tagid) { tag.active = state; } }); @@ -41,7 +41,7 @@ export function set(tagid, state, nosave = false) { export function toggle(tagid, nosave = false) { DB.getSnapshot().tags.forEach((tag) => { - if (tag.id === tagid) { + if (tag._id === tagid) { if (tag.active === undefined) { tag.active = true; } else { diff --git a/src/js/test/test-logic.js b/src/js/test/test-logic.js index b23962378..ea13693d5 100644 --- a/src/js/test/test-logic.js +++ b/src/js/test/test-logic.js @@ -1437,7 +1437,7 @@ export function finish(difficultyFailed = false) { DB.getSnapshot().tags.forEach((tag) => { if (tag.active === true) { activeTags.push(tag); - activeTagsIds.push(tag.id); + activeTagsIds.push(tag._id); } }); } catch (e) {} @@ -1605,7 +1605,7 @@ export function finish(difficultyFailed = false) { let annotationSide = "left"; activeTags.forEach(async (tag) => { let tpb = await DB.getLocalTagPB( - tag.id, + tag._id, Config.mode, mode2, Config.punctuation, @@ -1613,13 +1613,13 @@ export function finish(difficultyFailed = false) { Config.difficulty ); $("#result .stats .tags .bottom").append(` -
${tag.name}
+
${tag.name}
`); if (Config.mode != "quote") { if (tpb < stats.wpm) { //new pb for that tag DB.saveLocalTagPB( - tag.id, + tag._id, Config.mode, mode2, Config.punctuation, @@ -1631,12 +1631,11 @@ export function finish(difficultyFailed = false) { consistency ); $( - `#result .stats .tags .bottom div[tagid="${tag.id}"] .fas` + `#result .stats .tags .bottom div[tagid="${tag._id}"] .fas` ).removeClass("hidden"); - $(`#result .stats .tags .bottom div[tagid="${tag.id}"]`).attr( - "aria-label", - "+" + Misc.roundTo2(stats.wpm - tpb) - ); + $( + `#result .stats .tags .bottom div[tagid="${tag._id}"]` + ).attr("aria-label", "+" + Misc.roundTo2(stats.wpm - tpb)); // console.log("new pb for tag " + tag.name); } else { ChartController.result.options.annotation.annotations.push({