migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\AppInfo;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Recommendations\Listeners\FilesLoadAdditionalScriptsListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCA\Recommendations\Dashboard\RecommendationWidget;
class Application extends App implements IBootstrap {
public const APP_ID = 'recommendations';
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}
public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalScriptsEvent::class, FilesLoadAdditionalScriptsListener::class);
$context->registerDashboardWidget(RecommendationWidget::class);
}
public function boot(IBootContext $context): void {
}
}
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Command;
use OCA\Recommendations\Service\RecommendationService;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GetRecommendations extends Command {
private IUserManager $userManager;
private RecommendationService $recommendationService;
public function __construct(IUserManager $userManager,
RecommendationService $recommendationService) {
parent::__construct();
$this->userManager = $userManager;
$this->recommendationService = $recommendationService;
}
protected function configure() {
$this->setName('files:recommendations:recommend');
$this->addArgument(
'uid',
InputArgument::REQUIRED,
'user id'
);
$this->addArgument(
'max',
InputArgument::OPTIONAL,
'maximum results'
);
}
public function execute(InputInterface $input, OutputInterface $output) {
$user = $this->userManager->get(
$input->getArgument('uid')
);
if (is_null($user)) {
$output->writeln("user does not exist");
return 1;
}
if ($input->getArgument('max')) {
$recommendations = $this->recommendationService->getRecommendations($user, (int) $input->getArgument('max'));
} else {
$recommendations = $this->recommendationService->getRecommendations($user);
}
foreach ($recommendations as $recommendation) {
$reason = $recommendation->getReason();
$path = $recommendation->getNode()->getPath();
$ts = $recommendation->getTimestamp();
$output->writeln("$reason: $path ($ts)");
}
return 0;
}
}
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Controller;
use Exception;
use OCA\Recommendations\AppInfo\Application;
use OCA\Recommendations\Service\RecommendationService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;
class RecommendationController extends Controller {
private IUserSession $userSession;
private RecommendationService $recommendationService;
private IConfig $config;
public function __construct(IRequest $request,
IUserSession $userSession,
RecommendationService $recommendationService,
IConfig $config) {
parent::__construct(Application::APP_ID, $request);
$this->userSession = $userSession;
$this->recommendationService = $recommendationService;
$this->config = $config;
}
/**
* @NoAdminRequired
* @return JSONResponse
*/
public function index(): JSONResponse {
$user = $this->userSession->getUser();
if (is_null($user)) {
throw new Exception("Not logged in");
}
$response = [];
$response['enabled'] = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true';
if ($response['enabled']) {
$response['recommendations'] = $this->recommendationService->getRecommendations($user);
}
return new JSONResponse(
$response
);
}
/**
* @NoAdminRequired
* @return JSONResponse
*/
public function always(): JSONResponse {
$user = $this->userSession->getUser();
if (is_null($user)) {
throw new Exception("Not logged in");
}
$response = [
'enabled' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true',
'recommendations' => $this->recommendationService->getRecommendations($user),
];
return new JSONResponse(
$response
);
}
}
@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Gary Kim <gary@garykim.dev>
*
* @author 2019 Gary Kim <gary@garykim.dev>
*
* @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\Recommendations\Controller;
use OCA\Recommendations\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use Exception;
class SettingsController extends Controller {
private IConfig $config;
private IUserSession $userSession;
public function __construct($appName,
IRequest $request,
IConfig $config,
IUserSession $userSession) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userSession = $userSession;
}
/**
* @NoAdminRequired
*
* @throws Exception
*/
public function getSettings(): JSONResponse {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
throw new Exception("Not logged in");
}
return new JSONResponse([
'enabled' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true',
]);
}
/**
* @NoAdminRequired
*
* @throws Exception
*/
public function setSetting(string $key, string $value): JSONResponse {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
throw new Exception("Not logged in");
}
$availableSettings = ['enabled'];
if (!in_array($key, $availableSettings)) {
return new JSONResponse([
'message' => 'parameter does not exist',
], Http::STATUS_UNPROCESSABLE_ENTITY);
}
$this->config->setUserValue($user->getUID(), Application::APP_ID, $key, $value);
return new JSONResponse([
'key' => $key,
'value' => $value,
]);
}
}
@@ -0,0 +1,137 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @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\Recommendations\Dashboard;
use OCA\Recommendations\Service\IRecommendation;
use OCA\Recommendations\Service\RecommendationService;
use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IAPIWidgetV2;
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IWidget;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetItems;
use OCP\Files\IMimeTypeDetector;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
class RecommendationWidget implements IWidget, IIconWidget, IAPIWidget, IAPIWidgetV2 {
private IUserSession $userSession;
private IL10N $l10n;
private IURLGenerator $urlGenerator;
private IMimeTypeDetector $mimeTypeDetector;
private RecommendationService $recommendationService;
private IUserManager $userManager;
public function __construct(
IUserSession $userSession,
IL10N $l10n,
IURLGenerator $urlGenerator,
IMimeTypeDetector $mimeTypeDetector,
RecommendationService $recommendationService,
IUserManager $userManager
) {
$this->userSession = $userSession;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->recommendationService = $recommendationService;
$this->userManager = $userManager;
}
public function getId(): string {
return 'recommendations';
}
public function getTitle(): string {
return $this->l10n->t('Recommended files');
}
public function getOrder(): int {
return 0;
}
public function getIconClass(): string {
return 'icon-files-dark';
}
public function getIconUrl(): string {
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'app.svg'));
}
public function getUrl(): ?string {
return null;
}
public function load(): void {
}
public function getItems(string $userId, ?string $since = null, int $limit = 7): array {
$user = $this->userManager->get($userId);
if (!$user) {
return [];
}
$recommendations = $this->recommendationService->getRecommendations($user, $limit);
return array_map(function (IRecommendation $recommendation) {
$url = $this->urlGenerator->linkToRouteAbsolute(
'files.viewcontroller.showFile', ['fileid' => $recommendation->getNode()->getId()]
);
if ($recommendation->hasPreview()) {
$icon = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', [
'x' => 256,
'y' => 256,
'fileId' => $recommendation->getNode()->getId(),
'c' => $recommendation->getNode()->getEtag(),
]);
} else {
$icon = $this->urlGenerator->getAbsoluteURL(
$this->mimeTypeDetector->mimeTypeIcon($recommendation->getNode()->getMimetype())
);
}
return new WidgetItem(
$recommendation->getNode()->getName(),
'',
$url,
$icon,
(string)$recommendation->getTimestamp()
);
}, $recommendations);
}
public function getItemsV2(string $userId, ?string $since = null, int $limit = 7): WidgetItems {
$items = $this->getItems($userId, $since, $limit);
return new WidgetItems(
$items,
empty($items) ? $this->l10n->t('No recommendations yet') : '',
);
}
}
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de>
*
* @author Morris Jobke <hey@morrisjobke.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\Recommendations\Listeners;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Recommendations\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;
class FilesLoadAdditionalScriptsListener implements IEventListener {
public function handle(Event $event): void {
if (!$event instanceof LoadAdditionalScriptsEvent) {
return;
}
Util::addScript(Application::APP_ID, 'recommendations-main');
}
}
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use OCP\Comments\IComment;
use OCP\Files\Node;
class FileWithComments {
private string $directory;
private Node $node;
private IComment $comment;
public function __construct(string $directory, Node $node, IComment $comment) {
$this->directory = $directory;
$this->node = $node;
$this->comment = $comment;
}
public function getDirectory(): string {
return $this->directory;
}
public function getNode(): Node {
return $this->node;
}
public function getComment(): IComment {
return $this->comment;
}
}
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use JsonSerializable;
use OCP\Files\Node;
interface IRecommendation extends JsonSerializable {
public function getId(): string;
public function getTimestamp(): int;
public function getNode(): Node;
public function hasPreview(): bool;
public function setHasPreview(bool $state);
public function getReason(): string;
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use OCP\IUser;
interface IRecommendationSource {
/**
* @param IUser $user
* @param int $max maximum number of recommendations
*
* @return IRecommendation[]
*/
public function getMostRecentRecommendation(IUser $user, int $max): array ;
}
@@ -0,0 +1,158 @@
<?php
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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/>.
*/
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use function array_map;
use function array_slice;
use function iterator_to_array;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use function reset;
use function usort;
use Generator;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\IUser;
class RecentlyCommentedFilesSource implements IRecommendationSource {
private ICommentsManager $commentsManager;
private IRootFolder $rootFolder;
private IL10N $l10n;
public function __construct(ICommentsManager $commentsManager,
IRootFolder $rootFolder,
IL10N $l10n) {
$this->commentsManager = $commentsManager;
$this->rootFolder = $rootFolder;
$this->l10n = $l10n;
}
private function getCommentsPage(int $offset, int $pageSize): array {
return $this->commentsManager->search(
'',
'files',
'',
'',
$offset,
$pageSize
);
}
private function getCommentedFile(IComment $comment, Folder $userFolder): ?FileWithComments {
$nodes = $userFolder->getById((int)$comment->getObjectId());
$first = reset($nodes);
if ($first === false) {
return null;
}
return new FileWithComments(
$userFolder->getRelativePath($first->getParent()->getPath()),
$first,
$comment
);
}
/**
* @param IUser $user
*
* @return Generator<FileWithComments>
*/
private function getAllCommentedFiles(IUser $user): Generator {
$offset = 0;
$pageSize = 100;
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
while (count($page = $this->getCommentsPage($offset, $pageSize)) > 0) {
foreach ($page as $comment) {
$commentedFile = $this->getCommentedFile($comment, $userFolder);
if (!is_null($commentedFile)) {
yield $commentedFile;
}
}
$offset += $pageSize;
}
}
/**
* @param FileWithComments[] $original
* @return FileWithComments[]
*/
private function sortCommentedFiles(array $original): array {
usort($original, function (FileWithComments $a, FileWithComments $b) {
return $b->getComment()->getCreationDateTime()->getTimestamp() - $a->getComment()->getCreationDateTime()->getTimestamp();
});
return $original;
}
/**
* @param FileWithComments[] $comments
* @return FileWithComments[]
*/
private function getNMostRecentlyCommenedFiles(array $comments, int $n): array {
$sorted = $this->sortCommentedFiles($comments);
return array_slice($sorted, 0, $n);
}
/**
* @return IRecommendation[]
*/
public function getMostRecentRecommendation(IUser $user, int $max): array {
$all = iterator_to_array($this->getAllCommentedFiles($user));
return array_map(function (FileWithComments $file) {
return new RecommendedFile(
$file->getDirectory(),
$file->getNode(),
$file->getComment()->getCreationDateTime()->getTimestamp(),
$this->l10n->t("Recently commented")
);
}, $this->getNMostRecentlyCommenedFiles($all, $max));
}
}
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\StorageNotAvailableException;
use OCP\IL10N;
use OCP\IServerContainer;
use OCP\IUser;
class RecentlyEditedFilesSource implements IRecommendationSource {
private IServerContainer $serverContainer;
private IL10N $l10n;
public function __construct(IServerContainer $serverContainer,
IL10N $l10n) {
$this->serverContainer = $serverContainer;
$this->l10n = $l10n;
}
/**
* @return RecommendedFile[]
*/
public function getMostRecentRecommendation(IUser $user, int $max): array {
/** @var IRootFolder $rootFolder */
$rootFolder = $this->serverContainer->get(IRootFolder::class);
$userFolder = $rootFolder->getUserFolder($user->getUID());
return array_filter(array_map(function (Node $node) use ($userFolder): ?RecommendedFile {
try {
$parentPath = dirname($node->getPath());
if ($parentPath === '' || $parentPath === '.' || $parentPath === '/') {
$parentPath = $node->getParent()->getPath();
}
return new RecommendedFile(
$userFolder->getRelativePath($parentPath),
$node,
$node->getMTime(),
$this->l10n->t("Recently edited")
);
} catch (StorageNotAvailableException $e) {
return null;
}
}, $userFolder->getRecent($max)), function (?RecommendedFile $entry): bool {
return $entry !== null;
});
}
}
@@ -0,0 +1,125 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use function array_filter;
use function array_map;
use function array_merge;
use function array_slice;
use function iterator_to_array;
use OCP\Files\NotFoundException;
use function usort;
use Generator;
use OCP\Files\IRootFolder;
use OCP\IL10N;
use OCP\IUser;
use OCP\Share\IManager;
use OCP\Share\IShare;
class RecentlySharedFilesSource implements IRecommendationSource {
private IManager $shareManager;
private IRootFolder $rootFolder;
private IL10N $l10n;
public function __construct(IManager $shareManager,
IRootFolder $rootFolder,
IL10N $l10n) {
$this->shareManager = $shareManager;
$this->rootFolder = $rootFolder;
$this->l10n = $l10n;
}
/**
* @return Generator<IShare>
*/
private function getAllShares(IUser $user, int $shareType): Generator {
$offset = 0;
$pageSize = 50;
while (count($page = $this->shareManager->getSharedWith(
$user->getUID(),
$shareType,
null,
$pageSize,
$offset
))) {
foreach ($page as $share) {
yield $share;
}
$offset += $pageSize;
}
}
/**
* @param IShare[] $shares
*
* @return IShare[]
*/
private function sortShares(array $shares): array {
usort($shares, function (IShare $a, IShare $b) {
return $b->getShareTime()->getTimestamp() - $a->getShareTime()->getTimestamp();
});
return $shares;
}
/**
* @param IUser $user
*
* @todo load other share types as well
*
* @return IShare[]
*/
private function getMostRecentShares(IUser $user, int $max): array {
$shares = $this->sortShares(array_merge(
iterator_to_array($this->getAllShares($user, IShare::TYPE_USER)),
iterator_to_array($this->getAllShares($user, IShare::TYPE_GROUP))
));
return array_slice($shares, 0, $max);
}
/**
* @return IRecommendation[]
*/
public function getMostRecentRecommendation(IUser $user, int $max): array {
$shares = $this->getMostRecentShares($user, $max);
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
return array_filter(array_map(function (IShare $share) use ($userFolder): ?RecommendedFile {
try {
return new RecommendedFile(
$userFolder->getRelativePath($userFolder->get($share->getTarget())->getParent()->getPath()),
$share->getNode(),
$share->getShareTime()->getTimestamp(),
$this->l10n->t("Recently shared")
);
} catch (NotFoundException $ex) {
return null;
}
}, $shares));
}
}
@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use function array_merge;
use function array_reduce;
use function array_slice;
use OCP\IPreview;
use OCP\IUser;
use function usort;
class RecommendationService {
private const MAX_RECOMMENDATIONS = 7;
/** @var IRecommendationSource[] */
private array $sources;
private IPreview $previewManager;
public function __construct(RecentlyCommentedFilesSource $recentlyCommented,
RecentlyEditedFilesSource $recentlyEdited,
RecentlySharedFilesSource $recentlyShared,
IPreview $previewManager) {
$this->sources = [
$recentlyCommented,
$recentlyEdited,
$recentlyShared,
];
$this->previewManager = $previewManager;
}
/**
* @param IRecommendation[] $recommendations
*
* @return IRecommendation[]
*/
private function sortRecommendations(array $recommendations): array {
usort($recommendations, function (IRecommendation $a, IRecommendation $b) {
return $b->getTimestamp() - $a->getTimestamp();
});
return $recommendations;
}
/**
* @param IRecommendation[] $recommendations
*
* @return IRecommendation[]
*/
private function addPreviews(array $recommendations): array {
foreach ($recommendations as $recommendation) {
if ($this->previewManager->isAvailable($recommendation->getNode())) {
$recommendation->setHasPreview(true);
}
}
return $recommendations;
}
/**
* @param IUser $user
*
* @return IRecommendation[]
*/
public function getRecommendations(IUser $user, int $max = self::MAX_RECOMMENDATIONS): array {
$all = array_reduce($this->sources, function (array $carry, IRecommendationSource $source) use ($user) {
return array_merge($carry, $source->getMostRecentRecommendation($user, self::MAX_RECOMMENDATIONS));
}, []);
$sorted = $this->sortRecommendations($all);
$topX = $this->getDeduplicatedSlice($sorted, $max);
return $this->addPreviews($topX);
}
/**
* Deduplicate the sorted recommendations and return the top $max picks
*
* The first (most recent) recommendation wins, hence eventually show its
* recommendation reason
*
* @param IRecommendation[] $recommendations
* @param int $max
* @return IRecommendation[]
*/
private function getDeduplicatedSlice(array $recommendations, int $max): array {
$picks = [];
foreach ($recommendations as $recommendation) {
if (empty(array_filter($picks, function (IRecommendation $rec) use ($recommendation) {
return $recommendation->getNode()->getId() === $rec->getNode()->getId();
}))) {
$picks[] = $recommendation;
}
}
return array_slice($picks, 0, $max);
}
}
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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\Recommendations\Service;
use OCP\Files\Node;
class RecommendedFile implements IRecommendation {
private string $directory;
private Node $node;
private int $timestamp;
private string $reason;
private bool $hasPreview;
public function __construct(string $directory,
Node $node,
int $timestamp,
string $reason) {
$this->directory = $directory;
$this->node = $node;
$this->reason = $reason;
$this->timestamp = $timestamp;
$this->hasPreview = false;
}
public function getId(): string {
return (string)$this->node->getId();
}
public function getDirectory(): string {
return $this->directory;
}
public function getTimestamp(): int {
return $this->timestamp;
}
public function getNode(): Node {
return $this->node;
}
public function getReason(): string {
return $this->reason;
}
public function hasPreview(): bool {
return $this->hasPreview;
}
public function setHasPreview(bool $state) {
$this->hasPreview = $state;
}
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return [
'id' => $this->getId(),
'timestamp' => $this->getTimestamp(),
'name' => $this->node->getName(),
'directory' => $this->getDirectory(),
'extension' => $this->node->getExtension(),
'mimeType' => $this->node->getMimetype(),
'hasPreview' => $this->hasPreview(),
'reason' => $this->getReason(),
];
}
}