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
17 changes: 1 addition & 16 deletions lib/Activity/Provider/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ abstract class Base implements IProvider {
protected IUserManager $userManager;
protected Manager $manager;

/** @var string[] */
protected array $displayNames = [];

public function __construct(IFactory $languageFactory,
IURLGenerator $url,
Config $config,
Expand Down Expand Up @@ -134,22 +131,10 @@ protected function getFormerRoom(IL10N $l, int $roomId): array {
}

protected function getUser(string $uid): array {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
}

return [
'type' => 'user',
'id' => $uid,
'name' => $this->displayNames[$uid],
'name' => $this->userManager->getDisplayName($uid) ?? $uid,
];
}

protected function getDisplayName(string $uid): string {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
return $user->getDisplayName();
}
return $uid;
}
}
20 changes: 7 additions & 13 deletions lib/Chat/AutoComplete/SearchPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;

class SearchPlugin implements ISearchPlugin {
Expand Down Expand Up @@ -139,18 +138,18 @@ protected function searchUsers(string $search, array $userIds, ISearchResult $se
continue;
}

$user = $this->userManager->get($userId);
if (!$user instanceof IUser) {
$userDisplayName = $this->userManager->getDisplayName($userId);
if ($userDisplayName === null) {
continue;
}

if (strtolower($user->getDisplayName()) === $search) {
$exactMatches[] = $this->createResult('user', $user->getUID(), $user->getDisplayName());
if (strtolower($userDisplayName) === $search) {
$exactMatches[] = $this->createResult('user', $userId, $userDisplayName);
continue;
}

if (stripos($user->getDisplayName(), $search) !== false) {
$matches[] = $this->createResult('user', $user->getUID(), $user->getDisplayName());
if (stripos($userDisplayName, $search) !== false) {
$matches[] = $this->createResult('user', $userId, $userDisplayName);
continue;
}
}
Expand Down Expand Up @@ -207,12 +206,7 @@ protected function searchGuests(string $search, array $attendees, ISearchResult

protected function createResult(string $type, string $uid, string $name): array {
if ($type === 'user' && $name === '') {
$user = $this->userManager->get($uid);
if ($user instanceof IUser) {
$name = $user->getDisplayName();
} else {
$name = $uid;
}
$name = $this->userManager->getDisplayName($uid) ?? $uid;
}

return [
Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,11 @@ protected function shouldMentionedUserBeNotified(string $userId, IComment $comme
// so they can see the room in their room list and
// the notification can be parsed and links to an existing room,
// where they are a participant of.
$user = $this->userManager->get($userId);
$userDisplayName = $this->userManager->getDisplayName($userId);
$this->participantService->addUsers($room, [[
'actorType' => Attendee::ACTOR_USERS,
'actorId' => $userId,
'displayName' => $user ? $user->getDisplayName() : $userId,
'displayName' => $userDisplayName ?? $userId,
]]);
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Chat/Parser/UserMention.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use OCA\Talk\Room;
use OCP\Comments\ICommentsManager;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;

/**
Expand Down Expand Up @@ -91,8 +90,8 @@ public function parseMessage(Message $chatMessage): void {
}

if ($mention['type'] === 'user') {
$user = $this->userManager->get($mention['id']);
if (!$user instanceof IUser) {
$userDisplayName = $this->userManager->getDisplayName($mention['id']);
if ($userDisplayName === null) {
continue;
}
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Controller/CallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;

class CallController extends AEnvironmentAwareController {
Expand Down Expand Up @@ -78,9 +77,9 @@ public function getPeersForCall(): DataResponse {
if ($participant->getAttendee()->getDisplayName()) {
$displayName = $participant->getAttendee()->getDisplayName();
} else {
$user = $this->userManager->get($participant->getAttendee()->getActorId());
if ($user instanceof IUser) {
$displayName = $user->getDisplayName();
$userDisplayName = $this->userManager->getDisplayName($participant->getAttendee()->getActorId());
if ($userDisplayName !== null) {
$displayName = $userDisplayName;
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,11 +1011,11 @@ public function getParticipants(bool $includeStatus = false): DataResponse {

$result['displayName'] = $participant->getAttendee()->getDisplayName();
if (!$result['displayName']) {
$user = $this->userManager->get($userId);
if (!$user instanceof IUser) {
$userDisplayName = $this->userManager->getDisplayName($userId);
if ($userDisplayName === null) {
continue;
}
$result['displayName'] = $user->getDisplayName();
$result['displayName'] = $userDisplayName;
}

if (isset($statuses[$userId])) {
Expand Down
7 changes: 3 additions & 4 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1002,8 +1002,8 @@ public function resolveRoomDisplayName(Room $room, string $userId): string {
}

if ($otherParticipant === '' && $room->getName() !== '') {
$user = $this->userManager->get($room->getName());
$otherParticipant = $user instanceof IUser ? $user->getDisplayName() : $this->l->t('Deleted user (%s)', $room->getName());
$userDisplayName = $this->userManager->getDisplayName($room->getName());
$otherParticipant = $userDisplayName ?? $this->l->t('Deleted user (%s)', $room->getName());
}

return $otherParticipant;
Expand Down Expand Up @@ -1054,8 +1054,7 @@ protected function getRoomNameByParticipants(Room $room): string {
$displayNames = [];

foreach ($users as $participantId) {
$user = $this->userManager->get($participantId);
$displayNames[] = $user instanceof IUser ? $user->getDisplayName() : $participantId;
$displayNames[] = $this->userManager->getDisplayName($participantId) ?? $participantId;
}

$roomName = implode(', ', $displayNames);
Expand Down
26 changes: 13 additions & 13 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
$isGuest = false;
if ($subjectParameters['userType'] === 'users') {
$userId = $subjectParameters['userId'];
$user = $this->userManager->get($userId);
$userDisplayName = $this->userManager->getDisplayName($userId);

if ($user instanceof IUser) {
if ($userDisplayName !== null) {
$richSubjectUser = [
'type' => 'user',
'id' => $userId,
'name' => $user->getDisplayName(),
'name' => $userDisplayName,
];
}
} else {
Expand Down Expand Up @@ -559,8 +559,8 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
$parameters = $notification->getSubjectParameters();
$uid = $parameters['actorId'] ?? $parameters[0];

$user = $this->userManager->get($uid);
if (!$user instanceof IUser) {
$userDisplayName = $this->userManager->getDisplayName($uid);
if ($userDisplayName === null) {
throw new AlreadyProcessedException();
}

Expand All @@ -574,13 +574,13 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
}

$notification
->setParsedSubject(str_replace('{user}', $user->getDisplayName(), $subject))
->setParsedSubject(str_replace('{user}', $userDisplayName, $subject))
->setRichSubject(
$subject, [
'user' => [
'type' => 'user',
'id' => $uid,
'name' => $user->getDisplayName(),
'name' => $userDisplayName,
],
'call' => [
'type' => 'call',
Expand All @@ -599,13 +599,13 @@ protected function parseInvitation(INotification $notification, Room $room, IL10
}

$notification
->setParsedSubject(str_replace(['{user}', '{call}'], [$user->getDisplayName(), $roomName], $subject))
->setParsedSubject(str_replace(['{user}', '{call}'], [$userDisplayName, $roomName], $subject))
->setRichSubject(
$subject, [
'user' => [
'type' => 'user',
'id' => $uid,
'name' => $user->getDisplayName(),
'name' => $userDisplayName,
],
'call' => [
'type' => 'call',
Expand Down Expand Up @@ -639,8 +639,8 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
$parameters = $notification->getSubjectParameters();
$calleeId = $parameters['callee'];
$user = $this->userManager->get($calleeId);
if ($user instanceof IUser) {
$userDisplayName = $this->userManager->getDisplayName($calleeId);
if ($userDisplayName !== null) {
if ($this->notificationManager->isPreparingPushNotification() || $this->participantService->hasActiveSessionsInCall($room)) {
$notification = $this->addActionButton($notification, $l->t('Answer call'));
$subject = $l->t('{user} would like to talk with you');
Expand All @@ -650,13 +650,13 @@ protected function parseCall(INotification $notification, Room $room, IL10N $l):
}

$notification
->setParsedSubject(str_replace('{user}', $user->getDisplayName(), $subject))
->setParsedSubject(str_replace('{user}', $userDisplayName, $subject))
->setRichSubject(
$subject, [
'user' => [
'type' => 'user',
'id' => $calleeId,
'name' => $user->getDisplayName(),
'name' => $userDisplayName,
],
'call' => [
'type' => 'call',
Expand Down
8 changes: 4 additions & 4 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,12 @@ public function ensureOneToOneRoomIsFilled(Room $room): void {
$missingUsers = array_diff($users, $participants);

foreach ($missingUsers as $userId) {
$user = $this->userManager->get($userId);
if ($user instanceof IUser) {
$userDisplayName = $this->userManager->getDisplayName($userId);
if ($userDisplayName !== null) {
$this->addUsers($room, [[
'actorType' => Attendee::ACTOR_USERS,
'actorId' => $user->getUID(),
'displayName' => $user->getDisplayName(),
'actorId' => $userId,
'displayName' => $userDisplayName,
'participantType' => Participant::OWNER,
]]);
}
Expand Down
59 changes: 11 additions & 48 deletions tests/php/Activity/Provider/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,76 +254,39 @@ public function testGetRoom($type, $id, $name, $expectedName, $expectedType) {
], self::invokePrivate($provider, 'getRoom', [$room, 'user']));
}

public function dataGetUser() {
return [
['test', [], false, 'Test'],
['foo', ['admin' => 'Admin'], false, 'Bar'],
['admin', ['admin' => 'Administrator'], true, 'Administrator'],
];
}

/**
* @dataProvider dataGetUser
*
* @param string $uid
* @param array $cache
* @param bool $cacheHit
* @param string $name
*/
public function testGetUser($uid, $cache, $cacheHit, $name) {
$provider = $this->getProvider(['getDisplayName']);

self::invokePrivate($provider, 'displayNames', [$cache]);

if (!$cacheHit) {
$provider->expects($this->once())
->method('getDisplayName')
->with($uid)
->willReturn($name);
} else {
$provider->expects($this->never())
->method('getDisplayName');
}

$result = self::invokePrivate($provider, 'getUser', [$uid]);
$this->assertSame('user', $result['type']);
$this->assertSame($uid, $result['id']);
$this->assertSame($name, $result['name']);
}

public function dataGetDisplayName() {
public function dataGetUser(): array {
return [
['test', true, 'Test'],
['foo', false, 'foo'],
];
}

/**
* @dataProvider dataGetDisplayName
* @dataProvider dataGetUser
*
* @param string $uid
* @param bool $validUser
* @param string $name
*/
public function testGetDisplayName($uid, $validUser, $name) {
public function testGetUser(string $uid, bool $validUser, string $name): void {
$provider = $this->getProvider();

if ($validUser) {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($name);
$this->userManager->expects($this->once())
->method('get')
->method('getDisplayName')
->with($uid)
->willReturn($user);
->willReturn($name);
} else {
$this->userManager->expects($this->once())
->method('get')
->method('getDisplayName')
->with($uid)
->willReturn(null);
}

$this->assertSame($name, self::invokePrivate($provider, 'getDisplayName', [$uid]));
$this->assertSame([
'type' => 'user',
'id' => $uid,
'name' => $name,
], self::invokePrivate($provider, 'getUser', [$uid]));
}
}
Loading