Skip to content

Commit

Permalink
[WHIP] Added Profile action API
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@users.noreply.github.com>
  • Loading branch information
skjnldsv authored and Pytal committed Sep 9, 2021
1 parent 73a8074 commit 66ad22f
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 6 deletions.
69 changes: 63 additions & 6 deletions core/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,84 @@

namespace OC\Core\Controller;


use EmailAction;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\UserStatus\IManager;
use OCP\UserStatus\IUserStatus;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountProperty;
use OCP\App\IAppManager;
use OCP\IUserManager;
use OCP\Profile\IActionManager;

class ProfileController extends \OCP\AppFramework\Controller {

/** @var IL10N */
private $l10n;

/** @var IUserSession */
private $userSession;

/** @var IUserManager */
private $userManager;

/** @var IAccountManager */
private $accountManager;

/** @var IInitialState */
private $initialStateService;

/** @var IAppManager */
private $appManager;

/** @var IManager */
private $userStatusManager;

/** @var IActionManager */
// private $actionManager;

public function __construct(
$appName,
IRequest $request,
IL10N $l10n,
IUserSession $userSession,
IUserManager $userManager,
IAccountManager $accountManager,
IInitialState $initialStateService,
IAppManager $appManager
IAppManager $appManager,
IManager $userStatusManager,
// IActionManager $actionManager
) {
parent::__construct($appName, $request);
$this->l10n = $l10n;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->accountManager = $accountManager;
$this->initialStateService = $initialStateService;
$this->appManager = $appManager;
$this->userStatusManager = $userStatusManager;
// $this->actionManager = $actionManager;
}

public const PROPERTY_ACTIONS = [
IAccountManager::PROPERTY_EMAIL,
IAccountManager::PROPERTY_PHONE,
IAccountManager::PROPERTY_WEBSITE,
IAccountManager::PROPERTY_TWITTER,
];

/**
* @NoCSRFRequired
* @UseSession
* FIXME Public page annotation blocks the user session somehow
*/
public function index(string $userId = null): TemplateResponse {
$isLoggedIn = $this->userSession->isLoggedIn();
$account = $this->accountManager->getAccount(\OC::$server->getUserManager()->get($userId));
$account = $this->accountManager->getAccount($this->userManager->get($userId));

$profileEnabled = filter_var(
$account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue(),
Expand All @@ -95,8 +122,7 @@ public function index(string $userId = null): TemplateResponse {
);
}

$status = \OC::$server->get(IManager::class);
$status = $status->getUserStatuses([$userId]);
$status = $this->userStatusManager->getUserStatuses([$userId]);
$status = array_pop($status);

if ($status) {
Expand Down Expand Up @@ -158,4 +184,35 @@ function (IAccountProperty $property) {

return $profileParameters;
}

protected function initActions(IAccount $account) {
foreach(self::PROPERTY_ACTIONS as $property) {
$scope = $account->getProperty($property)->getScope();
$value = $account->getProperty($property)->getValue();

// TODO: handle talk verification
if ($scope === IAccountManager::SCOPE_PRIVATE) {
return;
}

// User is not logged in, we don't display the action
if ($scope === IAccountManager::SCOPE_LOCAL && !$this->userSession->isLoggedIn()) {
return;
}

// TODO: handle federation verification
if ($scope === IAccountManager::SCOPE_FEDERATED && false) {
return;
}

switch ($property) {
case IAccountManager::PROPERTY_EMAIL:
// $this->actionManager->registerAction(new EmailAction($value));
break;

default:
break;
}
}
}
}
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,8 @@
'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php',
'OCP\\Preview\\IProviderV2' => $baseDir . '/lib/public/Preview/IProviderV2.php',
'OCP\\Preview\\IVersionedPreviewFile' => $baseDir . '/lib/public/Preview/IVersionedPreviewFile.php',
'OCP\\Profile\\IActionManager' => $baseDir . '/lib/public/Profile/IActionManager.php',
'OCP\\Profile\\IProfileAction' => $baseDir . '/lib/public/Profile/IProfileAction.php',
'OCP\\Remote\\Api\\IApiCollection' => $baseDir . '/lib/public/Remote/Api/IApiCollection.php',
'OCP\\Remote\\Api\\IApiFactory' => $baseDir . '/lib/public/Remote/Api/IApiFactory.php',
'OCP\\Remote\\Api\\ICapabilitiesApi' => $baseDir . '/lib/public/Remote/Api/ICapabilitiesApi.php',
Expand Down Expand Up @@ -915,6 +917,7 @@
'OC\\Core\\Controller\\OCJSController' => $baseDir . '/core/Controller/OCJSController.php',
'OC\\Core\\Controller\\OCSController' => $baseDir . '/core/Controller/OCSController.php',
'OC\\Core\\Controller\\PreviewController' => $baseDir . '/core/Controller/PreviewController.php',
'OC\\Core\\Controller\\ProfileController' => $baseDir . '/core/Controller/ProfileController.php',
'OC\\Core\\Controller\\RecommendedAppsController' => $baseDir . '/core/Controller/RecommendedAppsController.php',
'OC\\Core\\Controller\\SearchController' => $baseDir . '/core/Controller/SearchController.php',
'OC\\Core\\Controller\\SetupController' => $baseDir . '/core/Controller/SetupController.php',
Expand Down
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php',
'OCP\\Preview\\IProviderV2' => __DIR__ . '/../../..' . '/lib/public/Preview/IProviderV2.php',
'OCP\\Preview\\IVersionedPreviewFile' => __DIR__ . '/../../..' . '/lib/public/Preview/IVersionedPreviewFile.php',
'OCP\\Profile\\IActionManager' => __DIR__ . '/../../..' . '/lib/public/Profile/IActionManager.php',
'OCP\\Profile\\IProfileAction' => __DIR__ . '/../../..' . '/lib/public/Profile/IProfileAction.php',
'OCP\\Remote\\Api\\IApiCollection' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiCollection.php',
'OCP\\Remote\\Api\\IApiFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiFactory.php',
'OCP\\Remote\\Api\\ICapabilitiesApi' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/ICapabilitiesApi.php',
Expand Down Expand Up @@ -944,6 +946,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Controller\\OCJSController' => __DIR__ . '/../../..' . '/core/Controller/OCJSController.php',
'OC\\Core\\Controller\\OCSController' => __DIR__ . '/../../..' . '/core/Controller/OCSController.php',
'OC\\Core\\Controller\\PreviewController' => __DIR__ . '/../../..' . '/core/Controller/PreviewController.php',
'OC\\Core\\Controller\\ProfileController' => __DIR__ . '/../../..' . '/core/Controller/ProfileController.php',
'OC\\Core\\Controller\\RecommendedAppsController' => __DIR__ . '/../../..' . '/core/Controller/RecommendedAppsController.php',
'OC\\Core\\Controller\\SearchController' => __DIR__ . '/../../..' . '/core/Controller/SearchController.php',
'OC\\Core\\Controller\\SetupController' => __DIR__ . '/../../..' . '/core/Controller/SetupController.php',
Expand Down
50 changes: 50 additions & 0 deletions lib/private/Profile/ActionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* 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/>.
*
*/

use OCP\Profile\IActionManager;
use OCP\Profile\IProfileAction;

/**
* @since 23
*/
class ActionManager implements IActionManager {

public function __construct() {}

/** @var array */
protected $actions = [];

/**
* @inheritDoc
*/
public function registerAction(IProfileAction $action) {
$this->actions[] = $action;
}

/**
* @inheritDoc
*/
public function getActions(): array {
return $this->actions;
}
}
70 changes: 70 additions & 0 deletions lib/private/Profile/EmailAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\Profile\IProfileAction;

/**
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* 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/>.
*
*/

class EmailAction implements IProfileAction {

/** @var string */
private $value;

/** @var IL10N */
private $l10n;

/** @var IUrlGenerator */
private $urlGenerator;

/**
* Personal Section constructor.
*
* @param string $appName
* @param IURLGenerator $urlGenerator
* @param IL10N $l10n
*/
public function __construct(string $value,
IL10N $l10n,
IURLGenerator $urlGenerator) {
$this->value = $value;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
}

public function getTitle(): string {
return $this->l10n->t('Mail %s', [$this->value]);
}

public function getPriority(): int {
return 20;
}

public function getIcon(): string {
return 'icon-mail';
}

public function getTarget(): string {
return 'mailto:' . $this->value;
}
}
43 changes: 43 additions & 0 deletions lib/public/Profile/IActionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* 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/>.
*
*/
namespace OCP\Profile;

/**
* @since 23
*/
interface IActionManager {

/**
* Register a new action for the user profile page
*
* @since 23
*/
public function registerAction(IProfileAction $action);

/**
* Returns the list of all registered profile actions
*
* @since 23
*/
public function getActions(): array;
}
Loading

0 comments on commit 66ad22f

Please sign in to comment.