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
matthias authored and call-me-matt committed Jul 31, 2020
1 parent dc11e86 commit 8cf1b21
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 15 deletions.
9 changes: 0 additions & 9 deletions .editorconfig

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Nextcloud Contacts
![Downloads](https://img.shields.io/github/downloads/nextcloud/contacts/total.svg?style=flat-square)
![Downloads](https://img.shields.io/github/downloads/nextcloud/contacts/total?style=flat-square)
[![Codacy Badge](https://img.shields.io/codacy/grade/ea24ea9fccb942419d73ec05105938aa.svg?style=flat-square)](https://app.codacy.com/app/skjnldsv/contacts)
[![Code coverage](https://img.shields.io/codecov/c/github/nextcloud/contacts.svg?style=flat-square)](https://codecov.io/gh/nextcloud/contacts/)
[![Dependabot status](https://img.shields.io/badge/Dependabot-enabled-brightgreen.svg?longCache=true&style=flat-square&logo=dependabot)](https://dependabot.com)
Expand Down
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 @@ -26,7 +26,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'],
]
];
8 changes: 8 additions & 0 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 @@ -26,6 +27,7 @@
use OCA\Contacts\Service\SocialApiService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;

use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\IRequest;
Expand Down Expand Up @@ -69,13 +71,19 @@ public function __construct(string $appName,
* Default routing
*/
public function index(): TemplateResponse {
$userId = \OC_User::getUser();

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

$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', $allowSocialSync);
$this->initialStateService->provideInitialState($this->appName, 'enableSocialSync', $enableSocialSync);

Util::addScript($this->appName, 'contacts');
Util::addStyle($this->appName, 'contacts');
Expand Down
32 changes: 32 additions & 0 deletions lib/Controller/SocialApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ 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 = \OC_User::getUser();
$this->config->setUserValue($user, $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 = \OC_User::getUser();
return $this->config->getUserValue($user, $this->appName, $key, 'null');
}


/**
* @NoAdminRequired
*
Expand Down
45 changes: 45 additions & 0 deletions lib/Cron/SocialUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 {
private $appName;

/** @var SocialUpdateService */
private $social;

public function __construct(string $AppName, SocialApiService $social) {
$this->appName = $AppName;
$this->social = $social;
}

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

// update contacts with first available social media profile
$this->social->updateAddressbooks('any', $userId);
}
}
91 changes: 91 additions & 0 deletions lib/Cron/SocialUpdateRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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 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(string $AppName,
// ITimeFactory $time,
IUserManager $userManager,
IConfig $config,
IJobList $jobList) {
//parent::__construct($time);

$this->appName = $AppName;
$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:
$isAdminEnabled = $this->config->getAppValue($this->appName, 'allowSocialSync', 'yes');
if (!($isAdminEnabled === 'yes')) {
return;
}

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

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

0 comments on commit 8cf1b21

Please sign in to comment.