From fd5453eb4434208c2d52b07cdd16778fdc1c986d Mon Sep 17 00:00:00 2001 From: rivexe Date: Tue, 27 Feb 2024 11:01:54 +0300 Subject: [PATCH 1/4] feat: support of user avatar in editors --- appinfo/routes.php | 1 + controller/editorapicontroller.php | 18 +++++++++ controller/editorcontroller.php | 63 ++++++++++++++++++++++++++++++ js/editor.js | 35 ++++++++++++----- 4 files changed, 107 insertions(+), 10 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 0edcc195..cac20538 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -27,6 +27,7 @@ ["name" => "editor#download", "url" => "/downloadas", "verb" => "GET"], ["name" => "editor#index", "url" => "/{fileId}", "verb" => "GET"], ["name" => "editor#public_page", "url" => "/s/{shareToken}", "verb" => "GET"], + ["name" => "editor#user_info", "url" => "/ajax/userInfo", "verb" => "GET"], ["name" => "editor#users", "url" => "/ajax/users", "verb" => "GET"], ["name" => "editor#mention", "url" => "/ajax/mention", "verb" => "POST"], ["name" => "editor#reference", "url" => "/ajax/reference", "verb" => "POST"], diff --git a/controller/editorapicontroller.php b/controller/editorapicontroller.php index 91558b84..4e86bd94 100644 --- a/controller/editorapicontroller.php +++ b/controller/editorapicontroller.php @@ -113,6 +113,13 @@ class EditorApiController extends OCSController { */ private $versionManager; + /** + * Avatar manager + * + * @var IAvatarManager + */ + private $avatarManager; + /** * Tag manager * @@ -167,6 +174,7 @@ public function __construct( $this->versionManager = new VersionManager($AppName, $root); $this->fileUtility = new FileUtility($AppName, $trans, $logger, $config, $shareManager, $session); + $this->avatarManager = \OC::$server->getAvatarManager(); } /** @@ -436,6 +444,16 @@ public function config($fileId, $filePath = null, $shareToken = null, $version = "id" => $this->buildUserId($userId), "name" => $user->getDisplayName() ]; + $avatar = $this->avatarManager->getAvatar($userId); + if ($avatar->exists()) { + $userAvatarUrl = $this->urlGenerator->getAbsoluteURL( + $this->urlGenerator->linkToRoute("core.avatar.getAvatar", [ + "userId" => $userId, + "size" => 64, + ]) + ); + $params["editorConfig"]["user"]["image"] = $userAvatarUrl; + } } $folderLink = null; diff --git a/controller/editorcontroller.php b/controller/editorcontroller.php index db269095..324f1878 100644 --- a/controller/editorcontroller.php +++ b/controller/editorcontroller.php @@ -140,6 +140,13 @@ class EditorController extends Controller { */ private $groupManager; + /** + * Avatar manager + * + * @var IAvatarManager + */ + private $avatarManager; + /** * @param string $AppName - application name * @param IRequest $request - request object @@ -186,6 +193,7 @@ public function __construct( $this->versionManager = new VersionManager($AppName, $root); $this->fileUtility = new FileUtility($AppName, $trans, $logger, $config, $shareManager, $session); + $this->avatarManager = \OC::$server->getAvatarManager(); } /** @@ -415,6 +423,46 @@ public function users($fileId, $operationType = null) { return $result; } + /** + * Get user for Info + * + * @param string $userIds - users identifiers + * + * @return array + * + * @NoAdminRequired + * @NoCSRFRequired + */ + public function userInfo($userIds) { + $result = []; + $userIds = json_decode($userIds, true); + + if ($userIds !== null && is_array($userIds)) { + foreach ($userIds as $userId) { + $userData = []; + $user = $this->userManager->get($this->getUserId($userId)); + if (!empty($user)) { + $userData = [ + "name" => $user->getDisplayName(), + "id" => $userId + ]; + $avatar = $this->avatarManager->getAvatar($user->getUID()); + if ($avatar->exists()) { + $userAvatarUrl = $this->urlGenerator->getAbsoluteURL( + $this->urlGenerator->linkToRoute("core.avatar.getAvatar", [ + "userId" => $user->getUID(), + "size" => 64, + ]) + ); + $userData["image"] = $userAvatarUrl; + } + array_push($result, $userData); + } + } + } + return $result; + } + /** * Send notify about mention * @@ -1460,6 +1508,21 @@ private function limitEnumerationToGroups() { return false; } + /** + * Get Nextcloud userId from unique user identifier + * + * @param string $userId - current user identifier + * + * @return string + */ + private function getUserId($userId) { + if (str_contains($userId, "_")) { + $userIdExp = explode("_", $userId); + $userId = end($userIdExp); + } + return $userId; + } + /** * Print error page * diff --git a/js/editor.js b/js/editor.js index 512df2a0..f637724c 100644 --- a/js/editor.js +++ b/js/editor.js @@ -511,16 +511,31 @@ OCA.Onlyoffice.onRequestUsers = function (event) { let operationType = typeof(event.data.c) !== "undefined" ? event.data.c : null; - $.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/users?fileId={fileId}&operationType=" + operationType, - { - fileId: OCA.Onlyoffice.fileId || 0 - }), - function onSuccess(response) { - OCA.Onlyoffice.docEditor.setUsers({ - "c": operationType, - "users": response - }); - }); + switch (operationType) { + case "info": + $.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/userInfo?userIds={userIds}", + { + userIds: JSON.stringify(event.data.id) + }), + function onSuccess(response) { + OCA.Onlyoffice.docEditor.setUsers({ + "c": operationType, + "users": response + }); + }); + break; + default: + $.get(OC.generateUrl("apps/" + OCA.Onlyoffice.AppName + "/ajax/users?fileId={fileId}&operationType=" + operationType, + { + fileId: OCA.Onlyoffice.fileId || 0 + }), + function onSuccess(response) { + OCA.Onlyoffice.docEditor.setUsers({ + "c": operationType, + "users": response + }); + }); + } }; OCA.Onlyoffice.onRequestReferenceData = function (event) { From f6801961c6da729ec91ccb525d3b0fb3974a2a4e Mon Sep 17 00:00:00 2001 From: rivexe Date: Tue, 27 Feb 2024 11:02:12 +0300 Subject: [PATCH 2/4] docs: user avatar to CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d331cb45..3ff449e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - opening a reference data source - changing a reference data source - setting for disable editors cron check +- support of user avatar in editor ## 8.2.3 ## Added From cf7e5bd125979e4742ddccfce5289efe02fa7a5a Mon Sep 17 00:00:00 2001 From: rivexe Date: Tue, 27 Feb 2024 11:14:10 +0300 Subject: [PATCH 3/4] refactor: format for linter --- controller/editorapicontroller.php | 11 +++++++---- controller/editorcontroller.php | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/controller/editorapicontroller.php b/controller/editorapicontroller.php index 4e86bd94..822b25e7 100644 --- a/controller/editorapicontroller.php +++ b/controller/editorapicontroller.php @@ -447,10 +447,13 @@ public function config($fileId, $filePath = null, $shareToken = null, $version = $avatar = $this->avatarManager->getAvatar($userId); if ($avatar->exists()) { $userAvatarUrl = $this->urlGenerator->getAbsoluteURL( - $this->urlGenerator->linkToRoute("core.avatar.getAvatar", [ - "userId" => $userId, - "size" => 64, - ]) + $this->urlGenerator->linkToRoute( + "core.avatar.getAvatar", + [ + "userId" => $userId, + "size" => 64, + ] + ) ); $params["editorConfig"]["user"]["image"] = $userAvatarUrl; } diff --git a/controller/editorcontroller.php b/controller/editorcontroller.php index 324f1878..4fe95373 100644 --- a/controller/editorcontroller.php +++ b/controller/editorcontroller.php @@ -449,10 +449,13 @@ public function userInfo($userIds) { $avatar = $this->avatarManager->getAvatar($user->getUID()); if ($avatar->exists()) { $userAvatarUrl = $this->urlGenerator->getAbsoluteURL( - $this->urlGenerator->linkToRoute("core.avatar.getAvatar", [ - "userId" => $user->getUID(), - "size" => 64, - ]) + $this->urlGenerator->linkToRoute( + "core.avatar.getAvatar", + [ + "userId" => $userId, + "size" => 64, + ] + ) ); $userData["image"] = $userAvatarUrl; } From 35311949e85ac2de835378430a131d4ccab848ed Mon Sep 17 00:00:00 2001 From: rivexe Date: Mon, 4 Mar 2024 13:13:59 +0300 Subject: [PATCH 4/4] fix: user id in avatar url --- controller/editorcontroller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/editorcontroller.php b/controller/editorcontroller.php index 4fe95373..b2d97e77 100644 --- a/controller/editorcontroller.php +++ b/controller/editorcontroller.php @@ -452,7 +452,7 @@ public function userInfo($userIds) { $this->urlGenerator->linkToRoute( "core.avatar.getAvatar", [ - "userId" => $userId, + "userId" => $user->getUID(), "size" => 64, ] )