diff --git a/account/edit-couchdb.ts b/account/edit-couchdb.ts index 30cfe96..be4de20 100644 --- a/account/edit-couchdb.ts +++ b/account/edit-couchdb.ts @@ -1,47 +1,109 @@ +#!/usr/bin/env tsx + // a script for making edits to the couchdb records - run with tsnode import Nano from "nano"; import { DocumentInsertResponse } from "nano"; import env from "./src/lib/env-vars"; import * as T from "../website/src/types/account-types"; -import { getAllLingdocsUsers, getLingdocsUser, insertLingdocsUser } from "./src/lib/couch-db"; +import { + getAllLingdocsUsers, + getLingdocsUser, + insertLingdocsUser, +} from "./src/lib/couch-db"; const nano = Nano(env.couchDbURL); -const usersDb = nano.db.use("lingdocs-users"); -const userDbPrefix = "userdb-"; +// const usersDb = nano.db.use("lingdocs-users"); +// const userDbPrefix = "userdb-"; -function processAPIResponse(user: T.LingdocsUser, response: DocumentInsertResponse): T.LingdocsUser | undefined { - if (response.ok !== true) return undefined; - return { - ...user, - _id: response.id, - _rev: response.rev, - }; - } +function processAPIResponse( + user: T.LingdocsUser, + response: DocumentInsertResponse +): T.LingdocsUser | undefined { + if (response.ok !== true) return undefined; + return { + ...user, + _id: response.id, + _rev: response.rev, + }; +} async function main() { - const users = await getAllLingdocsUsers(); - users.forEach(async (user) => { - if (user.tests.length) { - await insertLingdocsUser({ - ...user, - tests: removeRedundant(user.tests), - }); - } - }) - return "done"; + const users = await getAllLingdocsUsers(); + const usersWDbs = users.filter((x) => x.level !== "basic"); + const paidUsersEmails: string[] = []; + for (let user of usersWDbs) { + // if (!user.docs.length) return; + // const u = user.docs[0]; + // await authUsers.destroy(u._id, u._rev); + if (user.level === "basic") { + throw new Error(""); + } + process.stdout.write(`Checking for db for ${user.name} - ${user.email}...`); + const userDb = nano.db.use(user.wordlistDbName); + if (user.subscription) { + paidUsersEmails.push(user.email); + } + try { + // await userDb.insert( + // { + // admins: { + // names: [user.userId], + // roles: ["_admin"], + // }, + // members: { + // names: [user.userId], + // roles: ["_admin"], + // }, + // }, + // "_security" + // ); + const { admins, members } = await userDb.get("_security"); + if ( + admins?.names?.[0] === user.userId && + members?.names?.[0] === user.userId + ) { + console.log("✅"); + } else { + console.log("check", user.wordlistDbName); + console.log("uid", user.userId); + console.log("RR"); + } + } catch (e) { + console.log(e); + console.log("❌"); + console.log(`needs ${user.wordlistDbName} - ${user.userId}`); + } + } + const allDbs = await nano.db.list(); + const strayDbs = allDbs.reduce((acc, curr) => { + if (!curr.startsWith("userdb-")) { + return acc; + } + if ( + !usersWDbs.some((x) => x.level !== "basic" && x.wordlistDbName === curr) + ) { + return [...acc, curr]; + } + return acc; + }, []); + console.log("STRAY USERDBS"); + console.log(strayDbs); + console.log("PAID USERS EMAILS"); + console.log(paidUsersEmails.join(",")); + return "done"; } -function removeRedundant(tests: T.TestResult[]): T.TestResult[] { - if (tests.length === 0) return tests; - const first = tests[0]; - const rest = tests.slice(1); - const redundancies = rest.filter(x => ((x.id === first.id)) && (x.done === first.done)); - return redundancies.length < 2 - ? [first, ...removeRedundant(rest)] - : removeRedundant(rest); -} +// function removeRedundant(tests: T.TestResult[]): T.TestResult[] { +// if (tests.length === 0) return tests; +// const first = tests[0]; +// const rest = tests.slice(1); +// const redundancies = rest.filter(x => ((x.id === first.id)) && (x.done === first.done)); +// return redundancies.length < 2 +// ? [first, ...removeRedundant(rest)] +// : removeRedundant(rest); +// } -main().then(res => { - console.log(res); +main().then((res) => { + console.log(res); }); diff --git a/account/src/lib/couch-db.ts b/account/src/lib/couch-db.ts index 8062ce5..1eba76c 100644 --- a/account/src/lib/couch-db.ts +++ b/account/src/lib/couch-db.ts @@ -183,6 +183,21 @@ export async function addCouchDbAuthUser( password, }; await usersDb.insert(authUser); + const userDb = nano.db.use(userDbName); + await userDb.insert( + { + // @ts-ignore + admins: { + names: [uuid], + roles: ["_admin"], + }, + members: { + names: [uuid], + roles: ["_admin"], + }, + }, + "_security" + ); return { password, userDbName }; } diff --git a/website/src/components/AudioPlayButton.tsx b/website/src/components/AudioPlayButton.tsx index 5f25f3b..2e741b5 100644 --- a/website/src/components/AudioPlayButton.tsx +++ b/website/src/components/AudioPlayButton.tsx @@ -7,33 +7,31 @@ */ import { useEffect, useState } from "react"; -import { - getAudioAttachment, -} from "../lib/wordlist-database"; +import { getAudioAttachment } from "../lib/wordlist-database"; import { WordlistWord } from "../types/dictionary-types"; export function AudioPlayButton({ word }: { word: WordlistWord }) { - const [src, setSrc] = useState(undefined); - const [type, setType] = useState(undefined); - useEffect(() => { - getAudioAttachment(word).then((audio) => { - if (!audio) return; - // @ts-ignore // TODO: FIX THIS! - const src = URL.createObjectURL(audio); - setSrc(src); - setType("type" in audio ? audio.type : undefined); - return () => { - URL.revokeObjectURL(src); - }; - }).catch(console.error); - }, [word]); - return ( -
- -
- ); + const [src, setSrc] = useState(undefined); + const [type, setType] = useState(undefined); + useEffect(() => { + getAudioAttachment(word) + .then((audio) => { + if (!audio) return; + // @ts-ignore // TODO: FIX THIS! + const src = URL.createObjectURL(audio); + setSrc(src); + setType("type" in audio ? audio.type : undefined); + return () => { + URL.revokeObjectURL(src); + }; + }) + .catch(console.error); + }, [word]); + return ( +
+ +
+ ); } -export default AudioPlayButton; \ No newline at end of file +export default AudioPlayButton;