Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtered notifications that show in admin-x-activitypub #21903

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions apps/admin-x-activitypub/src/components/Activities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import Separator from './global/Separator';
import getUsername from '../utils/get-username';
import stripHtml from '../utils/strip-html';
import truncate from '../utils/truncate';
import {GET_ACTIVITIES_QUERY_KEY_NOTIFICATIONS, useActivitiesForUser} from '../hooks/useActivityPubQueries';
import {
GET_ACTIVITIES_QUERY_KEY_NOTIFICATIONS,
useActivitiesForUser,
useUserDataForUser
} from '../hooks/useActivityPubQueries';
import {type NotificationType} from './activities/NotificationIcon';
import {handleProfileClick} from '../utils/handle-profile-click';

Expand Down Expand Up @@ -144,7 +148,14 @@ const getGroupDescription = (group: GroupedActivity): JSX.Element => {
return <>{actorText} liked your post <span className='font-semibold'>{group.object?.name || ''}</span></>;
case ACTIVITY_TYPE.CREATE:
if (group.object?.inReplyTo && typeof group.object?.inReplyTo !== 'string') {
const content = stripHtml(group.object.inReplyTo.name);
let content = stripHtml(group.object.inReplyTo.content);

// If the post has a name, use that instead of the content (short
// form posts do not have a name)
if (group.object.inReplyTo.name) {
content = stripHtml(group.object.inReplyTo.name);
}

return <>{actorText} replied to your post <span className='font-semibold'>{truncate(content, 80)}</span></>;
}
}
Expand All @@ -153,6 +164,7 @@ const getGroupDescription = (group: GroupedActivity): JSX.Element => {

const Activities: React.FC<ActivitiesProps> = ({}) => {
const user = 'index';

const [openStates, setOpenStates] = React.useState<{[key: string]: boolean}>({});

const toggleOpen = (groupId: string) => {
Expand All @@ -164,19 +176,55 @@ const Activities: React.FC<ActivitiesProps> = ({}) => {

const maxAvatars = 5;

const {data: userProfile, isLoading: isLoadingProfile} = useUserDataForUser(user) as {data: ActorProperties | null, isLoading: boolean};

const {getActivitiesQuery} = useActivitiesForUser({
handle: user,
includeOwn: true,
includeReplies: true,
filter: {
type: ['Follow', 'Like', `Create:Note:isReplyToOwn`]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isReplyToOwn doesn't appear to be working correctly, and will be removed when we switch to a dedicated notifications endpoint

type: ['Follow', 'Like', `Create:Note`]
},
key: GET_ACTIVITIES_QUERY_KEY_NOTIFICATIONS
});

const {data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading} = getActivitiesQuery;
const {data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading: isLoadingActivities} = getActivitiesQuery;

const isLoading = isLoadingProfile === true || isLoadingActivities === true;

const groupedActivities = (data?.pages.flatMap((page) => {
const filtered = page.data.filter((activity, index, self) => index === self.findIndex(a => a.id === activity.id));
const filtered = page.data
// Remove duplicates
.filter(
(activity, index, self) => index === self.findIndex(a => a.id === activity.id)
)
// Remove our own likes
.filter((activity) => {
if (activity.type === ACTIVITY_TYPE.LIKE && activity.actor?.id === userProfile?.id) {
return false;
}

return true;
})
// Remove follower likes if they are not for our own posts
.filter((activity) => {
if (activity.type === ACTIVITY_TYPE.LIKE && activity.object?.attributedTo?.id !== userProfile?.id) {
return false;
}

return true;
})
// Remove create activities that are not replies to our own posts
.filter((activity) => {
if (
activity.type === ACTIVITY_TYPE.CREATE &&
activity.object?.inReplyTo?.attributedTo?.id !== userProfile?.id
) {
return false;
}

return true;
});

return groupActivities(filtered);
}) ?? []);
Expand Down Expand Up @@ -234,6 +282,7 @@ const Activities: React.FC<ActivitiesProps> = ({}) => {
break;
}
};

return (
<>
<MainNavigation page='activities'/>
Expand Down
Loading