mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-09-27 17:17:32 +08:00
tags now use _id
This commit is contained in:
parent
81bc9ded69
commit
7ddf37f8a3
10 changed files with 44 additions and 45 deletions
|
@ -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 } }
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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 + ", ";
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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(`<div class="button" filter="${tag.id}">${tag.name}</div>`);
|
||||
).append(`<div class="button" filter="${tag._id}">${tag.name}</div>`);
|
||||
});
|
||||
} 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;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -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(
|
||||
`<div class="icon"><i class="fas fa-fw fa-tag"></i></div><div>Tags > ` +
|
||||
txt
|
||||
);
|
||||
} else {
|
||||
$(
|
||||
`#commandLine .suggestions .entry[command='toggleTag${tag.id}']`
|
||||
`#commandLine .suggestions .entry[command='toggleTag${tag._id}']`
|
||||
).html(txt);
|
||||
}
|
||||
},
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 = {};
|
||||
}
|
||||
});
|
||||
|
|
|
@ -34,7 +34,7 @@ export function updateButtons() {
|
|||
$("#resultEditTagsPanel .buttons").empty();
|
||||
DB.getSnapshot().tags.forEach((tag) => {
|
||||
$("#resultEditTagsPanel .buttons").append(
|
||||
`<div class="button tag" tagid="${tag.id}">${tag.name}</div>`
|
||||
`<div class="button tag" tagid="${tag._id}">${tag.name}</div>`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
@ -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 + ", ";
|
||||
}
|
||||
});
|
||||
|
|
|
@ -422,7 +422,7 @@ function refreshTagsSettingsSection() {
|
|||
}
|
||||
tagsEl.append(`
|
||||
|
||||
<div class="buttons tag" id="${tag.id}">
|
||||
<div class="buttons tag" id="${tag._id}">
|
||||
<div class="button tagButton ${tag.active ? "active" : ""}" active="${
|
||||
tag.active
|
||||
}">
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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(`
|
||||
<div tagid="${tag.id}" aria-label="PB: ${tpb}" data-balloon-pos="up">${tag.name}<i class="fas fa-crown hidden"></i></div>
|
||||
<div tagid="${tag._id}" aria-label="PB: ${tpb}" data-balloon-pos="up">${tag.name}<i class="fas fa-crown hidden"></i></div>
|
||||
`);
|
||||
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({
|
||||
|
|
Loading…
Add table
Reference in a new issue