From a7e80c62615233a305fdb9f39e1e0451f04e987f Mon Sep 17 00:00:00 2001 From: Echo Date: Fri, 12 Jun 2026 11:33:47 +0200 Subject: [PATCH 01/10] Refactor: Typing statuses (#39374) --- .../mastodon/actions/importer/statuses.ts | 187 ++++++++++++++++++ .../mastodon/api_types/media_attachments.ts | 81 +++++++- app/javascript/mastodon/api_types/statuses.ts | 19 +- app/javascript/mastodon/models/status.ts | 96 ++++++++- app/javascript/mastodon/selectors/statuses.ts | 22 ++- app/javascript/testing/factories.ts | 3 +- 6 files changed, 388 insertions(+), 20 deletions(-) create mode 100644 app/javascript/mastodon/actions/importer/statuses.ts diff --git a/app/javascript/mastodon/actions/importer/statuses.ts b/app/javascript/mastodon/actions/importer/statuses.ts new file mode 100644 index 0000000000..7753844953 --- /dev/null +++ b/app/javascript/mastodon/actions/importer/statuses.ts @@ -0,0 +1,187 @@ +import escapeTextContentForBrowser from 'escape-html'; + +import type { ApiMediaAttachmentJSON } from '@/mastodon/api_types/media_attachments'; +import type { + ApiFilterResultJSON, + ApiStatusJSON, +} from '@/mastodon/api_types/statuses'; +import type { + FilterResult, + MediaAttachmentShape, + StatusShape, +} from '@/mastodon/models/status'; + +const domParser = new DOMParser(); + +export function normalizeStatus( + status: ApiStatusJSON, + normalOldStatus?: StatusShape, + { bogusQuotePolicy = false, expandSpoilers = false } = {}, +) { + const normalStatus: StatusShape = { + hidden: normalOldStatus?.hidden ?? false, + collapsed: normalOldStatus?.collapsed ?? false, + content: '', + contentHtml: '', + muted: false, + pinned: false, + bookmarked: false, + favourited: false, + quote_approval: null, + reblogged: false, + + ...status, + + account: status.account.id, + media_attachments: [], + poll: status.poll?.id, + reblog: status.reblog?.id, + quote: undefined, + filtered: [], + application: { + name: 'Web', + ...status.application, + }, + }; + + if (bogusQuotePolicy) { + normalStatus.quote_approval = null; + } + + if (status.quote?.quoted_status) { + normalStatus.quote = { + ...status.quote, + quoted_status: status.quote.quoted_status.id, + }; + } + + if (status.card) { + normalStatus.card = { + ...status.card, + authors: status.card.authors.map((author) => ({ + name: author.name, + url: author.url, + accountId: author.account?.id, + })), + }; + } + + if (status.filtered) { + normalStatus.filtered = status.filtered.map(normalizeFilterResult); + } + + // Only calculate these values when status first encountered and + // when the underlying values change. Otherwise keep the ones + // already in the reducer + if ( + normalOldStatus?.content === normalStatus.content && + normalOldStatus.spoiler_text === normalStatus.spoiler_text + ) { + normalStatus.search_index = normalOldStatus.search_index; + normalStatus.contentHtml = normalOldStatus.contentHtml; + normalStatus.spoilerHtml = normalOldStatus.spoilerHtml; + normalStatus.spoiler_text = normalOldStatus.spoiler_text; + normalStatus.hidden = normalOldStatus.hidden; + + if (normalOldStatus.translation) { + normalStatus.translation = normalOldStatus.translation; + } + } else { + // If the status has a CW but no contents, treat the CW as if it were the + // status' contents, to avoid having a CW toggle with seemingly no effect. + if ( + normalStatus.spoiler_text && + !normalStatus.content && + !normalStatus.quote + ) { + normalStatus.content = normalStatus.spoiler_text; + normalStatus.spoiler_text = ''; + } + + const spoilerText = normalStatus.spoiler_text ?? ''; + const searchContent = [spoilerText, status.content] + .concat( + status.poll?.options + ? status.poll.options.map((option) => option.title) + : [], + ) + .concat(status.media_attachments.map((att) => att.description)) + .join('\n\n') + .replace(//g, '\n') + .replace(/<\/p>

/g, '\n\n'); + + normalStatus.search_index = domParser.parseFromString( + searchContent, + 'text/html', + ).documentElement.textContent; + normalStatus.contentHtml = normalStatus.content; + normalStatus.spoilerHtml = escapeTextContentForBrowser(spoilerText); + normalStatus.hidden = expandSpoilers + ? false + : spoilerText.length > 0 || normalStatus.sensitive; + + // Remove quote fallback link from the DOM so it doesn't mess with paragraph margins + if (normalStatus.quote && normalStatus.contentHtml) { + normalStatus.contentHtml = stripQuoteFallback(normalStatus.contentHtml); + } + + if ( + normalStatus.url && + !( + normalStatus.url.startsWith('http://') || + normalStatus.url.startsWith('https://') + ) + ) { + normalStatus.url = null; + } + + normalStatus.url ??= normalStatus.uri; + + normalStatus.media_attachments = status.media_attachments.map( + (attachment) => + normalizeMediaAttachment( + attachment, + normalOldStatus?.media_attachments, + ), + ); + } + + if (normalOldStatus) { + normalStatus.quote_approval ??= normalOldStatus.quote_approval; + } + + return normalStatus; +} + +function normalizeMediaAttachment( + attachment: ApiMediaAttachmentJSON, + oldAttachments?: MediaAttachmentShape[], +): MediaAttachmentShape { + const { remote_url } = attachment; + return { + ...attachment, + remote_url: + remote_url && /^https?:\/\//.test(remote_url) ? remote_url : null, + translation: oldAttachments?.find( + (oldAttachment) => + oldAttachment.id === attachment.id && + oldAttachment.description === attachment.description, + )?.translation, + }; +} + +function normalizeFilterResult(input: ApiFilterResultJSON): FilterResult { + return { + ...input, + filter: input.filter.id, + }; +} + +function stripQuoteFallback(text: string) { + const wrapper = document.createElement('div'); + wrapper.innerHTML = text; + + wrapper.querySelector('.quote-inline')?.remove(); + + return wrapper.innerHTML; +} diff --git a/app/javascript/mastodon/api_types/media_attachments.ts b/app/javascript/mastodon/api_types/media_attachments.ts index fc027ccd2a..e4cfe95881 100644 --- a/app/javascript/mastodon/api_types/media_attachments.ts +++ b/app/javascript/mastodon/api_types/media_attachments.ts @@ -7,16 +7,85 @@ export type MediaAttachmentType = | 'unknown' | 'audio'; -export interface ApiMediaAttachmentJSON { +interface BaseApiMediaAttachmentJSON { id: string; type: MediaAttachmentType; url: string; preview_url: string; - remoteUrl: string; - preview_remote_url: string; - text_url: string; - // TODO: how to define this? - meta: unknown; + remote_url?: string; + preview_remote_url?: string; + text_url?: string; description?: string; blurhash: string; } + +interface ApiImageAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'image'; + meta: { + original: ApiImageAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + }; +} + +interface ApiAudioAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'audio'; + meta: { + colors: ApiColorsAttachmentMetaJSON; + original: ApiVideoAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + }; +} + +interface ApiVideoAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'video'; + meta: { + colors: ApiColorsAttachmentMetaJSON; + original: ApiVideoAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + focus: { + x: number; + y: number; + }; + }; +} + +interface ApiGifvAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'gifv'; + meta: { + original: ApiVideoAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + }; +} + +interface ApiUnknownAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'unknown'; + meta: unknown; +} + +export type ApiMediaAttachmentJSON = + | ApiImageAttachmentJSON + | ApiAudioAttachmentJSON + | ApiVideoAttachmentJSON + | ApiGifvAttachmentJSON + | ApiUnknownAttachmentJSON; + +export interface ApiImageAttachmentMetaJSON { + width: number; + height: number; + size: string; + aspect: number; +} + +export interface ApiVideoAttachmentMetaJSON { + width: number; + height: number; + frame_rate: string; + duration: number; + bitrate: number; +} + +export interface ApiColorsAttachmentMetaJSON { + background: string; + foreground: string; + accent: string; +} diff --git a/app/javascript/mastodon/api_types/statuses.ts b/app/javascript/mastodon/api_types/statuses.ts index d61d8ceed0..1d5353fd4f 100644 --- a/app/javascript/mastodon/api_types/statuses.ts +++ b/app/javascript/mastodon/api_types/statuses.ts @@ -1,6 +1,7 @@ // See app/serializers/rest/status_serializer.rb import type { ApiAccountJSON } from './accounts'; +import type { ApiCollectionJSON } from './collections'; import type { ApiCustomEmojiJSON } from './custom_emoji'; import type { ApiMediaAttachmentJSON } from './media_attachments'; import type { ApiPollJSON } from './polls'; @@ -94,11 +95,11 @@ export interface ApiStatusJSON { url: string; replies_count: number; reblogs_count: number; - favorites_count: number; + favourites_count: number; quotes_count: number; edited_at?: string; - favorited?: boolean; + favourited?: boolean; reblogged?: boolean; muted?: boolean; bookmarked?: boolean; @@ -116,6 +117,7 @@ export interface ApiStatusJSON { tags: ApiTagJSON[]; emojis: ApiCustomEmojiJSON[]; + tagged_collections: ApiCollectionJSON[]; card?: ApiPreviewCardJSON; poll?: ApiPollJSON; @@ -134,6 +136,19 @@ export interface ApiStatusSourceJSON { spoiler_text: string; } +export interface ApiStatusTranslationJSON { + detected_source_language: string; + language: string; + provider: string; + contentHtml: string; + spoilerHtml: string; + spoiler_text: string; + poll?: { + id: string; + options: { title: string }[]; + }; +} + export function isStatusVisibility( visibility: string, ): visibility is StatusVisibility { diff --git a/app/javascript/mastodon/models/status.ts b/app/javascript/mastodon/models/status.ts index 668546ea26..dc4138330f 100644 --- a/app/javascript/mastodon/models/status.ts +++ b/app/javascript/mastodon/models/status.ts @@ -1,15 +1,103 @@ import type { RecordOf } from 'immutable'; -import type { ApiCollectionJSON } from 'mastodon/api_types/collections'; -import type { ApiPreviewCardJSON } from 'mastodon/api_types/statuses'; +import type { ApiCollectionJSON } from '@/mastodon/api_types/collections'; +import type { ApiCustomEmojiJSON } from '@/mastodon/api_types/custom_emoji'; +import type { ApiMediaAttachmentJSON } from '@/mastodon/api_types/media_attachments'; +import type { + ApiQuoteJSON, + ApiQuotePolicyJSON, +} from '@/mastodon/api_types/quotes'; +import type { + ApiFilterResultJSON, + ApiMentionJSON, + ApiPreviewCardAuthorJSON, + ApiPreviewCardJSON, + ApiStatusTranslationJSON, + ApiTagJSON, + StatusVisibility, +} from '@/mastodon/api_types/statuses'; -export type { StatusVisibility } from 'mastodon/api_types/statuses'; +export type { StatusVisibility } from '@/mastodon/api_types/statuses'; // Temporary until we type it correctly export type Status = Immutable.Map; -export type Card = RecordOf; +export interface StatusShape { + id: string; + account: string; + created_at: string; + edited_at?: string; + application: { + name: string; + website?: string; + }; + hidden: boolean; + language: string; + muted: boolean; + pinned: boolean; + filtered: FilterResult[]; + sensitive: boolean; + collapsed: boolean; + uri: string; + url: string | null; + + // Content + content: string; + contentHtml: string; + in_reply_to_account_id?: string; + in_reply_to_id?: string; + search_index?: string; + spoilerHtml?: string; + spoiler_text?: string; + + // Embeds + card?: CardShape; + emojis: Pick[]; + media_attachments: MediaAttachmentShape[]; + mentions: ApiMentionJSON[]; + poll?: string; + quote?: Omit & { + quoted_status?: string; + }; + reblog?: string; + tagged_collections: ApiCollectionJSON[]; + tags: ApiTagJSON[]; + translation?: StatusTranslation; + + // Interactions + bookmarked: boolean; + favourited: boolean; + favourites_count: number; + quote_approval: ApiQuotePolicyJSON | null; + quotes_count: number; + reblogged: boolean; + reblogs_count: number; + replies_count: number; + visibility: StatusVisibility; +} + +export type CardShape = Omit & { + authors: (Omit & { + accountId?: string; + })[]; +}; + +export type Card = RecordOf; export type MediaAttachment = Immutable.Map; +export type MediaAttachmentShape = Omit< + ApiMediaAttachmentJSON, + 'remote_url' +> & { + remote_url: string | null; + translation?: string; +}; + export type CollectionAttachment = RecordOf; + +export type FilterResult = Omit & { + filter: string; +}; + +export type StatusTranslation = Omit; diff --git a/app/javascript/mastodon/selectors/statuses.ts b/app/javascript/mastodon/selectors/statuses.ts index 4d045e924a..dde0b8b227 100644 --- a/app/javascript/mastodon/selectors/statuses.ts +++ b/app/javascript/mastodon/selectors/statuses.ts @@ -1,15 +1,23 @@ -import { createSelector } from '@reduxjs/toolkit'; import type { OrderedSet as ImmutableOrderedSet } from 'immutable'; -import type { RootState } from 'mastodon/store'; +import { createAppSelector } from 'mastodon/store'; -export const getStatusList = createSelector( +import type { StatusShape } from '../models/status'; + +export const getStatusList = createAppSelector( [ - ( - state: RootState, - type: 'favourites' | 'bookmarks' | 'pins' | 'trending', - ) => + (state, type: 'favourites' | 'bookmarks' | 'pins' | 'trending') => state.status_lists.getIn([type, 'items']) as ImmutableOrderedSet, ], (items) => items.toList(), ); + +export const selectPlainStatus = createAppSelector( + [(state, statusId: string) => state.statuses.get(statusId)], + (status) => { + if (!status) { + return null; + } + return status.toJS() as unknown as StatusShape; + }, +); diff --git a/app/javascript/testing/factories.ts b/app/javascript/testing/factories.ts index 8ac03ec2ad..e28f461368 100644 --- a/app/javascript/testing/factories.ts +++ b/app/javascript/testing/factories.ts @@ -80,12 +80,13 @@ export const statusFactory: FactoryFunction = ({ replies_count: 0, reblogs_count: 0, quotes_count: 0, - favorites_count: 0, + favourites_count: 0, account: accountFactory(), media_attachments: [], mentions: [], tags: [], emojis: [], + tagged_collections: [], contentHtml: data.text ?? '

