diff --git a/.github/release-notes.md b/.github/release-notes.md index be41093..2e51d65 100644 --- a/.github/release-notes.md +++ b/.github/release-notes.md @@ -1,7 +1,7 @@ Profile picture uploads for Pterodactyl Panel v2. - Users upload a picture from their Account page. It is cropped to a square, resized to 256 pixels and re-encoded. -- It shows in the navigation bar, the admin user list and user page, and server subuser lists. +- It shows in the navigation bar, the admin user list and user page, server subuser lists and activity logs. - Admins can remove any user's picture. - Users without an upload get a default picture: their initial on a coloured circle. Nothing is loaded from Gravatar. diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b6232d..80a8693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.0.1 + +- Pictures now show in activity logs (a server's Activity tab, your account activity and the admin activity page). These lists drew a generated avatar and ignored the user's picture. + ## 3.0.0 - Renamed the extension from `rustix-avatars` to `user-avatars`, so its API and image URLs now start with `/extensions/user-avatars`. Installing it copies pictures from `rustix-avatars`; remove `rustix-avatars` afterwards. diff --git a/README.md b/README.md index 9f317c0..a6c08f5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # User Avatars for Pterodactyl -Let users upload their own profile picture, shown in the navigation bar, admin user lists and server subuser lists. +Let users upload their own profile picture, shown in the navigation bar, admin user lists, server subuser lists and activity logs. ## Features - **Upload a profile picture** from the Account page. PNG, JPEG, WebP and GIF are accepted. - **Safe storage**: every upload is cropped to a square, resized to 256 pixels and re-encoded (WebP when the server supports it), which strips metadata and anything hidden in the file. Pictures are stored outside the web root and only served to signed-in users. -- **Shown across the panel**: the navigation bar, the admin user list, the admin user page, a server's owner in admin, and the server Users list. +- **Shown across the panel**: the navigation bar, the admin user list, the admin user page, a server's owner in admin, the server Users list, and activity logs (a server's Activity tab, account activity and admin activity). - **Admin moderation**: admins can remove a user's picture from the user's admin page. - **A default picture for everyone else**: users without an upload get their initial on a coloured circle. Nothing is loaded from Gravatar or any other outside service. - **Settings** under **Admin → Extensions → User Avatars**: the largest upload size. @@ -17,7 +17,7 @@ Pterodactyl 2.0 has no avatar uploads. Core draws a generated pattern in the nav - Lists that show a user's `image` (admin users, server owner, subusers) get the user's picture because this extension replaces the panel's user transformers with versions that swap in the uploaded URL. If another extension also replaces `Pterodactyl\Transformers\Api\Client\UserTransformer` or `Pterodactyl\Transformers\Api\Admin\UserTransformer`, only one of them will win. - The navigation bar avatar is not a replaceable component in 2.0, so the user's picture is laid over it with CSS that targets the "Account Settings" link. A panel update that changes that link could break it; the rest of the extension is unaffected. -- The activity log keeps the panel's generated avatars. +- Activity rows draw a generated avatar from the username and are not replaceable either. The activity API already includes each actor's picture, so the extension matches rows to it by username and lays the picture over the generated avatar. A panel update that changes the activity row layout could break it. ## API diff --git a/extension.json b/extension.json index bfee848..49d453f 100644 --- a/extension.json +++ b/extension.json @@ -2,8 +2,8 @@ "$schema": "./node_modules/@pterodactyl/sdk/manifest.schema.json", "id": "user-avatars", "name": "User Avatars", - "version": "3.0.0", - "description": "Let users upload their own profile picture, shown in the navigation bar, admin user lists and server subuser lists.", + "version": "3.0.1", + "description": "Let users upload their own profile picture, shown in the navigation bar, admin user lists, server subuser lists and activity logs.", "author": "RustixDev", "icon": "circle-user", "requires": { diff --git a/package-lock.json b/package-lock.json index e72385e..22f41e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "user-avatars", - "version": "3.0.0", + "version": "3.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "user-avatars", - "version": "3.0.0", + "version": "3.0.1", "devDependencies": { "@base-ui/react": "^1.6.0", "@pterodactyl/sdk": "file:./sdk/pterodactyl-sdk-2.0.0-beta.4.tgz", diff --git a/package.json b/package.json index 165275f..59a3aec 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "user-avatars", "private": true, - "version": "3.0.0", + "version": "3.0.1", "type": "module", "scripts": { "build": "vite build", diff --git a/src/client/avatars.spec.tsx b/src/client/avatars.spec.tsx index 9009e3a..c021125 100644 --- a/src/client/avatars.spec.tsx +++ b/src/client/avatars.spec.tsx @@ -2,6 +2,7 @@ import { afterEach, beforeEach, expect, test } from 'vitest'; import { cleanup, render, screen, waitFor } from '@testing-library/react'; import { createExtensionTestHost } from '@pterodactyl/sdk/testing'; import AccountAvatarCard from './components/AccountAvatarCard'; +import ActivityAvatars, { ACTIVITY_CLASS } from './components/ActivityAvatars'; import AdminUserAvatar from './components/AdminUserAvatar'; import NavAvatar, { ROOT_CLASS, ROOT_VARIABLE } from './components/NavAvatar'; import { myAvatarQuery, userAvatarQuery } from './api'; @@ -67,3 +68,38 @@ test('admins see a remove button only for users with an upload', async () => { render(, { wrapper: host.Wrapper }); await waitFor(() => expect(screen.queryByText('Remove profile picture')).toBeNull()); }); + +test('activity rows show the actor picture in place of the generated avatar', async () => { + host.queryClient.setQueryData(['activity-test'], { + object: 'list', + data: [ + { + object: 'activity_log', + attributes: { + event: 'server:settings.rename', + relationships: { + actor: { object: 'user', attributes: { username: 'admin', image: UPLOADED.url } }, + }, + }, + }, + ], + }); + + const row = (username: string) => ` +
+
+
${username} - 
+
`; + const list = document.createElement('div'); + list.innerHTML = row('admin') + row('System'); + document.body.appendChild(list); + + render(, { wrapper: host.Wrapper }); + + const [admin, system] = list.querySelectorAll('div.rounded-full'); + await waitFor(() => expect(admin.classList.contains(ACTIVITY_CLASS)).toBe(true)); + expect(admin.style.backgroundImage).toContain(UPLOADED.url); + expect(system.classList.contains(ACTIVITY_CLASS)).toBe(false); + + list.remove(); +}); diff --git a/src/client/components/ActivityAvatars.tsx b/src/client/components/ActivityAvatars.tsx new file mode 100644 index 0000000..8566404 --- /dev/null +++ b/src/client/components/ActivityAvatars.tsx @@ -0,0 +1,89 @@ +import { useEffect } from 'react'; +import { useQueryClient, type QueryClient } from '@tanstack/react-query'; + +export const ACTIVITY_CLASS = 'uavt-activity-avatar'; + +interface ActorAttributes { + username?: string; + image?: string; +} + +/** Usernames to pictures, from every activity list the panel has loaded with its actors. */ +export function actorImages(client: QueryClient): Map { + const images = new Map(); + + for (const query of client.getQueryCache().getAll()) { + const rows = (query.state.data as { data?: unknown } | undefined)?.data; + if (!Array.isArray(rows)) { + continue; + } + + for (const row of rows) { + if (row?.object !== 'activity_log') { + continue; + } + + const actor = row.attributes?.relationships?.actor?.attributes as ActorAttributes | undefined; + if (actor?.username && actor.image) { + images.set(actor.username, actor.image); + } + } + } + + return images; +} + +/** Put each activity row's picture behind its generated avatar (hidden by styles.css). */ +export function decorateActivityRows(root: ParentNode, images: Map): void { + for (const svg of root.querySelectorAll('div.rounded-full > svg[role="img"]')) { + const circle = svg.parentElement as HTMLElement; + const row = circle.closest('.grid'); + const username = row?.querySelector('.text-foreground > span:first-child')?.textContent?.trim(); + const image = username ? images.get(username) : undefined; + + if (!image) { + if (circle.classList.contains(ACTIVITY_CLASS)) { + circle.classList.remove(ACTIVITY_CLASS); + circle.style.removeProperty('background-image'); + } + continue; + } + + const value = `url(${JSON.stringify(image)})`; + if (circle.style.backgroundImage !== value) { + circle.style.backgroundImage = value; + } + circle.classList.add(ACTIVITY_CLASS); + } +} + +/** + * Activity rows draw a generated avatar from the username and ignore the + * actor's picture, and extensions cannot replace that component. The activity + * API already returns each actor's picture, so this matches rows to it by + * username and shows it in place of the generated one. Renders nothing itself. + */ +export default function ActivityAvatars() { + const client = useQueryClient(); + + useEffect(() => { + let frame = 0; + const run = () => { + cancelAnimationFrame(frame); + frame = requestAnimationFrame(() => decorateActivityRows(document, actorImages(client))); + }; + + const observer = new MutationObserver(run); + observer.observe(document.body, { childList: true, subtree: true }); + const unsubscribe = client.getQueryCache().subscribe(run); + run(); + + return () => { + cancelAnimationFrame(frame); + observer.disconnect(); + unsubscribe(); + }; + }, [client]); + + return null; +} diff --git a/src/client/index.tsx b/src/client/index.tsx index 8934ce1..d6e160b 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -1,6 +1,7 @@ import './styles.css'; import { definePterodactylExtension } from '@pterodactyl/sdk'; import AccountAvatarCard from './components/AccountAvatarCard'; +import ActivityAvatars from './components/ActivityAvatars'; import AdminUserAvatar from './components/AdminUserAvatar'; import NavAvatar from './components/NavAvatar'; @@ -9,5 +10,8 @@ export default definePterodactylExtension({ slots.register('nav.items.after', NavAvatar); slots.register('account.overview.before', AccountAvatarCard); slots.register('panel.users.detail.actions', AdminUserAvatar); + slots.register('server.activity.after', ActivityAvatars); + slots.register('account.activity.after', ActivityAvatars); + slots.register('panel.activity.after', ActivityAvatars); }, }); diff --git a/src/client/styles.css b/src/client/styles.css index 57434ee..6f1a6c3 100644 --- a/src/client/styles.css +++ b/src/client/styles.css @@ -16,3 +16,12 @@ html.uavt-has-avatar a[aria-label='Account Settings'] > span { html.uavt-has-avatar a[aria-label='Account Settings'] > span > svg { visibility: hidden; } + +/* Activity rows: ActivityAvatars sets the actor's picture behind the generated avatar. */ +.uavt-activity-avatar { + background: center / cover no-repeat; +} + +.uavt-activity-avatar > svg { + visibility: hidden; +}