Skip to content

Commit

Permalink
allowing for background updates of social avatars
Browse files Browse the repository at this point in the history
Signed-off-by: call-me-matt <nextcloud@matthiasheinisch.de>
  • Loading branch information
call-me-matt committed Aug 4, 2020
1 parent b78f4d6 commit 973c288
Show file tree
Hide file tree
Showing 14 changed files with 581 additions and 27 deletions.
9 changes: 0 additions & 9 deletions .editorconfig

This file was deleted.

File renamed without changes.
5 changes: 5 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
</navigation>
</navigations>

<background-jobs>
<job>OCA\Contacts\Cron\SocialUpdateRegistration</job>
</background-jobs>

<settings>
<admin>OCA\Contacts\Settings\AdminSettings</admin>
</settings>

</info>
6 changes: 4 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
['name' => 'page#index', 'url' => '/{group}', 'verb' => 'GET', 'postfix' => 'group'],
['name' => 'page#index', 'url' => '/{group}/{contact}', 'verb' => 'GET', 'postfix' => 'group.contact'],
['name' => 'social_api#update_contact', 'url' => '/api/v1/social/avatar/{network}/{addressbookId}/{contactId}', 'verb' => 'PUT'],
['name' => 'social_api#set_app_config', 'url' => '/api/v1/social/config/{key}', 'verb' => 'POST'],
['name' => 'social_api#update_contact', 'url' => '/api/v1/social/avatar/{network}/{addressbookId}/{contactId}', 'verb' => 'PUT'],
['name' => 'social_api#set_app_config', 'url' => '/api/v1/social/config/global/{key}', 'verb' => 'PUT'],
['name' => 'social_api#set_user_config', 'url' => '/api/v1/social/config/user/{key}', 'verb' => 'PUT'],
['name' => 'social_api#get_user_config', 'url' => '/api/v1/social/config/user/{key}', 'verb' => 'GET'],
]
];
38 changes: 30 additions & 8 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -23,12 +24,14 @@

namespace OCA\Contacts\Controller;

use OCA\Contacts\AppInfo\Application;
use OCA\Contacts\Service\SocialApiService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;

