Default to whatever content type is supported

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-21 20:39:23 +02:00
parent 47c190cd16
commit c618e1f619
19 changed files with 66 additions and 38 deletions

View file

@ -87,7 +87,7 @@ const baseAccountSchema = v.object({
noindex: v.fallback(v.nullable(v.boolean()), null), noindex: v.fallback(v.nullable(v.boolean()), null),
suspended: v.fallback(v.optional(v.boolean()), undefined), suspended: v.fallback(v.optional(v.boolean()), undefined),
limited: v.fallback(v.optional(v.boolean()), undefined), limited: v.fallback(v.optional(v.boolean()), undefined),
created_at: v.fallback(datetimeSchema, new Date().toUTCString()), created_at: v.fallback(datetimeSchema, new Date().toISOString()),
last_status_at: v.fallback(v.nullable(v.pipe(v.string(), v.isoDate())), null), last_status_at: v.fallback(v.nullable(v.pipe(v.string(), v.isoDate())), null),
statuses_count: v.fallback(v.number(), 0), statuses_count: v.fallback(v.number(), 0),
followers_count: v.fallback(v.number(), 0), followers_count: v.fallback(v.number(), 0),

View file

@ -7,7 +7,7 @@ import { datetimeSchema, filteredArray } from '../utils';
import { adminIpSchema } from './ip'; import { adminIpSchema } from './ip';
/** @see {@link https://docs.joinmastodon.org/entities/Admin_Account/} */ /** @see {@link https://docs.joinmastodon.org/entities/Admin_Account/} */
const adminAccountSchema = v.pipe( const adminAccountSchema = v.pipe(
v.any(), v.any(),
v.transform((account: any) => { v.transform((account: any) => {
if (!account.account) { if (!account.account) {

View file

@ -4,7 +4,7 @@ import * as v from 'valibot';
import { announcementSchema } from '../announcement'; import { announcementSchema } from '../announcement';
/** @see {@link https://docs.pleroma.social/backend/development/API/admin_api/#get-apiv1pleromaadminannouncements} */ /** @see {@link https://docs.pleroma.social/backend/development/API/admin_api/#get-apiv1pleromaadminannouncements} */
const adminAnnouncementSchema = v.pipe( const adminAnnouncementSchema = v.pipe(
v.any(), v.any(),
v.transform((announcement: any) => ({ v.transform((announcement: any) => ({
...announcement, ...announcement,

View file

@ -1,6 +1,6 @@
import * as v from 'valibot'; import * as v from 'valibot';
const adminRelaySchema = v.pipe( const adminRelaySchema = v.pipe(
v.any(), v.any(),
v.transform((data: any) => ({ id: data.actor, ...data })), v.transform((data: any) => ({ id: data.actor, ...data })),
v.object({ v.object({

View file

@ -8,7 +8,7 @@ import { datetimeSchema, filteredArray } from '../utils';
import { adminAccountSchema } from './account'; import { adminAccountSchema } from './account';
/** @see {@link https://docs.joinmastodon.org/entities/Admin_Report/} */ /** @see {@link https://docs.joinmastodon.org/entities/Admin_Report/} */
const adminReportSchema = v.pipe( const adminReportSchema = v.pipe(
v.any(), v.any(),
v.transform((report: any) => { v.transform((report: any) => {
if (report.actor) { if (report.actor) {

View file

@ -203,7 +203,7 @@ const pleromaSchema = coerceObject({
)), )),
enabled: v.fallback(v.boolean(), false), enabled: v.fallback(v.boolean(), false),
}), }),
post_formats: v.fallback(v.optional(v.array(v.string())), undefined), post_formats: v.fallback(v.array(v.string()), ['text/plain']),
restrict_unauthenticated: coerceObject({ restrict_unauthenticated: coerceObject({
activities: coerceObject({ activities: coerceObject({
local: v.fallback(v.boolean(), false), local: v.fallback(v.boolean(), false),

View file

@ -7,7 +7,7 @@ const baseRuleSchema = v.object({
}); });
/** @see {@link https://docs.joinmastodon.org/entities/Rule/} */ /** @see {@link https://docs.joinmastodon.org/entities/Rule/} */
const ruleSchema = v.pipe( const ruleSchema = v.pipe(
v.any(), v.any(),
v.transform((data: any) => ({ v.transform((data: any) => ({
...data, ...data,

View file

@ -3,7 +3,7 @@ import * as v from 'valibot';
import { accountSchema } from './account'; import { accountSchema } from './account';
import { datetimeSchema } from './utils'; import { datetimeSchema } from './utils';
const scrobbleSchema = v.pipe( const scrobbleSchema = v.pipe(
v.any(), v.any(),
v.transform((scrobble: any) => scrobble ? { v.transform((scrobble: any) => scrobble ? {
external_link: scrobble.externalLink, external_link: scrobble.externalLink,

View file

@ -3,7 +3,7 @@ import * as v from 'valibot';
import { accountSchema } from './account'; import { accountSchema } from './account';
/** @see {@link https://docs.joinmastodon.org/entities/Suggestion} */ /** @see {@link https://docs.joinmastodon.org/entities/Suggestion} */
const suggestionSchema = v.pipe( const suggestionSchema = v.pipe(
v.any(), v.any(),
v.transform((suggestion: any) => { v.transform((suggestion: any) => {
/** /**

View file

@ -15,7 +15,7 @@ const translationMediaAttachment = v.object({
}); });
/** @see {@link https://docs.joinmastodon.org/entities/Translation/} */ /** @see {@link https://docs.joinmastodon.org/entities/Translation/} */
const translationSchema = v.pipe( const translationSchema = v.pipe(
v.any(), v.any(),
v.transform((translation: any) => { v.transform((translation: any) => {
/** /**

View file

@ -4,7 +4,7 @@ import { blurhashSchema } from './media-attachment';
import { historySchema } from './tag'; import { historySchema } from './tag';
/** @see {@link https://docs.joinmastodon.org/entities/PreviewCard/#trends-link} */ /** @see {@link https://docs.joinmastodon.org/entities/PreviewCard/#trends-link} */
const trendsLinkSchema = v.pipe( const trendsLinkSchema = v.pipe(
v.any(), v.any(),
v.transform((link: any) => ({ ...link, id: link.url })), v.transform((link: any) => ({ ...link, id: link.url })),
v.object({ v.object({

View file

@ -1000,6 +1000,7 @@ const getFeatures = (instance: Instance) => {
v.software === PLEROMA, v.software === PLEROMA,
v.software === MITRA, v.software === MITRA,
v.software === GOTOSOCIAL, v.software === GOTOSOCIAL,
instance.pleroma.metadata.post_formats.length > 1,
]), ]),
/** /**

View file

@ -1,6 +1,6 @@
{ {
"name": "pl-api", "name": "pl-api",
"version": "0.1.2", "version": "0.1.3",
"type": "module", "type": "module",
"homepage": "https://github.com/mkljczk/pl-fe/tree/fork/packages/pl-api", "homepage": "https://github.com/mkljczk/pl-fe/tree/fork/packages/pl-api",
"repository": { "repository": {

View file

@ -102,7 +102,7 @@
"mini-css-extract-plugin": "^2.9.1", "mini-css-extract-plugin": "^2.9.1",
"multiselect-react-dropdown": "^2.0.25", "multiselect-react-dropdown": "^2.0.25",
"path-browserify": "^1.0.1", "path-browserify": "^1.0.1",
"pl-api": "^0.1.2", "pl-api": "^0.1.3",
"postcss": "^8.4.47", "postcss": "^8.4.47",
"process": "^0.11.10", "process": "^0.11.10",
"punycode": "^2.1.1", "punycode": "^2.1.1",

View file

@ -29,19 +29,27 @@ const ContentTypeButton: React.FC<IContentTypeButton> = ({ composeId }) => {
const handleChange = (contentType: string) => () => dispatch(changeComposeContentType(composeId, contentType)); const handleChange = (contentType: string) => () => dispatch(changeComposeContentType(composeId, contentType));
const options = [ const postFormats = instance.pleroma.metadata.post_formats;
{
const options = [];
if (postFormats.includes('text/plain')) {
options.push({
icon: require('@tabler/icons/outline/pilcrow.svg'), icon: require('@tabler/icons/outline/pilcrow.svg'),
text: intl.formatMessage(messages.content_type_plaintext), text: intl.formatMessage(messages.content_type_plaintext),
value: 'text/plain', value: 'text/plain',
}, });
{ icon: require('@tabler/icons/outline/markdown.svg'), }
if (postFormats.includes('text/markdown')) {
options.push({
icon: require('@tabler/icons/outline/markdown.svg'),
text: intl.formatMessage(messages.content_type_markdown), text: intl.formatMessage(messages.content_type_markdown),
value: 'text/markdown', value: 'text/markdown',
}, });
]; }
if (instance.pleroma.metadata.post_formats?.includes('text/html')) { if (postFormats.includes('text/html')) {
options.push({ options.push({
icon: require('@tabler/icons/outline/html.svg'), icon: require('@tabler/icons/outline/html.svg'),
text: intl.formatMessage(messages.content_type_html), text: intl.formatMessage(messages.content_type_html),
@ -49,11 +57,13 @@ const ContentTypeButton: React.FC<IContentTypeButton> = ({ composeId }) => {
}); });
} }
options.push({ if (postFormats.includes('text/markdown')) {
icon: require('@tabler/icons/outline/text-caption.svg'), options.push({
text: intl.formatMessage(messages.content_type_wysiwyg), icon: require('@tabler/icons/outline/text-caption.svg'),
value: 'wysiwyg', text: intl.formatMessage(messages.content_type_wysiwyg),
}); value: 'wysiwyg',
});
}
const option = options.find(({ value }) => value === contentType); const option = options.find(({ value }) => value === contentType);

View file

@ -9,6 +9,7 @@ import { Mutliselect, SelectDropdown } from 'pl-fe/features/forms';
import SettingToggle from 'pl-fe/features/notifications/components/setting-toggle'; import SettingToggle from 'pl-fe/features/notifications/components/setting-toggle';
import { useAppDispatch } from 'pl-fe/hooks/useAppDispatch'; import { useAppDispatch } from 'pl-fe/hooks/useAppDispatch';
import { useFeatures } from 'pl-fe/hooks/useFeatures'; import { useFeatures } from 'pl-fe/hooks/useFeatures';
import { useInstance } from 'pl-fe/hooks/useInstance';
import { useSettings } from 'pl-fe/hooks/useSettings'; import { useSettings } from 'pl-fe/hooks/useSettings';
import ThemeToggle from '../ui/components/theme-toggle'; import ThemeToggle from '../ui/components/theme-toggle';
@ -98,6 +99,7 @@ const Preferences = () => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const features = useFeatures(); const features = useFeatures();
const settings = useSettings(); const settings = useSettings();
const instance = useInstance();
const onSelectChange = (event: React.ChangeEvent<HTMLSelectElement>, path: string[]) => { const onSelectChange = (event: React.ChangeEvent<HTMLSelectElement>, path: string[]) => {
dispatch(changeSetting(path, event.target.value, { showAlert: true })); dispatch(changeSetting(path, event.target.value, { showAlert: true }));
@ -123,11 +125,17 @@ const Preferences = () => {
private: intl.formatMessage(messages.privacy_followers_only), private: intl.formatMessage(messages.privacy_followers_only),
}), [settings.locale]); }), [settings.locale]);
const defaultContentTypeOptions = React.useMemo(() => ({ const defaultContentTypeOptions = React.useMemo(() => {
'text/plain': intl.formatMessage(messages.content_type_plaintext), const postFormats = instance.pleroma.metadata.post_formats;
'text/markdown': intl.formatMessage(messages.content_type_markdown),
'text/html': intl.formatMessage(messages.content_type_html), const options = Object.entries({
}), [settings.locale]); 'text/plain': intl.formatMessage(messages.content_type_plaintext),
'text/markdown': intl.formatMessage(messages.content_type_markdown),
'text/html': intl.formatMessage(messages.content_type_html),
}).filter(([key]) => postFormats.includes(key));
if (options.length > 1) return Object.fromEntries(options);
}, [settings.locale]);
return ( return (
<Form> <Form>
@ -179,7 +187,7 @@ const Preferences = () => {
</ListItem> </ListItem>
)} )}
{features.richText && ( {features.richText && !!defaultContentTypeOptions && (
<ListItem label={<FormattedMessage id='preferences.fields.content_type_label' defaultMessage='Default post format' />}> <ListItem label={<FormattedMessage id='preferences.fields.content_type_label' defaultMessage='Default post format' />}>
<SelectDropdown <SelectDropdown
className='max-w-[200px]' className='max-w-[200px]'

View file

@ -1,6 +1,7 @@
import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord, fromJS } from 'immutable'; import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord, fromJS } from 'immutable';
import { PLEROMA, type CredentialAccount, type MediaAttachment, type Tag } from 'pl-api'; import { Instance, PLEROMA, type CredentialAccount, type MediaAttachment, type Tag } from 'pl-api';
import { INSTANCE_FETCH_SUCCESS, InstanceAction } from 'pl-fe/actions/instance';
import { isNativeEmoji } from 'pl-fe/features/emoji'; import { isNativeEmoji } from 'pl-fe/features/emoji';
import { tagHistory } from 'pl-fe/settings'; import { tagHistory } from 'pl-fe/settings';
import { hasIntegerMediaIds } from 'pl-fe/utils/status'; import { hasIntegerMediaIds } from 'pl-fe/utils/status';
@ -276,6 +277,12 @@ const importAccount = (compose: Compose, account: CredentialAccount) => {
// } // }
// }; // };
const updateDefaultContentType = (compose: Compose, instance: Instance) => {
const postFormats = instance.pleroma.metadata.post_formats;
return compose.update('content_type', type => postFormats.includes(type) ? type : postFormats.includes('text/markdown') ? 'text/markdown' : postFormats[0]);
};
const updateCompose = (state: State, key: string, updater: (compose: Compose) => Compose) => const updateCompose = (state: State, key: string, updater: (compose: Compose) => Compose) =>
state.update(key, state.get('default')!, updater); state.update(key, state.get('default')!, updater);
@ -283,7 +290,7 @@ const initialState: State = ImmutableMap({
default: ReducerCompose({ idempotencyKey: crypto.randomUUID(), resetFileKey: getResetFileKey() }), default: ReducerCompose({ idempotencyKey: crypto.randomUUID(), resetFileKey: getResetFileKey() }),
}); });
const compose = (state = initialState, action: ComposeAction | EventsAction | MeAction | TimelineAction) => { const compose = (state = initialState, action: ComposeAction | EventsAction | InstanceAction | MeAction | TimelineAction) => {
switch (action.type) { switch (action.type) {
case COMPOSE_TYPE_CHANGE: case COMPOSE_TYPE_CHANGE:
return updateCompose(state, action.composeId, compose => compose.withMutations(map => { return updateCompose(state, action.composeId, compose => compose.withMutations(map => {
@ -589,6 +596,8 @@ const compose = (state = initialState, action: ComposeAction | EventsAction | Me
.set('quote', null)); .set('quote', null));
case COMPOSE_FEDERATED_CHANGE: case COMPOSE_FEDERATED_CHANGE:
return updateCompose(state, action.composeId, compose => compose.update('federated', value => !value)); return updateCompose(state, action.composeId, compose => compose.update('federated', value => !value));
case INSTANCE_FETCH_SUCCESS:
return updateCompose(state, 'default', (compose) => updateDefaultContentType(compose, action.instance));
default: default:
return state; return state;
} }

View file

@ -19,7 +19,7 @@ const settingsSchema = v.object({
deleteModal: v.fallback(v.boolean(), true), deleteModal: v.fallback(v.boolean(), true),
missingDescriptionModal: v.fallback(v.boolean(), true), missingDescriptionModal: v.fallback(v.boolean(), true),
defaultPrivacy: v.fallback(v.picklist(['public', 'unlisted', 'private', 'direct']), 'public'), defaultPrivacy: v.fallback(v.picklist(['public', 'unlisted', 'private', 'direct']), 'public'),
defaultContentType: v.fallback(v.picklist(['text/plain', 'text/markdown']), 'text/plain'), defaultContentType: v.fallback(v.picklist(['text/plain', 'text/markdown', 'text/html']), 'text/plain'),
themeMode: v.fallback(v.picklist(['system', 'light', 'dark', 'black']), 'system'), themeMode: v.fallback(v.picklist(['system', 'light', 'dark', 'black']), 'system'),
locale: v.fallback( locale: v.fallback(
v.pipe( v.pipe(

View file

@ -7570,10 +7570,10 @@ pkg-dir@^4.1.0:
dependencies: dependencies:
find-up "^4.0.0" find-up "^4.0.0"
pl-api@^0.1.2: pl-api@^0.1.3:
version "0.1.2" version "0.1.3"
resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.2.tgz#08794b017f64c58ce128074afdd2144f715359e2" resolved "https://registry.yarnpkg.com/pl-api/-/pl-api-0.1.3.tgz#72d434a0ec8d713e5b227b35497da33cf26975a6"
integrity sha512-HZNrEDvnL1+8yax7lZwe/vCELme8ieNQB6LzUKTQU8qqhMSk7EdLBeEUQok/csyAu0X+IaSsoWKMA91xYzpuGA== integrity sha512-vcl3aGOy3AocQek3+S97QB0jIcF+iV66FvMqrFB/qnfo3Ryte9dcG6XcDxQ5LpogFQAILKk/Mm5uY2W9RZtwHA==
dependencies: dependencies:
blurhash "^2.0.5" blurhash "^2.0.5"
http-link-header "^1.1.3" http-link-header "^1.1.3"