bigbuffet-rw/app/soapbox/utils/status.ts

80 lines
2.7 KiB
TypeScript
Raw Normal View History

import { isIntegerId } from 'soapbox/utils/numbers';
import type { IntlShape } from 'react-intl';
2023-06-20 12:24:39 -07:00
import type { Status } from 'soapbox/schemas';
/** Get the initial visibility of media attachments from user settings. */
2023-06-20 12:24:39 -07:00
export const defaultMediaVisibility = <T extends { reblog: T | string | null } & Pick<Status, 'visibility' | 'sensitive'>>(
status: T | undefined | null,
2023-06-19 14:49:42 -07:00
displayMedia: string,
): boolean => {
if (!status) return false;
2023-06-20 12:24:39 -07:00
status = getActualStatus(status);
const isUnderReview = status.visibility === 'self';
if (isUnderReview) {
return false;
}
2022-09-29 07:44:06 -07:00
return (displayMedia !== 'hide_all' && !status.sensitive || displayMedia === 'show_all');
};
2022-04-24 12:28:07 -07:00
/** Grab the first external link from a status. */
2023-06-19 14:49:42 -07:00
export const getFirstExternalLink = (status: Pick<Status, 'content'>): HTMLAnchorElement | null => {
try {
// Pulled from Pleroma's media parser
const selector = 'a:not(.mention,.hashtag,.attachment,[rel~="tag"])';
const element = document.createElement('div');
element.innerHTML = status.content;
return element.querySelector(selector);
} catch {
return null;
}
};
2022-04-24 12:28:07 -07:00
/** Whether the status is expected to have a Card after it loads. */
2023-06-19 14:49:42 -07:00
export const shouldHaveCard = (status: Pick<Status, 'content'>): boolean => {
return Boolean(getFirstExternalLink(status));
};
2022-04-24 12:28:07 -07:00
/** Whether the media IDs on this status have integer IDs (opposed to FlakeIds). */
// https://gitlab.com/soapbox-pub/soapbox/-/merge_requests/1087
2023-06-19 14:49:42 -07:00
export const hasIntegerMediaIds = (status: Pick<Status, 'media_attachments'>): boolean => {
return status.media_attachments.some(({ id }) => isIntegerId(id));
};
/** Sanitize status text for use with screen readers. */
2023-06-19 14:49:42 -07:00
export const textForScreenReader = (
intl: IntlShape,
status: Pick<Status, 'account' | 'spoiler_text' | 'hidden' | 'search_index' | 'created_at'>,
rebloggedByText?: string,
): string => {
const { account } = status;
if (!account || typeof account !== 'object') return '';
const displayName = account.display_name;
const values = [
displayName.length === 0 ? account.acct.split('@')[0] : displayName,
status.spoiler_text && status.hidden ? status.spoiler_text : status.search_index.slice(status.spoiler_text.length),
intl.formatDate(status.created_at, { hour: '2-digit', minute: '2-digit', month: 'short', day: 'numeric' }),
2023-06-19 14:49:42 -07:00
account.acct,
];
if (rebloggedByText) {
values.push(rebloggedByText);
}
return values.join(', ');
};
2022-08-08 20:26:30 -07:00
/** Get reblogged status if any, otherwise return the original status. */
2023-06-20 12:24:39 -07:00
export const getActualStatus = <T extends { reblog: T | string | null }>(status: T): T => {
2022-08-08 20:26:30 -07:00
if (status?.reblog && typeof status?.reblog === 'object') {
2023-06-20 12:24:39 -07:00
return status.reblog;
2022-08-08 20:26:30 -07:00
} else {
return status;
}
};