Skip to content

Commit

Permalink
fix: Local avatars prioritized over external avatar provider and remo…
Browse files Browse the repository at this point in the history
…ve remnant references on client of `Accounts_AvatarExternalProviderUrl` (#33296)

Co-authored-by: gabriellsh <[email protected]>
  • Loading branch information
2 people authored and KevLehman committed Sep 20, 2024
1 parent 1fc887c commit da060b1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
9 changes: 9 additions & 0 deletions .changeset/strong-grapes-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@rocket.chat/meteor': patch
---

Fixed remaining direct references to external user avatar URLs

Fixed local avatars having priority over external provider

It mainly corrects the behavior of E2E encryption messages and desktop notifications.
5 changes: 0 additions & 5 deletions apps/meteor/app/utils/client/getUserAvatarURL.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { settings } from '../../settings/client';
import { getAvatarURL } from './getAvatarURL';

export const getUserAvatarURL = function (username: string, cache = ''): string | undefined {
const externalSource = (settings.get<string>('Accounts_AvatarExternalProviderUrl') || '').trim().replace(/\/$/, '');
if (externalSource !== '') {
return externalSource.replace('{username}', username);
}
if (username == null) {
return;
}
Expand Down
5 changes: 0 additions & 5 deletions apps/meteor/app/utils/server/getUserAvatarURL.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { settings } from '../../settings/server';
import { getAvatarURL } from './getAvatarURL';

export const getUserAvatarURL = function (username: string, cache = ''): string | undefined {
const externalSource = (settings.get<string>('Accounts_AvatarExternalProviderUrl') || '').trim().replace(/\/$/, '');
if (externalSource !== '') {
return externalSource.replace('{username}', username);
}
if (username == null) {
return;
}
Expand Down
8 changes: 2 additions & 6 deletions apps/meteor/client/providers/AvatarUrlProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AvatarUrlContext, useSetting } from '@rocket.chat/ui-contexts';
import { AvatarUrlContext } from '@rocket.chat/ui-contexts';
import type { ReactNode } from 'react';
import React, { useMemo } from 'react';

Expand All @@ -10,19 +10,15 @@ type AvatarUrlProviderProps = {
};

const AvatarUrlProvider = ({ children }: AvatarUrlProviderProps) => {
const cdnAvatarUrl = String(useSetting('CDN_PREFIX') || '');
const contextValue = useMemo(
() => ({
getUserPathAvatar: ((): ((uid: string, etag?: string) => string) => {
if (cdnAvatarUrl) {
return (uid: string, etag?: string): string => `${cdnAvatarUrl}/avatar/${uid}${etag ? `?etag=${etag}` : ''}`;
}
return (uid: string, etag?: string): string => getURL(`/avatar/${uid}${etag ? `?etag=${etag}` : ''}`);
})(),
getRoomPathAvatar: ({ type, ...room }: any): string =>
roomCoordinator.getRoomDirectives(type || room.t).getAvatarPath({ username: room._id, ...room }) || '',
}),
[cdnAvatarUrl],
[],
);

return <AvatarUrlContext.Provider children={children} value={contextValue} />;
Expand Down
14 changes: 7 additions & 7 deletions apps/meteor/server/routes/avatar/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export const userAvatar = async function (req, res) {
return;
}

if (settings.get('Accounts_AvatarExternalProviderUrl')) {
const response = await fetch(settings.get('Accounts_AvatarExternalProviderUrl').replace('{username}', requestUsername));
response.headers.forEach((value, key) => res.setHeader(key, value));
response.body.pipe(res);
return;
}

const reqModifiedHeader = req.headers['if-modified-since'];

const file = await Avatars.findOneByName(requestUsername);
Expand All @@ -52,13 +59,6 @@ export const userAvatar = async function (req, res) {
return FileUpload.get(file, req, res);
}

if (settings.get('Accounts_AvatarExternalProviderUrl')) {
const response = await fetch(settings.get('Accounts_AvatarExternalProviderUrl').replace('{username}', requestUsername));
response.headers.forEach((value, key) => res.setHeader(key, value));
response.body.pipe(res);
return;
}

// if still using "letters fallback"
if (!wasFallbackModified(reqModifiedHeader, res)) {
res.writeHead(304);
Expand Down

0 comments on commit da060b1

Please sign in to comment.