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,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(),
];
}
}