mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-12-06 19:56:45 +10:30
feat: cloud pfp new standers inlcuding uploading
This commit is contained in:
1 parent
d5cac1e5fa
commit
54ae1a1417
5 files changed
+265
-199
No files matched your search
@@ -9,16 +9,20 @@
|
||||
isUseCloudPfpEnabled,
|
||||
notifyProfilePictureChanged,
|
||||
pullCloudProfilePictureFromServer,
|
||||
revertCloudProfilePicture,
|
||||
syncLocalProfilePictureToCloud,
|
||||
uploadCloudProfilePicture,
|
||||
} from '@/seqta/utils/cloudPfpSync'
|
||||
|
||||
let value = $state<string | undefined>(undefined)
|
||||
let fileInput = $state<HTMLInputElement | undefined>(undefined)
|
||||
let cloudFileInput = $state<HTMLInputElement | undefined>(undefined)
|
||||
let dragging = $state(false)
|
||||
let blobUrl = $state<string | undefined>(undefined)
|
||||
let useCloudPfp = $state(false)
|
||||
let refreshingCloud = $state(false)
|
||||
let cloudRefreshError = $state<string | undefined>(undefined)
|
||||
let cloudState = $state(cloudAuth.state)
|
||||
let cloudBusy = $state(false)
|
||||
let cloudError = $state<string | undefined>(undefined)
|
||||
|
||||
const store = localforage.createInstance({
|
||||
name: 'profile-picture-store',
|
||||
@@ -39,34 +43,32 @@
|
||||
|
||||
onMount(() => {
|
||||
void load()
|
||||
const unsub = cloudAuth.subscribe((s) => { cloudState = s })
|
||||
const onStorage = (
|
||||
changes: Record<string, browser.Storage.StorageChange>,
|
||||
areaName: string,
|
||||
) => {
|
||||
if (areaName === 'local' && changes['plugin.profile-picture.settings']) {
|
||||
void load()
|
||||
}
|
||||
if (areaName === 'local' && changes['plugin.profile-picture.settings']) void load()
|
||||
}
|
||||
browser.storage.onChanged.addListener(onStorage)
|
||||
return () => browser.storage.onChanged.removeListener(onStorage)
|
||||
return () => {
|
||||
browser.storage.onChanged.removeListener(onStorage)
|
||||
unsub()
|
||||
}
|
||||
})
|
||||
|
||||
async function afterProfilePictureChange() {
|
||||
window.dispatchEvent(new Event('profile-picture-updated'))
|
||||
if (await isUseCloudPfpEnabled()) {
|
||||
await syncLocalProfilePictureToCloud()
|
||||
}
|
||||
if (await isUseCloudPfpEnabled()) await syncLocalProfilePictureToCloud()
|
||||
await notifyProfilePictureChanged()
|
||||
}
|
||||
|
||||
async function refreshCloudPfp() {
|
||||
cloudRefreshError = undefined
|
||||
refreshingCloud = true
|
||||
const result = await pullCloudProfilePictureFromServer()
|
||||
refreshingCloud = false
|
||||
if (!result.success) {
|
||||
cloudRefreshError = result.error ?? 'Could not refresh cloud photo'
|
||||
}
|
||||
async function runCloud(action: () => Promise<{ success: boolean; error?: string }>) {
|
||||
cloudError = undefined
|
||||
cloudBusy = true
|
||||
const result = await action()
|
||||
cloudBusy = false
|
||||
if (!result.success) cloudError = result.error
|
||||
}
|
||||
|
||||
function triggerSelect() {
|
||||
@@ -75,19 +77,22 @@
|
||||
|
||||
async function handleFiles(files: FileList | null) {
|
||||
const file = files?.[0]
|
||||
if (!file) return
|
||||
|
||||
if (blobUrl) {
|
||||
URL.revokeObjectURL(blobUrl)
|
||||
}
|
||||
if (!file?.type.startsWith('image/')) return
|
||||
|
||||
if (blobUrl) URL.revokeObjectURL(blobUrl)
|
||||
await store.setItem('profile-picture', file)
|
||||
const newBlobUrl = URL.createObjectURL(file)
|
||||
value = newBlobUrl
|
||||
blobUrl = newBlobUrl
|
||||
blobUrl = URL.createObjectURL(file)
|
||||
value = blobUrl
|
||||
await afterProfilePictureChange()
|
||||
}
|
||||
|
||||
async function onCloudFileChange() {
|
||||
const file = cloudFileInput?.files?.[0]
|
||||
if (cloudFileInput) cloudFileInput.value = ''
|
||||
if (!file) return
|
||||
await runCloud(() => uploadCloudProfilePicture(file))
|
||||
}
|
||||
|
||||
function onFileChange() {
|
||||
handleFiles(fileInput?.files || null)
|
||||
}
|
||||
@@ -105,27 +110,22 @@
|
||||
}
|
||||
value = undefined
|
||||
await store.removeItem('profile-picture')
|
||||
if (await isUseCloudPfpEnabled()) {
|
||||
await clearCloudProfilePicture()
|
||||
}
|
||||
if (await isUseCloudPfpEnabled()) await clearCloudProfilePicture()
|
||||
await afterProfilePictureChange()
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="flex flex-col items-end gap-1 shrink-0">
|
||||
<div class="flex items-center gap-2">
|
||||
{#if useCloudPfp}
|
||||
<CloudPfpAvatar
|
||||
user={cloudAuth.state.user}
|
||||
class="object-cover rounded-full size-10 shrink-0"
|
||||
/>
|
||||
{#if useCloudPfp && cloudState.isLoggedIn}
|
||||
<CloudPfpAvatar user={cloudState.user} class="object-cover rounded-full size-10 shrink-0" />
|
||||
<button
|
||||
type="button"
|
||||
class="flex justify-center items-center shrink-0 rounded-md size-8 bg-zinc-200 dark:bg-zinc-700 hover:bg-zinc-300 dark:hover:bg-zinc-600 disabled:opacity-50"
|
||||
disabled={refreshingCloud}
|
||||
disabled={cloudBusy}
|
||||
title="Refresh from cloud"
|
||||
aria-label="Refresh from cloud"
|
||||
onclick={() => refreshCloudPfp()}
|
||||
onclick={() => runCloud(pullCloudProfilePictureFromServer)}
|
||||
>
|
||||
<span class="text-lg font-IconFamily dark:text-white">{'\uec79'}</span>
|
||||
</button>
|
||||
@@ -171,7 +171,41 @@
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{#if cloudRefreshError}
|
||||
<p class="max-w-[12rem] text-xs text-right text-red-500">{cloudRefreshError}</p>
|
||||
|
||||
{#if cloudState.isLoggedIn}
|
||||
<div class="flex flex-wrap gap-2 justify-end items-center max-w-[14rem]">
|
||||
<button
|
||||
type="button"
|
||||
class="text-xs text-zinc-600 dark:text-zinc-300 hover:underline disabled:opacity-50"
|
||||
disabled={cloudBusy}
|
||||
onclick={() => cloudFileInput?.click()}
|
||||
>
|
||||
Cloud upload
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="text-xs text-zinc-600 dark:text-zinc-300 hover:underline disabled:opacity-50"
|
||||
disabled={cloudBusy || !cloudState.user?.pfpUrl}
|
||||
onclick={() => runCloud(clearCloudProfilePicture)}
|
||||
>
|
||||
Clear cloud
|
||||
</button>
|
||||
{#each cloudState.user?.pfpHistory ?? [] as entry (entry.id)}
|
||||
<button
|
||||
type="button"
|
||||
class="size-7 overflow-hidden rounded-full ring-1 ring-zinc-300 dark:ring-zinc-600 disabled:opacity-50"
|
||||
title="Restore"
|
||||
disabled={cloudBusy}
|
||||
onclick={() => runCloud(() => revertCloudProfilePicture(entry.id))}
|
||||
>
|
||||
<img src={entry.r2Key} alt="" class="object-cover size-full" />
|
||||
</button>
|
||||
{/each}
|
||||
<input type="file" accept="image/*" class="hidden" bind:this={cloudFileInput} onchange={onCloudFileChange} />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if cloudError}
|
||||
<p class="max-w-[12rem] text-xs text-right text-red-500">{cloudError}</p>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -29,7 +29,7 @@ const profilePicturePlugin: Plugin<typeof settings> = {
|
||||
id: "profile-picture",
|
||||
name: "Custom Profile Picture",
|
||||
description: "Use your own image in place of the profile icon",
|
||||
version: "1.2.0",
|
||||
version: "1.3.0",
|
||||
settings: settings,
|
||||
disableToggle: true,
|
||||
defaultEnabled: false,
|
||||
@@ -96,9 +96,7 @@ const profilePicturePlugin: Plugin<typeof settings> = {
|
||||
}
|
||||
|
||||
if (api.settings.useCloudPfp && cloudAuth.state.isLoggedIn) {
|
||||
const { pullCloudProfilePictureFromServer } = await import(
|
||||
"@/seqta/utils/cloudPfpSync"
|
||||
);
|
||||
const { pullCloudProfilePictureFromServer } = await import("@/seqta/utils/cloudPfpSync");
|
||||
await pullCloudProfilePictureFromServer();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
export async function OpenStorePage(): Promise<void> {
|
||||
async function openThemesDestination(
|
||||
view: "store" | "community",
|
||||
): Promise<void> {
|
||||
const [{ requestSettingsDestination }, { openSettingsPopup }] =
|
||||
await Promise.all([
|
||||
import("@/seqta/utils/settingsNavigation"),
|
||||
import("@/seqta/utils/setupSettingsButton"),
|
||||
]);
|
||||
requestSettingsDestination({ page: "themes", view: "store" });
|
||||
requestSettingsDestination({ page: "themes", view });
|
||||
await openSettingsPopup();
|
||||
}
|
||||
|
||||
export async function OpenStorePage(): Promise<void> {
|
||||
await openThemesDestination("store");
|
||||
}
|
||||
|
||||
export async function OpenCommunityThemesPage(): Promise<void> {
|
||||
await openThemesDestination("community");
|
||||
}
|
||||
@@ -25,6 +25,7 @@ export type CloudUser = {
|
||||
displayName?: string;
|
||||
pfpUrl?: string;
|
||||
pfpHash?: string | null;
|
||||
pfpHistory?: { id: string; r2Key: string; createdAt: number }[];
|
||||
admin_level?: number;
|
||||
signup_number?: number | null;
|
||||
badges?: CloudBadge[];
|
||||
|
||||
+179
-156
@@ -1,45 +1,138 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import localforage from "localforage";
|
||||
import { cloudAuth, type CloudUser } from "@/seqta/utils/CloudAuth";
|
||||
import {
|
||||
clearCloudPfpCache,
|
||||
pfpUrlWithHash,
|
||||
} from "@/seqta/utils/cloudPfpCache";
|
||||
import { clearCloudPfpCache, pfpUrlWithHash } from "@/seqta/utils/cloudPfpCache";
|
||||
|
||||
const ACCOUNTS_BASE = "https://accounts.betterseqta.org";
|
||||
const BSPLUS_PFP = `${ACCOUNTS_BASE}/api/bsplus/pfp`;
|
||||
const PLUGIN_SETTINGS_KEY = "plugin.profile-picture.settings";
|
||||
const UPLOAD_MAX_EDGE = 512;
|
||||
const MAX_SOURCE_BYTES_WEBP = 20 * 1024 * 1024;
|
||||
const MAX_SOURCE_BYTES_LEGACY = 10 * 1024 * 1024;
|
||||
|
||||
function webpExportSupported(): boolean {
|
||||
try {
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = 1;
|
||||
canvas.height = 1;
|
||||
return canvas.toDataURL("image/webp").startsWith("data:image/webp");
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function maxSourceBytes(): number {
|
||||
return webpExportSupported() ? MAX_SOURCE_BYTES_WEBP : MAX_SOURCE_BYTES_LEGACY;
|
||||
}
|
||||
|
||||
const profileStore = localforage.createInstance({
|
||||
name: "profile-picture-store",
|
||||
storeName: "profilePicture",
|
||||
});
|
||||
|
||||
/** Downscale before upload to reduce ingress (server still normalizes). */
|
||||
async function downscaleForUpload(blob: Blob, maxEdge = 512): Promise<Blob> {
|
||||
if (!blob.type.startsWith("image/")) return blob;
|
||||
type PfpPayload = {
|
||||
pfpUrl: string | null;
|
||||
pfpHash: string | null;
|
||||
pfpHistory: CloudUser["pfpHistory"];
|
||||
};
|
||||
|
||||
const bitmap = await createImageBitmap(blob);
|
||||
const maxSide = Math.max(bitmap.width, bitmap.height);
|
||||
if (maxSide <= maxEdge) {
|
||||
bitmap.close();
|
||||
return blob;
|
||||
async function parseJson(res: Response): Promise<Record<string, unknown>> {
|
||||
const text = await res.text();
|
||||
try {
|
||||
return text ? (JSON.parse(text) as Record<string, unknown>) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function parsePfpPayload(data: Record<string, unknown>): PfpPayload {
|
||||
const rawUrl = data.pfpUrl as string | null | undefined;
|
||||
const history = Array.isArray(data.pfpHistory) ? data.pfpHistory : [];
|
||||
const pfpHistory = history.flatMap((item) => {
|
||||
if (!item || typeof item !== "object") return [];
|
||||
const o = item as Record<string, unknown>;
|
||||
if (
|
||||
typeof o.id === "string" &&
|
||||
typeof o.r2Key === "string" &&
|
||||
typeof o.createdAt === "number"
|
||||
) {
|
||||
return [{ id: o.id, r2Key: o.r2Key, createdAt: o.createdAt }];
|
||||
}
|
||||
return [];
|
||||
});
|
||||
return {
|
||||
pfpUrl: rawUrl == null || rawUrl === "" ? null : rawUrl,
|
||||
pfpHash: (data.pfpHash as string | null | undefined) ?? null,
|
||||
pfpHistory,
|
||||
};
|
||||
}
|
||||
|
||||
function mergePfpIntoUser(user: CloudUser, pfp: PfpPayload): CloudUser {
|
||||
return {
|
||||
...user,
|
||||
pfpUrl: pfp.pfpUrl ? pfpUrlWithHash(pfp.pfpUrl, pfp.pfpHash) : undefined,
|
||||
pfpHash: pfp.pfpHash,
|
||||
pfpHistory: pfp.pfpHistory,
|
||||
};
|
||||
}
|
||||
|
||||
async function applyPfpPayload(data: Record<string, unknown>): Promise<void> {
|
||||
const user = cloudAuth.state.user;
|
||||
if (!user) return;
|
||||
await cloudAuth.setUser(mergePfpIntoUser(user, parsePfpPayload(data)));
|
||||
if (user.id) await clearCloudPfpCache(user.id);
|
||||
}
|
||||
|
||||
async function bsplusPfpRequest(
|
||||
path: string,
|
||||
init?: RequestInit,
|
||||
): Promise<{ ok: boolean; data: Record<string, unknown>; error?: string }> {
|
||||
const token = await cloudAuth.getStoredToken();
|
||||
if (!token) return { ok: false, data: {}, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
|
||||
const res = await fetch(`${BSPLUS_PFP}${path}`, {
|
||||
...init,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
});
|
||||
const data = await parseJson(res);
|
||||
if (!res.ok) {
|
||||
return {
|
||||
ok: false,
|
||||
data,
|
||||
error: (data.error as string) ?? `Request failed (${res.status})`,
|
||||
};
|
||||
}
|
||||
return { ok: true, data };
|
||||
}
|
||||
|
||||
async function prepareUploadFile(source: Blob): Promise<File> {
|
||||
if (!source.type.startsWith("image/")) throw new Error("Invalid file type");
|
||||
const limit = maxSourceBytes();
|
||||
if (source.size > limit) {
|
||||
const mb = Math.round(limit / (1024 * 1024));
|
||||
throw new Error(`File too large (max ${mb}MB)`);
|
||||
}
|
||||
|
||||
const scale = maxEdge / maxSide;
|
||||
const w = Math.max(1, Math.round(bitmap.width * scale));
|
||||
const h = Math.max(1, Math.round(bitmap.height * scale));
|
||||
const bitmap = await createImageBitmap(source);
|
||||
try {
|
||||
const size = Math.min(bitmap.width, bitmap.height);
|
||||
const sx = Math.round((bitmap.width - size) / 2);
|
||||
const sy = Math.round((bitmap.height - size) / 2);
|
||||
const canvas = new OffscreenCanvas(UPLOAD_MAX_EDGE, UPLOAD_MAX_EDGE);
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) throw new Error("Could not prepare image");
|
||||
ctx.drawImage(bitmap, sx, sy, size, size, 0, 0, UPLOAD_MAX_EDGE, UPLOAD_MAX_EDGE);
|
||||
|
||||
const canvas = new OffscreenCanvas(w, h);
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) {
|
||||
const webp = webpExportSupported();
|
||||
const mime = webp ? "image/webp" : "image/jpeg";
|
||||
const blob = await canvas.convertToBlob({ type: mime, quality: webp ? 0.88 : 0.85 });
|
||||
return new File([blob], webp ? "profile.webp" : "profile.jpg", { type: mime });
|
||||
} finally {
|
||||
bitmap.close();
|
||||
return blob;
|
||||
}
|
||||
ctx.drawImage(bitmap, 0, 0, w, h);
|
||||
bitmap.close();
|
||||
|
||||
const out = await canvas.convertToBlob({ type: "image/jpeg", quality: 0.85 });
|
||||
return out;
|
||||
}
|
||||
|
||||
export async function isUseCloudPfpEnabled(): Promise<boolean> {
|
||||
@@ -48,15 +141,6 @@ export async function isUseCloudPfpEnabled(): Promise<boolean> {
|
||||
return !!settings?.useCloudPfp;
|
||||
}
|
||||
|
||||
async function parseJsonResponse(r: Response): Promise<Record<string, unknown>> {
|
||||
const text = await r.text();
|
||||
try {
|
||||
return text ? (JSON.parse(text) as Record<string, unknown>) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function mergeMeIntoUser(current: CloudUser, data: Record<string, unknown>): CloudUser {
|
||||
const raw = (data.user as Record<string, unknown> | undefined) ?? data;
|
||||
const pfpUrlRaw = raw.pfpUrl as string | null | undefined;
|
||||
@@ -80,34 +164,30 @@ function mergeMeIntoUser(current: CloudUser, data: Record<string, unknown>): Clo
|
||||
};
|
||||
}
|
||||
|
||||
/** Fetch `/api/auth/me` and update stored user (pfpUrl / pfpHash). */
|
||||
export async function refreshCloudUserFromServer(): Promise<{
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
export async function refreshCloudUserFromServer(): Promise<{ success: boolean; error?: string }> {
|
||||
if (!cloudAuth.state.isLoggedIn) {
|
||||
return { success: false, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
}
|
||||
|
||||
const token = await cloudAuth.getStoredToken();
|
||||
if (!token) return { success: false, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
|
||||
const current = cloudAuth.state.user;
|
||||
if (!current?.id) return { success: false, error: "No cloud user on this device" };
|
||||
if (!token || !current?.id) {
|
||||
return { success: false, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`${ACCOUNTS_BASE}/api/auth/me`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
const data = await parseJsonResponse(res);
|
||||
const data = await parseJson(res);
|
||||
if (!res.ok) {
|
||||
return {
|
||||
success: false,
|
||||
error: (data.error as string) ?? `Could not refresh account (${res.status})`,
|
||||
};
|
||||
return { success: false, error: (data.error as string) ?? `Could not refresh account (${res.status})` };
|
||||
}
|
||||
|
||||
await cloudAuth.setUser(mergeMeIntoUser(current, data));
|
||||
let merged = mergeMeIntoUser(current, data);
|
||||
const pfp = await bsplusPfpRequest("");
|
||||
if (pfp.ok) merged = mergePfpIntoUser(merged, parsePfpPayload(pfp.data));
|
||||
await cloudAuth.setUser(merged);
|
||||
await clearCloudPfpCache(current.id);
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
@@ -117,138 +197,81 @@ export async function refreshCloudUserFromServer(): Promise<{
|
||||
}
|
||||
}
|
||||
|
||||
/** Pull cloud avatar metadata from the server and refresh the in-page profile image. */
|
||||
export async function pullCloudProfilePictureFromServer(): Promise<{
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
const refreshed = await refreshCloudUserFromServer();
|
||||
if (!refreshed.success) return refreshed;
|
||||
export async function pullCloudProfilePictureFromServer(): Promise<{ success: boolean; error?: string }> {
|
||||
const current = cloudAuth.state.user;
|
||||
if (!cloudAuth.state.isLoggedIn || !current?.id) {
|
||||
return { success: false, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
}
|
||||
|
||||
const userId = cloudAuth.state.user?.id;
|
||||
if (userId) await clearCloudPfpCache(userId);
|
||||
const pfp = await bsplusPfpRequest("");
|
||||
if (!pfp.ok) return { success: false, error: pfp.error ?? "Could not refresh cloud photo" };
|
||||
|
||||
await applyPfpPayload(pfp.data);
|
||||
await notifyProfilePictureChanged();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/** When cloud PFP is enabled: upload local image if present, otherwise pull from server. */
|
||||
export async function onUseCloudPfpToggled(enabled: boolean): Promise<void> {
|
||||
if (!enabled) {
|
||||
await notifyProfilePictureChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = await profileStore.getItem<Blob>("profile-picture");
|
||||
if (blob instanceof Blob) {
|
||||
await syncLocalProfilePictureToCloud();
|
||||
} else {
|
||||
await pullCloudProfilePictureFromServer();
|
||||
}
|
||||
if (blob instanceof Blob) await syncLocalProfilePictureToCloud();
|
||||
else await pullCloudProfilePictureFromServer();
|
||||
}
|
||||
|
||||
export async function syncLocalProfilePictureToCloud(): Promise<{
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
if (!(await isUseCloudPfpEnabled()) || !cloudAuth.state.isLoggedIn) {
|
||||
return { success: true };
|
||||
export async function uploadCloudProfilePicture(source: Blob): Promise<{ success: boolean; error?: string }> {
|
||||
if (!cloudAuth.state.isLoggedIn) {
|
||||
return { success: false, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
}
|
||||
|
||||
const token = await cloudAuth.getStoredToken();
|
||||
if (!token) return { success: false, error: "Not logged in" };
|
||||
|
||||
const user = cloudAuth.state.user;
|
||||
const userId = user?.id;
|
||||
|
||||
const blob = await profileStore.getItem<Blob>("profile-picture");
|
||||
|
||||
try {
|
||||
if (!blob || !(blob instanceof Blob)) {
|
||||
// No local upload — keep the server avatar; do not clear cloud.
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (!blob.type.startsWith("image/")) {
|
||||
return { success: false, error: "Invalid file type" };
|
||||
}
|
||||
if (blob.size > 5 * 1024 * 1024) {
|
||||
return { success: false, error: "File too large (max 5MB)" };
|
||||
}
|
||||
|
||||
const uploadBlob = await downscaleForUpload(blob);
|
||||
const formData = new FormData();
|
||||
formData.append("file", uploadBlob, "profile-picture.jpg");
|
||||
|
||||
const res = await fetch(`${ACCOUNTS_BASE}/api/user/pfp`, {
|
||||
method: "POST",
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
body: formData,
|
||||
});
|
||||
const data = await parseJsonResponse(res);
|
||||
if (!res.ok) {
|
||||
return { success: false, error: (data.error as string) ?? `Upload failed (${res.status})` };
|
||||
}
|
||||
|
||||
const pfpUrl = data.pfpUrl as string | undefined;
|
||||
const pfpHash = (data.pfpHash as string | null | undefined) ?? null;
|
||||
if (user && pfpUrl) {
|
||||
await cloudAuth.setUser({
|
||||
...user,
|
||||
pfpUrl: pfpUrlWithHash(pfpUrl, pfpHash),
|
||||
pfpHash,
|
||||
});
|
||||
}
|
||||
if (userId) await clearCloudPfpCache(userId);
|
||||
const file = await prepareUploadFile(source);
|
||||
const form = new FormData();
|
||||
form.append("file", file, file.name);
|
||||
form.append("clientProcessed", "true");
|
||||
form.append("preCropped", "true");
|
||||
const res = await bsplusPfpRequest("", { method: "POST", body: form });
|
||||
if (!res.ok) return { success: false, error: res.error ?? "Upload failed" };
|
||||
await applyPfpPayload(res.data);
|
||||
await notifyProfilePictureChanged();
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Cloud profile picture sync failed",
|
||||
};
|
||||
return { success: false, error: err instanceof Error ? err.message : "Upload failed" };
|
||||
}
|
||||
}
|
||||
|
||||
/** Upload local image to cloud, or clear cloud only when explicitly removing local image. */
|
||||
export async function clearCloudProfilePicture(): Promise<{
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
export async function syncLocalProfilePictureToCloud(): Promise<{ success: boolean; error?: string }> {
|
||||
if (!(await isUseCloudPfpEnabled()) || !cloudAuth.state.isLoggedIn) return { success: true };
|
||||
const blob = await profileStore.getItem<Blob>("profile-picture");
|
||||
if (!(blob instanceof Blob)) return { success: true };
|
||||
return uploadCloudProfilePicture(blob);
|
||||
}
|
||||
|
||||
export async function clearCloudProfilePicture(): Promise<{ success: boolean; error?: string }> {
|
||||
if (!cloudAuth.state.isLoggedIn) return { success: true };
|
||||
const res = await bsplusPfpRequest("/clear", { method: "POST" });
|
||||
if (!res.ok) return { success: false, error: res.error ?? "Clear failed" };
|
||||
await applyPfpPayload(res.data);
|
||||
await notifyProfilePictureChanged();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
const token = await cloudAuth.getStoredToken();
|
||||
if (!token) return { success: false, error: "Not logged in" };
|
||||
|
||||
const user = cloudAuth.state.user;
|
||||
const userId = user?.id;
|
||||
|
||||
try {
|
||||
const res = await fetch(`${ACCOUNTS_BASE}/api/user/pfp/clear`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
const data = await parseJsonResponse(res);
|
||||
if (!res.ok) {
|
||||
return { success: false, error: (data.error as string) ?? `Clear failed (${res.status})` };
|
||||
}
|
||||
if (user) {
|
||||
await cloudAuth.setUser({ ...user, pfpUrl: undefined, pfpHash: null });
|
||||
}
|
||||
if (userId) await clearCloudPfpCache(userId);
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Cloud profile picture clear failed",
|
||||
};
|
||||
export async function revertCloudProfilePicture(historyId: string): Promise<{ success: boolean; error?: string }> {
|
||||
if (!cloudAuth.state.isLoggedIn) {
|
||||
return { success: false, error: "Not signed in to BetterSEQTA Cloud" };
|
||||
}
|
||||
const res = await bsplusPfpRequest("/revert", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ historyId }),
|
||||
});
|
||||
if (!res.ok) return { success: false, error: res.error ?? "Revert failed" };
|
||||
await applyPfpPayload(res.data);
|
||||
await notifyProfilePictureChanged();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/** Notify SEQTA content scripts to refresh the in-page profile image. */
|
||||
export async function notifyProfilePictureChanged(): Promise<void> {
|
||||
const revision = Date.now();
|
||||
await browser.storage.local.set({ profile_picture_revision: revision });
|
||||
await browser.storage.local.set({ profile_picture_revision: Date.now() });
|
||||
}
|
||||
Reference in new issue
Block a user