[ADF-5582] Fix avatar rendering logic in adf-node-comments (#10995)

* [ADF-5582] Update NodeCommentsService to take userId as an argument in getUserImage

* [ADF-5582] Add unit tests for avatar caching and retrieval in NodeCommentsService

* Refactor NodeCommentsService unit tests to avoid accessing private members

* [ADF-5582] Remove picture Id from getUserImage

* [ADF-5582] Add comment to getAvatarCache and fix sonarcloud issues

* Update node-comments.service.spec.ts

* Trigger sonar

* [ADF-5582] Add method to return avatar url in people api

* [ADF-5582] Fix sonar cloud issue

* [ADF-5582] Add comment to getUserImage and fix request address for getAvatarImageUrl
This commit is contained in:
Shivangi Shree
2025-08-05 16:54:58 +05:30
committed by GitHub
parent 8ef0aee768
commit 9a0b75caa1
5 changed files with 88 additions and 21 deletions

View File

@@ -38,7 +38,6 @@ describe('NodeCommentsService', () => {
]
});
service = TestBed.inject(NodeCommentsService);
jasmine.Ajax.install();
});
@@ -81,5 +80,17 @@ describe('NodeCommentsService', () => {
responseText: JSON.stringify(fakeContentComments)
});
});
it('should return avatar image URL for given userId', () => {
const userId = 'fake-user-id';
const expectedUrl = 'https://fake-avatar-url.com/avatar.png';
spyOn(service.peopleApi, 'getAvatarImageUrl').and.returnValue(expectedUrl);
const result = service.getUserImage(userId);
expect(service.peopleApi.getAvatarImageUrl).toHaveBeenCalledWith(userId);
expect(result).toBe(expectedUrl);
});
});
});

View File

@@ -16,11 +16,10 @@
*/
import { CommentModel, CommentsService, User } from '@alfresco/adf-core';
import { CommentEntry, CommentsApi, Comment } from '@alfresco/js-api';
import { CommentEntry, CommentsApi, Comment, PeopleApi } from '@alfresco/js-api';
import { Injectable } from '@angular/core';
import { Observable, from } from 'rxjs';
import { map } from 'rxjs/operators';
import { ContentService } from '../../common/services/content.service';
import { AlfrescoApiService } from '../../services/alfresco-api.service';
@Injectable({
@@ -33,7 +32,13 @@ export class NodeCommentsService implements CommentsService {
return this._commentsApi;
}
constructor(private apiService: AlfrescoApiService, private contentService: ContentService) {}
private _peopleApi: PeopleApi;
get peopleApi(): PeopleApi {
this._peopleApi = this._peopleApi ?? new PeopleApi(this.apiService.getInstance());
return this._peopleApi;
}
constructor(private readonly apiService: AlfrescoApiService) {}
/**
* Gets all comments that have been added to a task.
@@ -81,7 +86,13 @@ export class NodeCommentsService implements CommentsService {
});
}
getUserImage(avatarId: string): string {
return this.contentService.getContentUrl(avatarId);
/**
* Gets the avatar image URL for a given user ID.
*
* @param userId ID of the user
* @returns The URL of the user's avatar image
*/
getUserImage(userId: string): string {
return this.peopleApi.getAvatarImageUrl(userId);
}
}

View File

@@ -282,6 +282,21 @@ export class PeopleApi extends BaseApi {
});
}
/**
* Get avatar image URL
*
* Builds and returns the direct URL to fetch the avatar image for the person `personId`.
* This includes the current authentication ticket in the URL to allow secure access.
*
* You can use the `-me-` string in place of <personId> to specify the currently authenticated user.
* @param personId The identifier of a person.
* @returns A string URL to the user's avatar image.
*/
getAvatarImageUrl(personId: string): string {
const ticket = this.alfrescoApi.contentClient.getAlfTicket(undefined);
return `${this.apiClient.basePath}/people/${personId}/avatar?placeholder=true${ticket}`;
}
/**
* Update person
*

View File

@@ -2,17 +2,18 @@
All URIs are relative to *https://localhost/alfresco/api/-default-/public/alfresco/versions/1*
| Method | HTTP request | Description |
|-----------------------------------------------|----------------------------------------------------|------------------------|
| [createPerson](#createPerson) | **POST** /people | Create person |
| [deleteAvatarImage](#deleteAvatarImage) | **DELETE** /people/{personId}/avatar | Delete avatar image |
| [getAvatarImage](#getAvatarImage) | **GET** /people/{personId}/avatar | Get avatar image |
| [getPerson](#getPerson) | **GET** /people/{personId} | Get a person |
| [listPeople](#listPeople) | **GET** /people | List people |
| [requestPasswordReset](#requestPasswordReset) | **POST** /people/{personId}/request-password-reset | Request password reset |
| [resetPassword](#resetPassword) | **POST** /people/{personId}/reset-password | Reset password |
| [updateAvatarImage](#updateAvatarImage) | **PUT** /people/{personId}/avatar | Update avatar image |
| [updatePerson](#updatePerson) | **PUT** /people/{personId} | Update person |
| Method | HTTP request | Description |
|-----------------------------------------------|----------------------------------------------------|-------------------------|
| [createPerson](#createPerson) | **POST** /people | Create person |
| [deleteAvatarImage](#deleteAvatarImage) | **DELETE** /people/{personId}/avatar | Delete avatar image |
| [getAvatarImage](#getAvatarImage) | **GET** /people/{personId}/avatar | Get avatar image |
| [getPerson](#getPerson) | **GET** /people/{personId} | Get a person |
| [listPeople](#listPeople) | **GET** /people | List people |
| [requestPasswordReset](#requestPasswordReset) | **POST** /people/{personId}/request-password-reset | Request password reset |
| [resetPassword](#resetPassword) | **POST** /people/{personId}/reset-password | Reset password |
| [updateAvatarImage](#updateAvatarImage) | **PUT** /people/{personId}/avatar | Update avatar image |
| [getAvatarImageUrl](#getAvatarImageUrl) | **GET** /people/{personId}/avatar?placeholder=true | Returns avatar image url|
| [updatePerson](#updatePerson) | **PUT** /people/{personId} | Update person |
## createPerson
@@ -323,6 +324,35 @@ peopleApi.updateAvatarImage(`<personId>`, contentBodyUpdate).then(() => {
});
```
## getAvatarImageUrl
Returns the direct URL to the avatar image of the specified person.
> this is a local utility method that builds a direct URL to the avatar image, including the current authentication ticket to allow secure access.
> it does not make a network request — it only returns the constructed URL.
You can use the -me- string in place of <personId> to specify the currently authenticated user.
This method is useful for displaying profile images using direct <img> tags or background image URLs.
**Parameters**
| Name | Type | Description |
| ------------ | ------ | --------------------------- |
| **personId** | string | The identifier of a person. |
**Example**
```javascript
import { AlfrescoApi, PeopleApi } from '@alfresco/js-api';
const alfrescoApi = new AlfrescoApi(/*..*/);
const peopleApi = new PeopleApi(alfrescoApi);
const avatarUrl = peopleApi.getAvatarImageUrl('<personId>');
console.log('Avatar URL:', avatarUrl);
```
## updatePerson
Update the given person's details.