2022-03-31 15:00:31 -07:00
|
|
|
import {
|
|
|
|
Map as ImmutableMap,
|
|
|
|
List as ImmutableList,
|
|
|
|
OrderedSet as ImmutableOrderedSet,
|
2022-03-31 16:10:34 -07:00
|
|
|
fromJS,
|
2022-03-31 15:00:31 -07:00
|
|
|
} from 'immutable';
|
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
|
|
|
|
import { getSettings } from 'soapbox/actions/settings';
|
2023-06-28 19:42:56 -07:00
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
2022-03-31 15:00:31 -07:00
|
|
|
import { getDomain } from 'soapbox/utils/accounts';
|
|
|
|
import { validId } from 'soapbox/utils/auth';
|
2022-11-15 12:46:23 -08:00
|
|
|
import ConfigDB from 'soapbox/utils/config-db';
|
2023-03-03 13:40:39 -08:00
|
|
|
import { getFeatures } from 'soapbox/utils/features';
|
2022-03-31 15:00:31 -07:00
|
|
|
import { shouldFilter } from 'soapbox/utils/timelines';
|
|
|
|
|
2023-07-20 13:03:23 -07:00
|
|
|
import type { EntityStore } from 'soapbox/entity-store/types';
|
2023-02-07 06:38:31 -08:00
|
|
|
import type { ContextType } from 'soapbox/normalizers/filter';
|
2022-04-12 09:52:56 -07:00
|
|
|
import type { ReducerChat } from 'soapbox/reducers/chats';
|
2023-07-20 13:03:23 -07:00
|
|
|
import type { Account as AccountSchema } from 'soapbox/schemas';
|
2022-03-31 15:00:31 -07:00
|
|
|
import type { RootState } from 'soapbox/store';
|
2023-06-28 19:42:56 -07:00
|
|
|
import type { Account, Filter as FilterEntity, Notification, Status } from 'soapbox/types/entities';
|
2022-03-31 15:00:31 -07:00
|
|
|
|
|
|
|
const normalizeId = (id: any): string => typeof id === 'string' ? id : '';
|
|
|
|
|
2023-07-20 13:03:23 -07:00
|
|
|
export function selectAccount(state: RootState, accountId: string) {
|
|
|
|
return state.entities[Entities.ACCOUNTS]?.store[accountId] as AccountSchema | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function selectOwnAccount(state: RootState) {
|
|
|
|
if (state.me) {
|
|
|
|
return selectAccount(state, state.me);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-21 10:59:31 -07:00
|
|
|
export const accountIdsToAccts = (state: RootState, ids: string[]) => ids.map((id) => selectAccount(state, id)!.acct);
|
|
|
|
|
2023-06-28 19:42:56 -07:00
|
|
|
const getAccountBase = (state: RootState, id: string) => state.entities[Entities.ACCOUNTS]?.store[id] as Account | undefined;
|
2022-03-31 15:00:31 -07:00
|
|
|
const getAccountRelationship = (state: RootState, id: string) => state.relationships.get(id);
|
|
|
|
|
|
|
|
export const makeGetAccount = () => {
|
|
|
|
return createSelector([
|
|
|
|
getAccountBase,
|
|
|
|
getAccountRelationship,
|
2023-06-28 19:42:56 -07:00
|
|
|
], (account, relationship) => {
|
|
|
|
if (!account) return null;
|
|
|
|
return { ...account, relationship };
|
2022-03-31 15:00:31 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-07 06:38:31 -08:00
|
|
|
const toServerSideType = (columnType: string): ContextType => {
|
2022-03-31 15:00:31 -07:00
|
|
|
switch (columnType) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case 'home':
|
|
|
|
case 'notifications':
|
|
|
|
case 'public':
|
|
|
|
case 'thread':
|
|
|
|
return columnType;
|
|
|
|
default:
|
2022-08-12 04:47:32 -07:00
|
|
|
if (columnType.includes('list:')) {
|
2022-05-11 14:06:35 -07:00
|
|
|
return 'home';
|
|
|
|
} else {
|
|
|
|
return 'public'; // community, account, hashtag
|
|
|
|
}
|
2022-03-31 15:00:31 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-04 12:17:24 -07:00
|
|
|
type FilterContext = { contextType?: string };
|
2022-03-31 15:00:31 -07:00
|
|
|
|
2022-04-04 12:17:24 -07:00
|
|
|
export const getFilters = (state: RootState, query: FilterContext) => {
|
2022-06-12 11:15:34 -07:00
|
|
|
return state.filters.filter((filter) => {
|
2023-02-07 06:38:31 -08:00
|
|
|
return (!query?.contextType || filter.context.includes(toServerSideType(query.contextType)))
|
|
|
|
&& (filter.expires_at === null || Date.parse(filter.expires_at) > new Date().getTime());
|
2022-03-31 15:00:31 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const escapeRegExp = (string: string) =>
|
|
|
|
string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
export const regexFromFilters = (filters: ImmutableList<FilterEntity>) => {
|
2022-03-31 15:00:31 -07:00
|
|
|
if (filters.size === 0) return null;
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
return new RegExp(filters.map(filter =>
|
|
|
|
filter.keywords.map(keyword => {
|
|
|
|
let expr = escapeRegExp(keyword.keyword);
|
2022-03-31 15:00:31 -07:00
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
if (keyword.whole_word) {
|
|
|
|
if (/^[\w]/.test(expr)) {
|
|
|
|
expr = `\\b${expr}`;
|
|
|
|
}
|
2022-03-31 15:00:31 -07:00
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
if (/[\w]$/.test(expr)) {
|
|
|
|
expr = `${expr}\\b`;
|
|
|
|
}
|
2022-03-31 15:00:31 -07:00
|
|
|
}
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
return expr;
|
|
|
|
}).join('|'),
|
|
|
|
).join('|'), 'i');
|
2022-03-31 15:00:31 -07:00
|
|
|
};
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
const checkFiltered = (index: string, filters: ImmutableList<FilterEntity>) =>
|
|
|
|
filters.reduce((result, filter) =>
|
|
|
|
result.concat(filter.keywords.reduce((result, keyword) => {
|
|
|
|
let expr = escapeRegExp(keyword.keyword);
|
2023-03-03 13:40:39 -08:00
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
if (keyword.whole_word) {
|
|
|
|
if (/^[\w]/.test(expr)) {
|
|
|
|
expr = `\\b${expr}`;
|
|
|
|
}
|
2023-03-03 13:40:39 -08:00
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
if (/[\w]$/.test(expr)) {
|
|
|
|
expr = `${expr}\\b`;
|
|
|
|
}
|
2023-03-03 13:40:39 -08:00
|
|
|
}
|
|
|
|
|
2023-03-04 03:43:27 -08:00
|
|
|
const regex = new RegExp(expr);
|
|
|
|
|
|
|
|
if (regex.test(index)) return result.concat(filter.title);
|
|
|
|
return result;
|
|
|
|
}, ImmutableList<string>())), ImmutableList<string>());
|
2023-03-03 13:40:39 -08:00
|
|
|
|
2022-04-04 12:17:24 -07:00
|
|
|
type APIStatus = { id: string, username?: string };
|
2022-03-31 15:00:31 -07:00
|
|
|
|
|
|
|
export const makeGetStatus = () => {
|
|
|
|
return createSelector(
|
|
|
|
[
|
2023-03-20 18:57:49 -07:00
|
|
|
(state: RootState, { id }: APIStatus) => state.statuses.get(id) as Status | undefined,
|
|
|
|
(state: RootState, { id }: APIStatus) => state.statuses.get(state.statuses.get(id)?.reblog || '') as Status | undefined,
|
2022-03-31 15:00:31 -07:00
|
|
|
(_state: RootState, { username }: APIStatus) => username,
|
|
|
|
getFilters,
|
|
|
|
(state: RootState) => state.me,
|
2023-03-03 13:40:39 -08:00
|
|
|
(state: RootState) => getFeatures(state.instance),
|
2022-03-31 15:00:31 -07:00
|
|
|
],
|
|
|
|
|
2023-06-26 09:50:53 -07:00
|
|
|
(statusBase, statusReblog, username, filters, me, features) => {
|
2023-06-20 13:24:35 -07:00
|
|
|
if (!statusBase) return null;
|
2023-06-26 09:50:53 -07:00
|
|
|
const { account } = statusBase;
|
|
|
|
const accountUsername = account.acct;
|
2022-03-31 15:00:31 -07:00
|
|
|
|
2023-06-26 09:50:53 -07:00
|
|
|
// Must be owner of status if username exists.
|
2022-03-31 15:00:31 -07:00
|
|
|
if (accountUsername !== username && username !== undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-20 18:57:49 -07:00
|
|
|
return statusBase.withMutations((map: Status) => {
|
2022-03-31 15:00:31 -07:00
|
|
|
map.set('reblog', statusReblog || null);
|
2023-03-03 13:40:39 -08:00
|
|
|
|
2023-06-26 09:50:53 -07:00
|
|
|
if ((features.filters) && account.id !== me) {
|
2023-03-03 13:40:39 -08:00
|
|
|
const filtered = checkFiltered(statusReblog?.search_index || statusBase.search_index, filters);
|
|
|
|
|
|
|
|
map.set('filtered', filtered);
|
|
|
|
}
|
2022-03-31 15:00:31 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const makeGetNotification = () => {
|
|
|
|
return createSelector([
|
|
|
|
(_state: RootState, notification: Notification) => notification,
|
2023-07-20 13:03:23 -07:00
|
|
|
(state: RootState, notification: Notification) => selectAccount(state, normalizeId(notification.account)),
|
|
|
|
(state: RootState, notification: Notification) => selectAccount(state, normalizeId(notification.target)),
|
2022-03-31 15:00:31 -07:00
|
|
|
(state: RootState, notification: Notification) => state.statuses.get(normalizeId(notification.status)),
|
|
|
|
], (notification, account, target, status) => {
|
|
|
|
return notification.merge({
|
|
|
|
// @ts-ignore
|
|
|
|
account: account || null,
|
|
|
|
// @ts-ignore
|
|
|
|
target: target || null,
|
|
|
|
// @ts-ignore
|
|
|
|
status: status || null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAccountGallery = createSelector([
|
2022-06-23 15:33:23 -07:00
|
|
|
(state: RootState, id: string) => state.timelines.get(`account:${id}:media`)?.items || ImmutableOrderedSet<string>(),
|
2023-06-23 09:45:12 -07:00
|
|
|
(state: RootState) => state.statuses,
|
|
|
|
], (statusIds, statuses) => {
|
2022-03-31 15:00:31 -07:00
|
|
|
return statusIds.reduce((medias: ImmutableList<any>, statusId: string) => {
|
|
|
|
const status = statuses.get(statusId);
|
|
|
|
if (!status) return medias;
|
|
|
|
if (status.reblog) return medias;
|
|
|
|
|
|
|
|
return medias.concat(
|
2023-06-23 09:41:10 -07:00
|
|
|
status.media_attachments.map(media => media.merge({ status, account: status.account })));
|
2022-12-18 04:17:45 -08:00
|
|
|
}, ImmutableList());
|
|
|
|
});
|
|
|
|
|
|
|
|
export const getGroupGallery = createSelector([
|
|
|
|
(state: RootState, id: string) => state.timelines.get(`group:${id}:media`)?.items || ImmutableOrderedSet<string>(),
|
|
|
|
(state: RootState) => state.statuses,
|
2023-06-23 09:45:12 -07:00
|
|
|
], (statusIds, statuses) => {
|
2022-12-18 04:17:45 -08:00
|
|
|
return statusIds.reduce((medias: ImmutableList<any>, statusId: string) => {
|
|
|
|
const status = statuses.get(statusId);
|
|
|
|
if (!status) return medias;
|
|
|
|
if (status.reblog) return medias;
|
|
|
|
|
|
|
|
return medias.concat(
|
2023-06-23 09:45:12 -07:00
|
|
|
status.media_attachments.map(media => media.merge({ status, account: status.account })));
|
2022-03-31 15:00:31 -07:00
|
|
|
}, ImmutableList());
|
|
|
|
});
|
|
|
|
|
|
|
|
type APIChat = { id: string, last_message: string };
|
|
|
|
|
|
|
|
export const makeGetChat = () => {
|
|
|
|
return createSelector(
|
|
|
|
[
|
2022-06-10 10:56:22 -07:00
|
|
|
(state: RootState, { id }: APIChat) => state.chats.items.get(id) as ReducerChat,
|
2023-07-20 13:03:23 -07:00
|
|
|
(state: RootState, { id }: APIChat) => selectAccount(state, state.chats.items.getIn([id, 'account']) as string),
|
2022-03-31 15:00:31 -07:00
|
|
|
(state: RootState, { last_message }: APIChat) => state.chat_messages.get(last_message),
|
|
|
|
],
|
|
|
|
|
2022-04-12 09:52:56 -07:00
|
|
|
(chat, account, lastMessage) => {
|
|
|
|
if (!chat || !account) return null;
|
2022-03-31 15:00:31 -07:00
|
|
|
|
2022-04-12 09:52:56 -07:00
|
|
|
return chat.withMutations((map) => {
|
|
|
|
// @ts-ignore
|
2022-03-31 15:00:31 -07:00
|
|
|
map.set('account', account);
|
2022-04-12 09:52:56 -07:00
|
|
|
// @ts-ignore
|
2022-03-31 15:00:31 -07:00
|
|
|
map.set('last_message', lastMessage);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const makeGetReport = () => {
|
|
|
|
const getStatus = makeGetStatus();
|
|
|
|
|
|
|
|
return createSelector(
|
|
|
|
[
|
2022-03-31 16:10:34 -07:00
|
|
|
(state: RootState, id: string) => state.admin.reports.get(id),
|
2023-07-20 13:03:23 -07:00
|
|
|
(state: RootState, id: string) => selectAccount(state, state.admin.reports.get(id)?.account || ''),
|
|
|
|
(state: RootState, id: string) => selectAccount(state, state.admin.reports.get(id)?.target_account || ''),
|
2022-05-15 06:11:59 -07:00
|
|
|
(state: RootState, id: string) => ImmutableList(fromJS(state.admin.reports.get(id)?.statuses)).map(
|
2022-03-31 16:10:34 -07:00
|
|
|
statusId => state.statuses.get(normalizeId(statusId)))
|
2022-03-31 15:00:31 -07:00
|
|
|
.filter((s: any) => s)
|
|
|
|
.map((s: any) => getStatus(state, s.toJS())),
|
|
|
|
],
|
|
|
|
|
2022-05-15 06:11:59 -07:00
|
|
|
(report, account, targetAccount, statuses) => {
|
2022-03-31 15:00:31 -07:00
|
|
|
if (!report) return null;
|
2022-05-15 06:11:59 -07:00
|
|
|
return report.withMutations((report) => {
|
|
|
|
// @ts-ignore
|
|
|
|
report.set('account', account);
|
|
|
|
// @ts-ignore
|
|
|
|
report.set('target_account', targetAccount);
|
|
|
|
// @ts-ignore
|
|
|
|
report.set('statuses', statuses);
|
|
|
|
});
|
2022-03-31 15:00:31 -07:00
|
|
|
},
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const getAuthUserIds = createSelector([
|
2022-12-25 15:31:07 -08:00
|
|
|
(state: RootState) => state.auth.users,
|
2022-03-31 15:00:31 -07:00
|
|
|
], authUsers => {
|
2022-12-25 15:31:07 -08:00
|
|
|
return authUsers.reduce((ids: ImmutableOrderedSet<string>, authUser) => {
|
2022-03-31 15:00:31 -07:00
|
|
|
try {
|
2022-12-31 10:48:31 -08:00
|
|
|
const id = authUser.id;
|
2022-03-31 15:00:31 -07:00
|
|
|
return validId(id) ? ids.add(id) : ids;
|
|
|
|
} catch {
|
|
|
|
return ids;
|
|
|
|
}
|
2022-12-25 15:31:07 -08:00
|
|
|
}, ImmutableOrderedSet<string>());
|
2022-03-31 15:00:31 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export const makeGetOtherAccounts = () => {
|
|
|
|
return createSelector([
|
2023-07-20 13:03:23 -07:00
|
|
|
(state: RootState) => state.entities[Entities.ACCOUNTS]?.store as EntityStore<AccountSchema>,
|
2022-03-31 15:00:31 -07:00
|
|
|
getAuthUserIds,
|
|
|
|
(state: RootState) => state.me,
|
|
|
|
],
|
|
|
|
(accounts, authUserIds, me) => {
|
|
|
|
return authUserIds
|
|
|
|
.reduce((list: ImmutableList<any>, id: string) => {
|
|
|
|
if (id === me) return list;
|
2023-07-20 13:03:23 -07:00
|
|
|
const account = accounts[id];
|
2022-03-31 15:00:31 -07:00
|
|
|
return account ? list.push(account) : list;
|
|
|
|
}, ImmutableList());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSimplePolicy = createSelector([
|
|
|
|
(state: RootState) => state.admin.configs,
|
2022-03-31 16:10:34 -07:00
|
|
|
(state: RootState) => state.instance.pleroma.getIn(['metadata', 'federation', 'mrf_simple'], ImmutableMap()) as ImmutableMap<string, any>,
|
2022-03-31 15:00:31 -07:00
|
|
|
], (configs, instancePolicy: ImmutableMap<string, any>) => {
|
|
|
|
return instancePolicy.merge(ConfigDB.toSimplePolicy(configs));
|
|
|
|
});
|
|
|
|
|
2023-07-20 13:03:23 -07:00
|
|
|
const getRemoteInstanceFavicon = (state: RootState, host: string) => {
|
|
|
|
const accounts = state.entities[Entities.ACCOUNTS]?.store as EntityStore<AccountSchema>;
|
|
|
|
const account = Object.entries(accounts).find(([_, account]) => account && getDomain(account) === host)?.[1];
|
|
|
|
return account?.pleroma?.favicon;
|
|
|
|
};
|
2022-03-31 15:00:31 -07:00
|
|
|
|
|
|
|
const getRemoteInstanceFederation = (state: RootState, host: string) => (
|
|
|
|
getSimplePolicy(state)
|
|
|
|
.map(hosts => hosts.includes(host))
|
|
|
|
);
|
|
|
|
|
|
|
|
export const makeGetHosts = () => {
|
|
|
|
return createSelector([getSimplePolicy], (simplePolicy) => {
|
|
|
|
return simplePolicy
|
|
|
|
.deleteAll(['accept', 'reject_deletes', 'report_removal'])
|
|
|
|
.reduce((acc, hosts) => acc.union(hosts), ImmutableOrderedSet())
|
|
|
|
.sort();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const makeGetRemoteInstance = () => {
|
|
|
|
return createSelector([
|
|
|
|
(_state: RootState, host: string) => host,
|
|
|
|
getRemoteInstanceFavicon,
|
|
|
|
getRemoteInstanceFederation,
|
|
|
|
], (host, favicon, federation) => {
|
|
|
|
return ImmutableMap({
|
|
|
|
host,
|
|
|
|
favicon,
|
|
|
|
federation,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
type ColumnQuery = { type: string, prefix?: string };
|
|
|
|
|
|
|
|
export const makeGetStatusIds = () => createSelector([
|
|
|
|
(state: RootState, { type, prefix }: ColumnQuery) => getSettings(state).get(prefix || type, ImmutableMap()),
|
2022-06-23 15:33:23 -07:00
|
|
|
(state: RootState, { type }: ColumnQuery) => state.timelines.get(type)?.items || ImmutableOrderedSet(),
|
2022-06-09 11:56:14 -07:00
|
|
|
(state: RootState) => state.statuses,
|
2023-06-20 12:24:39 -07:00
|
|
|
], (columnSettings: any, statusIds: ImmutableOrderedSet<string>, statuses) => {
|
2022-03-31 15:00:31 -07:00
|
|
|
return statusIds.filter((id: string) => {
|
|
|
|
const status = statuses.get(id);
|
|
|
|
if (!status) return true;
|
|
|
|
return !shouldFilter(status, columnSettings);
|
|
|
|
});
|
|
|
|
});
|