-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a8581f
commit f283ebb
Showing
3 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as DB from "./db.js"; | ||
{ | ||
const DEFAULT_AVATAR_PATHS = [ | ||
|
||
].map(s => `"/data/img/avatars/"${s}`); | ||
|
||
/** @param {string} id The user's UID */ | ||
function getDefaultAvatarPath(id) { | ||
let counter = 0; | ||
for(let i = 0; i < id.length; i++) { | ||
counter += id.charCodeAt(i); | ||
} | ||
const index = counter % DEFAULT_AVATAR_PATHS.length; | ||
return DEFAULT_AVATAR_PATHS[index]; | ||
} | ||
|
||
/** @param {string} id The user's UID */ | ||
async function getDefaultAvatar(id) { | ||
const path = getDefaultAvatarPath(id); | ||
const response = await fetch(path); | ||
if(!response.ok) { | ||
console.error("Failed to fetch default profile picture: ", response); | ||
return null; | ||
} | ||
return await response.blob(); | ||
} | ||
|
||
/** @param {string} id The user's UID */ | ||
async function getAvatar(id) { | ||
try { | ||
const avatar = await DB.getAvatarById(id); | ||
if(avatar) return avatar; | ||
} catch (err) { | ||
console.warn(`Failed to fetch avatar for ${id}: ${err}`); | ||
} | ||
return await getDefaultAvatar(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.