-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathRecentPagesService.php
143 lines (120 loc) · 4.28 KB
/
RecentPagesService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Collectives\Service;
use OCA\Collectives\Model\PageInfo;
use OCA\Collectives\Model\RecentPage;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use RuntimeException;
class RecentPagesService {
public function __construct(
protected CollectiveService $collectiveService,
protected IDBConnection $dbc,
protected IConfig $config,
protected IMimeTypeLoader $mimeTypeLoader,
protected IURLGenerator $urlGenerator,
protected IL10N $l10n,
protected IRootFolder $rootFolder,
) {
}
/**
* @throws MissingDependencyException
* @throws Exception
*/
public function forUser(IUser $user, int $limit = 10): array {
try {
$collectives = $this->collectiveService->getCollectives($user->getUID());
} catch (NotFoundException|NotPermittedException) {
return [];
}
if (!$collectives) {
return [];
}
$qb = $this->dbc->getQueryBuilder();
$appData = $this->getAppDataFolderName();
$storageId = $this->rootFolder->get($appData)->getStorage()->getCache()->getNumericStorageId();
$mimeTypeMd = $this->mimeTypeLoader->getId('text/markdown');
$expressions = [];
$collectivesMap = [];
foreach ($collectives as $collective) {
$value = sprintf($appData . '/collectives/%d/%%', $collective->getId());
$expressions[] = $qb->expr()->like('f.path', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR));
$collectivesMap[$collective->getId()] = $collective;
}
unset($collective);
$qb->select('p.*', 'f.mtime as timestamp', 'f.name as filename', 'f.path as path')
->from('filecache', 'f')
->leftJoin('f', 'collectives_pages', 'p', $qb->expr()->eq('f.fileid', 'p.file_id'))
->where($qb->expr()->eq('f.storage', $qb->createNamedParameter($storageId, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->orX(...$expressions))
->andWhere($qb->expr()->eq('f.mimetype', $qb->createNamedParameter($mimeTypeMd, IQueryBuilder::PARAM_INT)))
->orderBy('f.mtime', 'DESC')
->setMaxResults($limit);
$r = $qb->executeQuery();
$pages = [];
while ($row = $r->fetch()) {
$collectiveId = (int)explode('/', $row['path'], 4)[2];
if (!isset($collectivesMap[$collectiveId])) {
continue;
}
// cut out $appDataDir/collectives/%d/ prefix from front, and filename at the rear
$splitPath = explode('/', $row['path'], 4);
$internalPath = dirname(array_pop($splitPath));
unset($splitPath);
// prepare link and title
$pathParts = [$collectivesMap[$collectiveId]->getName()];
if ($internalPath !== '' && $internalPath !== '.') {
$pathParts = array_merge($pathParts, explode('/', $internalPath));
}
if ($row['filename'] !== 'Readme.md') {
$pathParts[] = basename($row['filename'], PageInfo::SUFFIX);
$title = basename($row['filename'], PageInfo::SUFFIX);
$pagePath = $internalPath;
} elseif ($internalPath === '' || $internalPath === '.') {
$title = $this->l10n->t('Landing page');
$pagePath = '';
} else {
$title = basename($internalPath);
$pagePath = dirname($internalPath);
}
if ($pagePath === '.') {
$pagePath = '';
}
$fileIdSuffix = '?fileId=' . $row['file_id'];
$url = $this->urlGenerator->linkToRoute('collectives.start.indexPath', ['path' => implode('/', $pathParts)]) . $fileIdSuffix;
// build result model
// not returning a PageInfo instance because it would be either incomplete or too expensive to build completely
$recentPage = new RecentPage();
$recentPage
->setCollectiveName($this->collectiveService->getCollectiveNameWithEmoji($collectivesMap[$collectiveId]))
->setTitle($title)
->setPagePath($pagePath)
->setPageUrl($url)
->setTimestamp($row['timestamp']);
if ($row['emoji']) {
$recentPage->setEmoji($row['emoji']);
}
$pages[] = $recentPage;
}
$r->closeCursor();
return $pages;
}
private function getAppDataFolderName(): string {
$instanceId = $this->config->getSystemValueString('instanceid', '');
if ($instanceId === '') {
throw new RuntimeException('no instance id!');
}
return 'appdata_' . $instanceId;
}
}