From 2b4fb11cf6971697632e3c061814eb07db38f8d7 Mon Sep 17 00:00:00 2001 From: Echo Date: Thu, 25 Jun 2026 18:34:11 +0200 Subject: [PATCH] [Glitch] Status media attachments refactor Port 3f98bee57f25a025b613d62cb75194ef0cd01d81 to glitch-soc Signed-off-by: Claire --- .../glitch/actions/picture_in_picture.ts | 5 + .../flavours/glitch/features/audio/index.tsx | 15 +-- .../features/status/components/card.tsx | 93 +++++++++---------- .../flavours/glitch/features/video/index.tsx | 13 +-- .../flavours/glitch/models/account.ts | 8 ++ .../flavours/glitch/models/status.ts | 54 +++++++++-- .../glitch/reducers/picture_in_picture.ts | 10 +- .../flavours/glitch/selectors/accounts.ts | 11 ++- .../flavours/glitch/selectors/filters.ts | 10 +- .../flavours/glitch/selectors/statuses.ts | 87 ++++++++++++++++- 10 files changed, 218 insertions(+), 88 deletions(-) diff --git a/app/javascript/flavours/glitch/actions/picture_in_picture.ts b/app/javascript/flavours/glitch/actions/picture_in_picture.ts index 9ad88ae29b..0f70534842 100644 --- a/app/javascript/flavours/glitch/actions/picture_in_picture.ts +++ b/app/javascript/flavours/glitch/actions/picture_in_picture.ts @@ -29,3 +29,8 @@ export const deployPictureInPicture = createAppAsyncThunk( } }, ); + +export type DeployPictureInPictureCallback = ( + type: 'audio' | 'video', + props: PIPMediaProps, +) => void; diff --git a/app/javascript/flavours/glitch/features/audio/index.tsx b/app/javascript/flavours/glitch/features/audio/index.tsx index c07c6df20f..70ca5e5222 100644 --- a/app/javascript/flavours/glitch/features/audio/index.tsx +++ b/app/javascript/flavours/glitch/features/audio/index.tsx @@ -6,6 +6,7 @@ import classNames from 'classnames'; import { useSpring, animated, config } from '@react-spring/web'; +import type { DeployPictureInPictureCallback } from '@/flavours/glitch/actions/picture_in_picture'; import DownloadIcon from '@/material-icons/400-24px/download.svg?react'; import Forward5Icon from '@/material-icons/400-24px/forward_5-fill.svg?react'; import PauseIcon from '@/material-icons/400-24px/pause-fill.svg?react'; @@ -68,19 +69,7 @@ export const Audio: React.FC<{ startPlaying?: boolean; startVolume?: number; startMuted?: boolean; - deployPictureInPicture?: ( - type: string, - mediaProps: { - src: string; - muted: boolean; - volume: number; - currentTime: number; - poster?: string; - backgroundColor: string; - foregroundColor: string; - accentColor: string; - }, - ) => void; + deployPictureInPicture?: DeployPictureInPictureCallback; matchedFilters?: string[]; }> = ({ src, diff --git a/app/javascript/flavours/glitch/features/status/components/card.tsx b/app/javascript/flavours/glitch/features/status/components/card.tsx index 4595936cee..debf9fc291 100644 --- a/app/javascript/flavours/glitch/features/status/components/card.tsx +++ b/app/javascript/flavours/glitch/features/status/components/card.tsx @@ -1,9 +1,11 @@ -import { useCallback, useId, useState } from 'react'; +import { useCallback, useId, useMemo, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import classNames from 'classnames'; +import { isMap } from 'immutable'; + import DescriptionIcon from '@/material-icons/400-24px/description-fill.svg?react'; import OpenInNewIcon from '@/material-icons/400-24px/open_in_new.svg?react'; import PlayArrowIcon from '@/material-icons/400-24px/play_arrow-fill.svg?react'; @@ -12,7 +14,10 @@ import { Icon } from 'flavours/glitch/components/icon'; import { MoreFromAuthor } from 'flavours/glitch/components/more_from_author'; import { RelativeTimestamp } from 'flavours/glitch/components/relative_timestamp'; import { displayMedia, useBlurhash } from 'flavours/glitch/initial_state'; -import type { Card as CardType } from 'flavours/glitch/models/status'; +import type { + CardShape, + Card as CardType, +} from 'flavours/glitch/models/status'; import { decode as decodeIDNA } from 'flavours/glitch/utils/idna'; const getHostname = (url: string) => { @@ -52,27 +57,26 @@ const handleIframeUrl = (html: string, url: string, providerName: string) => { const hideAllMedia = displayMedia === 'hide_all'; interface CardProps { - card: CardType | null; + card: CardType | CardShape | null; sensitive?: boolean; } -const CardVideo: React.FC> = ({ card }) => ( +const CardVideo: React.FC<{ card: CardShape }> = ({ card }) => (
); -const Card: React.FC = ({ card, sensitive }) => { +const Card: React.FC = ({ card: rawCard, sensitive }) => { + const card: CardShape | null = useMemo( + () => (isMap(rawCard) ? (rawCard.toJS() as CardShape) : rawCard), + [rawCard], + ); + const [previewLoaded, setPreviewLoaded] = useState(false); const [embedded, setEmbedded] = useState(false); const [revealed, setRevealed] = useState(!sensitive && !hideAllMedia); @@ -102,52 +106,43 @@ const Card: React.FC = ({ card, sensitive }) => { } const provider = - card.get('provider_name').length === 0 - ? decodeIDNA(getHostname(card.get('url'))) - : card.get('provider_name'); - const interactive = card.get('type') === 'video'; - const language = card.get('language') ?? ''; - const hasImage = (card.get('image')?.length ?? 0) > 0; - const largeImage = - (hasImage && card.get('width') > card.get('height')) || interactive; - const showAuthor = !!card.getIn(['authors', 0, 'accountId']); + card.provider_name.length === 0 + ? decodeIDNA(getHostname(card.url)) + : card.provider_name; + const interactive = card.type === 'video'; + const language = card.language ?? ''; + const hasImage = (card.image?.length ?? 0) > 0; + const largeImage = (hasImage && card.width > card.height) || interactive; + const author = card.authors.at(0)?.accountId; const description = (
{provider} - {card.get('published_at') && ( + {card.published_at && ( <> {' '} - ·{' '} - + · )} - - {card.get('title')} + + {card.title} - {!showAuthor && - (card.get('author_name').length > 0 ? ( + {!author && + (card.author_name.length > 0 ? ( {card.get('author_name')} }} + values={{ name: {card.author_name} }} /> ) : ( - {card.get('description')} + {card.description} ))}
@@ -158,7 +153,7 @@ const Card: React.FC = ({ card, sensitive }) => { aspectRatio: '1', }; - if (largeImage && card.get('type') === 'video') { + if (largeImage && card.type === 'video') { thumbnailStyle.aspectRatio = `16 / 9`; } else if (largeImage) { thumbnailStyle.aspectRatio = '1.91 / 1'; @@ -171,15 +166,15 @@ const Card: React.FC = ({ card, sensitive }) => { className={classNames('status-card__image-preview', { 'status-card__image-preview--hidden': revealed && previewLoaded, })} - hash={card.get('blurhash')} + hash={card.blurhash} dummy={!useBlurhash} /> ); - const thumbnailDescription = card.get('image_description'); + const thumbnailDescription = card.image_description; const thumbnail = ( {thumbnailDescription} = ({ card, sensitive }) => { = ({ card, sensitive }) => { ); - } else if (card.get('image')) { + } else if (card.image) { embed = (
{canvas} @@ -286,10 +281,10 @@ const Card: React.FC = ({ card, sensitive }) => { return ( <> = ({ card, sensitive }) => { {description} - {showAuthor && ( - - )} + {author && } ); }; diff --git a/app/javascript/flavours/glitch/features/video/index.tsx b/app/javascript/flavours/glitch/features/video/index.tsx index 248ddb83c3..71831c9f74 100644 --- a/app/javascript/flavours/glitch/features/video/index.tsx +++ b/app/javascript/flavours/glitch/features/video/index.tsx @@ -7,6 +7,7 @@ import classNames from 'classnames'; import { useSpring, animated, config } from '@react-spring/web'; import { throttle } from 'lodash'; +import type { DeployPictureInPictureCallback } from '@/flavours/glitch/actions/picture_in_picture'; import Forward5Icon from '@/material-icons/400-24px/forward_5-fill.svg?react'; import FullscreenIcon from '@/material-icons/400-24px/fullscreen.svg?react'; import FullscreenExitIcon from '@/material-icons/400-24px/fullscreen_exit.svg?react'; @@ -174,15 +175,7 @@ export const Video: React.FC<{ alwaysVisible?: boolean; visible?: boolean; onToggleVisibility?: () => void; - deployPictureInPicture?: ( - type: string, - mediaProps: { - src: string; - muted: boolean; - volume: number; - currentTime: number; - }, - ) => void; + deployPictureInPicture?: DeployPictureInPictureCallback; blurhash?: string; startPlaying?: boolean; startTime?: number; @@ -245,7 +238,7 @@ export const Video: React.FC<{ (c: HTMLVideoElement | null) => { if (videoRef.current && !videoRef.current.paused && c === null) { deployPictureInPicture?.('video', { - src: src, + src, currentTime: videoRef.current.currentTime, muted: videoRef.current.muted, volume: videoRef.current.volume, diff --git a/app/javascript/flavours/glitch/models/account.ts b/app/javascript/flavours/glitch/models/account.ts index 1fe8341022..3d237ffe3f 100644 --- a/app/javascript/flavours/glitch/models/account.ts +++ b/app/javascript/flavours/glitch/models/account.ts @@ -55,6 +55,14 @@ export interface AccountShape extends Required< moved: string | null; url: string; } +export type AccountShapeFull = Omit< + AccountShape, + 'emojis' | 'fields' | 'roles' +> & { + emojis: CustomEmoji[]; + fields: AccountFieldShape[]; + roles: AccountRoleShape[]; +}; export type Account = RecordOf; diff --git a/app/javascript/flavours/glitch/models/status.ts b/app/javascript/flavours/glitch/models/status.ts index 45e0ae35fe..8325382c49 100644 --- a/app/javascript/flavours/glitch/models/status.ts +++ b/app/javascript/flavours/glitch/models/status.ts @@ -2,7 +2,15 @@ import type { RecordOf } from 'immutable'; import type { ApiCollectionJSON } from '@/flavours/glitch/api_types/collections'; import type { ApiCustomEmojiJSON } from '@/flavours/glitch/api_types/custom_emoji'; -import type { ApiMediaAttachmentJSON } from '@/flavours/glitch/api_types/media_attachments'; +import type { + ApiAudioAttachmentJSON, + ApiGifvAttachmentJSON, + ApiImageAttachmentJSON, + ApiMediaAttachmentJSON, + ApiUnknownAttachmentJSON, + ApiVideoAttachmentJSON, + MediaAttachmentType, +} from '@/flavours/glitch/api_types/media_attachments'; import type { ApiQuoteJSON, ApiQuotePolicyJSON, @@ -17,6 +25,8 @@ import type { StatusVisibility, } from '@/flavours/glitch/api_types/statuses'; +import type { AccountShapeFull } from './account'; + export type { StatusVisibility } from '@/flavours/glitch/api_types/statuses'; // Temporary until we type it correctly @@ -75,6 +85,10 @@ export interface StatusShape { replies_count: number; visibility: StatusVisibility; } +export type ExpandedStatusShape = Omit & { + account: AccountShapeFull; + reblog?: Omit; +}; export type CardShape = Omit & { authors: (Omit & { @@ -86,14 +100,42 @@ export type Card = RecordOf; export type MediaAttachment = Immutable.Map; -export type MediaAttachmentShape = Omit< - ApiMediaAttachmentJSON, - 'remote_url' -> & { +export type MediaAttachmentShape< + TAttachmentJSON extends ApiMediaAttachmentJSON = ApiMediaAttachmentJSON, +> = Omit & { remote_url: string | null; - translation?: string; + translation?: { + description: string; + }; }; +export function isMediaAttachmentOfType( + attachment: MediaAttachmentShape, + type: 'image', +): attachment is MediaAttachmentShape; +export function isMediaAttachmentOfType( + attachment: MediaAttachmentShape, + type: 'video', +): attachment is MediaAttachmentShape; +export function isMediaAttachmentOfType( + attachment: MediaAttachmentShape, + type: 'gifv', +): attachment is MediaAttachmentShape; +export function isMediaAttachmentOfType( + attachment: MediaAttachmentShape, + type: 'audio', +): attachment is MediaAttachmentShape; +export function isMediaAttachmentOfType( + attachment: MediaAttachmentShape, + type: 'unknown', +): attachment is MediaAttachmentShape; +export function isMediaAttachmentOfType( + attachment: MediaAttachmentShape, + type: MediaAttachmentType, +) { + return attachment.type === type; +} + export type CollectionAttachment = RecordOf; export type FilterResult = Omit & { diff --git a/app/javascript/flavours/glitch/reducers/picture_in_picture.ts b/app/javascript/flavours/glitch/reducers/picture_in_picture.ts index 6ad31d42bf..4176179042 100644 --- a/app/javascript/flavours/glitch/reducers/picture_in_picture.ts +++ b/app/javascript/flavours/glitch/reducers/picture_in_picture.ts @@ -11,10 +11,10 @@ export interface PIPMediaProps { muted: boolean; volume: number; currentTime: number; - poster: string; - backgroundColor: string; - foregroundColor: string; - accentColor: string; + poster?: string; + backgroundColor?: string; + foregroundColor?: string; + accentColor?: string; } interface PIPStateWithValue extends Partial { @@ -34,7 +34,7 @@ const initialState = { muted: false, volume: 0, currentTime: 0, -}; +} satisfies PIPStateEmpty; export const pictureInPictureReducer: Reducer = ( state = initialState, diff --git a/app/javascript/flavours/glitch/selectors/accounts.ts b/app/javascript/flavours/glitch/selectors/accounts.ts index aec7668e3a..0a3252b68b 100644 --- a/app/javascript/flavours/glitch/selectors/accounts.ts +++ b/app/javascript/flavours/glitch/selectors/accounts.ts @@ -3,7 +3,11 @@ import { Record as ImmutableRecord, List as ImmutableList } from 'immutable'; import { me } from 'flavours/glitch/initial_state'; import { accountDefaultValues } from 'flavours/glitch/models/account'; -import type { Account, AccountShape } from 'flavours/glitch/models/account'; +import type { + Account, + AccountShape, + AccountShapeFull, +} from 'flavours/glitch/models/account'; import type { Relationship } from 'flavours/glitch/models/relationship'; import { createAppSelector } from 'flavours/glitch/store'; import type { RootState } from 'flavours/glitch/store'; @@ -50,6 +54,11 @@ export function makeGetAccount() { ); } +export const selectPlainAccount = createAppSelector( + [(state, accountId: string) => state.accounts.get(accountId)], + (account) => (account ? (account.toJS() as AccountShapeFull) : null), +); + export const getAccountHidden = createAppSelector( [ (state, id: string) => state.accounts.get(id)?.hidden, diff --git a/app/javascript/flavours/glitch/selectors/filters.ts b/app/javascript/flavours/glitch/selectors/filters.ts index 5d7bfcbdc7..5d5181274c 100644 --- a/app/javascript/flavours/glitch/selectors/filters.ts +++ b/app/javascript/flavours/glitch/selectors/filters.ts @@ -3,6 +3,14 @@ import { createSelector } from '@reduxjs/toolkit'; import type { RootState } from 'flavours/glitch/store'; import { toServerSideType } from 'flavours/glitch/utils/filters'; +export interface FilterShape { + id: string; + title: string; + context: string[]; + expires_at: string | null; + filter_action: 'hide' | 'blur' | 'warn'; +} + // TODO: move to `app/javascript/flavours/glitch/models` and use more globally type Filter = Immutable.Map; @@ -12,7 +20,7 @@ type FilterResult = Immutable.Map; export const getFilters = createSelector( [ (state: RootState) => state.filters as Immutable.Map, - (_, { contextType }: { contextType: string }) => contextType, + (_, { contextType }: { contextType?: string }) => contextType, ], (filters, contextType) => { if (!contextType) { diff --git a/app/javascript/flavours/glitch/selectors/statuses.ts b/app/javascript/flavours/glitch/selectors/statuses.ts index 30ed451bea..80edefcf59 100644 --- a/app/javascript/flavours/glitch/selectors/statuses.ts +++ b/app/javascript/flavours/glitch/selectors/statuses.ts @@ -2,7 +2,11 @@ import type { OrderedSet as ImmutableOrderedSet } from 'immutable'; import { createAppSelector } from 'flavours/glitch/store'; -import type { StatusShape } from '../models/status'; +import type { ExpandedStatusShape, StatusShape } from '../models/status'; + +import { selectPlainAccount } from './accounts'; +import type { FilterShape } from './filters'; +import { getFilters } from './filters'; export const getStatusList = createAppSelector( [ @@ -21,3 +25,84 @@ export const selectPlainStatus = createAppSelector( return status.toJS() as unknown as StatusShape; }, ); + +export const selectAccountStatus = createAppSelector( + [ + selectPlainStatus, + (state, statusId: string) => { + const accountId = state.statuses.getIn(statusId, 'account'); + if (typeof accountId !== 'string') { + return null; + } + return selectPlainAccount(state, accountId); + }, + ], + (status, account) => { + if (!status || !account) { + return null; + } + return { + ...status, + account, + }; + }, +); + +export const selectExpandedStatus = createAppSelector( + [ + selectAccountStatus, + (state, statusId: string) => { + const reblogId = state.statuses.getIn(statusId, 'reblog'); + if (typeof reblogId !== 'string') { + return null; + } + return selectAccountStatus(state, reblogId); + }, + ], + (status, reblog): ExpandedStatusShape | null => { + if (!status) { + return null; + } + + return { + ...status, + reblog: reblog ?? undefined, + }; + }, +); + +export const selectPictureInPicture = createAppSelector( + [ + (state, statusId: string) => + state.picture_in_picture.type !== null && + state.picture_in_picture.statusId === statusId, + (state) => state.meta.get('layout') !== 'mobile', + ], + (inUse, available) => ({ + inUse: inUse && available, + available, + }), +); + +export const selectMediaMatchFilters = createAppSelector( + [ + (state, { statusId }: { statusId: string }) => + selectPlainStatus(state, statusId), + getFilters, + ], + (status, immutableFilters) => { + const filters = immutableFilters + ? (immutableFilters.toJS() as unknown as Record) + : null; + const mediaFilters: string[] = []; + if (status?.filtered && filters) { + for (const { filter } of status.filtered) { + if (filters[filter]?.filter_action === 'blur') { + mediaFilters.push(filters[filter].title); + } + } + } + + return mediaFilters; + }, +);