Skip to content

Commit

Permalink
[Glitch] Fix “Mention” appearing for otherwise filtered posts
Browse files Browse the repository at this point in the history
Port f75eb1a to glitch-soc

Signed-off-by: Claire <[email protected]>
  • Loading branch information
ClearlyClaire committed Oct 14, 2024
1 parent 1a9be3e commit 9b3aaa9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import type { IconProp } from 'flavours/glitch/components/icon';
import { Icon } from 'flavours/glitch/components/icon';
import Status from 'flavours/glitch/containers/status_container';
import { getStatusHidden } from 'flavours/glitch/selectors/filters';
import { useAppSelector, useAppDispatch } from 'flavours/glitch/store';

import { DisplayedName } from './displayed_name';
Expand Down Expand Up @@ -51,6 +52,12 @@ export const NotificationWithStatus: React.FC<{
(state) => state.statuses.getIn([statusId, 'visibility']) === 'direct',
);

const isFiltered = useAppSelector(
(state) =>
statusId &&
getStatusHidden(state, { id: statusId, contextType: 'notifications' }),
);

const handlers = useMemo(
() => ({
open: () => {
Expand All @@ -77,7 +84,7 @@ export const NotificationWithStatus: React.FC<{
[dispatch, statusId],
);

if (!statusId) return null;
if (!statusId || isFiltered) return null;

return (
<HotKeys handlers={handlers}>
Expand Down
50 changes: 50 additions & 0 deletions app/javascript/flavours/glitch/selectors/filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createSelector } from '@reduxjs/toolkit';

import type { RootState } from 'flavours/glitch/store';
import { toServerSideType } from 'flavours/glitch/utils/filters';

// TODO: move to `app/javascript/flavours/glitch/models` and use more globally
type Filter = Immutable.Map<string, unknown>;

// TODO: move to `app/javascript/flavours/glitch/models` and use more globally
type FilterResult = Immutable.Map<string, unknown>;

export const getFilters = createSelector(
[
(state: RootState) => state.filters as Immutable.Map<string, Filter>,
(_, { contextType }: { contextType: string }) => contextType,
],
(filters, contextType) => {
if (!contextType) {
return null;
}

const now = new Date();
const serverSideType = toServerSideType(contextType);

return filters.filter((filter) => {
const context = filter.get('context') as Immutable.List<string>;
const expiration = filter.get('expires_at') as Date | null;
return (
context.includes(serverSideType) &&
(expiration === null || expiration > now)
);
});
},
);

export const getStatusHidden = (
state: RootState,
{ id, contextType }: { id: string; contextType: string },
) => {
const filters = getFilters(state, { contextType });
if (filters === null) return false;

const filtered = state.statuses.getIn([id, 'filtered']) as
| Immutable.List<FilterResult>
| undefined;
return filtered?.some(
(result) =>
filters.getIn([result.get('filter'), 'filter_action']) === 'hide',
);
};
15 changes: 2 additions & 13 deletions app/javascript/flavours/glitch/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { createSelector } from '@reduxjs/toolkit';
import { List as ImmutableList, Map as ImmutableMap } from 'immutable';

import { toServerSideType } from 'flavours/glitch/utils/filters';

import { me } from '../initial_state';

export { makeGetAccount } from "./accounts";

const getFilters = createSelector([state => state.get('filters'), (_, { contextType }) => contextType], (filters, contextType) => {
if (!contextType) {
return null;
}
import { getFilters } from './filters';

const now = new Date();
const serverSideType = toServerSideType(contextType);

return filters.filter(filter => filter.get('context').includes(serverSideType) && (filter.get('expires_at') === null || filter.get('expires_at') > now));
});
export { makeGetAccount } from "./accounts";

export const makeGetStatus = () => {
return createSelector(
Expand Down

0 comments on commit 9b3aaa9

Please sign in to comment.