Files
decePubClient/wwwroot/main.js
T
thepraandClaude Opus 5.5 70cb389537 Media survive failures: uploaded when attached, the server's limits, alt text asked for
The composer uploads each file as soon as it is attached (streamed from the browser file, under its own ten-minute
timeout), shows it uploading, processing (a 202 followed until ready), ready or failed with Retry, and cancels one
removed; publishing sends the ids, so a failed publish or a retried upload sends nothing twice. Size limits and types
come from /api/v2/instance, a file without a browser type gets one from its extension, and each refusal names the file
and why. Previews are 320 px thumbnails, not whole files in memory. A picture without a description is asked about once
("Publish anyway"; a setting turns it off), descriptions are sent with PUT, and an edit keeps the post's media, language
and sensitive flag.

Profile pictures are checked against the server's types and can be removed. A card shows "Media unavailable" with the
original's link when a picture fails, the blurhash behind a sensitive placeholder, and plays a GIF only on tap under
reduced motion. A failed export says so instead of downloading part of a file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw
2026-10-07 11:56:59 +02:00

237 lines
6.6 KiB
JavaScript

window.cascadingStateInstanceReference = (dotNetReference) => {
try {
window.cascadingStateInstance = dotNetReference
} catch (e) {
console.error(e)
}
}
window.logFromJs = (message, where) => {
try {
if (!window.cascadingStateInstance ||
typeof window.cascadingStateInstance.invokeMethodAsync != "function")
return
window.cascadingStateInstance.invokeMethodAsync('LogFromJs', message, where)
} catch (e) {
console.error(e)
}
}
window.isServiceWorkerAvailable = () => {
try {
return navigator.serviceWorker.valueOf().controller != null
} catch (e) {
console.error(e)
window.logFromJs(e.message, "isServiceWorkerAvailable")
}
}
window.getLanguage = () => {
try {
return navigator.language.substr(0, 2)
} catch (e) {
console.error(e)
window.logFromJs(e.message, "getLanguage")
}
}
window.setLanguage = (lang) => {
try {
navigator.language = lang
} catch (e) {
console.error(e)
window.logFromJs(e.message, "setLanguage")
}
}
window.isOnline = async () => {
try {
const isOnline = navigator.onLine
//const url = "https://contents.nuvola.xyz/api/assets/6d9f5e55-2034-456f-92e7-d9ec51066443"
//let isOnlineStatusCode = true
//try {
// const online = await fetch(url)
// isOnlineStatusCode = online.status >= 200 && online.status < 399
//} catch (_) {
// isOnlineStatusCode = false
//}
if (navigator.connection !== undefined &&
navigator.connection.effectiveType !== undefined &&
navigator.connection.type !== undefined) {
if (!isOnline)
return isOnline
return navigator.connection.type === "cellular" ?
navigator.connection.effectiveType.indexOf("2g") === -1 /*&& isOnlineStatusCode*/ :
true
} else
return isOnline //&& isOnlineStatusCode
} catch (e) {
console.error(e)
window.logFromJs(e.message, "isOnline")
}
}
window.clearCache = async () => {
try {
const cachesNames = await caches.keys()
for (let i = 0; i < cachesNames.length; i++)
await caches.delete(cachesNames[i])
} catch (e) {
console.error(e)
window.logFromJs(e.message, "clearCache")
}
}
window.clearLocalStorage = () => {
try {
localStorage.removeItem("PrivateCacheData")
localStorage.removeItem("AuthData")
} catch (e) {
console.error(e)
window.logFromJs(e.message, "clearLocalStorage")
}
}
window.reloadPage = () => {
try {
location.reload(true)
} catch (e) {
console.error(e)
window.logFromJs(e.message, "reloadPage")
}
}
window.getHeight = (classId) => {
try {
const elements = document.querySelectorAll(classId)
let px = 0
if (!elements) return 0
elements.forEach(v => px += v.offsetHeight)
return px
} catch (e) {
console.error(e)
window.logFromJs(e.message, "getHeight")
}
}
window.setAppBadge = (counter) => {
try {
navigator.setAppBadge(counter)
} catch (e) {
console.error(e)
window.logFromJs(e.message, "setAppBadge")
}
}
window.clearAppBadge = () => {
try {
navigator.clearAppBadge()
} catch (e) {
console.error(e)
window.logFromJs(e.message, "clearAppBadge")
}
}
// On a touch screen a tap on a post (not on its links, controls or menus, and not ending a text selection) opens its
// thread, through the card's own thread link (Content.razor, data-thread-link), which Blazor's router takes.
document.addEventListener("click", event => {
try {
if (window.matchMedia("(hover: hover)").matches || !(event.target instanceof Element))
return
const card = event.target.closest("[data-thread]")
if (!card || event.target.closest("a, button, input, textarea, select, label, summary, video, audio, [role=button], .dropdown, .modal, [data-sheet]"))
return
if (!window.getSelection()?.isCollapsed)
return
card.querySelector("a[data-thread-link]")?.click()
} catch (e) {
console.error(e)
window.logFromJs(e.message, "openThread")
}
})
// The Back button (a popstate) as opposed to the app's own navigation: Blazor's location-changing handlers can't tell
// them apart, and MainLayout lets Back close an open overlay but lets the app's navigation through. Registered for the
// capture phase, so it runs before Blazor's own listener.
let backAt = 0
window.addEventListener("popstate", () => backAt = Date.now(), { capture: true })
window.tookBack = () => {
const back = Date.now() - backAt < 1000
backAt = 0
return back
}
window.canHover = () => {
try {
return window.matchMedia("(hover: hover)").matches
} catch (e) {
console.error(e)
window.logFromJs(e.message, "canHover")
return true
}
}
// the selected tab of a sideways-scrolling strip, brought into its middle
window.revealActive = strip => {
try {
strip?.querySelector("[aria-selected=true]")?.scrollIntoView({ inline: "center", block: "nearest" })
} catch (e) {
console.error(e)
window.logFromJs(e.message, "revealActive")
}
}
// a phone: narrower than Tailwind's md, or held sideways (as short as the short: variant)
// the reader asked for less motion: what would move by itself (a gifv) waits for a tap
window.prefersReducedMotion = () => {
try {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches
} catch (e) {
console.error(e)
window.logFromJs(e.message, "prefersReducedMotion")
return false
}
}
window.isMobileMedia = () => {
try {
return window.matchMedia("(max-width: 767.98px), (max-height: 30rem)").matches
} catch (e) {
console.error(e)
window.logFromJs(e.message, "isMobileMedia")
}
}
window.canShareStuff = () => {
try {
return navigator.share !== undefined
} catch (e) {
console.error(e)
window.logFromJs(e.message, "canShareStuff")
}
}
window.shareTextUrl = (title, text, url) => {
try {
navigator.share({
title: title,
text: text,
url: url
})
} catch (e) {
console.error(e)
window.logFromJs(e.message, "shareTextUrl")
}
}
window.shareText = (title, text) => {
try {
navigator.share({
title: title,
text: text
})
} catch (e) {
console.error(e)
window.logFromJs(e.message, "shareText")
}
}// This device's position for located posts and the Nearby feed, asked only when the reader chooses one of them. It
// goes to PrivaPub only, which keeps located posts on the server and rounds what it stores.
window.feedPosition = () => new Promise(resolve => {
try {
if (!navigator.geolocation)
return resolve({ error: "unsupported" })
navigator.geolocation.getCurrentPosition(
position => resolve({ latitude: position.coords.latitude, longitude: position.coords.longitude, accuracy: position.coords.accuracy }),
error => resolve({ error: error.message || String(error.code) }),
{ enableHighAccuracy: false, timeout: 15000, maximumAge: 600000 })
} catch (e) {
console.error(e)
window.logFromJs(e.message, "feedPosition")
resolve({ error: e.message })
}
})