Skip to content

Commit

Permalink
Polish. Use try-catch-finally. Some restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey18106 committed Jun 30, 2023
1 parent df4298d commit 8e97371
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 184 deletions.
97 changes: 48 additions & 49 deletions lib/Reference/ProfilePickerReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\Contacts\Reference;


use OC\Collaboration\Reference\LinkReferenceProvider;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\Reference;
Expand All @@ -39,10 +40,7 @@
use OCP\IUserManager;

class ProfilePickerReferenceProvider extends ADiscoverableReferenceProvider {

// private const RICH_OBJECT_TYPE = Application::APP_ID . '_profile_picker';
private const RICH_OBJECT_TYPE = 'users_picker_profile';

private ?string $userId;
private IL10N $l10n;
private IURLGenerator $urlGenerator;
Expand Down Expand Up @@ -105,52 +103,53 @@ public function matchReference(string $referenceText): bool {
* @inheritDoc
*/
public function resolveReference(string $referenceText): ?IReference {
if ($this->matchReference($referenceText)) {
$userId = $this->getObjectId($referenceText);
$user = $this->userManager->get($userId);
if ($user !== null) {
$reference = new Reference($referenceText);

$userDisplayName = $user->getDisplayName();
$userEmail = $user->getEMailAddress();
$userAvatarUrl = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => '64']);

$bio = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
$bio = $bio->getScope() !== IAccountManager::SCOPE_PRIVATE ? $bio->getValue() : null;
$headline = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_HEADLINE);
$location = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ADDRESS);
$website = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_WEBSITE);
$organisation = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ORGANISATION);
$role = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ROLE);

// for clients who can't render the reference widgets
$reference->setTitle($userDisplayName);
$reference->setDescription($userEmail ?? $userDisplayName);
$reference->setImageUrl($userAvatarUrl);

// for the Vue reference widget
$reference->setRichObject(
self::RICH_OBJECT_TYPE,
[
'user_id' => $userId,
'title' => $userDisplayName,
'subline' => $userEmail ?? $userDisplayName,
'email' => $userEmail,
'bio' => isset($bio) && $bio !== '' ? substr_replace($bio, '...', 80, strlen($bio)) : null,
'headline' => $headline->getScope() !== IAccountManager::SCOPE_PRIVATE ? $headline->getValue() : null,
'location' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $location->getValue() : null,
'location_url' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $this->getOpenStreetLocationUrl($location->getValue()) : null,
'website' => $website->getScope() !== IAccountManager::SCOPE_PRIVATE ? $website->getValue() : null,
'organisation' => $organisation->getScope() !== IAccountManager::SCOPE_PRIVATE ? $organisation->getValue() : null,
'role' => $role->getScope() !== IAccountManager::SCOPE_PRIVATE ? $role->getValue() : null,
'url' => $referenceText,
]
);
return $reference;
}
return $this->linkReferenceProvider->resolveReference($referenceText);
if (!$this->matchReference($referenceText)) {
return null;
}
return null;

$userId = $this->getObjectId($referenceText);
$user = $this->userManager->get($userId);
if ($user !== null) {
$reference = new Reference($referenceText);

$userDisplayName = $user->getDisplayName();
$userEmail = $user->getEMailAddress();
$userAvatarUrl = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => '64']);

$bio = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
$bio = $bio->getScope() !== IAccountManager::SCOPE_PRIVATE ? $bio->getValue() : null;
$headline = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_HEADLINE);
$location = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ADDRESS);
$website = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_WEBSITE);
$organisation = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ORGANISATION);
$role = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ROLE);

// for clients who can't render the reference widgets
$reference->setTitle($userDisplayName);
$reference->setDescription($userEmail ?? $userDisplayName);
$reference->setImageUrl($userAvatarUrl);

// for the Vue reference widget
$reference->setRichObject(
self::RICH_OBJECT_TYPE,
[
'user_id' => $userId,
'title' => $userDisplayName,
'subline' => $userEmail ?? $userDisplayName,
'email' => $userEmail,
'bio' => isset($bio) && $bio !== '' ? substr_replace($bio, '...', 80, strlen($bio)) : null,
'headline' => $headline->getScope() !== IAccountManager::SCOPE_PRIVATE ? $headline->getValue() : null,
'location' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $location->getValue() : null,
'location_url' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $this->getOpenStreetLocationUrl($location->getValue()) : null,
'website' => $website->getScope() !== IAccountManager::SCOPE_PRIVATE ? $website->getValue() : null,
'organisation' => $organisation->getScope() !== IAccountManager::SCOPE_PRIVATE ? $organisation->getValue() : null,
'role' => $role->getScope() !== IAccountManager::SCOPE_PRIVATE ? $role->getValue() : null,
'url' => $referenceText,
]
);
return $reference;
}
return $this->linkReferenceProvider->resolveReference($referenceText);
}

private function getObjectId(string $url): ?string {
Expand All @@ -169,7 +168,7 @@ private function getObjectId(string $url): ?string {
return null;
}

private function getOpenStreetLocationUrl($location) {
private function getOpenStreetLocationUrl($location): string {
return 'https://www.openstreetmap.org/search?query=' . urlencode($location);
}

Expand Down
90 changes: 34 additions & 56 deletions src/components/ProfilePicker/ProfilePickerReferenceWidget.vue
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
<!--
- @copyright Copyright (c) 2023 Andrey Borysenko <andrey18106x@gmail.com>
-
- @author 2023 Andrey Borysenko <[email protected]>
-
- @license AGPL-3.0-or-later
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->

<template>
<div class="profile_picker-referece">
<div class="profile_picker-wrapper">
<div class="profile-card-header">
<NcAvatar :user="richObject.user_id" :size="48" class="profile-avatar" />
<div class="profile-title">
<div class="profile-reference">
<div class="profile-reference__wrapper">
<div class="profile-card__header">
<NcAvatar :user="richObject.user_id" :size="48" class="profile-card__avatar" />
<div class="profile-card__title">
<a :href="richObject.url" target="_blank">
<UserIcon :size="20" />
<Account :size="20" />
<strong>
{{ richObject.email !== null ? richObject.title + ' - ' + richObject.email : richObject.title }}
</strong>
</a>
</div>
</div>
<div class="profile-content">
<p class="profile-subline">
<p class="profile-content__subline">
<span v-if="richObject.headline" class="headline">
{{ richObject.headline }}
</span>
Expand Down Expand Up @@ -73,23 +51,23 @@
<script>
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'
import Account from 'vue-material-design-icons/Account.vue'
import MapMarker from 'vue-material-design-icons/MapMarker.vue'
import TextAccount from 'vue-material-design-icons/TextAccount.vue'
import UserIcon from './icons/UserIcon.vue'
import Domain from 'vue-material-design-icons/Domain.vue'
import Web from 'vue-material-design-icons/Web.vue'
import Domain from 'vue-material-design-icons/Domain.vue'
import Handshake from 'vue-material-design-icons/Handshake.vue'
import TextAccount from 'vue-material-design-icons/TextAccount.vue'
export default {
name: 'ProfilePickerReferenceWidget',
components: {
NcAvatar,
Account,
MapMarker,
TextAccount,
UserIcon,
Domain,
Web,
Domain,
Handshake,
TextAccount,
},
props: {
richObjectType: {
Expand All @@ -109,44 +87,44 @@ export default {
</script>

<style scoped lang="scss">
.profile_picker-referece {
.profile-reference {
width: 100%;
white-space: normal;
display: flex;
.profile_picker-wrapper {
.profile-reference__wrapper {
width: 100%;
display: flex;
align-items: center;
flex-direction: column;
.profile-card-header {
.profile-card__header {
width: 100%;
min-height: 70px;
background-color: var(--color-primary);
background-image: var(--gradient-primary-background);
position: relative;
}
.profile-card__avatar {
position: relative;
bottom: -50%;
left: 10px;
}
.profile-avatar {
position: relative;
bottom: -50%;
left: 10px;
.profile-card__title {
display: flex;
position: relative;
bottom: 5px;
left: 70px;
& span {
margin-right: 5px;
}
.profile-title {
& a {
display: flex;
position: relative;
bottom: 5px;
left: 70px;

& span {
margin-right: 5px;
}

& a {
display: flex;
color: #fff;
}
color: #fff;
}
}
Expand All @@ -164,7 +142,7 @@ export default {
padding-left: 5px;
}
.profile-subline {
.profile-content__subline {
padding: 0 0 0 10px;
& span.material-design-icon {
Expand Down
Loading

0 comments on commit 8e97371

Please sign in to comment.