This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/avatar.js

23 lines
730 B
JavaScript
Raw Normal View History

2020-12-24 04:00:37 -08:00
import {fallbackAvatar, gravatar, now} from "../src/helpers";
2020-11-02 12:12:15 -08:00
import SQL from "sql-template-strings";
export default async (db, user) => {
if (user.avatarSource && user.avatarSource.startsWith('https://')) {
2021-01-08 06:14:53 -08:00
return user.avatarSource;
} else if (user.avatarSource === 'gravatar') {
2020-12-24 04:00:37 -08:00
return gravatar(user);
} else if (user.avatarSource) {
2020-11-02 12:12:15 -08:00
const auth = await db.get(SQL`
SELECT payload FROM authenticators
WHERE type = ${user.avatarSource}
AND userId = ${user.id}
2020-11-02 12:45:45 -08:00
AND (validUntil IS NULL OR validUntil > ${now()})
2020-11-02 12:12:15 -08:00
`)
if (auth) {
return JSON.parse(auth.payload).avatar;
}
}
2020-12-24 04:00:37 -08:00
return fallbackAvatar(user);
2020-11-02 12:12:15 -08:00
}