Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/release-notes.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "user-avatars",
"private": true,
"version": "3.0.0",
"version": "3.0.1",
"type": "module",
"scripts": {
"build": "vite build",
Expand Down
36 changes: 36 additions & 0 deletions src/client/avatars.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -67,3 +68,38 @@ test('admins see a remove button only for users with an upload', async () => {
render(<AdminUserAvatar data={user(6)} />, { 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) => `
<div class="grid grid-cols-10">
<div><div class="w-10 h-10 rounded-full overflow-hidden"><svg role="img"></svg></div></div>
<div><div class="flex items-center text-foreground"><span>${username}</span><span>&nbsp;-&nbsp;</span></div></div>
</div>`;
const list = document.createElement('div');
list.innerHTML = row('admin') + row('System');
document.body.appendChild(list);

render(<ActivityAvatars />, { wrapper: host.Wrapper });

const [admin, system] = list.querySelectorAll<HTMLElement>('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();
});
89 changes: 89 additions & 0 deletions src/client/components/ActivityAvatars.tsx
Original file line number Diff line number Diff line change
@@ -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<string, string> {
const images = new Map<string, string>();

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<string, string>): void {
for (const svg of root.querySelectorAll<SVGElement>('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;
}
4 changes: 4 additions & 0 deletions src/client/index.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
},
});
9 changes: 9 additions & 0 deletions src/client/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading