Skip to content

Commit

Permalink
Merge pull request #462 from ONLYOFFICE/feature/user-avatar
Browse files Browse the repository at this point in the history
Feature/user avatar
  • Loading branch information
LinneyS authored Mar 4, 2024
2 parents 4d0c0ab + 3531194 commit b5a9e83
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
21 changes: 21 additions & 0 deletions controller/editorapicontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class EditorApiController extends OCSController {
*/
private $versionManager;

/**
* Avatar manager
*
* @var IAvatarManager
*/
private $avatarManager;

/**
* Tag manager
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -433,6 +441,19 @@ 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;
Expand Down
66 changes: 66 additions & 0 deletions controller/editorcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -415,6 +423,49 @@ 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
*
Expand Down Expand Up @@ -1477,6 +1528,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
*
Expand Down
35 changes: 25 additions & 10 deletions js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,16 +513,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) {
Expand Down

0 comments on commit b5a9e83

Please sign in to comment.