This is a test status.

', ...data, }); From bbf4bd0162de3b61eace111f023c108238e45434 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Fri, 12 Jun 2026 12:39:07 +0200 Subject: [PATCH 02/10] Update unhead monorepo to v3.1.4 (#39316) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --- yarn.lock | 307 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 272 insertions(+), 35 deletions(-) diff --git a/yarn.lock b/yarn.lock index 48ba75487f..ec58a79932 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1897,6 +1897,21 @@ __metadata: languageName: node linkType: hard +"@devframes/hub@npm:^0.5.2": + version: 0.5.4 + resolution: "@devframes/hub@npm:0.5.4" + dependencies: + birpc: "npm:^4.0.0" + nostics: "npm:^0.2.0" + pathe: "npm:^2.0.3" + perfect-debounce: "npm:^2.1.0" + tinyexec: "npm:^1.2.2" + peerDependencies: + devframe: 0.5.4 + checksum: 10c0/335f3f8510e11b564cc6af3e0b7783c311ab3682fa4e1c0aea4fad4ca151de0c8cd7fe0d3e058aa4b3f235fa6630816d279669ad9ef7b139bbb866f8c70bfd7e + languageName: node + linkType: hard + "@dnd-kit/accessibility@npm:^3.1.1": version: 3.1.1 resolution: "@dnd-kit/accessibility@npm:3.1.1" @@ -3224,6 +3239,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-android-arm-eabi@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-android-arm-eabi@npm:0.134.0" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + "@oxc-parser/binding-android-arm64@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-android-arm64@npm:0.132.0" @@ -3231,6 +3253,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-android-arm64@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-android-arm64@npm:0.134.0" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + "@oxc-parser/binding-darwin-arm64@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-darwin-arm64@npm:0.132.0" @@ -3238,6 +3267,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-darwin-arm64@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-darwin-arm64@npm:0.134.0" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + "@oxc-parser/binding-darwin-x64@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-darwin-x64@npm:0.132.0" @@ -3245,6 +3281,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-darwin-x64@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-darwin-x64@npm:0.134.0" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + "@oxc-parser/binding-freebsd-x64@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-freebsd-x64@npm:0.132.0" @@ -3252,6 +3295,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-freebsd-x64@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-freebsd-x64@npm:0.134.0" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + "@oxc-parser/binding-linux-arm-gnueabihf@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-arm-gnueabihf@npm:0.132.0" @@ -3259,6 +3309,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-arm-gnueabihf@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-arm-gnueabihf@npm:0.134.0" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + "@oxc-parser/binding-linux-arm-musleabihf@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-arm-musleabihf@npm:0.132.0" @@ -3266,6 +3323,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-arm-musleabihf@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-arm-musleabihf@npm:0.134.0" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + "@oxc-parser/binding-linux-arm64-gnu@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-arm64-gnu@npm:0.132.0" @@ -3273,6 +3337,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-arm64-gnu@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-arm64-gnu@npm:0.134.0" + conditions: os=linux & cpu=arm64 & libc=glibc + languageName: node + linkType: hard + "@oxc-parser/binding-linux-arm64-musl@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-arm64-musl@npm:0.132.0" @@ -3280,6 +3351,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-arm64-musl@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-arm64-musl@npm:0.134.0" + conditions: os=linux & cpu=arm64 & libc=musl + languageName: node + linkType: hard + "@oxc-parser/binding-linux-ppc64-gnu@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-ppc64-gnu@npm:0.132.0" @@ -3287,6 +3365,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-ppc64-gnu@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-ppc64-gnu@npm:0.134.0" + conditions: os=linux & cpu=ppc64 & libc=glibc + languageName: node + linkType: hard + "@oxc-parser/binding-linux-riscv64-gnu@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-riscv64-gnu@npm:0.132.0" @@ -3294,6 +3379,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-riscv64-gnu@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-riscv64-gnu@npm:0.134.0" + conditions: os=linux & cpu=riscv64 & libc=glibc + languageName: node + linkType: hard + "@oxc-parser/binding-linux-riscv64-musl@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-riscv64-musl@npm:0.132.0" @@ -3301,6 +3393,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-riscv64-musl@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-riscv64-musl@npm:0.134.0" + conditions: os=linux & cpu=riscv64 & libc=musl + languageName: node + linkType: hard + "@oxc-parser/binding-linux-s390x-gnu@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-s390x-gnu@npm:0.132.0" @@ -3308,6 +3407,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-s390x-gnu@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-s390x-gnu@npm:0.134.0" + conditions: os=linux & cpu=s390x & libc=glibc + languageName: node + linkType: hard + "@oxc-parser/binding-linux-x64-gnu@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-x64-gnu@npm:0.132.0" @@ -3315,6 +3421,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-x64-gnu@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-x64-gnu@npm:0.134.0" + conditions: os=linux & cpu=x64 & libc=glibc + languageName: node + linkType: hard + "@oxc-parser/binding-linux-x64-musl@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-linux-x64-musl@npm:0.132.0" @@ -3322,6 +3435,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-linux-x64-musl@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-linux-x64-musl@npm:0.134.0" + conditions: os=linux & cpu=x64 & libc=musl + languageName: node + linkType: hard + "@oxc-parser/binding-openharmony-arm64@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-openharmony-arm64@npm:0.132.0" @@ -3329,6 +3449,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-openharmony-arm64@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-openharmony-arm64@npm:0.134.0" + conditions: os=openharmony & cpu=arm64 + languageName: node + linkType: hard + "@oxc-parser/binding-wasm32-wasi@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-wasm32-wasi@npm:0.132.0" @@ -3340,6 +3467,17 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-wasm32-wasi@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-wasm32-wasi@npm:0.134.0" + dependencies: + "@emnapi/core": "npm:1.10.0" + "@emnapi/runtime": "npm:1.10.0" + "@napi-rs/wasm-runtime": "npm:^1.1.4" + conditions: cpu=wasm32 + languageName: node + linkType: hard + "@oxc-parser/binding-win32-arm64-msvc@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-win32-arm64-msvc@npm:0.132.0" @@ -3347,6 +3485,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-win32-arm64-msvc@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-win32-arm64-msvc@npm:0.134.0" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + "@oxc-parser/binding-win32-ia32-msvc@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-win32-ia32-msvc@npm:0.132.0" @@ -3354,6 +3499,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-win32-ia32-msvc@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-win32-ia32-msvc@npm:0.134.0" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + "@oxc-parser/binding-win32-x64-msvc@npm:0.132.0": version: 0.132.0 resolution: "@oxc-parser/binding-win32-x64-msvc@npm:0.132.0" @@ -3361,6 +3513,13 @@ __metadata: languageName: node linkType: hard +"@oxc-parser/binding-win32-x64-msvc@npm:0.134.0": + version: 0.134.0 + resolution: "@oxc-parser/binding-win32-x64-msvc@npm:0.134.0" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@oxc-project/types@npm:=0.133.0": version: 0.133.0 resolution: "@oxc-project/types@npm:0.133.0" @@ -3375,6 +3534,13 @@ __metadata: languageName: node linkType: hard +"@oxc-project/types@npm:^0.134.0": + version: 0.134.0 + resolution: "@oxc-project/types@npm:0.134.0" + checksum: 10c0/d90ee664206091d539a24de6ec703bdb2124e1181680609ac3c27326273a3189c5bb6e0177f3171c85a78e8c01eb43a9903cbbe13257053c7df141c030579c16 + languageName: node + linkType: hard + "@oxfmt/binding-android-arm-eabi@npm:0.47.0": version: 0.47.0 resolution: "@oxfmt/binding-android-arm-eabi@npm:0.47.0" @@ -4948,22 +5114,22 @@ __metadata: languageName: node linkType: hard -"@unhead/bundler@npm:3.1.1": - version: 3.1.1 - resolution: "@unhead/bundler@npm:3.1.1" +"@unhead/bundler@npm:3.1.3": + version: 3.1.3 + resolution: "@unhead/bundler@npm:3.1.3" dependencies: - "@vitejs/devtools-kit": "npm:^0.2.0" + "@vitejs/devtools-kit": "npm:^0.3.1" magic-string: "npm:^0.30.21" - oxc-parser: "npm:^0.132.0" + oxc-parser: "npm:^0.134.0" oxc-walker: "npm:^1.0.0" ufo: "npm:^1.6.4" unplugin: "npm:^3.0.0" peerDependencies: - "@unhead/cli": ^3.1.1 + "@unhead/cli": ^3.1.3 esbuild: ">=0.17.0" lightningcss: ">=1.20.0" rolldown: ">=1.0.0-beta.0" - unhead: ^3.1.1 + unhead: ^3.1.3 vite: ">=6.4.2" webpack: ">=5.0.0" peerDependenciesMeta: @@ -4979,18 +5145,18 @@ __metadata: optional: true webpack: optional: true - checksum: 10c0/9fa29f8da2335356c8cbf6ec8ee92d00481d06873b980cf96c863c1f05920022902fed771a97db96c8eed89b220a26bdc75d02496aa6754832d4da75fe5c48a9 + checksum: 10c0/bd1600a324bcd5a7b4d14a02185a77c5d89cd7693126b725dc2a2a1394081b2987a44bed82f7061725da056c1d69bea57121cb5ac3c59c92488c4fcfe852adf3 languageName: node linkType: hard "@unhead/react@npm:^3.1.0": - version: 3.1.1 - resolution: "@unhead/react@npm:3.1.1" + version: 3.1.3 + resolution: "@unhead/react@npm:3.1.3" dependencies: - "@unhead/bundler": "npm:3.1.1" + "@unhead/bundler": "npm:3.1.3" magic-string: "npm:^0.30.21" oxc-walker: "npm:^1.0.0" - unhead: "npm:3.1.1" + unhead: "npm:3.1.3" unplugin: "npm:^3.0.0" peerDependencies: react: ">=19.2.4" @@ -5001,7 +5167,7 @@ __metadata: optional: true webpack: optional: true - checksum: 10c0/74d95b9fd6d6e0ef65eeb5dd8b3729bba290c365f2575f47ac0ee5be5a507bf6a7545817cc89326818495460dad8b8d7025a5c820c94a7c7d10f812500b7daef + checksum: 10c0/8c65fbefd81e28ed578eee0d91d38d3bd4545f2ba486ecaa8fbc16b46768c25abd7b3f655a4bc18dab06a79c00ca4d4d61d1ed5f93eb5cea73a46e88a9c89e17 languageName: node linkType: hard @@ -5174,20 +5340,21 @@ __metadata: languageName: node linkType: hard -"@vitejs/devtools-kit@npm:^0.2.0": - version: 0.2.0 - resolution: "@vitejs/devtools-kit@npm:0.2.0" +"@vitejs/devtools-kit@npm:^0.3.1": + version: 0.3.1 + resolution: "@vitejs/devtools-kit@npm:0.3.1" dependencies: + "@devframes/hub": "npm:^0.5.2" birpc: "npm:^4.0.0" - devframe: "npm:^0.4.1" + devframe: "npm:^0.5.2" mlly: "npm:^1.8.2" nostics: "npm:^0.2.0" pathe: "npm:^2.0.3" perfect-debounce: "npm:^2.1.0" - tinyexec: "npm:^1.1.2" + tinyexec: "npm:^1.2.2" peerDependencies: vite: "*" - checksum: 10c0/19cbc4981a8d0c1be0fa551746702aff67d105e5b4b4611fdd596c21e1e0a8a6a0de4718cfad804f196597cf2ef81e816aa76ebfaa540cab8d7611abcf94d77d + checksum: 10c0/b405393ef7653c31f3363d972ecd7dd51c818e33375b21aa890769f483db22692590cf4d5dfe98d449445cc809177263a4e43ed342e5994694b8d092e0e52634 languageName: node linkType: hard @@ -6865,9 +7032,9 @@ __metadata: languageName: node linkType: hard -"devframe@npm:^0.4.1": - version: 0.4.1 - resolution: "devframe@npm:0.4.1" +"devframe@npm:^0.5.2": + version: 0.5.4 + resolution: "devframe@npm:0.5.4" dependencies: "@valibot/to-json-schema": "npm:^1.7.0" birpc: "npm:^4.0.0" @@ -6876,14 +7043,14 @@ __metadata: mrmime: "npm:^2.0.1" nostics: "npm:^0.2.0" pathe: "npm:^2.0.3" - valibot: "npm:^1.4.0" - ws: "npm:^8.20.0" + valibot: "npm:^1.4.1" + ws: "npm:^8.21.0" peerDependencies: "@modelcontextprotocol/sdk": ^1.0.0 peerDependenciesMeta: "@modelcontextprotocol/sdk": optional: true - checksum: 10c0/aa7be58332c43788fdfa46213d66bfd73799ab98ceb3f84bb6f5e7676f246e21b8aecb728a60ad5ea964dcb8cfc4bc7a6a18c75667939f290554d93170e0728f + checksum: 10c0/c5651a1e1501a0f4da80c07bdad96bc8cd8d3cef05ecd2ac33fdaf42fa7a481e17d59bf1922b31e46cbc8d82c4837eefeabbc907fdadab9e9d417aeb42b6af62 languageName: node linkType: hard @@ -10660,6 +10827,76 @@ __metadata: languageName: node linkType: hard +"oxc-parser@npm:^0.134.0": + version: 0.134.0 + resolution: "oxc-parser@npm:0.134.0" + dependencies: + "@oxc-parser/binding-android-arm-eabi": "npm:0.134.0" + "@oxc-parser/binding-android-arm64": "npm:0.134.0" + "@oxc-parser/binding-darwin-arm64": "npm:0.134.0" + "@oxc-parser/binding-darwin-x64": "npm:0.134.0" + "@oxc-parser/binding-freebsd-x64": "npm:0.134.0" + "@oxc-parser/binding-linux-arm-gnueabihf": "npm:0.134.0" + "@oxc-parser/binding-linux-arm-musleabihf": "npm:0.134.0" + "@oxc-parser/binding-linux-arm64-gnu": "npm:0.134.0" + "@oxc-parser/binding-linux-arm64-musl": "npm:0.134.0" + "@oxc-parser/binding-linux-ppc64-gnu": "npm:0.134.0" + "@oxc-parser/binding-linux-riscv64-gnu": "npm:0.134.0" + "@oxc-parser/binding-linux-riscv64-musl": "npm:0.134.0" + "@oxc-parser/binding-linux-s390x-gnu": "npm:0.134.0" + "@oxc-parser/binding-linux-x64-gnu": "npm:0.134.0" + "@oxc-parser/binding-linux-x64-musl": "npm:0.134.0" + "@oxc-parser/binding-openharmony-arm64": "npm:0.134.0" + "@oxc-parser/binding-wasm32-wasi": "npm:0.134.0" + "@oxc-parser/binding-win32-arm64-msvc": "npm:0.134.0" + "@oxc-parser/binding-win32-ia32-msvc": "npm:0.134.0" + "@oxc-parser/binding-win32-x64-msvc": "npm:0.134.0" + "@oxc-project/types": "npm:^0.134.0" + dependenciesMeta: + "@oxc-parser/binding-android-arm-eabi": + optional: true + "@oxc-parser/binding-android-arm64": + optional: true + "@oxc-parser/binding-darwin-arm64": + optional: true + "@oxc-parser/binding-darwin-x64": + optional: true + "@oxc-parser/binding-freebsd-x64": + optional: true + "@oxc-parser/binding-linux-arm-gnueabihf": + optional: true + "@oxc-parser/binding-linux-arm-musleabihf": + optional: true + "@oxc-parser/binding-linux-arm64-gnu": + optional: true + "@oxc-parser/binding-linux-arm64-musl": + optional: true + "@oxc-parser/binding-linux-ppc64-gnu": + optional: true + "@oxc-parser/binding-linux-riscv64-gnu": + optional: true + "@oxc-parser/binding-linux-riscv64-musl": + optional: true + "@oxc-parser/binding-linux-s390x-gnu": + optional: true + "@oxc-parser/binding-linux-x64-gnu": + optional: true + "@oxc-parser/binding-linux-x64-musl": + optional: true + "@oxc-parser/binding-openharmony-arm64": + optional: true + "@oxc-parser/binding-wasm32-wasi": + optional: true + "@oxc-parser/binding-win32-arm64-msvc": + optional: true + "@oxc-parser/binding-win32-ia32-msvc": + optional: true + "@oxc-parser/binding-win32-x64-msvc": + optional: true + checksum: 10c0/e61a7429869a0abd1adc162999c3413b96fff77b4b77460436f8fd87d74d254731aea66cdc6a5c996185e8e227f0a2f61f165de74a52b8163646dd1464c561b9 + languageName: node + linkType: hard + "oxc-walker@npm:^1.0.0": version: 1.0.0 resolution: "oxc-walker@npm:1.0.0" @@ -13795,10 +14032,10 @@ __metadata: languageName: node linkType: hard -"tinyexec@npm:^1.0.2, tinyexec@npm:^1.1.2": - version: 1.1.2 - resolution: "tinyexec@npm:1.1.2" - checksum: 10c0/9e0ef6c001ce54688cf16833a02f70a339276219ca947b88930b124267de2cffc764ff44e87e7369384b1d75ab63491465412cbbdf06f2437956b9ab66ab4491 +"tinyexec@npm:^1.0.2, tinyexec@npm:^1.1.2, tinyexec@npm:^1.2.2": + version: 1.2.4 + resolution: "tinyexec@npm:1.2.4" + checksum: 10c0/153b8db6b080194b558ff145b9cffc36b80a6e07babd644dcfbe49c807eee668c876049d28bdee90b96304476f883352f2dad91b3f86bc23832532f4363e66ff languageName: node linkType: hard @@ -14215,9 +14452,9 @@ __metadata: languageName: node linkType: hard -"unhead@npm:3.1.1": - version: 3.1.1 - resolution: "unhead@npm:3.1.1" +"unhead@npm:3.1.3": + version: 3.1.3 + resolution: "unhead@npm:3.1.3" dependencies: hookable: "npm:^6.1.1" unplugin: "npm:^3.0.0" @@ -14226,7 +14463,7 @@ __metadata: peerDependenciesMeta: vite: optional: true - checksum: 10c0/8a2bb6801d66cd272bbf36450e0f81bc69fd19f2f576e95a6a0d0a9fa2d2a0a6e02da39eed4ab32de78d3f1b22de339554dc5790585682a7a3cb5c22193d3ef0 + checksum: 10c0/410dbb58aa7048e5b51d94513b08da54884275cff391542dd3a9be9e8692fca4a7c1183eb92f4144ed081f73becc86ad6e6434183b5f090417da269ccac60466 languageName: node linkType: hard @@ -14492,7 +14729,7 @@ __metadata: languageName: node linkType: hard -"valibot@npm:^1.4.0": +"valibot@npm:^1.4.1": version: 1.4.1 resolution: "valibot@npm:1.4.1" peerDependencies: @@ -14921,7 +15158,7 @@ __metadata: languageName: node linkType: hard -"ws@npm:^8.12.1, ws@npm:^8.18.0, ws@npm:^8.19.0, ws@npm:^8.20.0": +"ws@npm:^8.12.1, ws@npm:^8.18.0, ws@npm:^8.19.0, ws@npm:^8.21.0": version: 8.21.0 resolution: "ws@npm:8.21.0" peerDependencies: From ad3ee384fded597f7f189f975465128b3a91efac Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 12 Jun 2026 09:44:29 -0400 Subject: [PATCH 03/10] Add basic coverage for `UnmergeWorker` job (#39398) --- spec/workers/unmerge_worker_spec.rb | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 spec/workers/unmerge_worker_spec.rb diff --git a/spec/workers/unmerge_worker_spec.rb b/spec/workers/unmerge_worker_spec.rb new file mode 100644 index 0000000000..b04e6d7303 --- /dev/null +++ b/spec/workers/unmerge_worker_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe UnmergeWorker do + let(:worker) { described_class.new } + + describe '#perform' do + subject { worker.perform(account_id, into_id, type) } + + let(:account_id) { account.id } + let(:account) { Fabricate :account } + let(:into_id) { nil } + let(:type) { nil } + + context 'when account is invalid' do + let(:account_id) { 123_123_123 } + + it { is_expected.to be(true) } + end + + context 'when type is invalid' do + it { is_expected.to be_nil } + end + + context 'when type is list' do + let(:type) { 'list' } + + context 'when target is invalid' do + let(:into_id) { 123_123_123 } + + it { is_expected.to be(true) } + end + + context 'when target is valid' do + let(:into_id) { list.id } + let(:list) { Fabricate :list } + + let(:manager_service) { instance_double(FeedManager, unmerge_from_list: nil) } + + before { allow(FeedManager).to receive(:instance).and_return manager_service } + + it 'unmerges from list feed' do + subject + + expect(manager_service) + .to have_received(:unmerge_from_list).with(account, list) + end + end + end + + context 'when type is home' do + let(:type) { 'home' } + + context 'when target is invalid' do + let(:into_id) { 123_123_123 } + + it { is_expected.to be(true) } + end + + context 'when target is valid' do + let(:into_id) { target_account.id } + let(:target_account) { Fabricate :account } + + let(:manager_service) { instance_double(FeedManager, unmerge_from_home: nil) } + + before { allow(FeedManager).to receive(:instance).and_return manager_service } + + it 'unmerges from list feed' do + subject + + expect(manager_service) + .to have_received(:unmerge_from_home).with(account, target_account) + end + end + end + end +end From a78d1150a9c8f385450bb0cfaa947c164c8c69d6 Mon Sep 17 00:00:00 2001 From: diondiondion Date: Fri, 12 Jun 2026 15:44:34 +0200 Subject: [PATCH 04/10] Fix crash of standalone wrapstodon and embedded status pages (#39400) --- app/javascript/entrypoints/wrapstodon.tsx | 5 ++++- .../mastodon/components/navigation_focus_target/index.tsx | 8 +------- .../mastodon/features/standalone/status/index.tsx | 5 ++++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/javascript/entrypoints/wrapstodon.tsx b/app/javascript/entrypoints/wrapstodon.tsx index 9fff41a133..814703f865 100644 --- a/app/javascript/entrypoints/wrapstodon.tsx +++ b/app/javascript/entrypoints/wrapstodon.tsx @@ -5,6 +5,7 @@ import { Provider as ReduxProvider } from 'react-redux'; import { importFetchedStatuses } from '@/mastodon/actions/importer'; import { hydrateStore } from '@/mastodon/actions/store'; import type { ApiAnnualReportResponse } from '@/mastodon/api/annual_report'; +import { FocusTargetProvider } from '@/mastodon/components/navigation_focus_target'; import { Router } from '@/mastodon/components/router'; import { WrapstodonSharedPage } from '@/mastodon/features/annual_report/shared_page'; import { IntlProvider, loadLocale } from '@/mastodon/locales'; @@ -52,7 +53,9 @@ function loaded() { - + + + , diff --git a/app/javascript/mastodon/components/navigation_focus_target/index.tsx b/app/javascript/mastodon/components/navigation_focus_target/index.tsx index f4b42926a8..ceb26842db 100644 --- a/app/javascript/mastodon/components/navigation_focus_target/index.tsx +++ b/app/javascript/mastodon/components/navigation_focus_target/index.tsx @@ -102,15 +102,9 @@ export const FocusTargetProvider: React.FC<{ export function useFocusOnNavigation(targetName?: string) { const focusTargetRef = useContext(FocusTargetContext); - if (focusTargetRef === null) { - throw Error( - 'useFocusTargetContext must be used inside of a FocusTargetProvider', - ); - } - return useCallback( (element: HTMLElement | null) => { - const focusTarget = focusTargetRef.current; + const focusTarget = focusTargetRef?.current; // Bail out if focusTarget was set to `false` if (!element || !focusTarget) { diff --git a/app/javascript/mastodon/features/standalone/status/index.tsx b/app/javascript/mastodon/features/standalone/status/index.tsx index a53d6e6b23..ccc3c2c5be 100644 --- a/app/javascript/mastodon/features/standalone/status/index.tsx +++ b/app/javascript/mastodon/features/standalone/status/index.tsx @@ -8,6 +8,7 @@ import { Provider } from 'react-redux'; import { fetchStatus, toggleStatusSpoilers } from 'mastodon/actions/statuses'; import { hydrateStore } from 'mastodon/actions/store'; +import { FocusTargetProvider } from 'mastodon/components/navigation_focus_target'; import { Router } from 'mastodon/components/router'; import { DetailedStatus } from 'mastodon/features/status/components/detailed_status'; import { useRenderSignal } from 'mastodon/hooks/useRenderSignal'; @@ -79,7 +80,9 @@ export const Status: React.FC<{ id: string }> = ({ id }) => { - + + + From 61fdcf33303f9be249fbec4f576153bde2c9048b Mon Sep 17 00:00:00 2001 From: Federico Rao <157750791+Federicorao@users.noreply.github.com> Date: Fri, 12 Jun 2026 16:05:57 +0200 Subject: [PATCH 05/10] Guard against undefined AudioContext in useAudioContext hook (#39397) --- app/javascript/mastodon/hooks/useAudioContext.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/javascript/mastodon/hooks/useAudioContext.ts b/app/javascript/mastodon/hooks/useAudioContext.ts index 84acf5ac7f..2cad7ee3a5 100644 --- a/app/javascript/mastodon/hooks/useAudioContext.ts +++ b/app/javascript/mastodon/hooks/useAudioContext.ts @@ -18,7 +18,7 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => { const gainNodeRef = useRef(); useEffect(() => { - if (!audioElementRef.current) { + if (!audioElementRef.current || typeof AudioContext === 'undefined') { return; } @@ -43,13 +43,17 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => { }, [audioElementRef]); const playAudio = useCallback(() => { - void audioElementRef.current?.play(); - void audioContextRef.current?.resume(); + if (audioContextRef.current && audioElementRef.current) { + void audioElementRef.current.play(); + void audioContextRef.current.resume(); + } }, [audioElementRef]); const pauseAudio = useCallback(() => { - audioElementRef.current?.pause(); - void audioContextRef.current?.suspend(); + if (audioContextRef.current && audioElementRef.current) { + audioElementRef.current.pause(); + void audioContextRef.current.suspend(); + } }, [audioElementRef]); return { From 963a54664804e60ea8e30795090a0f20de7a48dc Mon Sep 17 00:00:00 2001 From: Echo Date: Fri, 12 Jun 2026 17:30:27 +0200 Subject: [PATCH 06/10] Emoji: Add back to state (#39402) --- .../mastodon/actions/importer/emoji.ts | 4 - .../account_edit/modals/fields_modals.tsx | 2 +- .../components/emoji_picker_dropdown.jsx | 5 +- .../mastodon/features/emoji/emoji_picker.tsx | 12 +- .../mastodon/features/emoji/index.ts | 61 ++++--- .../mastodon/features/emoji/mode.ts | 2 - .../mastodon/features/emoji/picker.ts | 152 +++++++----------- .../mastodon/features/emoji/render.ts | 2 +- .../mastodon/features/emoji/types.ts | 11 +- .../mastodon/features/emoji/worker.ts | 10 +- .../mastodon/hooks/useCustomEmojis.ts | 53 +++--- .../mastodon/reducers/slices/emojis.ts | 71 ++++++++ .../mastodon/reducers/slices/index.ts | 2 + 13 files changed, 217 insertions(+), 170 deletions(-) create mode 100644 app/javascript/mastodon/reducers/slices/emojis.ts diff --git a/app/javascript/mastodon/actions/importer/emoji.ts b/app/javascript/mastodon/actions/importer/emoji.ts index 36fb04b51e..db15a8a9bf 100644 --- a/app/javascript/mastodon/actions/importer/emoji.ts +++ b/app/javascript/mastodon/actions/importer/emoji.ts @@ -22,10 +22,6 @@ export async function importCustomEmoji(emojis: ApiCustomEmojiJSON[]) { await clearCache('custom'); await loadCustomEmoji(); - const { reloadCustomEmojis } = - await import('@/mastodon/features/emoji/picker'); - await reloadCustomEmojis(); - log('Custom emojis updated, reloaded cache and picker data.'); } } diff --git a/app/javascript/mastodon/features/account_edit/modals/fields_modals.tsx b/app/javascript/mastodon/features/account_edit/modals/fields_modals.tsx index 9fc26ac525..0f2e7147ff 100644 --- a/app/javascript/mastodon/features/account_edit/modals/fields_modals.tsx +++ b/app/javascript/mastodon/features/account_edit/modals/fields_modals.tsx @@ -140,7 +140,7 @@ export const EditFieldModal = forwardRef< const customEmojis = useCustomEmojis(); const customEmojiCodes = useMemo( - () => Object.keys(customEmojis ?? {}), + () => Object.keys(customEmojis), [customEmojis], ); const checkField = useCallback( diff --git a/app/javascript/mastodon/features/compose/components/emoji_picker_dropdown.jsx b/app/javascript/mastodon/features/compose/components/emoji_picker_dropdown.jsx index 3da7bf1812..5da6eb8609 100644 --- a/app/javascript/mastodon/features/compose/components/emoji_picker_dropdown.jsx +++ b/app/javascript/mastodon/features/compose/components/emoji_picker_dropdown.jsx @@ -323,10 +323,7 @@ class EmojiPickerDropdown extends PureComponent { EmojiPickerAsync().then(EmojiMart => { EmojiPicker = EmojiMart.Picker; Emoji = EmojiMart.Emoji; - - void EmojiMart.loadCustomEmojiData().then(() => { - this.setState({ loading: false }); - }); + this.setState({ loading: false }); }).catch(() => { this.setState({ loading: false, active: false }); }); diff --git a/app/javascript/mastodon/features/emoji/emoji_picker.tsx b/app/javascript/mastodon/features/emoji/emoji_picker.tsx index 3661a7193b..15adf9fdc8 100644 --- a/app/javascript/mastodon/features/emoji/emoji_picker.tsx +++ b/app/javascript/mastodon/features/emoji/emoji_picker.tsx @@ -13,8 +13,6 @@ import { usePickerEmojis } from './picker'; const backgroundImageFnDefault = () => `${assetHost}/emoji/sheet_16_0.png`; -export { fetchCustomEmojiData as loadCustomEmojiData } from './picker'; - export const Picker: FC = ({ set = 'twitter', sheetSize = 32, @@ -24,17 +22,13 @@ export const Picker: FC = ({ ...props }) => { const { mode } = useEmojiAppState(); - const { customCategories, customEmojis } = usePickerEmojis(); - - if (!customEmojis) { - return null; - } + const { categories, emojis } = usePickerEmojis(); return ( ) => { - const { data: message } = event; + tempWorker.addEventListener( + 'message', + (event: MessageEvent) => { + const { data: message } = event; - worker ??= tempWorker; - clearTimeout(timeoutId); + worker ??= tempWorker; + clearTimeout(timeoutId); - if (message !== 'ready') { - workerLog(message); - return; - } + const { type } = message; + if (type === 'log') { + workerLog(message.message); + } else if (type === 'done' && message.storeName === 'custom') { + void loadEmojisToStore(); + } - const debugValue = localStorage.getItem('debug'); - if (debugValue) { - messageWorker({ type: 'debug', debugValue }); - } + if (type !== 'ready') { + return; // Exit for other messages. + } - workerLog('loading data'); - messageWorker(userLocale); - messageWorker('custom'); - messageWorker('shortcodes'); - }); + const debugValue = localStorage.getItem('debug'); + if (debugValue) { + messageWorker({ type: 'debug', debugValue }); + } + + workerLog('loading data'); + messageWorker(userLocale); + messageWorker('custom'); + messageWorker('shortcodes'); + void loadEmojisToStore(); + }, + ); } async function fallbackLoad() { @@ -71,8 +80,8 @@ async function fallbackLoad() { const customEmojis = await importCustomEmojiData(); if (customEmojis && customEmojis.length > 0) { log('loaded %d custom emojis', customEmojis.length); - await reloadCustomEmojis(); } + const shortcodes = await importLegacyShortcodes(); if (shortcodes?.length) { log('loaded %d legacy shortcodes', shortcodes.length); @@ -82,6 +91,7 @@ async function fallbackLoad() { if (emojis) { log('loaded %d emojis to locale %s', emojis.length, userLocale); } + await loadEmojisToStore(); } export async function loadCustomEmoji() { @@ -92,9 +102,9 @@ export async function loadCustomEmoji() { const emojis = await importCustomEmojiData(); if (emojis && emojis.length > 0) { log('loaded %d custom emojis', emojis.length); - await reloadCustomEmojis(); } } + await loadEmojisToStore(); } function messageWorker(data: EmojiWorkerMessage | string) { @@ -110,3 +120,14 @@ function messageWorker(data: EmojiWorkerMessage | string) { worker.postMessage(data); } } + +async function loadEmojisToStore() { + const { store } = await import('@/mastodon/store'); + const { loadCustomEmojis, loadLocale } = + await import('@/mastodon/reducers/slices/emojis'); + + loadLocale(userLocale); + await store.dispatch(loadCustomEmojis()); + + log('loaded emoji data into store'); +} diff --git a/app/javascript/mastodon/features/emoji/mode.ts b/app/javascript/mastodon/features/emoji/mode.ts index f7b0ea0216..32a0f2a893 100644 --- a/app/javascript/mastodon/features/emoji/mode.ts +++ b/app/javascript/mastodon/features/emoji/mode.ts @@ -27,7 +27,6 @@ export function useEmojiAppState(): EmojiAppState { return { currentLocale: locale, - locales: [locale], mode, darkTheme: isDarkMode(), assetHost, @@ -64,7 +63,6 @@ export function getEmojiAppState(): EmojiAppState { return { currentLocale, - locales: [currentLocale], mode: determineEmojiMode(emojiStyle), darkTheme: isDarkMode(), assetHost, diff --git a/app/javascript/mastodon/features/emoji/picker.ts b/app/javascript/mastodon/features/emoji/picker.ts index 7acf878f51..474eb0d48a 100644 --- a/app/javascript/mastodon/features/emoji/picker.ts +++ b/app/javascript/mastodon/features/emoji/picker.ts @@ -1,83 +1,18 @@ -import { useEffect, useState } from 'react'; - import type { CategoryName, CustomEmoji } from 'emoji-mart'; import { autoPlayGif } from '@/mastodon/initial_state'; +import { + createAppSelector, + useAppSelector, +} from '@/mastodon/store/typed_functions'; import { createLimitedCache } from '@/mastodon/utils/cache'; import { emojiLogger } from './utils'; const log = emojiLogger('picker'); -let customEmojis: CustomEmoji[] | null = null; -let customCategories = [ - 'recent', - 'people', - 'nature', - 'foods', - 'activity', - 'places', - 'objects', - 'symbols', - 'flags', -] as CategoryName[]; - const searchCache = createLimitedCache({ maxSize: 10, log }); -export async function fetchCustomEmojiData() { - if (customEmojis !== null) { - return customEmojis; - } - - const { loadAllCustomEmoji } = await import('./database'); - const emojisRaw = await loadAllCustomEmoji(); - - // If it returns null then custom emojis aren't even loaded yet. - if (emojisRaw === null) { - return []; - } - - // If it's empty, then they are loaded but there aren't any. - if (emojisRaw.length === 0) { - customEmojis = []; - return customEmojis; - } - - const categories = new Set(['custom']); - const emojis = []; - for (const emoji of emojisRaw) { - const name = emoji.shortcode.replaceAll(':', ''); - emojis.push({ - name, - id: name, - custom: true, - short_names: [name], - imageUrl: autoPlayGif ? emoji.url : emoji.static_url, - customCategory: emoji.category, - }); - - if (emoji.category) { - categories.add(`custom-${emoji.category}`); - } - } - - customEmojis = emojis.toSorted((a, b) => { - return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); - }); - customCategories = customCategories.toSpliced( - 1, - 0, - ...(Array.from(categories).toSorted() as CategoryName[]), - ); - log( - 'loaded %d custom emojis in %d categories', - customEmojis.length, - categories.size, - ); - - return customEmojis; -} - type LegacyEmoji = | { id: string; custom?: false; native: string } | { @@ -85,16 +20,6 @@ type LegacyEmoji = custom: true; }; -export async function reloadCustomEmojis() { - customEmojis = null; - - const { loadEmojisIntoCache } = - await import('@/mastodon/hooks/useCustomEmojis'); - - await Promise.all([fetchCustomEmojiData(), loadEmojisIntoCache()]); - searchCache.clear(); -} - // Replicates the old legacy search function. export async function emojiMartSearch( token: string, @@ -127,19 +52,64 @@ export async function emojiMartSearch( return legacyResults; } -export function usePickerEmojis() { - const [, setLoaded] = useState(customEmojis !== null); +const defaultCategories = [ + 'people', + 'nature', + 'foods', + 'activity', + 'places', + 'objects', + 'symbols', + 'flags', +] as CategoryName[]; - useEffect(() => { - if (customEmojis === null) { - void fetchCustomEmojiData().then(() => { - setLoaded(true); - }); +const selectPickerData = createAppSelector( + [(state) => state.emojis.custom, (state) => state.emojis.customCategories], + (emojis, categories) => { + // Create a map of shortcode to category name. + const categoryMap = new Map(); + for (const category in categories) { + const catEmojis = categories[category]; + if (!catEmojis?.length) { + continue; + } + for (const shortcode of catEmojis) { + categoryMap.set(shortcode, category); + } } - }, []); - return { - customEmojis, - customCategories, - }; + const customEmojis: CustomEmoji[] = []; + for (const shortcode in emojis) { + const emoji = emojis[shortcode]; + if (!emoji) { + continue; + } + + customEmojis.push({ + name: shortcode, + id: shortcode, + custom: true, + short_names: [shortcode], + imageUrl: autoPlayGif ? emoji.url : emoji.static_url, + customCategory: categoryMap.get(shortcode), + } as CustomEmoji); + } + + searchCache.clear(); + log('regenerated the picker data'); + + return { + emojis: customEmojis, + categories: [ + 'recent', + 'custom', + ...Object.keys(categories).toSorted(), + ...defaultCategories, + ] as CategoryName[], + }; + }, +); + +export function usePickerEmojis() { + return useAppSelector(selectPickerData); } diff --git a/app/javascript/mastodon/features/emoji/render.ts b/app/javascript/mastodon/features/emoji/render.ts index affa8e0f16..e5ac5bcc6e 100644 --- a/app/javascript/mastodon/features/emoji/render.ts +++ b/app/javascript/mastodon/features/emoji/render.ts @@ -109,7 +109,7 @@ export async function updateHtmlWithEmoji({ }: { element: Element; locale: string; -} & Omit) { +} & Omit) { if (mode === EMOJI_MODE_NATIVE) { return; } diff --git a/app/javascript/mastodon/features/emoji/types.ts b/app/javascript/mastodon/features/emoji/types.ts index 5c9dc6e5c6..b6dbad47d2 100644 --- a/app/javascript/mastodon/features/emoji/types.ts +++ b/app/javascript/mastodon/features/emoji/types.ts @@ -28,7 +28,6 @@ export type CacheKey = | LocaleWithShortcodes; export interface EmojiAppState { - locales: Locale[]; currentLocale: Locale; mode: EmojiMode; darkTheme: boolean; @@ -82,10 +81,20 @@ export type ExtraCustomEmojiMap = Record< >; export type EmojiWorkerMessage = + | { type: 'ready' } | { type: 'load'; storeName: string; } + | { + type: 'done'; + storeName: string; + importCount: number; + } + | { + type: 'log'; + message: string; + } | { type: 'debug'; debugValue: string; diff --git a/app/javascript/mastodon/features/emoji/worker.ts b/app/javascript/mastodon/features/emoji/worker.ts index d4f002daf6..49b7ce45f1 100644 --- a/app/javascript/mastodon/features/emoji/worker.ts +++ b/app/javascript/mastodon/features/emoji/worker.ts @@ -9,13 +9,13 @@ import { import type { EmojiWorkerMessage } from './types'; addEventListener('message', handleMessage); -self.postMessage('ready'); // After the worker is ready, notify the main thread +self.postMessage({ type: 'ready' } satisfies EmojiWorkerMessage); // After the worker is ready, notify the main thread function handleMessage(event: MessageEvent) { const { data } = event; if (data.type === 'debug') { debug.enable(data.debugValue); - } else { + } else if (data.type === 'load') { void loadData(data.storeName); } } @@ -31,6 +31,10 @@ async function loadData(storeName: string) { } if (importCount) { - self.postMessage(`loaded ${importCount} emojis into ${storeName}`); + self.postMessage({ + type: 'done', + storeName, + importCount, + } satisfies EmojiWorkerMessage); } } diff --git a/app/javascript/mastodon/hooks/useCustomEmojis.ts b/app/javascript/mastodon/hooks/useCustomEmojis.ts index 6eb3c17fbf..213b7de062 100644 --- a/app/javascript/mastodon/hooks/useCustomEmojis.ts +++ b/app/javascript/mastodon/hooks/useCustomEmojis.ts @@ -1,40 +1,25 @@ -import { useEffect, useState } from 'react'; +import { createAppSelector, useAppSelector } from '@/mastodon/store'; import type { ExtraCustomEmojiMap } from '../features/emoji/types'; -import { emojiLogger } from '../features/emoji/utils'; -let emojis: ExtraCustomEmojiMap | null = null; - -const log = emojiLogger('useCustomEmojis'); +const selectCustomEmojis = createAppSelector( + [(state) => state.emojis.custom], + (custom) => { + const emojis: ExtraCustomEmojiMap = {}; + for (const shortcode in custom) { + const emoji = custom[shortcode]; + if (!emoji) { + continue; + } + emojis[shortcode] = { + shortcode, + ...emoji, + }; + } + return emojis; + }, +); export function useCustomEmojis() { - const [, setLoaded] = useState(emojis !== null); - useEffect(() => { - if (!emojis) { - void loadEmojisIntoCache().then(() => { - setLoaded(true); - }); - } - }, []); - - return emojis; -} - -export async function loadEmojisIntoCache() { - const { loadAllCustomEmoji } = await import('../features/emoji/database'); - const emojisRaw = await loadAllCustomEmoji(); - if (emojisRaw === null) { - log('Custom emojis not loaded yet'); - return; - } - - emojis = {}; - for (const emoji of emojisRaw) { - emojis[emoji.shortcode] = { - url: emoji.url, - shortcode: emoji.shortcode, - static_url: emoji.static_url, - }; - } - log('Loaded %d custom emojis into cache', Object.keys(emojis).length); + return useAppSelector(selectCustomEmojis); } diff --git a/app/javascript/mastodon/reducers/slices/emojis.ts b/app/javascript/mastodon/reducers/slices/emojis.ts new file mode 100644 index 0000000000..28e9430c40 --- /dev/null +++ b/app/javascript/mastodon/reducers/slices/emojis.ts @@ -0,0 +1,71 @@ +import type { PayloadAction } from '@reduxjs/toolkit'; +import { createSlice } from '@reduxjs/toolkit'; + +import type { Locale } from 'emojibase'; + +import type { ApiCustomEmojiJSON } from '@/mastodon/api_types/custom_emoji'; +import { toSupportedLocale } from '@/mastodon/features/emoji/locale'; +import { createAsyncThunk } from '@/mastodon/store/typed_functions'; + +interface EmojisState { + custom: Record>; + customCategories: Record; // { name: shortcodes[] } + customLoaded: boolean; + localesLoaded: Locale[]; +} + +const emojisSlice = createSlice({ + name: 'emojis', + initialState: { + custom: {}, + customCategories: {}, + customLoaded: false, + localesLoaded: [], + } as EmojisState, + reducers: { + loadLocale(state, action: PayloadAction) { + const locale = toSupportedLocale(action.payload); + if (!state.localesLoaded.includes(locale)) { + state.localesLoaded.push(locale); + } + }, + }, + extraReducers(builder) { + builder.addAsyncThunk(loadCustomEmojis, { + fulfilled(state, action) { + if (!action.payload?.length) { + return; + } + + for (const emoji of action.payload) { + const { shortcode, category, url, static_url } = emoji; + state.custom[shortcode] = { + url, + static_url, + }; + + if (category) { + state.customCategories[category] ??= []; + if (!state.customCategories[category].includes(shortcode)) { + state.customCategories[category].push(shortcode); + } + } + + state.customLoaded = true; + } + }, + }); + }, +}); + +export const emojis = emojisSlice.reducer; +export const { loadLocale } = emojisSlice.actions; + +export const loadCustomEmojis = createAsyncThunk( + `${emojisSlice.name}/loadCustomEmojis`, + async () => { + const { loadAllCustomEmoji } = + await import('@/mastodon/features/emoji/database'); + return loadAllCustomEmoji(); + }, +); diff --git a/app/javascript/mastodon/reducers/slices/index.ts b/app/javascript/mastodon/reducers/slices/index.ts index 8d4ecd552d..1b94687cc6 100644 --- a/app/javascript/mastodon/reducers/slices/index.ts +++ b/app/javascript/mastodon/reducers/slices/index.ts @@ -1,9 +1,11 @@ import { annualReport } from './annual_report'; import { collections } from './collections'; +import { emojis } from './emojis'; import { profileEdit } from './profile_edit'; export const sliceReducers = { annualReport, collections, + emojis, profileEdit, }; From 79eb9f4b0b767fa9e2e3f27c9f51c4f6e18f6a49 Mon Sep 17 00:00:00 2001 From: Echo Date: Fri, 12 Jun 2026 11:33:47 +0200 Subject: [PATCH 07/10] [Glitch] Refactor: Typing statuses Port a7e80c62615233a305fdb9f39e1e0451f04e987f to glitch-soc Signed-off-by: Claire --- .../glitch/actions/importer/statuses.ts | 187 ++++++++++++++++++ .../glitch/api_types/media_attachments.ts | 81 +++++++- .../flavours/glitch/api_types/statuses.ts | 19 +- .../flavours/glitch/models/status.ts | 96 ++++++++- .../flavours/glitch/selectors/statuses.ts | 22 ++- 5 files changed, 386 insertions(+), 19 deletions(-) create mode 100644 app/javascript/flavours/glitch/actions/importer/statuses.ts diff --git a/app/javascript/flavours/glitch/actions/importer/statuses.ts b/app/javascript/flavours/glitch/actions/importer/statuses.ts new file mode 100644 index 0000000000..ff6dec7539 --- /dev/null +++ b/app/javascript/flavours/glitch/actions/importer/statuses.ts @@ -0,0 +1,187 @@ +import escapeTextContentForBrowser from 'escape-html'; + +import type { ApiMediaAttachmentJSON } from '@/flavours/glitch/api_types/media_attachments'; +import type { + ApiFilterResultJSON, + ApiStatusJSON, +} from '@/flavours/glitch/api_types/statuses'; +import type { + FilterResult, + MediaAttachmentShape, + StatusShape, +} from '@/flavours/glitch/models/status'; + +const domParser = new DOMParser(); + +export function normalizeStatus( + status: ApiStatusJSON, + normalOldStatus?: StatusShape, + { bogusQuotePolicy = false, expandSpoilers = false } = {}, +) { + const normalStatus: StatusShape = { + hidden: normalOldStatus?.hidden ?? false, + collapsed: normalOldStatus?.collapsed ?? false, + content: '', + contentHtml: '', + muted: false, + pinned: false, + bookmarked: false, + favourited: false, + quote_approval: null, + reblogged: false, + + ...status, + + account: status.account.id, + media_attachments: [], + poll: status.poll?.id, + reblog: status.reblog?.id, + quote: undefined, + filtered: [], + application: { + name: 'Web', + ...status.application, + }, + }; + + if (bogusQuotePolicy) { + normalStatus.quote_approval = null; + } + + if (status.quote?.quoted_status) { + normalStatus.quote = { + ...status.quote, + quoted_status: status.quote.quoted_status.id, + }; + } + + if (status.card) { + normalStatus.card = { + ...status.card, + authors: status.card.authors.map((author) => ({ + name: author.name, + url: author.url, + accountId: author.account?.id, + })), + }; + } + + if (status.filtered) { + normalStatus.filtered = status.filtered.map(normalizeFilterResult); + } + + // Only calculate these values when status first encountered and + // when the underlying values change. Otherwise keep the ones + // already in the reducer + if ( + normalOldStatus?.content === normalStatus.content && + normalOldStatus.spoiler_text === normalStatus.spoiler_text + ) { + normalStatus.search_index = normalOldStatus.search_index; + normalStatus.contentHtml = normalOldStatus.contentHtml; + normalStatus.spoilerHtml = normalOldStatus.spoilerHtml; + normalStatus.spoiler_text = normalOldStatus.spoiler_text; + normalStatus.hidden = normalOldStatus.hidden; + + if (normalOldStatus.translation) { + normalStatus.translation = normalOldStatus.translation; + } + } else { + // If the status has a CW but no contents, treat the CW as if it were the + // status' contents, to avoid having a CW toggle with seemingly no effect. + if ( + normalStatus.spoiler_text && + !normalStatus.content && + !normalStatus.quote + ) { + normalStatus.content = normalStatus.spoiler_text; + normalStatus.spoiler_text = ''; + } + + const spoilerText = normalStatus.spoiler_text ?? ''; + const searchContent = [spoilerText, status.content] + .concat( + status.poll?.options + ? status.poll.options.map((option) => option.title) + : [], + ) + .concat(status.media_attachments.map((att) => att.description)) + .join('\n\n') + .replace(//g, '\n') + .replace(/<\/p>

