#150 [user] allow disabling gravatar

This commit is contained in:
Avris 2020-12-24 13:00:37 +01:00
parent 807c3e92b9
commit 5227fa9cf4
6 changed files with 45 additions and 23 deletions

View File

@ -6,18 +6,26 @@
<p class="mb-0"> <p class="mb-0">
<Avatar :user="$user()"/> <Avatar :user="$user()"/>
</p> </p>
<p v-if="$user().avatarSource" class="mt-3"> <div class="mb-2">
<div v-if="$user().avatarSource !== 'gravatar'" class="mt-3">
Gravatar: Gravatar:
<a href="#" @click.prevent="setAvatar(null)"> <a href="#" @click.prevent="setAvatar('gravatar')">
<Avatar :user="$user()" grav dsize="2rem"/> <Avatar :user="$user()" :src="gravatar($user())" dsize="2rem"/>
</a> </a>
</p> </div>
<p v-else> <div v-else>
<a href="https://gravatar.com" target="_blank" rel="noopener" class="small"> <a href="https://gravatar.com" target="_blank" rel="noopener" class="small">
<Icon v="external-link"/> <Icon v="external-link"/>
<T>user.avatar.change</T> <T>user.avatar.change</T>
</a> </a>
</p> </div>
<div v-if="$user().avatarSource">
<a href="#" @click.prevent="setAvatar(null)" class="small">
<Icon v="trash"/>
<T>crud.remove</T>
</a>
</div>
</div>
<p v-if="$admin()"> <p v-if="$admin()">
<nuxt-link to="/admin" class="badge badge-primary"><T>user.account.admin</T></nuxt-link> <nuxt-link to="/admin" class="badge badge-primary"><T>user.account.admin</T></nuxt-link>
</p> </p>
@ -112,6 +120,7 @@
<script> <script>
import {socialProviders} from "../src/data"; import {socialProviders} from "../src/data";
import {gravatar} from "../src/helpers";
export default { export default {
data() { data() {
@ -127,6 +136,8 @@
socialProviders, socialProviders,
socialConnections: undefined, socialConnections: undefined,
gravatar,
} }
}, },
async mounted() { async mounted() {

View File

@ -1,10 +1,10 @@
<template> <template>
<img :src="grav ? gravatar : (src || user.avatar || gravatar)" alt="" class="rounded-circle" <img :src="src || user.avatar || (user.avatarSource === 'gravatar' ? gravatar(user, size) : fallbackAvatar(user, size))" alt="" class="rounded-circle"
:style="`width: 100%;max-width: ${dsize};max-height: ${dsize};`"/> :style="`width: 100%;max-width: ${dsize};max-height: ${dsize};`"/>
</template> </template>
<script> <script>
import { gravatar } from "../src/helpers"; import { fallbackAvatar, gravatar } from "../src/helpers";
export default { export default {
props: { props: {
@ -12,11 +12,11 @@
src: {}, src: {},
size: { 'default': 128 }, size: { 'default': 128 },
dsize: { 'default': '6rem' }, dsize: { 'default': '6rem' },
grav: { type: Boolean },
}, },
data() { data() {
return { return {
gravatar: gravatar(this.user, this.size), fallbackAvatar,
gravatar,
}; };
}, },
} }

View File

@ -0,0 +1,7 @@
-- Up
UPDATE users SET avatarSource = 'gravatar' WHERE avatarSource IS NULL;
-- Down
UPDATE users SET avatarSource = NULL WHERE avatarSource = 'gravatar';

View File

@ -1,8 +1,10 @@
import {gravatar, now} from "../src/helpers"; import {fallbackAvatar, gravatar, now} from "../src/helpers";
import SQL from "sql-template-strings"; import SQL from "sql-template-strings";
export default async (db, user) => { export default async (db, user) => {
if (user.avatarSource) { if (user.avatarSource === 'gravatar') {
return gravatar(user);
} else if (user.avatarSource) {
const auth = await db.get(SQL` const auth = await db.get(SQL`
SELECT payload FROM authenticators SELECT payload FROM authenticators
WHERE type = ${user.avatarSource} WHERE type = ${user.avatarSource}
@ -14,5 +16,5 @@ export default async (db, user) => {
} }
} }
return gravatar(user); return fallbackAvatar(user);
} }

View File

@ -65,7 +65,7 @@ const defaultUsername = async (db, email) => {
} }
} }
const fetchOrCreateUser = async (db, user, avatarSource = null) => { const fetchOrCreateUser = async (db, user, avatarSource = 'gravatar') => {
let dbUser = await db.get(SQL`SELECT * FROM users WHERE email = ${normalise(user.email)}`); let dbUser = await db.get(SQL`SELECT * FROM users WHERE email = ${normalise(user.email)}`);
if (!dbUser) { if (!dbUser) {
dbUser = { dbUser = {

View File

@ -78,10 +78,12 @@ export const makeId = (length, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi
return result; return result;
} }
export const gravatar = (user, size = 128) => { export const fallbackAvatar = (user, size = 128) => {
const fallback = `https://avi.avris.it/${size}/${Base64.encode(user.username).replace(/\+/g, '-').replace(/\//g, '_')}.png`; return `https://avi.avris.it/${size}/${Base64.encode(user.username).replace(/\+/g, '-').replace(/\//g, '_')}.png`;
}
return `https://www.gravatar.com/avatar/${user.emailHash || md5(user.email)}?d=${encodeURIComponent(fallback)}&s=${size}`; export const gravatar = (user, size = 128) => {
return `https://www.gravatar.com/avatar/${user.emailHash || md5(user.email)}?d=${encodeURIComponent(fallbackAvatar(user, size))}&s=${size}`;
} }
export const dictToList = dict => { export const dictToList = dict => {