Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image refactor refactor #541

Merged
merged 8 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/typeDefs/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default gql`
setSettings(tipDefault: Int!, turboTipping: Boolean!, fiatCurrency: String!, noteItemSats: Boolean!,
noteEarning: Boolean!, noteAllDescendants: Boolean!, noteMentions: Boolean!, noteDeposits: Boolean!,
noteInvites: Boolean!, noteJobIndicator: Boolean!, noteCowboyHat: Boolean!, hideInvoiceDesc: Boolean!,
hideFromTopUsers: Boolean!, hideCowboyHat: Boolean!, clickToLoadImg: Boolean!,
hideFromTopUsers: Boolean!, hideCowboyHat: Boolean!, imgproxyOnly: Boolean!,
wildWestMode: Boolean!, greeterMode: Boolean!, nostrPubkey: String, nostrRelays: [String!], hideBookmarks: Boolean!,
noteForwardedSats: Boolean!, hideWalletBalance: Boolean!, hideIsContributor: Boolean!, diagnostics: Boolean!): User
setPhoto(photoId: ID!): Int!
Expand Down Expand Up @@ -89,7 +89,7 @@ export default gql`
hideWelcomeBanner: Boolean!
hideWalletBalance: Boolean!
diagnostics: Boolean!
clickToLoadImg: Boolean!
imgproxyOnly: Boolean!
wildWestMode: Boolean!
greeterMode: Boolean!
lastCheckedJobs: String
Expand Down
17 changes: 10 additions & 7 deletions components/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function ImageOriginal ({ src, topLevel, nofollow, tab, children, onClick, ...pr
const [showImage, setShowImage] = useState(false)

useEffect(() => {
if (me?.clickToLoadImg && tab !== 'preview') return
if (me?.imgproxyOnly && tab !== 'preview') return
// make sure it's not a false negative by trying to load URL as <img>
const img = new window.Image()
img.onload = () => setShowImage(true)
Expand All @@ -31,8 +31,7 @@ function ImageOriginal ({ src, topLevel, nofollow, tab, children, onClick, ...pr
}
}, [src, showImage])

if (showImage && (tab === 'preview' || !me?.clickToLoadImg)) {
// image is still processing and user is okay with loading original url automatically
if (showImage) {
return (
<img
className={topLevel ? styles.topLevel : undefined}
Expand All @@ -42,13 +41,17 @@ function ImageOriginal ({ src, topLevel, nofollow, tab, children, onClick, ...pr
/>
)
} else {
// image is still processing or user is not okay with loading original url automatically
// user is not okay with loading original url automatically or there was an error loading the image

// If element parsed by markdown is a raw URL, we use src as the text to not mislead users.
// This will not be the case if [text](url) format is used. Then we will show what was chosen as text.
const isRawURL = /^https?:\/\//.test(children?.[0])
return (
<a
target='_blank'
rel={`noreferrer ${nofollow ? 'nofollow' : ''} noopener`}
href={src}
>{children || src}
>{isRawURL ? src : children}
</a>
)
}
Expand Down Expand Up @@ -89,8 +92,8 @@ function ImageProxy ({ src, srcSet: srcSetObj, onClick, topLevel, onError, ...pr
export function ZoomableImage ({ src, srcSet, ...props }) {
const showModal = useShowModal()

// if `srcSet` is undefined, it means the image was not processed by worker yet
const [imgproxy, setImgproxy] = useState(srcSet || IMGPROXY_URL_REGEXP.test(src))
// if `srcSet` is falsy, it means the image was not processed by worker yet
const [imgproxy, setImgproxy] = useState(!!srcSet || IMGPROXY_URL_REGEXP.test(src))

// backwards compatibility:
// src may already be imgproxy url since we used to replace image urls with imgproxy urls
Expand Down
9 changes: 7 additions & 2 deletions components/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ export default memo(function Text ({ nofollow, imgproxyUrls, children, tab, ...o

const Img = useCallback(({ node, src, ...props }) => {
const url = IMGPROXY_URL_REGEXP.test(src) ? decodeOriginalUrl(src) : src
// if `srcSet` is undefined, it means the image was not processed by worker yet
// if `srcSet` is null, image was processed but this specific url was not detected as an image by the worker
const srcSet = imgproxyUrls?.[url]
return <ZoomableImage srcSet={srcSet} tab={tab} src={src} {...props} {...outerProps} />
}, [imgproxyUrls, outerProps, tab])
Expand All @@ -126,6 +124,13 @@ export default memo(function Text ({ nofollow, imgproxyUrls, children, tab, ...o
return <>{children}</>
}

// If [text](url) was parsed as <a> and text is not empty and not a link itself,
// we don't render it as an image since it was probably a concious choice to include text.
const text = children?.[0]
if (!!text && !/^https?:\/\//.test(text)) {
return <a target='_blank' rel={`noreferrer ${nofollow ? 'nofollow' : ''} noopener`} href={href}>{text}</a>
}

// assume the link is an image which will fallback to link if it's not
return <Img src={href} nofollow={nofollow} {...props}>{children}</Img>
},
Expand Down
8 changes: 4 additions & 4 deletions fragments/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const ME = gql`
hideInvoiceDesc
hideFromTopUsers
hideCowboyHat
clickToLoadImg
imgproxyOnly
diagnostics
wildWestMode
greeterMode
Expand Down Expand Up @@ -61,7 +61,7 @@ export const SETTINGS_FIELDS = gql`
hideCowboyHat
hideBookmarks
hideIsContributor
clickToLoadImg
imgproxyOnly
hideWalletBalance
diagnostics
nostrPubkey
Expand Down Expand Up @@ -91,14 +91,14 @@ ${SETTINGS_FIELDS}
mutation setSettings($tipDefault: Int!, $turboTipping: Boolean!, $fiatCurrency: String!, $noteItemSats: Boolean!,
$noteEarning: Boolean!, $noteAllDescendants: Boolean!, $noteMentions: Boolean!, $noteDeposits: Boolean!,
$noteInvites: Boolean!, $noteJobIndicator: Boolean!, $noteCowboyHat: Boolean!, $hideInvoiceDesc: Boolean!,
$hideFromTopUsers: Boolean!, $hideCowboyHat: Boolean!, $clickToLoadImg: Boolean!,
$hideFromTopUsers: Boolean!, $hideCowboyHat: Boolean!, $imgproxyOnly: Boolean!,
$wildWestMode: Boolean!, $greeterMode: Boolean!, $nostrPubkey: String, $nostrRelays: [String!], $hideBookmarks: Boolean!,
$noteForwardedSats: Boolean!, $hideWalletBalance: Boolean!, $hideIsContributor: Boolean!, $diagnostics: Boolean!) {
setSettings(tipDefault: $tipDefault, turboTipping: $turboTipping, fiatCurrency: $fiatCurrency,
noteItemSats: $noteItemSats, noteEarning: $noteEarning, noteAllDescendants: $noteAllDescendants,
noteMentions: $noteMentions, noteDeposits: $noteDeposits, noteInvites: $noteInvites,
noteJobIndicator: $noteJobIndicator, noteCowboyHat: $noteCowboyHat, hideInvoiceDesc: $hideInvoiceDesc,
hideFromTopUsers: $hideFromTopUsers, hideCowboyHat: $hideCowboyHat, clickToLoadImg: $clickToLoadImg,
hideFromTopUsers: $hideFromTopUsers, hideCowboyHat: $hideCowboyHat, imgproxyOnly: $imgproxyOnly,
wildWestMode: $wildWestMode, greeterMode: $greeterMode, nostrPubkey: $nostrPubkey, nostrRelays: $nostrRelays, hideBookmarks: $hideBookmarks,
noteForwardedSats: $noteForwardedSats, hideWalletBalance: $hideWalletBalance, hideIsContributor: $hideIsContributor, diagnostics: $diagnostics) {
...SettingsFields
Expand Down
16 changes: 13 additions & 3 deletions pages/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function Settings ({ ssrData }) {
hideInvoiceDesc: settings?.hideInvoiceDesc,
hideFromTopUsers: settings?.hideFromTopUsers,
hideCowboyHat: settings?.hideCowboyHat,
clickToLoadImg: settings?.clickToLoadImg,
imgproxyOnly: settings?.imgproxyOnly,
wildWestMode: settings?.wildWestMode,
greeterMode: settings?.greeterMode,
nostrPubkey: settings?.nostrPubkey ? bech32encode(settings.nostrPubkey) : '',
Expand Down Expand Up @@ -254,8 +254,18 @@ export default function Settings ({ ssrData }) {
groupClassName='mb-0'
/>}
<Checkbox
label={<>click to load external images</>}
name='clickToLoadImg'
label={
<div className='d-flex align-items-center'>only load images from proxy
<Info>
<ul className='fw-bold'>
<li>only load images from our image proxy automatically</li>
<li>this prevents IP address leaks to arbitrary sites</li>
<li>if we fail to load an image, the raw link will be shown</li>
</ul>
</Info>
</div>
}
name='imgproxyOnly'
groupClassName='mb-0'
/>
<Checkbox
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "users" RENAME COLUMN "clickToLoadImg" TO "imgproxyOnly";
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ model User {
fiatCurrency String @default("USD")
hideFromTopUsers Boolean @default(false)
turboTipping Boolean @default(false)
clickToLoadImg Boolean @default(false)
imgproxyOnly Boolean @default(false)
hideWalletBalance Boolean @default(false)
referrerId Int?
nostrPubkey String?
Expand Down