/g, '\n\n'); + + normalStatus.search_index = domParser.parseFromString( + searchContent, + 'text/html', + ).documentElement.textContent; + normalStatus.contentHtml = normalStatus.content; + normalStatus.spoilerHtml = escapeTextContentForBrowser(spoilerText); + normalStatus.hidden = expandSpoilers + ? false + : spoilerText.length > 0 || normalStatus.sensitive; + + // Remove quote fallback link from the DOM so it doesn't mess with paragraph margins + if (normalStatus.quote && normalStatus.contentHtml) { + normalStatus.contentHtml = stripQuoteFallback(normalStatus.contentHtml); + } + + if ( + normalStatus.url && + !( + normalStatus.url.startsWith('http://') || + normalStatus.url.startsWith('https://') + ) + ) { + normalStatus.url = null; + } + + normalStatus.url ??= normalStatus.uri; + + normalStatus.media_attachments = status.media_attachments.map( + (attachment) => + normalizeMediaAttachment( + attachment, + normalOldStatus?.media_attachments, + ), + ); + } + + if (normalOldStatus) { + normalStatus.quote_approval ??= normalOldStatus.quote_approval; + } + + return normalStatus; +} + +function normalizeMediaAttachment( + attachment: ApiMediaAttachmentJSON, + oldAttachments?: MediaAttachmentShape[], +): MediaAttachmentShape { + const { remote_url } = attachment; + return { + ...attachment, + remote_url: + remote_url && /^https?:\/\//.test(remote_url) ? remote_url : null, + translation: oldAttachments?.find( + (oldAttachment) => + oldAttachment.id === attachment.id && + oldAttachment.description === attachment.description, + )?.translation, + }; +} + +function normalizeFilterResult(input: ApiFilterResultJSON): FilterResult { + return { + ...input, + filter: input.filter.id, + }; +} + +function stripQuoteFallback(text: string) { + const wrapper = document.createElement('div'); + wrapper.innerHTML = text; + + wrapper.querySelector('.quote-inline')?.remove(); + + return wrapper.innerHTML; +} diff --git a/app/javascript/flavours/glitch/api_types/media_attachments.ts b/app/javascript/flavours/glitch/api_types/media_attachments.ts index fc027ccd2a..e4cfe95881 100644 --- a/app/javascript/flavours/glitch/api_types/media_attachments.ts +++ b/app/javascript/flavours/glitch/api_types/media_attachments.ts @@ -7,16 +7,85 @@ export type MediaAttachmentType = | 'unknown' | 'audio'; -export interface ApiMediaAttachmentJSON { +interface BaseApiMediaAttachmentJSON { id: string; type: MediaAttachmentType; url: string; preview_url: string; - remoteUrl: string; - preview_remote_url: string; - text_url: string; - // TODO: how to define this? - meta: unknown; + remote_url?: string; + preview_remote_url?: string; + text_url?: string; description?: string; blurhash: string; } + +interface ApiImageAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'image'; + meta: { + original: ApiImageAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + }; +} + +interface ApiAudioAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'audio'; + meta: { + colors: ApiColorsAttachmentMetaJSON; + original: ApiVideoAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + }; +} + +interface ApiVideoAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'video'; + meta: { + colors: ApiColorsAttachmentMetaJSON; + original: ApiVideoAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + focus: { + x: number; + y: number; + }; + }; +} + +interface ApiGifvAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'gifv'; + meta: { + original: ApiVideoAttachmentMetaJSON; + small: ApiImageAttachmentMetaJSON; + }; +} + +interface ApiUnknownAttachmentJSON extends BaseApiMediaAttachmentJSON { + type: 'unknown'; + meta: unknown; +} + +export type ApiMediaAttachmentJSON = + | ApiImageAttachmentJSON + | ApiAudioAttachmentJSON + | ApiVideoAttachmentJSON + | ApiGifvAttachmentJSON + | ApiUnknownAttachmentJSON; + +export interface ApiImageAttachmentMetaJSON { + width: number; + height: number; + size: string; + aspect: number; +} + +export interface ApiVideoAttachmentMetaJSON { + width: number; + height: number; + frame_rate: string; + duration: number; + bitrate: number; +} + +export interface ApiColorsAttachmentMetaJSON { + background: string; + foreground: string; + accent: string; +} diff --git a/app/javascript/flavours/glitch/api_types/statuses.ts b/app/javascript/flavours/glitch/api_types/statuses.ts index 157be8441d..acf192c12d 100644 --- a/app/javascript/flavours/glitch/api_types/statuses.ts +++ b/app/javascript/flavours/glitch/api_types/statuses.ts @@ -1,6 +1,7 @@ // See app/serializers/rest/status_serializer.rb import type { ApiAccountJSON } from './accounts'; +import type { ApiCollectionJSON } from './collections'; import type { ApiCustomEmojiJSON } from './custom_emoji'; import type { ApiMediaAttachmentJSON } from './media_attachments'; import type { ApiPollJSON } from './polls'; @@ -94,11 +95,11 @@ export interface ApiStatusJSON { url: string; replies_count: number; reblogs_count: number; - favorites_count: number; + favourites_count: number; quotes_count: number; edited_at?: string; - favorited?: boolean; + favourited?: boolean; reblogged?: boolean; muted?: boolean; bookmarked?: boolean; @@ -116,6 +117,7 @@ export interface ApiStatusJSON { tags: ApiTagJSON[]; emojis: ApiCustomEmojiJSON[]; + tagged_collections: ApiCollectionJSON[]; card?: ApiPreviewCardJSON; poll?: ApiPollJSON; @@ -138,6 +140,19 @@ export interface ApiStatusSourceJSON { spoiler_text: string; } +export interface ApiStatusTranslationJSON { + detected_source_language: string; + language: string; + provider: string; + contentHtml: string; + spoilerHtml: string; + spoiler_text: string; + poll?: { + id: string; + options: { title: string }[]; + }; +} + export function isStatusVisibility( visibility: string, ): visibility is StatusVisibility { diff --git a/app/javascript/flavours/glitch/models/status.ts b/app/javascript/flavours/glitch/models/status.ts index 5ca02a3935..45e0ae35fe 100644 --- a/app/javascript/flavours/glitch/models/status.ts +++ b/app/javascript/flavours/glitch/models/status.ts @@ -1,15 +1,103 @@ import type { RecordOf } from 'immutable'; -import type { ApiCollectionJSON } from 'flavours/glitch/api_types/collections'; -import type { ApiPreviewCardJSON } from 'flavours/glitch/api_types/statuses'; +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 { + ApiQuoteJSON, + ApiQuotePolicyJSON, +} from '@/flavours/glitch/api_types/quotes'; +import type { + ApiFilterResultJSON, + ApiMentionJSON, + ApiPreviewCardAuthorJSON, + ApiPreviewCardJSON, + ApiStatusTranslationJSON, + ApiTagJSON, + StatusVisibility, +} from '@/flavours/glitch/api_types/statuses'; -export type { StatusVisibility } from 'flavours/glitch/api_types/statuses'; +export type { StatusVisibility } from '@/flavours/glitch/api_types/statuses'; // Temporary until we type it correctly export type Status = Immutable.Map; -export type Card = RecordOf; +export interface StatusShape { + id: string; + account: string; + created_at: string; + edited_at?: string; + application: { + name: string; + website?: string; + }; + hidden: boolean; + language: string; + muted: boolean; + pinned: boolean; + filtered: FilterResult[]; + sensitive: boolean; + collapsed: boolean; + uri: string; + url: string | null; + + // Content + content: string; + contentHtml: string; + in_reply_to_account_id?: string; + in_reply_to_id?: string; + search_index?: string; + spoilerHtml?: string; + spoiler_text?: string; + + // Embeds + card?: CardShape; + emojis: Pick[]; + media_attachments: MediaAttachmentShape[]; + mentions: ApiMentionJSON[]; + poll?: string; + quote?: Omit & { + quoted_status?: string; + }; + reblog?: string; + tagged_collections: ApiCollectionJSON[]; + tags: ApiTagJSON[]; + translation?: StatusTranslation; + + // Interactions + bookmarked: boolean; + favourited: boolean; + favourites_count: number; + quote_approval: ApiQuotePolicyJSON | null; + quotes_count: number; + reblogged: boolean; + reblogs_count: number; + replies_count: number; + visibility: StatusVisibility; +} + +export type CardShape = Omit & { + authors: (Omit & { + accountId?: string; + })[]; +}; + +export type Card = RecordOf; export type MediaAttachment = Immutable.Map; +export type MediaAttachmentShape = Omit< + ApiMediaAttachmentJSON, + 'remote_url' +> & { + remote_url: string | null; + translation?: string; +}; + export type CollectionAttachment = RecordOf; + +export type FilterResult = Omit & { + filter: string; +}; + +export type StatusTranslation = Omit; diff --git a/app/javascript/flavours/glitch/selectors/statuses.ts b/app/javascript/flavours/glitch/selectors/statuses.ts index 96ea36dc1b..30ed451bea 100644 --- a/app/javascript/flavours/glitch/selectors/statuses.ts +++ b/app/javascript/flavours/glitch/selectors/statuses.ts @@ -1,15 +1,23 @@ -import { createSelector } from '@reduxjs/toolkit'; import type { OrderedSet as ImmutableOrderedSet } from 'immutable'; -import type { RootState } from 'flavours/glitch/store'; +import { createAppSelector } from 'flavours/glitch/store'; -export const getStatusList = createSelector( +import type { StatusShape } from '../models/status'; + +export const getStatusList = createAppSelector( [ - ( - state: RootState, - type: 'favourites' | 'bookmarks' | 'pins' | 'trending', - ) => + (state, type: 'favourites' | 'bookmarks' | 'pins' | 'trending') => state.status_lists.getIn([type, 'items']) as ImmutableOrderedSet, ], (items) => items.toList(), ); + +export const selectPlainStatus = createAppSelector( + [(state, statusId: string) => state.statuses.get(statusId)], + (status) => { + if (!status) { + return null; + } + return status.toJS() as unknown as StatusShape; + }, +); From 2704d25e845d581975ee820ed64ceb5ff093bb47 Mon Sep 17 00:00:00 2001 From: diondiondion Date: Fri, 12 Jun 2026 15:44:34 +0200 Subject: [PATCH 08/10] [Glitch] Fix crash of standalone wrapstodon and embedded status pages Port a78d1150a9c8f385450bb0cfaa947c164c8c69d6 to glitch-soc Signed-off-by: Claire --- .../glitch/components/navigation_focus_target/index.tsx | 8 +------- app/javascript/flavours/glitch/entrypoints/wrapstodon.tsx | 5 ++++- .../flavours/glitch/features/standalone/status/index.tsx | 5 ++++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/javascript/flavours/glitch/components/navigation_focus_target/index.tsx b/app/javascript/flavours/glitch/components/navigation_focus_target/index.tsx index f4b42926a8..ceb26842db 100644 --- a/app/javascript/flavours/glitch/components/navigation_focus_target/index.tsx +++ b/app/javascript/flavours/glitch/components/navigation_focus_target/index.tsx @@ -102,15 +102,9 @@ export const FocusTargetProvider: React.FC<{ export function useFocusOnNavigation(targetName?: string) { const focusTargetRef = useContext(FocusTargetContext); - if (focusTargetRef === null) { - throw Error( - 'useFocusTargetContext must be used inside of a FocusTargetProvider', - ); - } - return useCallback( (element: HTMLElement | null) => { - const focusTarget = focusTargetRef.current; + const focusTarget = focusTargetRef?.current; // Bail out if focusTarget was set to `false` if (!element || !focusTarget) { diff --git a/app/javascript/flavours/glitch/entrypoints/wrapstodon.tsx b/app/javascript/flavours/glitch/entrypoints/wrapstodon.tsx index 4efcf619ad..7bb1ae9128 100644 --- a/app/javascript/flavours/glitch/entrypoints/wrapstodon.tsx +++ b/app/javascript/flavours/glitch/entrypoints/wrapstodon.tsx @@ -5,6 +5,7 @@ import { Provider as ReduxProvider } from 'react-redux'; import { importFetchedStatuses } from '@/flavours/glitch/actions/importer'; import { hydrateStore } from '@/flavours/glitch/actions/store'; import type { ApiAnnualReportResponse } from '@/flavours/glitch/api/annual_report'; +import { FocusTargetProvider } from '@/flavours/glitch/components/navigation_focus_target'; import { Router } from '@/flavours/glitch/components/router'; import { WrapstodonSharedPage } from '@/flavours/glitch/features/annual_report/shared_page'; import { IntlProvider, loadLocale } from '@/flavours/glitch/locales'; @@ -52,7 +53,9 @@ function loaded() { - + + + , diff --git a/app/javascript/flavours/glitch/features/standalone/status/index.tsx b/app/javascript/flavours/glitch/features/standalone/status/index.tsx index aeddc3ef54..ba0f6de239 100644 --- a/app/javascript/flavours/glitch/features/standalone/status/index.tsx +++ b/app/javascript/flavours/glitch/features/standalone/status/index.tsx @@ -11,6 +11,7 @@ import { toggleStatusSpoilers, } from 'flavours/glitch/actions/statuses'; import { hydrateStore } from 'flavours/glitch/actions/store'; +import { FocusTargetProvider } from 'flavours/glitch/components/navigation_focus_target'; import { Router } from 'flavours/glitch/components/router'; import { DetailedStatus } from 'flavours/glitch/features/status/components/detailed_status'; import { useRenderSignal } from 'flavours/glitch/hooks/useRenderSignal'; @@ -86,7 +87,9 @@ export const Status: React.FC<{ id: string }> = ({ id }) => { - + + + From 0d5c0ac85df2c84974136873c87d1d7ed1d31ce8 Mon Sep 17 00:00:00 2001 From: Federico Rao <157750791+Federicorao@users.noreply.github.com> Date: Fri, 12 Jun 2026 16:05:57 +0200 Subject: [PATCH 09/10] [Glitch] Guard against undefined AudioContext in useAudioContext hook Port 61fdcf33303f9be249fbec4f576153bde2c9048b to glitch-soc Signed-off-by: Claire --- .../flavours/glitch/hooks/useAudioContext.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/javascript/flavours/glitch/hooks/useAudioContext.ts b/app/javascript/flavours/glitch/hooks/useAudioContext.ts index 84acf5ac7f..2cad7ee3a5 100644 --- a/app/javascript/flavours/glitch/hooks/useAudioContext.ts +++ b/app/javascript/flavours/glitch/hooks/useAudioContext.ts @@ -18,7 +18,7 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => { const gainNodeRef = useRef(); useEffect(() => { - if (!audioElementRef.current) { + if (!audioElementRef.current || typeof AudioContext === 'undefined') { return; } @@ -43,13 +43,17 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => { }, [audioElementRef]); const playAudio = useCallback(() => { - void audioElementRef.current?.play(); - void audioContextRef.current?.resume(); + if (audioContextRef.current && audioElementRef.current) { + void audioElementRef.current.play(); + void audioContextRef.current.resume(); + } }, [audioElementRef]); const pauseAudio = useCallback(() => { - audioElementRef.current?.pause(); - void audioContextRef.current?.suspend(); + if (audioContextRef.current && audioElementRef.current) { + audioElementRef.current.pause(); + void audioContextRef.current.suspend(); + } }, [audioElementRef]); return { From c469d2288712744a2ac58bffe72837f727465c01 Mon Sep 17 00:00:00 2001 From: Echo Date: Fri, 12 Jun 2026 17:30:27 +0200 Subject: [PATCH 10/10] [Glitch] Emoji: Add back to state Port 963a54664804e60ea8e30795090a0f20de7a48dc to glitch-soc Signed-off-by: Claire --- .../flavours/glitch/actions/importer/emoji.ts | 4 - .../account_edit/modals/fields_modals.tsx | 2 +- .../components/emoji_picker_dropdown.jsx | 5 +- .../glitch/features/emoji/emoji_picker.tsx | 12 +- .../flavours/glitch/features/emoji/index.ts | 61 ++++--- .../flavours/glitch/features/emoji/mode.ts | 2 - .../flavours/glitch/features/emoji/picker.ts | 152 +++++++----------- .../flavours/glitch/features/emoji/render.ts | 2 +- .../flavours/glitch/features/emoji/types.ts | 11 +- .../flavours/glitch/features/emoji/worker.ts | 10 +- .../flavours/glitch/hooks/useCustomEmojis.ts | 53 +++--- .../flavours/glitch/reducers/slices/emojis.ts | 71 ++++++++ .../flavours/glitch/reducers/slices/index.ts | 2 + 13 files changed, 217 insertions(+), 170 deletions(-) create mode 100644 app/javascript/flavours/glitch/reducers/slices/emojis.ts diff --git a/app/javascript/flavours/glitch/actions/importer/emoji.ts b/app/javascript/flavours/glitch/actions/importer/emoji.ts index beccbcebae..8bc6bbbcb3 100644 --- a/app/javascript/flavours/glitch/actions/importer/emoji.ts +++ b/app/javascript/flavours/glitch/actions/importer/emoji.ts @@ -22,10 +22,6 @@ export async function importCustomEmoji(emojis: ApiCustomEmojiJSON[]) { await clearCache('custom'); await loadCustomEmoji(); - const { reloadCustomEmojis } = - await import('@/flavours/glitch/features/emoji/picker'); - await reloadCustomEmojis(); - log('Custom emojis updated, reloaded cache and picker data.'); } } diff --git a/app/javascript/flavours/glitch/features/account_edit/modals/fields_modals.tsx b/app/javascript/flavours/glitch/features/account_edit/modals/fields_modals.tsx index 702122f0fd..ecc043cef5 100644 --- a/app/javascript/flavours/glitch/features/account_edit/modals/fields_modals.tsx +++ b/app/javascript/flavours/glitch/features/account_edit/modals/fields_modals.tsx @@ -140,7 +140,7 @@ export const EditFieldModal = forwardRef< const customEmojis = useCustomEmojis(); const customEmojiCodes = useMemo( - () => Object.keys(customEmojis ?? {}), + () => Object.keys(customEmojis), [customEmojis], ); const checkField = useCallback( diff --git a/app/javascript/flavours/glitch/features/compose/components/emoji_picker_dropdown.jsx b/app/javascript/flavours/glitch/features/compose/components/emoji_picker_dropdown.jsx index 9beaabaaac..6d685ba32f 100644 --- a/app/javascript/flavours/glitch/features/compose/components/emoji_picker_dropdown.jsx +++ b/app/javascript/flavours/glitch/features/compose/components/emoji_picker_dropdown.jsx @@ -323,10 +323,7 @@ class EmojiPickerDropdown extends PureComponent { EmojiPickerAsync().then(EmojiMart => { EmojiPicker = EmojiMart.Picker; Emoji = EmojiMart.Emoji; - - void EmojiMart.loadCustomEmojiData().then(() => { - this.setState({ loading: false }); - }); + this.setState({ loading: false }); }).catch(() => { this.setState({ loading: false, active: false }); }); diff --git a/app/javascript/flavours/glitch/features/emoji/emoji_picker.tsx b/app/javascript/flavours/glitch/features/emoji/emoji_picker.tsx index 39fc548fd6..5a8ddb7ff1 100644 --- a/app/javascript/flavours/glitch/features/emoji/emoji_picker.tsx +++ b/app/javascript/flavours/glitch/features/emoji/emoji_picker.tsx @@ -13,8 +13,6 @@ import { usePickerEmojis } from './picker'; const backgroundImageFnDefault = () => `${assetHost}/emoji/sheet_16_0.png`; -export { fetchCustomEmojiData as loadCustomEmojiData } from './picker'; - export const Picker: FC = ({ set = 'twitter', sheetSize = 32, @@ -24,17 +22,13 @@ export const Picker: FC = ({ ...props }) => { const { mode } = useEmojiAppState(); - const { customCategories, customEmojis } = usePickerEmojis(); - - if (!customEmojis) { - return null; - } + const { categories, emojis } = usePickerEmojis(); return ( ) => { - const { data: message } = event; + tempWorker.addEventListener( + 'message', + (event: MessageEvent) => { + const { data: message } = event; - worker ??= tempWorker; - clearTimeout(timeoutId); + worker ??= tempWorker; + clearTimeout(timeoutId); - if (message !== 'ready') { - workerLog(message); - return; - } + const { type } = message; + if (type === 'log') { + workerLog(message.message); + } else if (type === 'done' && message.storeName === 'custom') { + void loadEmojisToStore(); + } - const debugValue = localStorage.getItem('debug'); - if (debugValue) { - messageWorker({ type: 'debug', debugValue }); - } + if (type !== 'ready') { + return; // Exit for other messages. + } - workerLog('loading data'); - messageWorker(userLocale); - messageWorker('custom'); - messageWorker('shortcodes'); - }); + const debugValue = localStorage.getItem('debug'); + if (debugValue) { + messageWorker({ type: 'debug', debugValue }); + } + + workerLog('loading data'); + messageWorker(userLocale); + messageWorker('custom'); + messageWorker('shortcodes'); + void loadEmojisToStore(); + }, + ); } async function fallbackLoad() { @@ -71,8 +80,8 @@ async function fallbackLoad() { const customEmojis = await importCustomEmojiData(); if (customEmojis && customEmojis.length > 0) { log('loaded %d custom emojis', customEmojis.length); - await reloadCustomEmojis(); } + const shortcodes = await importLegacyShortcodes(); if (shortcodes?.length) { log('loaded %d legacy shortcodes', shortcodes.length); @@ -82,6 +91,7 @@ async function fallbackLoad() { if (emojis) { log('loaded %d emojis to locale %s', emojis.length, userLocale); } + await loadEmojisToStore(); } export async function loadCustomEmoji() { @@ -92,9 +102,9 @@ export async function loadCustomEmoji() { const emojis = await importCustomEmojiData(); if (emojis && emojis.length > 0) { log('loaded %d custom emojis', emojis.length); - await reloadCustomEmojis(); } } + await loadEmojisToStore(); } function messageWorker(data: EmojiWorkerMessage | string) { @@ -110,3 +120,14 @@ function messageWorker(data: EmojiWorkerMessage | string) { worker.postMessage(data); } } + +async function loadEmojisToStore() { + const { store } = await import('@/flavours/glitch/store'); + const { loadCustomEmojis, loadLocale } = + await import('@/flavours/glitch/reducers/slices/emojis'); + + loadLocale(userLocale); + await store.dispatch(loadCustomEmojis()); + + log('loaded emoji data into store'); +} diff --git a/app/javascript/flavours/glitch/features/emoji/mode.ts b/app/javascript/flavours/glitch/features/emoji/mode.ts index 9cd45621cc..769e2859b8 100644 --- a/app/javascript/flavours/glitch/features/emoji/mode.ts +++ b/app/javascript/flavours/glitch/features/emoji/mode.ts @@ -27,7 +27,6 @@ export function useEmojiAppState(): EmojiAppState { return { currentLocale: locale, - locales: [locale], mode, darkTheme: isDarkMode(), assetHost, @@ -64,7 +63,6 @@ export function getEmojiAppState(): EmojiAppState { return { currentLocale, - locales: [currentLocale], mode: determineEmojiMode(emojiStyle), darkTheme: isDarkMode(), assetHost, diff --git a/app/javascript/flavours/glitch/features/emoji/picker.ts b/app/javascript/flavours/glitch/features/emoji/picker.ts index 9e131f7036..f9d3d84cb3 100644 --- a/app/javascript/flavours/glitch/features/emoji/picker.ts +++ b/app/javascript/flavours/glitch/features/emoji/picker.ts @@ -1,83 +1,18 @@ -import { useEffect, useState } from 'react'; - import type { CategoryName, CustomEmoji } from 'emoji-mart'; import { autoPlayGif } from '@/flavours/glitch/initial_state'; +import { + createAppSelector, + useAppSelector, +} from '@/flavours/glitch/store/typed_functions'; import { createLimitedCache } from '@/flavours/glitch/utils/cache'; import { emojiLogger } from './utils'; const log = emojiLogger('picker'); -let customEmojis: CustomEmoji[] | null = null; -let customCategories = [ - 'recent', - 'people', - 'nature', - 'foods', - 'activity', - 'places', - 'objects', - 'symbols', - 'flags', -] as CategoryName[]; - const searchCache = createLimitedCache({ maxSize: 10, log }); -export async function fetchCustomEmojiData() { - if (customEmojis !== null) { - return customEmojis; - } - - const { loadAllCustomEmoji } = await import('./database'); - const emojisRaw = await loadAllCustomEmoji(); - - // If it returns null then custom emojis aren't even loaded yet. - if (emojisRaw === null) { - return []; - } - - // If it's empty, then they are loaded but there aren't any. - if (emojisRaw.length === 0) { - customEmojis = []; - return customEmojis; - } - - const categories = new Set(['custom']); - const emojis = []; - for (const emoji of emojisRaw) { - const name = emoji.shortcode.replaceAll(':', ''); - emojis.push({ - name, - id: name, - custom: true, - short_names: [name], - imageUrl: autoPlayGif ? emoji.url : emoji.static_url, - customCategory: emoji.category, - }); - - if (emoji.category) { - categories.add(`custom-${emoji.category}`); - } - } - - customEmojis = emojis.toSorted((a, b) => { - return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); - }); - customCategories = customCategories.toSpliced( - 1, - 0, - ...(Array.from(categories).toSorted() as CategoryName[]), - ); - log( - 'loaded %d custom emojis in %d categories', - customEmojis.length, - categories.size, - ); - - return customEmojis; -} - type LegacyEmoji = | { id: string; custom?: false; native: string } | { @@ -85,16 +20,6 @@ type LegacyEmoji = custom: true; }; -export async function reloadCustomEmojis() { - customEmojis = null; - - const { loadEmojisIntoCache } = - await import('@/flavours/glitch/hooks/useCustomEmojis'); - - await Promise.all([fetchCustomEmojiData(), loadEmojisIntoCache()]); - searchCache.clear(); -} - // Replicates the old legacy search function. export async function emojiMartSearch( token: string, @@ -127,19 +52,64 @@ export async function emojiMartSearch( return legacyResults; } -export function usePickerEmojis() { - const [, setLoaded] = useState(customEmojis !== null); +const defaultCategories = [ + 'people', + 'nature', + 'foods', + 'activity', + 'places', + 'objects', + 'symbols', + 'flags', +] as CategoryName[]; - useEffect(() => { - if (customEmojis === null) { - void fetchCustomEmojiData().then(() => { - setLoaded(true); - }); +const selectPickerData = createAppSelector( + [(state) => state.emojis.custom, (state) => state.emojis.customCategories], + (emojis, categories) => { + // Create a map of shortcode to category name. + const categoryMap = new Map(); + for (const category in categories) { + const catEmojis = categories[category]; + if (!catEmojis?.length) { + continue; + } + for (const shortcode of catEmojis) { + categoryMap.set(shortcode, category); + } } - }, []); - return { - customEmojis, - customCategories, - }; + const customEmojis: CustomEmoji[] = []; + for (const shortcode in emojis) { + const emoji = emojis[shortcode]; + if (!emoji) { + continue; + } + + customEmojis.push({ + name: shortcode, + id: shortcode, + custom: true, + short_names: [shortcode], + imageUrl: autoPlayGif ? emoji.url : emoji.static_url, + customCategory: categoryMap.get(shortcode), + } as CustomEmoji); + } + + searchCache.clear(); + log('regenerated the picker data'); + + return { + emojis: customEmojis, + categories: [ + 'recent', + 'custom', + ...Object.keys(categories).toSorted(), + ...defaultCategories, + ] as CategoryName[], + }; + }, +); + +export function usePickerEmojis() { + return useAppSelector(selectPickerData); } diff --git a/app/javascript/flavours/glitch/features/emoji/render.ts b/app/javascript/flavours/glitch/features/emoji/render.ts index affa8e0f16..e5ac5bcc6e 100644 --- a/app/javascript/flavours/glitch/features/emoji/render.ts +++ b/app/javascript/flavours/glitch/features/emoji/render.ts @@ -109,7 +109,7 @@ export async function updateHtmlWithEmoji({ }: { element: Element; locale: string; -} & Omit) { +} & Omit) { if (mode === EMOJI_MODE_NATIVE) { return; } diff --git a/app/javascript/flavours/glitch/features/emoji/types.ts b/app/javascript/flavours/glitch/features/emoji/types.ts index ea27a8f699..a5c408903e 100644 --- a/app/javascript/flavours/glitch/features/emoji/types.ts +++ b/app/javascript/flavours/glitch/features/emoji/types.ts @@ -28,7 +28,6 @@ export type CacheKey = | LocaleWithShortcodes; export interface EmojiAppState { - locales: Locale[]; currentLocale: Locale; mode: EmojiMode; darkTheme: boolean; @@ -82,10 +81,20 @@ export type ExtraCustomEmojiMap = Record< >; export type EmojiWorkerMessage = + | { type: 'ready' } | { type: 'load'; storeName: string; } + | { + type: 'done'; + storeName: string; + importCount: number; + } + | { + type: 'log'; + message: string; + } | { type: 'debug'; debugValue: string; diff --git a/app/javascript/flavours/glitch/features/emoji/worker.ts b/app/javascript/flavours/glitch/features/emoji/worker.ts index d4f002daf6..49b7ce45f1 100644 --- a/app/javascript/flavours/glitch/features/emoji/worker.ts +++ b/app/javascript/flavours/glitch/features/emoji/worker.ts @@ -9,13 +9,13 @@ import { import type { EmojiWorkerMessage } from './types'; addEventListener('message', handleMessage); -self.postMessage('ready'); // After the worker is ready, notify the main thread +self.postMessage({ type: 'ready' } satisfies EmojiWorkerMessage); // After the worker is ready, notify the main thread function handleMessage(event: MessageEvent) { const { data } = event; if (data.type === 'debug') { debug.enable(data.debugValue); - } else { + } else if (data.type === 'load') { void loadData(data.storeName); } } @@ -31,6 +31,10 @@ async function loadData(storeName: string) { } if (importCount) { - self.postMessage(`loaded ${importCount} emojis into ${storeName}`); + self.postMessage({ + type: 'done', + storeName, + importCount, + } satisfies EmojiWorkerMessage); } } diff --git a/app/javascript/flavours/glitch/hooks/useCustomEmojis.ts b/app/javascript/flavours/glitch/hooks/useCustomEmojis.ts index 6eb3c17fbf..26b4ebefab 100644 --- a/app/javascript/flavours/glitch/hooks/useCustomEmojis.ts +++ b/app/javascript/flavours/glitch/hooks/useCustomEmojis.ts @@ -1,40 +1,25 @@ -import { useEffect, useState } from 'react'; +import { createAppSelector, useAppSelector } from '@/flavours/glitch/store'; import type { ExtraCustomEmojiMap } from '../features/emoji/types'; -import { emojiLogger } from '../features/emoji/utils'; -let emojis: ExtraCustomEmojiMap | null = null; - -const log = emojiLogger('useCustomEmojis'); +const selectCustomEmojis = createAppSelector( + [(state) => state.emojis.custom], + (custom) => { + const emojis: ExtraCustomEmojiMap = {}; + for (const shortcode in custom) { + const emoji = custom[shortcode]; + if (!emoji) { + continue; + } + emojis[shortcode] = { + shortcode, + ...emoji, + }; + } + return emojis; + }, +); export function useCustomEmojis() { - const [, setLoaded] = useState(emojis !== null); - useEffect(() => { - if (!emojis) { - void loadEmojisIntoCache().then(() => { - setLoaded(true); - }); - } - }, []); - - return emojis; -} - -export async function loadEmojisIntoCache() { - const { loadAllCustomEmoji } = await import('../features/emoji/database'); - const emojisRaw = await loadAllCustomEmoji(); - if (emojisRaw === null) { - log('Custom emojis not loaded yet'); - return; - } - - emojis = {}; - for (const emoji of emojisRaw) { - emojis[emoji.shortcode] = { - url: emoji.url, - shortcode: emoji.shortcode, - static_url: emoji.static_url, - }; - } - log('Loaded %d custom emojis into cache', Object.keys(emojis).length); + return useAppSelector(selectCustomEmojis); } diff --git a/app/javascript/flavours/glitch/reducers/slices/emojis.ts b/app/javascript/flavours/glitch/reducers/slices/emojis.ts new file mode 100644 index 0000000000..df19f23206 --- /dev/null +++ b/app/javascript/flavours/glitch/reducers/slices/emojis.ts @@ -0,0 +1,71 @@ +import type { PayloadAction } from '@reduxjs/toolkit'; +import { createSlice } from '@reduxjs/toolkit'; + +import type { Locale } from 'emojibase'; + +import type { ApiCustomEmojiJSON } from '@/flavours/glitch/api_types/custom_emoji'; +import { toSupportedLocale } from '@/flavours/glitch/features/emoji/locale'; +import { createAsyncThunk } from '@/flavours/glitch/store/typed_functions'; + +interface EmojisState { + custom: Record>; + customCategories: Record; // { name: shortcodes[] } + customLoaded: boolean; + localesLoaded: Locale[]; +} + +const emojisSlice = createSlice({ + name: 'emojis', + initialState: { + custom: {}, + customCategories: {}, + customLoaded: false, + localesLoaded: [], + } as EmojisState, + reducers: { + loadLocale(state, action: PayloadAction) { + const locale = toSupportedLocale(action.payload); + if (!state.localesLoaded.includes(locale)) { + state.localesLoaded.push(locale); + } + }, + }, + extraReducers(builder) { + builder.addAsyncThunk(loadCustomEmojis, { + fulfilled(state, action) { + if (!action.payload?.length) { + return; + } + + for (const emoji of action.payload) { + const { shortcode, category, url, static_url } = emoji; + state.custom[shortcode] = { + url, + static_url, + }; + + if (category) { + state.customCategories[category] ??= []; + if (!state.customCategories[category].includes(shortcode)) { + state.customCategories[category].push(shortcode); + } + } + + state.customLoaded = true; + } + }, + }); + }, +}); + +export const emojis = emojisSlice.reducer; +export const { loadLocale } = emojisSlice.actions; + +export const loadCustomEmojis = createAsyncThunk( + `${emojisSlice.name}/loadCustomEmojis`, + async () => { + const { loadAllCustomEmoji } = + await import('@/flavours/glitch/features/emoji/database'); + return loadAllCustomEmoji(); + }, +); diff --git a/app/javascript/flavours/glitch/reducers/slices/index.ts b/app/javascript/flavours/glitch/reducers/slices/index.ts index 8d4ecd552d..1b94687cc6 100644 --- a/app/javascript/flavours/glitch/reducers/slices/index.ts +++ b/app/javascript/flavours/glitch/reducers/slices/index.ts @@ -1,9 +1,11 @@ import { annualReport } from './annual_report'; import { collections } from './collections'; +import { emojis } from './emojis'; import { profileEdit } from './profile_edit'; export const sliceReducers = { annualReport, collections, + emojis, profileEdit, };