use OCA\Contacts\AppInfo\Application;
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IUserSession;
use OCP\IRequest;
use OCP\L10N\IFactory;
use OCP\Util;
Expand All @@ -43,19 +46,25 @@ class PageController extends Controller {
/** @var IFactory */
private $languageFactory;

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

/** @var SocialApiService */
private $socialApiService;

public function __construct(IRequest $request,
IConfig $config,
IInitialStateService $initialStateService,
IFactory $languageFactory,
IUserSession $userSession,
SocialApiService $socialApiService) {
parent::__construct(Application::APP_ID, $request);

$this->appName = Application::APP_ID;
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->languageFactory = $languageFactory;
$this->userSession = $userSession;
$this->socialApiService = $socialApiService;
}

Expand All @@ -66,17 +75,30 @@ public function __construct(IRequest $request,
* Default routing
*/
public function index(): TemplateResponse {
$user = $this->userSession->getUser();
$userId = '';
if (!is_null($user)) {
$userId = $user->getUid();
}

$locales = $this->languageFactory->findAvailableLocales();
$defaultProfile = $this->config->getAppValue(Application::APP_ID, 'defaultProfile', 'HOME');
$defaultProfile = $this->config->getAppValue($this->appName, 'defaultProfile', 'HOME');
$supportedNetworks = $this->socialApiService->getSupportedNetworks();
$syncAllowedByAdmin = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes'); // allow users to retrieve avatars from social networks (default: yes)
$bgSyncEnabledByUser = $this->config->getUserValue($userId, $this->appName, 'enableSocialSync', 'no'); // automated background syncs for social avatars (default: no)

$this->initialStateService->provideInitialState(Application::APP_ID, 'locales', $locales);
$this->initialStateService->provideInitialState(Application::APP_ID, 'defaultProfile', $defaultProfile);
$this->initialStateService->provideInitialState(Application::APP_ID, 'supportedNetworks', $supportedNetworks);
$this->initialStateService->provideInitialState($this->appName, 'locales', $locales);
$this->initialStateService->provideInitialState($this->appName, 'defaultProfile', $defaultProfile);
$this->initialStateService->provideInitialState($this->appName, 'supportedNetworks', $supportedNetworks);
$this->initialStateService->provideInitialState($this->appName, 'locales', $locales);
$this->initialStateService->provideInitialState($this->appName, 'defaultProfile', $defaultProfile);
$this->initialStateService->provideInitialState($this->appName, 'supportedNetworks', $supportedNetworks);
$this->initialStateService->provideInitialState($this->appName, 'allowSocialSync', $syncAllowedByAdmin);
$this->initialStateService->provideInitialState($this->appName, 'enableSocialSync', $bgSyncEnabledByUser);

Util::addScript(Application::APP_ID, 'contacts');
Util::addStyle(Application::APP_ID, 'contacts');
Util::addScript($this->appName, 'contacts');
Util::addStyle($this->appName, 'contacts');

return new TemplateResponse(Application::APP_ID, 'main');
return new TemplateResponse($this->appName, 'main');
}
}
48 changes: 48 additions & 0 deletions lib/Controller/SocialApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

class SocialApiController extends ApiController {
protected $appName;

/** @var IConfig */
private $config;

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

/** @var SocialApiService */
private $socialApiService;

public function __construct(IRequest $request,
IConfig $config,
IUserSession $userSession,
SocialApiService $socialApiService) {
parent::__construct(Application::APP_ID, $request);

$this->config = $config;
$this->appName = Application::APP_ID;
$this->userSession = $userSession;
$this->socialApiService = $socialApiService;
}

Expand All @@ -66,6 +74,46 @@ public function setAppConfig($key, $allow) {
return new JSONResponse([], Http::STATUS_OK);
}

/**
* @NoAdminRequired
*
* update appconfig (user setting)
*
* @param {String} key the identifier to change
* @param {String} allow the value to set
*
* @returns {JSONResponse} an empty JSONResponse with respective http status code
*/
public function setUserConfig($key, $allow) {
$user = $this->userSession->getUser();
if (is_null($user)) {
return new JSONResponse([], Http::STATUS_PRECONDITION_FAILED);
}
$userId = $user->getUid();
$this->config->setUserValue($userId, $this->appName, $key, $allow);
return new JSONResponse([], Http::STATUS_OK);
}


/**
* @NoAdminRequired
*
* retrieve appconfig (user setting)
*
* @param {String} key the identifier to retrieve
*
* @returns {string} the desired value or null if not existing
*/
public function getUserConfig($key) {
$user = $this->userSession->getUser();
if (is_null($user)) {
return null;
}
$userId = $user->getUid();
return $this->config->getUserValue($userId, $this->appName, $key, 'null');
}


/**
* @NoAdminRequired
*
Expand Down
42 changes: 42 additions & 0 deletions lib/Cron/SocialUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @copyright 2020 Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @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 OCA\Contacts\Cron;

use OCA\Contacts\Service\SocialApiService;

class SocialUpdate extends \OC\BackgroundJob\QueuedJob {
/** @var SocialUpdateService */
private $social;

public function __construct(SocialApiService $social) {
$this->social = $social;
}

protected function run($arguments) {
$userId = $arguments['userId'];

// update contacts with first available social media profile
$this->social->updateAddressbooks('any', $userId);
}
}
93 changes: 93 additions & 0 deletions lib/Cron/SocialUpdateRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Matthias Heinisch <nextcloud@matthiasheinisch.de>
*
* @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 OCA\Contacts\Cron;

use OCA\Contacts\AppInfo\Application;

use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\IUser;
use OCP\IConfig;
use OCP\IUserManager;

class SocialUpdateRegistration extends \OC\BackgroundJob\TimedJob {
private $appName;

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

/** @var IJobList */
private $jobList;

/** @var IConfig */
private $config;

/**
* RegisterSocialUpdate constructor.
*
* @param ITimeFactory $time
* @param IUserManager $userManager
* @param IJobList $jobList
*/
public function __construct(
// ITimeFactory $time,
IUserManager $userManager,
IConfig $config,
IJobList $jobList) {
//parent::__construct($time);

$this->appName = Application::APP_ID;
$this->userManager = $userManager;
$this->config = $config;
$this->jobList = $jobList;

// Run once a week
parent::setInterval(7 * 24 * 60 * 60);
}

/**
* @inheritDoc
*/
protected function run($arguments) {

// check if admin allows for social updates:
$syncAllowedByAdmin = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes');
if (!($syncAllowedByAdmin === 'yes')) {
return;
}

$this->userManager->callForSeenUsers(function (IUser $user) {

// check that user opted-in:
$bgSyncEnabledByUser = $this->config->getUserValue($user->getUID(), $this->appName, 'enableSocialSync', 'no');
if ($bgSyncEnabledByUser === 'yes') {
$this->jobList->add(SocialUpdate::class, [
'userId' => $user->getUID()
]);
}
});
}
}
Loading

0 comments on commit 973c288

Please sign in to comment.