diff --git a/migrations/037-card-dark.sql b/migrations/037-card-dark.sql new file mode 100644 index 00000000..83f6b9b4 --- /dev/null +++ b/migrations/037-card-dark.sql @@ -0,0 +1,7 @@ +-- Up + +ALTER TABLE profiles ADD COLUMN cardDark TEXT NULL; + +UPDATE profiles SET cardDark = replace(card, '.png', '-dark.png') WHERE card IS NOT NULL; + +-- Down diff --git a/routes/profile.vue b/routes/profile.vue index 9888bc02..ba9218e3 100644 --- a/routes/profile.vue +++ b/routes/profile.vue @@ -36,27 +36,31 @@ profile.card.link: - - + - + profile.card.generating - + + + mode.light + + + + mode.dark + @@ -152,13 +156,13 @@ }, }, methods: { - async generateCard() { - await this.$axios.$post(`/profile/request-card`); - this.profile.card = ''; + async generateCard(dark) { + await this.$axios.$post(`/profile/request-card?dark=${dark ? '1' : '0'}`); + this.profile[dark ? 'cardDark' : 'card'] = ''; this.startCheckingForCard(); }, startCheckingForCard() { - if (this.cardCheckHandle || !this.profile || this.profile.card) { + if (this.cardCheckHandle || !this.profile || this.profile.card || this.profile.cardDark) { return; } this.cardCheckHandle = setInterval(this.checkForCard, 3000); @@ -166,9 +170,9 @@ async checkForCard() { try { const card = await this.$axios.$get(`/profile/has-card`); - console.log(card); - if (card) { - this.profile.card = card; + if (card.card || card.cardDark) { + this.profile.card = card.card; + this.profile.cardDark = card.cardDark; clearInterval(this.cardCheckHandle); } } catch { diff --git a/server/cards.js b/server/cards.js index c9f8a41f..1471a9d8 100644 --- a/server/cards.js +++ b/server/cards.js @@ -23,75 +23,83 @@ const sleep = ms => new Promise(res => setTimeout(res, ms)); const modes = ['light', 'dark']; -(async () => { - const db = await dbConnection(); - while (true) { - await sleep(3000); - - const profiles = (await db.all(` +const shoot = async (db, mode) => { + const profiles = (await db.all(` SELECT profiles.id, profiles.locale, users.username FROM profiles - LEFT JOIN users on profiles.userId = users.id - WHERE profiles.card = '' + LEFT JOIN users on profiles.userId = users.id + WHERE profiles.${mode === 'dark' ? 'cardDark' : 'card'} = '' ORDER BY RANDOM() LIMIT 6 `)).filter(({locale}) => !isHighLoadTime(locale)); - if (profiles.length === 0) { - console.log('No profiles in the queue'); + if (profiles.length === 0) { + console.log('No profiles in the queue'); + return; + } + + const results = {} + + try { + const pr = new Pageres({ + darkMode: mode === 'dark', + delay: 3, + scale: 2, + }); + + for (let {locale, username} of profiles) { + console.log(`Shooting @${username} (${locale}, ${mode})`); + pr.src(urlBases[locale] + username, ['1024x300']) + } + + for (let buffer of await pr.run()) { + const [, domain, username] = buffer.filename.match(/(.*)!card!@(.*)-1024x300\.png/); + const locale = domainLocaleMap[domain]; + results[locale + '/' + username] = buffer; + } + } catch (e) { + console.error(e); + return; + } + + for (let {id, locale, username} of profiles) { + const cardId = ulid(); + let key = `card/${locale}/${username}-${cardId}.png`; + if (mode === 'dark') { + key = mode === 'dark' ? key.replace('.png', '-dark.png') : key; + } + + console.log(`Uploading @${username} (${locale}, ${mode}) – ${cardId}`); + + const buffer = results[locale + '/' + username.replace(/\.+$/, '')]; + + if (buffer === undefined) { + console.error('Cannot find the proper buffer!'); continue; } - const results = { - 'light': {}, - 'dark': {}, - } + const s3putResponse = await s3.putObject({ + Key: key, + Body: buffer, + ContentType: 'image/png', + ACL: 'public-read', + }).promise(); - try { - for (let mode of modes) { - const pr = new Pageres({ - darkMode: mode === 'dark', - delay: 3, - scale: 2, - }); + await db.get(` + UPDATE profiles + SET ${mode === 'dark' ? 'cardDark' : 'card'}='https://${awsConfig.params.Bucket}.s3.${awsConfig.region}.amazonaws.com/${key}' + WHERE id='${id}'` + ); + } +} - for (let {locale, username} of profiles) { - console.log(`Shooting @${username} (${locale}, ${mode})`); - pr.src(urlBases[locale] + username, ['1024x300']) - } - - for (let buffer of await pr.run()) { - const [, domain, username] = buffer.filename.match(/(.*)!card!@(.*)-1024x300\.png/); - const locale = domainLocaleMap[domain]; - results[mode][locale + '/' + username] = buffer; - } - } - } catch (e) { - console.error(e); - continue; - } - - for (let {id, locale, username} of profiles) { - const cardId = ulid(); - const key = `card/${locale}/${username}-${cardId}.png`; - for (let mode of modes) { - console.log(`Uploading @${username} (${locale}, ${mode}) – ${cardId}`); - const buffer = results[mode][locale + '/' + username.replace(/\.+$/, '')]; - - if (buffer === undefined) { - console.error('Cannot find the proper buffer!'); - continue; - } - - const s3putResponse = await s3.putObject({ - Key: mode === 'dark' ? key.replace('.png', '-dark.png') : key, - Body: buffer, - ContentType: 'image/png', - ACL: 'public-read', - }).promise(); - } - - await db.get(`UPDATE profiles SET card='https://${awsConfig.params.Bucket}.s3.${awsConfig.region}.amazonaws.com/${key}' WHERE id='${id}'`) +(async () => { + const db = await dbConnection(); + while (true) { + for (let mode of modes) { + await sleep(3000); + console.log(`Starting mode: ${mode}`) + await shoot(db, mode); } } })(); diff --git a/server/routes/profile.js b/server/routes/profile.js index aa92fe9e..f25ec7bf 100644 --- a/server/routes/profile.js +++ b/server/routes/profile.js @@ -51,6 +51,7 @@ const fetchProfiles = async (db, username, self, isAdmin) => { credentialsLevel: profile.credentialsLevel, credentialsName: profile.credentialsName, card: profile.card, + cardDark: profile.cardDark, }; } return p; @@ -224,11 +225,19 @@ router.post('/profile/request-card', handleErrorAsync(async (req, res) => { return res.status(400).json({error: 'Missing user'}); } - await req.db.get(SQL` - UPDATE profiles - SET card = '' - WHERE userId=${req.user.id} AND locale=${global.config.locale} AND card IS NULL - `); + if (req.query.dark === '1') { + await req.db.get(SQL` + UPDATE profiles + SET cardDark = '' + WHERE userId=${req.user.id} AND locale=${global.config.locale} AND cardDark IS NULL + `); + } else { + await req.db.get(SQL` + UPDATE profiles + SET card = '' + WHERE userId=${req.user.id} AND locale=${global.config.locale} AND card IS NULL + `); + } return res.json('OK'); })); @@ -239,12 +248,12 @@ router.get('/profile/has-card', handleErrorAsync(async (req, res) => { } const card = await req.db.get(SQL` - SELECT card + SELECT card, cardDark FROM profiles WHERE userId=${req.user.id} AND locale=${global.config.locale} `); - return res.json(card ? card.card : null); + return res.json(card); })); export default router;