diff options
Diffstat (limited to 'lib')
33 files changed, 3589 insertions, 31 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 00000000..a1192ed8 --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,78 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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 OCA\Social\AppInfo; + +use OCP\AppFramework\App; +use OCP\AppFramework\IAppContainer; + +class Application extends App { + + const APP_NAME = 'social'; + + /** @var IAppContainer */ + private $container; + + + /** + * Application constructor. + * + * @param array $params + */ + public function __construct(array $params = []) { + parent::__construct(self::APP_NAME, $params); + + $this->container = $this->getContainer(); + } + +// +// /** +// * Register Navigation Tab +// */ +// public function registerNavigation() { +// +// $urlGen = \OC::$server->getURLGenerator(); +// $navName = \OC::$server->getL10N(self::APP_NAME) +// ->t('Social'); +// +// $social = [ +// 'id' => self::APP_NAME, +// 'order' => 5, +// 'href' => $urlGen->linkToRoute('social.Navigation.navigate'), +// 'icon' => $urlGen->imagePath(self::APP_NAME, 'social.svg'), +// 'name' => $navName +// ]; +// +// $this->container->getServer() +// ->getNavigationManager() +// ->add($social); +// } + +} + diff --git a/lib/Controller/ActivityStreamsController.php b/lib/Controller/ActivityStreamsController.php new file mode 100644 index 00000000..5f55302a --- /dev/null +++ b/lib/Controller/ActivityStreamsController.php @@ -0,0 +1,112 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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 OCA\Social\Controller; + +use daita\Traits\TArrayTools; +use daita\Traits\TNCDataResponse; +use Exception; +use OCA\Social\AppInfo\Application; +use OCA\Social\Service\ActivityStreamsService; +use OCA\Social\Service\ConfigService; +use OCA\Social\Service\MiscService; +use OCA\Social\Service\ServiceAccountsService; +use OCA\Social\Service\ServicesService; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; + + +class ActivityStreamsController extends Controller { + + use TArrayTools; + use TNCDataResponse; + + /** @var string */ + private $userId; + + /** @var ConfigService */ + private $configService; + + /** @var ServiceAccountsService */ + private $serviceAccountsService; + + /** @var ActivityStreamsService */ + private $activityStreamsService; + + /** @var MiscService */ + private $miscService; + + + /** + * ServiceAccountsController constructor. + * + * @param IRequest $request + * @param string $userId + * @param ConfigService $configService + * @param ServiceAccountsService $serviceAccountsService + * @param ActivityStreamsService $activityStreamsService + * @param MiscService $miscService + */ + public function __construct( + IRequest $request, string $userId, ConfigService $configService, + ServiceAccountsService $serviceAccountsService, + ActivityStreamsService $activityStreamsService, MiscService $miscService + ) { + parent::__construct(Application::APP_NAME, $request); + + $this->userId = $userId; + $this->configService = $configService; + $this->serviceAccountsService = $serviceAccountsService; + $this->activityStreamsService = $activityStreamsService; + $this->miscService = $miscService; + } + + /** + * @NoAdminRequired + * + * @param int $accountId + * + * @return DataResponse + */ + public function test(int $accountId): DataResponse { + try { + $account = $this->serviceAccountsService->getAccount($this->userId, $accountId); + + $result = $this->activityStreamsService->test($account); + + return $this->success($result); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + +} + diff --git a/lib/Controller/NavigationController.php b/lib/Controller/NavigationController.php new file mode 100644 index 00000000..2d6f8423 --- /dev/null +++ b/lib/Controller/NavigationController.php @@ -0,0 +1,87 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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 OCA\Social\Controller; + + +use OCA\Social\AppInfo\Application; +use OCA\Social\Service\MiscService; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\TemplateResponse; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IURLGenerator; + +class NavigationController extends Controller { + + /** @var IConfig */ + private $config; + + /** @var IURLGenerator */ + private $urlGenerator; + + /** @var MiscService */ + private $miscService; + + + /** + * NavigationController constructor. + * + * @param IRequest $request + * @param IConfig $config + * @param IURLGenerator $urlGenerator + * @param MiscService $miscService + */ + public function __construct( + IRequest $request, IConfig $config, IURLGenerator $urlGenerator, MiscService $miscService + ) { + parent::__construct(Application::APP_NAME, $request); + + $this->config = $config; + $this->urlGenerator = $urlGenerator; + + $this->miscService = $miscService; + } + + + /** + * @NoCSRFRequired + * @NoAdminRequired + * @NoSubAdminRequired + * + * @return TemplateResponse + */ + public function navigate() { + $data = []; + + return new TemplateResponse(Application::APP_NAME, 'main', $data); + } + + +} diff --git a/lib/Controller/OAuth2Controller.php b/lib/Controller/OAuth2Controller.php new file mode 100644 index 00000000..ef74aa5e --- /dev/null +++ b/lib/Controller/OAuth2Controller.php @@ -0,0 +1,109 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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 OCA\Social\Controller; + + +use daita\Traits\TNCDataResponse; +use Exception; +use OCA\Social\AppInfo\Application; +use OCA\Social\Service\MiscService; +use OCA\Social\Service\ServiceAccountsService; +use OCP\AppFramework\Controller; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IURLGenerator; + +class OAuth2Controller extends Controller { + + + use TNCDataResponse; + + + /** @var IConfig */ + private $config; + + /** @var string */ + private $userId; + + /** @var IURLGenerator */ + private $urlGenerator; + + /** @var ServiceAccountsService */ + private $serviceAccountsService; + + /** @var MiscService */ + private $miscService; + + + /** + * NavigationController constructor. + * + * @param IRequest $request + * @param IConfig $config + * @param string $userId + * @param IURLGenerator $urlGenerator + * @param ServiceAccountsService $serviceAccountsService + * @param MiscService $miscService + */ + public function __construct( + IRequest $request, IConfig $config, string $userId, IURLGenerator $urlGenerator, + ServiceAccountsService $serviceAccountsService, MiscService $miscService + ) { + parent::__construct(Application::APP_NAME, $request); + + $this->config = $config; + $this->userId = $userId; + $this->urlGenerator = $urlGenerator; + + $this->serviceAccountsService = $serviceAccountsService; + $this->miscService = $miscService; + } + + + /** + * @NoCSRFRequired + * @NoAdminRequired + * @NoSubAdminRequired + * + * @param int $serviceId + * + * @throws Exception + */ + public function setCode(int $serviceId) { + + $code = $_GET['code']; + // TODO: verify $state + $state = $_GET['state']; + + $this->serviceAccountsService->generateAccount($this->userId, $serviceId, $code); + } + + +} diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php deleted file mode 100644 index fae72e84..00000000 --- a/lib/Controller/PageController.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php -namespace OCA\Social\Controller; - -use OCP\AppFramework\Http\ContentSecurityPolicy; -use OCP\IL10N; -use OCP\IRequest; -use OCP\AppFramework\Http\TemplateResponse; -use OCP\AppFramework\Http\DataResponse; -use OCP\AppFramework\Controller; -use OCP\Mail\IEMailTemplate; -use OCP\Mail\IMailer; - -class PageController extends Controller { - private $userId; - - public function __construct($AppName, IRequest $request, $UserId, IL10N $l10n){ - parent::__construct($AppName, $request); - $this->userId = $UserId; - $this->l10n = $l10n; - } - - /** - * @NoAdminRequired - * @NoCSRFRequired - */ - public function index() { - $response = new TemplateResponse('social', 'main'); - return $response; - } - -} diff --git a/lib/Controller/ServiceAccountsController.php b/lib/Controller/ServiceAccountsController.php new file mode 100644 index 00000000..0cbec6ca --- /dev/null +++ b/lib/Controller/ServiceAccountsController.php @@ -0,0 +1,145 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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 OCA\Social\Controller; + +use daita\Traits\TArrayTools; +use daita\Traits\TNCDataResponse; +use Exception; +use OCA\Social\AppInfo\Application; +use OCA\Social\Service\ActivityStreamsService; +use OCA\Social\Service\ConfigService; +use OCA\Social\Service\MiscService; +use OCA\Social\Service\ServiceAccountsService; +use OCA\Social\Service\ServicesService; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\DataResponse; +use OCP\IRequest; + + +class ServiceAccountsController extends Controller { + + use TArrayTools; + use TNCDataResponse; + + /** @var string */ + private $userId; + + /** @var ConfigService */ + private $configService; + + /** @var ServicesService */ + private $servicesService; + + /** @var ServiceAccountsService */ + private $serviceAccountsService; + + /** @var ActivityStreamsService */ + private $activityStreamsService; + + /** @var MiscService */ + private $miscService; + + + /** + * ServiceAccountsController constructor. + * + * @param IRequest $request + * @param string $userId + * @param ConfigService $configService + * @param ServicesService $servicesService + * @param ServiceAccountsService $serviceAccountsService + * @param ActivityStreamsService $activityStreamsService + * @param MiscService $miscService + */ + public function __construct( + IRequest $request, string $userId, ConfigService $configService, + ServicesService $servicesService, ServiceAccountsService $serviceAccountsService, + ActivityStreamsService $activityStreamsService, MiscService $miscService + ) { + parent::__construct(Application::APP_NAME, $request); + + $this->userId = $userId; + $this->configService = $configService; + $this->servicesService = $servicesService; + $this->serviceAccountsService = $serviceAccountsService; + $this->activityStreamsService = $activityStreamsService; + $this->miscService = $miscService; + } + + + /** + * @NoAdminRequired + * + * @return DataResponse + */ + public function getAvailableAccounts(): DataResponse { + try { + $ret = + ['accounts' => $this->serviceAccountsService->getAvailableAccounts($this->userId)]; + + return $this->success($ret); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + + /** + * @NoAdminRequired + * + * @param array $data + * + * @return DataResponse + */ + public function create(array $data): DataResponse { + try { + $instance = strtolower($this->get('instance', $data, '')); + if ($instance === '') { + throw new Exception('Empty address'); + } + + $service = $this->servicesService->createFromInstance($instance); + + $authUrl = $this->serviceAccountsService->getAuthorizationUrl($service); + $data = [ + 'protocol' => 'OAuth2', + 'authorizationUrl' => $authUrl + ]; + $this->miscService->log('___' . json_encode($data)); + + return $this->success($data); + } catch (Exception $e) { + return $this->fail($e->getMessage()); + } + } + + +} + diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php new file mode 100644 index 00000000..eea39b91 --- /dev/null +++ b/lib/Db/CoreRequestBuilder.php @@ -0,0 +1,204 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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 OCA\Social\Db; + + +use Doctrine\DBAL\Query\QueryBuilder; +use OCA\Social\Service\ConfigService; +use OCA\Social\Service\MiscService; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; + +class CoreRequestBuilder { + + const TABLE_SERVICES = 'social_services'; + const TABLE_ACCOUNTS = 'social_accounts'; + const TABLE_OAUTH2_TOKENS = 'social_oauth2_tokens'; + + + /** @var IDBConnection */ + protected $dbConnection; + + /** @var ConfigService */ + protected $configService; + + /** @var MiscService */ + protected $miscService; + + + /** @var string */ + protected $defaultSelectAlias; + + + /** + * CoreRequestBuilder constructor. + * + * @param IDBConnection $connection + * @param ConfigService $configService + * @param MiscService $miscService + */ + public function __construct( + IDBConnection $connection, ConfigService $configService, MiscService $miscService + ) { + $this->dbConnection = $connection; + $this->configService = $configService; + $this->miscService = $miscService; + } + + + /** + * Limit the request to the Id + * + * @param IQueryBuilder $qb + * @param int $id + */ + protected function limitToId(IQueryBuilder &$qb, $id) { + $this->limitToDBField($qb, 'id', $id); + } + + + /** + * Limit the request to the OwnerId + * + * @param IQueryBuilder $qb + * @param string $userId + */ + protected function limitToUserId(IQueryBuilder &$qb, $userId) { + $this->limitToDBField($qb, 'user_id', $userId); + } + + + /** + * Limit the request to the OwnerId + * + * @param IQueryBuilder $qb + * @param int $accountId + */ + protected function limitToAccountId(IQueryBuilder &$qb, int $accountId) { + $this->limitToDBField($qb, 'account_id', $accountId); + } + + + /** + * Limit the request to the ServiceId + * + * @param IQueryBuilder $qb + * @param int $serviceId + */ + protected function limitToServiceId(IQueryBuilder &$qb, int $serviceId) { + $this->limitToDBField($qb, 'service_id', $serviceId); + } + + + /** + * Limit the request to the account + * + * @param IQueryBuilder $qb + * @param string $account + */ + protected function limitToAccount(IQueryBuilder &$qb, string $account) { + $this->limitToDBField($qb, 'account', $account); + } + + + /** + * Limit the request to the status + * + * @param IQueryBuilder $qb + * @param string $status + */ + protected function limitToStatus(IQueryBuilder &$qb, $status) { + $this->limitToDBField($qb, 'status', $status); + } + + + /** + * Limit the request to the instance + * + * @param IQueryBuilder $qb + * @param string $address + */ + protected function limitToAddress(IQueryBuilder &$qb, $address) { + $this->limitToDBField($qb, 'address', $address); + } + + + /** + * @param IQueryBuilder $qb + * @param string $field + * @param string|integer|array $values + */ + private function limitToDBField(IQueryBuilder &$qb, $field, $values) { + $expr = $qb->expr(); + $pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->defaultSelectAlias . '.' : ''; + $field = $pf . $field; + + if (!is_array($values)) { + $values = [$values]; + } + + $orX = $expr->orX(); + foreach ($values as $value) { + $orX->add($expr->eq($field, $qb->createNamedParameter($value))); + } + + $qb->andWhere($orX); + } + + + /** + * Left Join service to get info about the serviceId + * + * @param IQueryBuilder $qb + */ + public function leftJoinService(IQueryBuilder &$qb) { + + if ($qb->getType() !== QueryBuilder::SELECT) { + return; + } + + $expr = $qb->expr(); + $pf = $this->defaultSelectAlias; + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $qb->selectAlias('s.address', 'service_address') + ->selectAlias('s.status', 'service_status') + ->selectAlias('s.config', 'service_config') + ->selectAlias('s.type', 'service_type') + ->leftJoin( + $this->defaultSelectAlias, CoreRequestBuilder::TABLE_SERVICES, 's', + $expr->eq($pf . '.service_id', 's.id') + ); + } |