Merge branch 'pick-utils' into 'develop'
utils: pick only needed fields See merge request soapbox-pub/soapbox!2564
This commit is contained in:
commit
5d515f0f21
3 changed files with 23 additions and 17 deletions
|
@ -1,7 +1,6 @@
|
||||||
import type { Account } from 'soapbox/schemas';
|
import type { Account } from 'soapbox/schemas';
|
||||||
import type { Account as AccountEntity } from 'soapbox/types/entities';
|
|
||||||
|
|
||||||
const getDomainFromURL = (account: AccountEntity): string => {
|
const getDomainFromURL = (account: Pick<Account, 'url'>): string => {
|
||||||
try {
|
try {
|
||||||
const url = account.url;
|
const url = account.url;
|
||||||
return new URL(url).host;
|
return new URL(url).host;
|
||||||
|
@ -10,12 +9,12 @@ const getDomainFromURL = (account: AccountEntity): string => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDomain = (account: AccountEntity): string => {
|
export const getDomain = (account: Pick<Account, 'acct' | 'url'>): string => {
|
||||||
const domain = account.acct.split('@')[1];
|
const domain = account.acct.split('@')[1];
|
||||||
return domain ? domain : getDomainFromURL(account);
|
return domain ? domain : getDomainFromURL(account);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBaseURL = (account: AccountEntity): string => {
|
export const getBaseURL = (account: Pick<Account, 'url'>): string => {
|
||||||
try {
|
try {
|
||||||
return new URL(account.url).origin;
|
return new URL(account.url).origin;
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -27,12 +26,12 @@ export const getAcct = (account: Pick<Account, 'fqn' | 'acct'>, displayFqn: bool
|
||||||
displayFqn === true ? account.fqn : account.acct
|
displayFqn === true ? account.fqn : account.acct
|
||||||
);
|
);
|
||||||
|
|
||||||
export const isLocal = (account: AccountEntity | Account): boolean => {
|
export const isLocal = (account: Pick<Account, 'acct'>): boolean => {
|
||||||
const domain: string = account.acct.split('@')[1];
|
const domain: string = account.acct.split('@')[1];
|
||||||
return domain === undefined ? true : false;
|
return domain === undefined ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isRemote = (account: AccountEntity): boolean => !isLocal(account);
|
export const isRemote = (account: Pick<Account, 'acct'>): boolean => !isLocal(account);
|
||||||
|
|
||||||
/** Default header filenames from various backends */
|
/** Default header filenames from various backends */
|
||||||
const DEFAULT_HEADERS = [
|
const DEFAULT_HEADERS = [
|
||||||
|
|
|
@ -4,7 +4,7 @@ import type { Ad } from 'soapbox/schemas';
|
||||||
const AD_EXPIRY_THRESHOLD = 5 * 60 * 1000;
|
const AD_EXPIRY_THRESHOLD = 5 * 60 * 1000;
|
||||||
|
|
||||||
/** Whether the ad is expired or about to expire. */
|
/** Whether the ad is expired or about to expire. */
|
||||||
const isExpired = (ad: Ad, threshold = AD_EXPIRY_THRESHOLD): boolean => {
|
const isExpired = (ad: Pick<Ad, 'expires_at'>, threshold = AD_EXPIRY_THRESHOLD): boolean => {
|
||||||
if (ad.expires_at) {
|
if (ad.expires_at) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
return now.getTime() > (new Date(ad.expires_at).getTime() - threshold);
|
return now.getTime() > (new Date(ad.expires_at).getTime() - threshold);
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
import { isIntegerId } from 'soapbox/utils/numbers';
|
import { isIntegerId } from 'soapbox/utils/numbers';
|
||||||
|
|
||||||
import type { IntlShape } from 'react-intl';
|
import type { IntlShape } from 'react-intl';
|
||||||
import type { Status as StatusEntity } from 'soapbox/types/entities';
|
import type { Status } from 'soapbox/types/entities';
|
||||||
|
|
||||||
/** Get the initial visibility of media attachments from user settings. */
|
/** Get the initial visibility of media attachments from user settings. */
|
||||||
export const defaultMediaVisibility = (status: StatusEntity | undefined | null, displayMedia: string): boolean => {
|
export const defaultMediaVisibility = (
|
||||||
|
status: Pick<Status, 'reblog' | 'visibility' | 'sensitive'> | undefined | null,
|
||||||
|
displayMedia: string,
|
||||||
|
): boolean => {
|
||||||
if (!status) return false;
|
if (!status) return false;
|
||||||
|
|
||||||
if (status.reblog && typeof status.reblog === 'object') {
|
if (status.reblog && typeof status.reblog === 'object') {
|
||||||
|
@ -21,7 +24,7 @@ export const defaultMediaVisibility = (status: StatusEntity | undefined | null,
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Grab the first external link from a status. */
|
/** Grab the first external link from a status. */
|
||||||
export const getFirstExternalLink = (status: StatusEntity): HTMLAnchorElement | null => {
|
export const getFirstExternalLink = (status: Pick<Status, 'content'>): HTMLAnchorElement | null => {
|
||||||
try {
|
try {
|
||||||
// Pulled from Pleroma's media parser
|
// Pulled from Pleroma's media parser
|
||||||
const selector = 'a:not(.mention,.hashtag,.attachment,[rel~="tag"])';
|
const selector = 'a:not(.mention,.hashtag,.attachment,[rel~="tag"])';
|
||||||
|
@ -34,18 +37,22 @@ export const getFirstExternalLink = (status: StatusEntity): HTMLAnchorElement |
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Whether the status is expected to have a Card after it loads. */
|
/** Whether the status is expected to have a Card after it loads. */
|
||||||
export const shouldHaveCard = (status: StatusEntity): boolean => {
|
export const shouldHaveCard = (status: Pick<Status, 'content'>): boolean => {
|
||||||
return Boolean(getFirstExternalLink(status));
|
return Boolean(getFirstExternalLink(status));
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Whether the media IDs on this status have integer IDs (opposed to FlakeIds). */
|
/** Whether the media IDs on this status have integer IDs (opposed to FlakeIds). */
|
||||||
// https://gitlab.com/soapbox-pub/soapbox/-/merge_requests/1087
|
// https://gitlab.com/soapbox-pub/soapbox/-/merge_requests/1087
|
||||||
export const hasIntegerMediaIds = (status: StatusEntity): boolean => {
|
export const hasIntegerMediaIds = (status: Pick<Status, 'media_attachments'>): boolean => {
|
||||||
return status.media_attachments.some(({ id }) => isIntegerId(id));
|
return status.media_attachments.some(({ id }) => isIntegerId(id));
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Sanitize status text for use with screen readers. */
|
/** Sanitize status text for use with screen readers. */
|
||||||
export const textForScreenReader = (intl: IntlShape, status: StatusEntity, rebloggedByText?: string): string => {
|
export const textForScreenReader = (
|
||||||
|
intl: IntlShape,
|
||||||
|
status: Pick<Status, 'account' | 'spoiler_text' | 'hidden' | 'search_index' | 'created_at'>,
|
||||||
|
rebloggedByText?: string,
|
||||||
|
): string => {
|
||||||
const { account } = status;
|
const { account } = status;
|
||||||
if (!account || typeof account !== 'object') return '';
|
if (!account || typeof account !== 'object') return '';
|
||||||
|
|
||||||
|
@ -55,7 +62,7 @@ export const textForScreenReader = (intl: IntlShape, status: StatusEntity, reblo
|
||||||
displayName.length === 0 ? account.acct.split('@')[0] : displayName,
|
displayName.length === 0 ? account.acct.split('@')[0] : displayName,
|
||||||
status.spoiler_text && status.hidden ? status.spoiler_text : status.search_index.slice(status.spoiler_text.length),
|
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' }),
|
intl.formatDate(status.created_at, { hour: '2-digit', minute: '2-digit', month: 'short', day: 'numeric' }),
|
||||||
status.getIn(['account', 'acct']),
|
account.acct,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (rebloggedByText) {
|
if (rebloggedByText) {
|
||||||
|
@ -68,12 +75,12 @@ export const textForScreenReader = (intl: IntlShape, status: StatusEntity, reblo
|
||||||
/** Get reblogged status if any, otherwise return the original status. */
|
/** Get reblogged status if any, otherwise return the original status. */
|
||||||
// @ts-ignore The type seems right, but TS doesn't like it.
|
// @ts-ignore The type seems right, but TS doesn't like it.
|
||||||
export const getActualStatus: {
|
export const getActualStatus: {
|
||||||
(status: StatusEntity): StatusEntity
|
<T extends Pick<Status, 'reblog'>>(status: T): T
|
||||||
(status: undefined): undefined
|
(status: undefined): undefined
|
||||||
(status: null): null
|
(status: null): null
|
||||||
} = (status) => {
|
} = <T extends Pick<Status, 'reblog'>>(status: T | null | undefined) => {
|
||||||
if (status?.reblog && typeof status?.reblog === 'object') {
|
if (status?.reblog && typeof status?.reblog === 'object') {
|
||||||
return status.reblog as StatusEntity;
|
return status.reblog as Status;
|
||||||
} else {
|
} else {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue