forked from tangled.org/core
this repo has no description

avatar: generate a random color for users without a pfp

Signed-off-by: Anirudh Oppiliappan <anirudh@tangled.sh>

anirudh.fi 8aad4c6d f4bff878

verified
Changed files
+33 -4
avatar
src
+33 -4
avatar/src/index.js
···
export default {
async fetch(request, env) {
const url = new URL(request.url);
const { pathname, searchParams } = url;
···
const profile = await profileResponse.json();
const avatar = profile.avatar;
-
if (!avatar) {
-
return new Response(`avatar not found for ${actor}.`, { status: 404 });
}
// Resize if requested
let avatarResponse;
if (resizeToTiny) {
-
avatarResponse = await fetch(avatar, {
cf: {
image: {
width: 32,
···
},
});
} else {
-
avatarResponse = await fetch(avatar);
}
if (!avatarResponse.ok) {
···
export default {
async fetch(request, env) {
+
// Helper function to generate a color from a string
+
const stringToColor = (str) => {
+
let hash = 0;
+
for (let i = 0; i < str.length; i++) {
+
hash = str.charCodeAt(i) + ((hash << 5) - hash);
+
}
+
let color = "#";
+
for (let i = 0; i < 3; i++) {
+
const value = (hash >> (i * 8)) & 0xff;
+
color += ("00" + value.toString(16)).substr(-2);
+
}
+
return color;
+
};
+
const url = new URL(request.url);
const { pathname, searchParams } = url;
···
const profile = await profileResponse.json();
const avatar = profile.avatar;
+
let avatarUrl = profile.avatar;
+
+
if (!avatarUrl) {
+
// Generate a random color based on the actor string
+
const bgColor = stringToColor(actor);
+
const size = resizeToTiny ? 32 : 128;
+
const svg = `<svg width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" xmlns="http://www.w3.org/2000/svg"><rect width="${size}" height="${size}" fill="${bgColor}"/></svg>`;
+
const svgData = new TextEncoder().encode(svg);
+
+
response = new Response(svgData, {
+
headers: {
+
"Content-Type": "image/svg+xml",
+
"Cache-Control": "public, max-age=43200",
+
},
+
});
+
await cache.put(cacheKey, response.clone());
+
return response;
}
// Resize if requested
let avatarResponse;
if (resizeToTiny) {
+
avatarResponse = await fetch(avatarUrl, {
cf: {
image: {
width: 32,
···
},
});
} else {
+
avatarResponse = await fetch(avatarUrl);
}
if (!avatarResponse.ok) {