[card] make people chose light or dark mode

This commit is contained in:
Avris 2021-10-13 21:56:39 +02:00
parent 4de6994510
commit cb2d328d03
4 changed files with 116 additions and 88 deletions

View File

@ -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

View File

@ -36,27 +36,31 @@
<Icon v="id-card"/>
<T>profile.card.link</T>:
</small>
<small v-if="profile.card === null">
<button class="btn btn-outline-success btn-sm" @click="generateCard">
<T>profile.card.generate</T>
<small v-if="profile.card === null && profile.cardDark === null">
(<T>profile.card.generate</T>)
<button class="btn btn-outline-success btn-sm" @click="generateCard(false)">
<Icon v="sun"/>
<T>mode.light</T>
</button>
<button class="btn btn-outline-success btn-sm" @click="generateCard(true)">
<Icon v="moon"/>
<T>mode.dark</T>
</button>
</small>
<small v-else-if="profile.card === ''">
<small v-else-if="profile.card === '' || profile.cardDark === ''">
<Spinner/>
<T>profile.card.generating</T>
</small>
<template v-if="profile.card">
<a :href="profile.card" target="_blank" rel="noopener"
class="btn btn-outline-success btn-sm mx-1">
<Icon v="sun"/>
<T>mode.light</T>
</a>
<a :href="profile.card.replace('.png', '-dark.png')" target="_blank" rel="noopener"
class="btn btn-outline-success btn-sm mx-1">
<Icon v="moon"/>
<T>mode.dark</T>
</a>
</template>
<a v-if="profile.card" :href="profile.card" target="_blank" rel="noopener"
class="btn btn-outline-success btn-sm mx-1">
<Icon v="sun"/>
<T>mode.light</T>
</a>
<a v-if="profile.cardDark" :href="profile.cardDark" target="_blank" rel="noopener"
class="btn btn-outline-success btn-sm mx-1">
<Icon v="moon"/>
<T>mode.dark</T>
</a>
</div>
</div>
</Profile>
@ -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 {

View File

@ -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);
}
}
})();

View File

@ -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;