|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
6
|
|
|
* @author Christoph Wurst <[email protected]> |
|
7
|
|
|
* @author Morris Jobke <[email protected]> |
|
8
|
|
|
* @author Robin Appelman <[email protected]> |
|
9
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @license AGPL-3.0 |
|
12
|
|
|
* |
|
13
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
15
|
|
|
* as published by the Free Software Foundation. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OCA\DAV\Connector\Sabre; |
|
28
|
|
|
|
|
29
|
|
|
use OCP\Comments\ICommentsManager; |
|
30
|
|
|
use OCP\IUserSession; |
|
31
|
|
|
use Sabre\DAV\PropFind; |
|
32
|
|
|
use Sabre\DAV\ServerPlugin; |
|
33
|
|
|
|
|
34
|
|
|
class CommentPropertiesPlugin extends ServerPlugin { |
|
35
|
|
|
public const PROPERTY_NAME_HREF = '{http://owncloud.org/ns}comments-href'; |
|
36
|
|
|
public const PROPERTY_NAME_COUNT = '{http://owncloud.org/ns}comments-count'; |
|
37
|
|
|
public const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread'; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \Sabre\DAV\Server */ |
|
40
|
|
|
protected $server; |
|
41
|
|
|
|
|
42
|
|
|
/** @var ICommentsManager */ |
|
43
|
|
|
private $commentsManager; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IUserSession */ |
|
46
|
|
|
private $userSession; |
|
47
|
|
|
|
|
48
|
|
|
private $cachedUnreadCount = []; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) { |
|
51
|
|
|
$this->commentsManager = $commentsManager; |
|
52
|
|
|
$this->userSession = $userSession; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* This initializes the plugin. |
|
57
|
|
|
* |
|
58
|
|
|
* This function is called by Sabre\DAV\Server, after |
|
59
|
|
|
* addPlugin is called. |
|
60
|
|
|
* |
|
61
|
|
|
* This method should set up the required event subscriptions. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \Sabre\DAV\Server $server |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function initialize(\Sabre\DAV\Server $server) { |
|
67
|
|
|
$this->server = $server; |
|
68
|
|
|
$this->server->on('propFind', [$this, 'handleGetProperties']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function cacheDirectory(Directory $directory) { |
|
72
|
|
|
$children = $directory->getChildren(); |
|
73
|
|
|
|
|
74
|
|
|
$ids = []; |
|
75
|
|
|
foreach ($children as $child) { |
|
76
|
|
|
if (!($child instanceof File || $child instanceof Directory)) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$id = $child->getId(); |
|
81
|
|
|
if ($id === null) { |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$ids[] = (string)$id; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$ids[] = (string) $directory->getId(); |
|
89
|
|
|
$unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser()); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($unread as $id => $count) { |
|
92
|
|
|
$this->cachedUnreadCount[(int)$id] = $count; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Adds tags and favorites properties to the response, |
|
98
|
|
|
* if requested. |
|
99
|
|
|
* |
|
100
|
|
|
* @param PropFind $propFind |
|
101
|
|
|
* @param \Sabre\DAV\INode $node |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
public function handleGetProperties( |
|
105
|
|
|
PropFind $propFind, |
|
106
|
|
|
\Sabre\DAV\INode $node |
|
107
|
|
|
) { |
|
108
|
|
|
if (!($node instanceof File) && !($node instanceof Directory)) { |
|
109
|
|
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// need prefetch ? |
|
113
|
|
|
if ($node instanceof \OCA\DAV\Connector\Sabre\Directory |
|
114
|
|
|
&& $propFind->getDepth() !== 0 |
|
115
|
|
|
&& !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD)) |
|
116
|
|
|
) { |
|
117
|
|
|
$this->cacheDirectory($node); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node) { |
|
121
|
|
|
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId()); |
|
122
|
|
|
}); |
|
123
|
|
|
|
|
124
|
|
|
$propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node) { |
|
125
|
|
|
return $this->getCommentsLink($node); |
|
126
|
|
|
}); |
|
127
|
|
|
|
|
128
|
|
|
$propFind->handle(self::PROPERTY_NAME_UNREAD, function () use ($node) { |
|
129
|
|
|
if (isset($this->cachedUnreadCount[$node->getId()])) { |
|
130
|
|
|
return $this->cachedUnreadCount[$node->getId()]; |
|
131
|
|
|
} |
|
132
|
|
|
return $this->getUnreadCount($node); |
|
133
|
|
|
}); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* returns a reference to the comments node |
|
138
|
|
|
* |
|
139
|
|
|
* @param Node $node |
|
140
|
|
|
* @return mixed|string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getCommentsLink(Node $node) { |
|
143
|
|
|
$href = $this->server->getBaseUri(); |
|
144
|
|
|
$entryPoint = strpos($href, '/remote.php/'); |
|
145
|
|
|
if ($entryPoint === false) { |
|
146
|
|
|
// in case we end up somewhere else, unexpectedly. |
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
$commentsPart = 'dav/comments/files/' . rawurldecode($node->getId()); |
|
150
|
|
|
$href = substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/')); |
|
151
|
|
|
return $href; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* returns the number of unread comments for the currently logged in user |
|
156
|
|
|
* on the given file or directory node |
|
157
|
|
|
* |
|
158
|
|
|
* @param Node $node |
|
159
|
|
|
* @return Int|null |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getUnreadCount(Node $node) { |
|
162
|
|
|
$user = $this->userSession->getUser(); |
|
163
|
|
|
if (is_null($user)) { |
|
164
|
|
|
return null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user); |
|
168
|
|
|
|
|
169
|
|
|
return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|