snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-16 01:21:02 +05:00
commit 30810eecd7
10161 changed files with 987110 additions and 0 deletions

View File

@@ -0,0 +1,160 @@
import type { IUser, AvatarObject } from '@rocket.chat/core-typings';
import { Box, Button, Avatar, IconButton } from '@rocket.chat/fuselage';
import { Field, FieldLabel, FieldRow, FieldError, TextInput } from '@rocket.chat/fuselage-forms';
import { UserAvatar } from '@rocket.chat/ui-avatar';
import { useToastMessageDispatch, useSetting } from '@rocket.chat/ui-contexts';
import type { ChangeEvent } from 'react';
import { useState, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import type { UserAvatarSuggestion } from './UserAvatarSuggestion';
import UserAvatarSuggestions from './UserAvatarSuggestions';
import { readFileAsDataURL } from './readFileAsDataURL';
import { useSingleFileInput } from '../../../hooks/useSingleFileInput';
import { isSafeAvatarUrl } from '../../../lib/utils/isSafeAvatarUrl';
import { isValidImageFormat } from '../../../lib/utils/isValidImageFormat';
export type UserAvatarEditorProps = {
currentUsername: IUser['username'];
username: IUser['username'];
setAvatarObj: (obj: AvatarObject) => void;
disabled?: boolean;
etag: IUser['avatarETag'];
name: IUser['name'];
};
function UserAvatarEditor({ currentUsername, username, setAvatarObj, name, disabled, etag }: UserAvatarEditorProps) {
const { t } = useTranslation();
const useFullNameForDefaultAvatar = useSetting('UI_Use_Name_Avatar');
const rotateImages = useSetting('FileUpload_RotateImages');
const [avatarFromUrl, setAvatarFromUrl] = useState('');
const [newAvatarSource, setNewAvatarSource] = useState<string>();
const dispatchToastMessage = useToastMessageDispatch();
const [avatarUrlError, setAvatarUrlError] = useState<string | undefined>(undefined);
const setUploadedPreview = useCallback(
async (file: File, avatarObj: AvatarObject) => {
setAvatarObj(avatarObj);
try {
const dataURL = await readFileAsDataURL(file);
if (await isValidImageFormat(dataURL)) {
setNewAvatarSource(dataURL);
}
} catch (error) {
dispatchToastMessage({ type: 'error', message: t('Avatar_format_invalid') });
}
},
[setAvatarObj, t, dispatchToastMessage],
);
const [clickUpload] = useSingleFileInput(setUploadedPreview);
const canAddUrl = !disabled && Boolean(avatarFromUrl) && !avatarUrlError;
const handleAddUrl = async (): Promise<void> => {
if (!canAddUrl) {
return;
}
if (!isSafeAvatarUrl(avatarFromUrl)) {
setAvatarUrlError(t('error-invalid-image-url'));
return;
}
if (!(await isValidImageFormat(avatarFromUrl))) {
setAvatarUrlError(t('error-invalid-image-url'));
return;
}
setNewAvatarSource(avatarFromUrl);
setAvatarObj({ avatarUrl: avatarFromUrl });
setAvatarUrlError(undefined);
dispatchToastMessage({ type: 'info', message: t('Avatar_preview_updated') });
};
const clickReset = (): void => {
setNewAvatarSource(`/avatar/%40${useFullNameForDefaultAvatar ? name : username}`);
setAvatarObj('reset');
};
const url = newAvatarSource;
const handleAvatarFromUrlChange = (event: ChangeEvent<HTMLInputElement>): void => {
if (avatarUrlError) {
setAvatarUrlError(undefined);
}
const { value } = event.currentTarget;
setAvatarFromUrl(value);
};
const handleSelectSuggestion = useCallback(
(suggestion: UserAvatarSuggestion) => {
setAvatarObj(suggestion as unknown as AvatarObject);
setNewAvatarSource(suggestion.blob);
},
[setAvatarObj, setNewAvatarSource],
);
return (
<Box display='flex' flexDirection='column' fontScale='p2m' color='default'>
{t('Profile_picture')}
<Box display='flex' flexDirection='row' marginBlockStart={4}>
<UserAvatar
size='x124'
url={url}
key={url}
alt={t('__username__profile_picture', { username: currentUsername || 'user' })}
username={currentUsername || ''}
etag={etag}
objectFit='contain'
style={{
imageOrientation: rotateImages ? 'from-image' : 'none',
}}
onError={() => setAvatarUrlError(t('error-invalid-image-url'))}
/>
<Box display='flex' flexDirection='column' flexGrow='1' marginInlineStart={4}>
<Box display='flex' flexDirection='row' marginBlockStart='none'>
<Button square disabled={disabled} marginInline={4} title={t('Accounts_SetDefaultAvatar')} onClick={clickReset}>
<Avatar url={`/avatar/%40${useFullNameForDefaultAvatar ? name : username}`} />
</Button>
<IconButton icon='upload' secondary disabled={disabled} title={t('Upload')} marginInline={4} onClick={clickUpload} />
<UserAvatarSuggestions disabled={disabled} onSelectOne={handleSelectSuggestion} />
</Box>
<Field paddingInlineStart={4} marginBlockStart={12}>
<FieldLabel>{t('Use_url_for_avatar')}</FieldLabel>
<FieldRow>
<TextInput
placeholder={t('Use_url_for_avatar')}
endAddon={
<IconButton
icon='permalink'
secondary
small
disabled={!canAddUrl}
title={t('Add_URL')}
onClick={handleAddUrl}
marginBlock={-4}
marginInlineEnd={-4}
/>
}
disabled={disabled}
value={avatarFromUrl}
onChange={handleAvatarFromUrlChange}
error={avatarUrlError}
onKeyDown={(event): void => {
if (event.key === 'Enter') {
handleAddUrl();
}
}}
/>
</FieldRow>
{avatarUrlError && <FieldError>{avatarUrlError}</FieldError>}
</Field>
</Box>
</Box>
</Box>
);
}
export default UserAvatarEditor;

View File

@@ -0,0 +1,6 @@
export type UserAvatarSuggestion = {
blob: string;
contentType: string;
service: string;
url: string;
};

View File

@@ -0,0 +1,31 @@
import { Button, Avatar } from '@rocket.chat/fuselage';
import { useCallback } from 'react';
import type { UserAvatarSuggestion } from './UserAvatarSuggestion';
import { useUserAvatarSuggestions } from './useUserAvatarSuggestions';
export type UserAvatarSuggestionsProps = {
disabled?: boolean;
onSelectOne?: (suggestion: UserAvatarSuggestion) => void;
};
function UserAvatarSuggestions({ disabled, onSelectOne }: UserAvatarSuggestionsProps) {
const { data: suggestions = [] } = useUserAvatarSuggestions();
const handleClick = useCallback((suggestion: UserAvatarSuggestion) => () => onSelectOne?.(suggestion), [onSelectOne]);
return (
<>
{suggestions.map(
(suggestion) =>
suggestion.blob && (
<Button key={suggestion.service} square disabled={disabled} marginInline={4} onClick={handleClick(suggestion)}>
<Avatar title={suggestion.service} url={suggestion.blob} />
</Button>
),
)}
</>
);
}
export default UserAvatarSuggestions;

View File

@@ -0,0 +1 @@
export { default } from './UserAvatarEditor';

View File

@@ -0,0 +1,16 @@
export const readFileAsDataURL = (file: File) =>
new Promise<string>((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = (event) => {
const result = event.target?.result;
if (typeof result === 'string') {
resolve(result);
return;
}
reject(new Error('Failed to read file'));
};
reader.onerror = (event) => {
reject(new Error(`Failed to read file: ${event}`));
};
reader.readAsDataURL(file);
});

View File

@@ -0,0 +1,12 @@
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
export const useUserAvatarSuggestions = () => {
const getAvatarSuggestions = useEndpoint('GET', '/v1/users.getAvatarSuggestion');
return useQuery({
queryKey: ['account', 'profile', 'avatar-suggestions'],
queryFn: async () => getAvatarSuggestions(),
select: (data) => Object.values(data.suggestions),
});
};