Completed
Pull Request — master (#4146)
by Robin
24:09 queued 09:57
created
apps/dav/lib/Connector/Sabre/CommentPropertiesPlugin.php 1 patch
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -29,132 +29,132 @@
 block discarded – undo
29 29
 
30 30
 class CommentPropertiesPlugin extends ServerPlugin {
31 31
 
32
-	const PROPERTY_NAME_HREF   = '{http://owncloud.org/ns}comments-href';
33
-	const PROPERTY_NAME_COUNT  = '{http://owncloud.org/ns}comments-count';
34
-	const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread';
35
-
36
-	/** @var  \Sabre\DAV\Server */
37
-	protected $server;
38
-
39
-	/** @var ICommentsManager */
40
-	private $commentsManager;
41
-
42
-	/** @var IUserSession */
43
-	private $userSession;
44
-
45
-	private $cachedUnreadCount = [];
46
-
47
-	private $cachedFolders = [];
48
-
49
-	public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
50
-		$this->commentsManager = $commentsManager;
51
-		$this->userSession = $userSession;
52
-	}
53
-
54
-	/**
55
-	 * This initializes the plugin.
56
-	 *
57
-	 * This function is called by Sabre\DAV\Server, after
58
-	 * addPlugin is called.
59
-	 *
60
-	 * This method should set up the required event subscriptions.
61
-	 *
62
-	 * @param \Sabre\DAV\Server $server
63
-	 * @return void
64
-	 */
65
-	function initialize(\Sabre\DAV\Server $server) {
66
-		$this->server = $server;
67
-		$this->server->on('propFind', array($this, 'handleGetProperties'));
68
-	}
69
-
70
-	/**
71
-	 * Adds tags and favorites properties to the response,
72
-	 * if requested.
73
-	 *
74
-	 * @param PropFind $propFind
75
-	 * @param \Sabre\DAV\INode $node
76
-	 * @return void
77
-	 */
78
-	public function handleGetProperties(
79
-		PropFind $propFind,
80
-		\Sabre\DAV\INode $node
81
-	) {
82
-		if (!($node instanceof File) && !($node instanceof Directory)) {
83
-			return;
84
-		}
85
-
86
-		// need prefetch ?
87
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
88
-			&& $propFind->getDepth() !== 0
89
-			&& !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
90
-		) {
91
-			$unreadCounts = $this->commentsManager->getNumberOfUnreadCommentsForFolder($node->getId(), $this->userSession->getUser());
92
-			$this->cachedFolders[] = $node->getPath();
93
-			foreach ($unreadCounts as $id => $count) {
94
-				$this->cachedUnreadCount[$id] = $count;
95
-			}
96
-		}
97
-
98
-		$propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) {
99
-			return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()));
100
-		});
101
-
102
-		$propFind->handle(self::PROPERTY_NAME_HREF, function() use ($node) {
103
-			return $this->getCommentsLink($node);
104
-		});
105
-
106
-		$propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) {
107
-			if (isset($this->cachedUnreadCount[$node->getId()])) {
108
-				return $this->cachedUnreadCount[$node->getId()];
109
-			} else {
110
-				list($parentPath,) = \Sabre\Uri\split($node->getPath());
111
-				if ($parentPath === '') {
112
-					$parentPath = '/';
113
-				}
114
-				// if we already cached the folder this file is in we know there are no shares for this file
115
-				if (array_search($parentPath, $this->cachedFolders) === false) {
116
-					return $this->getUnreadCount($node);
117
-				} else {
118
-					return 0;
119
-				}
120
-			}
121
-		});
122
-	}
123
-
124
-	/**
125
-	 * returns a reference to the comments node
126
-	 *
127
-	 * @param Node $node
128
-	 * @return mixed|string
129
-	 */
130
-	public function getCommentsLink(Node $node) {
131
-		$href =  $this->server->getBaseUri();
132
-		$entryPoint = strpos($href, '/remote.php/');
133
-		if($entryPoint === false) {
134
-			// in case we end up somewhere else, unexpectedly.
135
-			return null;
136
-		}
137
-		$commentsPart = 'dav/comments/files/' . rawurldecode($node->getId());
138
-		$href = substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
139
-		return $href;
140
-	}
141
-
142
-	/**
143
-	 * returns the number of unread comments for the currently logged in user
144
-	 * on the given file or directory node
145
-	 *
146
-	 * @param Node $node
147
-	 * @return Int|null
148
-	 */
149
-	public function getUnreadCount(Node $node) {
150
-		$user = $this->userSession->getUser();
151
-		if(is_null($user)) {
152
-			return null;
153
-		}
154
-
155
-		$lastRead = $this->commentsManager->getReadMark('files', strval($node->getId()), $user);
156
-
157
-		return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()), $lastRead);
158
-	}
32
+    const PROPERTY_NAME_HREF   = '{http://owncloud.org/ns}comments-href';
33
+    const PROPERTY_NAME_COUNT  = '{http://owncloud.org/ns}comments-count';
34
+    const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread';
35
+
36
+    /** @var  \Sabre\DAV\Server */
37
+    protected $server;
38
+
39
+    /** @var ICommentsManager */
40
+    private $commentsManager;
41
+
42
+    /** @var IUserSession */
43
+    private $userSession;
44
+
45
+    private $cachedUnreadCount = [];
46
+
47
+    private $cachedFolders = [];
48
+
49
+    public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
50
+        $this->commentsManager = $commentsManager;
51
+        $this->userSession = $userSession;
52
+    }
53
+
54
+    /**
55
+     * This initializes the plugin.
56
+     *
57
+     * This function is called by Sabre\DAV\Server, after
58
+     * addPlugin is called.
59
+     *
60
+     * This method should set up the required event subscriptions.
61
+     *
62
+     * @param \Sabre\DAV\Server $server
63
+     * @return void
64
+     */
65
+    function initialize(\Sabre\DAV\Server $server) {
66
+        $this->server = $server;
67
+        $this->server->on('propFind', array($this, 'handleGetProperties'));
68
+    }
69
+
70
+    /**
71
+     * Adds tags and favorites properties to the response,
72
+     * if requested.
73
+     *
74
+     * @param PropFind $propFind
75
+     * @param \Sabre\DAV\INode $node
76
+     * @return void
77
+     */
78
+    public function handleGetProperties(
79
+        PropFind $propFind,
80
+        \Sabre\DAV\INode $node
81
+    ) {
82
+        if (!($node instanceof File) && !($node instanceof Directory)) {
83
+            return;
84
+        }
85
+
86
+        // need prefetch ?
87
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
88
+            && $propFind->getDepth() !== 0
89
+            && !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
90
+        ) {
91
+            $unreadCounts = $this->commentsManager->getNumberOfUnreadCommentsForFolder($node->getId(), $this->userSession->getUser());
92
+            $this->cachedFolders[] = $node->getPath();
93
+            foreach ($unreadCounts as $id => $count) {
94
+                $this->cachedUnreadCount[$id] = $count;
95
+            }
96
+        }
97
+
98
+        $propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) {
99
+            return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()));
100
+        });
101
+
102
+        $propFind->handle(self::PROPERTY_NAME_HREF, function() use ($node) {
103
+            return $this->getCommentsLink($node);
104
+        });
105
+
106
+        $propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) {
107
+            if (isset($this->cachedUnreadCount[$node->getId()])) {
108
+                return $this->cachedUnreadCount[$node->getId()];
109
+            } else {
110
+                list($parentPath,) = \Sabre\Uri\split($node->getPath());
111
+                if ($parentPath === '') {
112
+                    $parentPath = '/';
113
+                }
114
+                // if we already cached the folder this file is in we know there are no shares for this file
115
+                if (array_search($parentPath, $this->cachedFolders) === false) {
116
+                    return $this->getUnreadCount($node);
117
+                } else {
118
+                    return 0;
119
+                }
120
+            }
121
+        });
122
+    }
123
+
124
+    /**
125
+     * returns a reference to the comments node
126
+     *
127
+     * @param Node $node
128
+     * @return mixed|string
129
+     */
130
+    public function getCommentsLink(Node $node) {
131
+        $href =  $this->server->getBaseUri();
132
+        $entryPoint = strpos($href, '/remote.php/');
133
+        if($entryPoint === false) {
134
+            // in case we end up somewhere else, unexpectedly.
135
+            return null;
136
+        }
137
+        $commentsPart = 'dav/comments/files/' . rawurldecode($node->getId());
138
+        $href = substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
139
+        return $href;
140
+    }
141
+
142
+    /**
143
+     * returns the number of unread comments for the currently logged in user
144
+     * on the given file or directory node
145
+     *
146
+     * @param Node $node
147
+     * @return Int|null
148
+     */
149
+    public function getUnreadCount(Node $node) {
150
+        $user = $this->userSession->getUser();
151
+        if(is_null($user)) {
152
+            return null;
153
+        }
154
+
155
+        $lastRead = $this->commentsManager->getReadMark('files', strval($node->getId()), $user);
156
+
157
+        return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()), $lastRead);
158
+    }
159 159
 
160 160
 }
Please login to merge, or discard this patch.
lib/public/Comments/ICommentsManager.php 1 patch
Indentation   +233 added lines, -233 removed lines patch added patch discarded remove patch
@@ -35,255 +35,255 @@
 block discarded – undo
35 35
  */
36 36
 interface ICommentsManager {
37 37
 
38
-	/**
39
-	 * @const DELETED_USER type and id for a user that has been deleted
40
-	 * @see deleteReferencesOfActor
41
-	 * @since 9.0.0
42
-	 *
43
-	 * To be used as replacement for user type actors in deleteReferencesOfActor().
44
-	 *
45
-	 * User interfaces shall show "Deleted user" as display name, if needed.
46
-	 */
47
-	const DELETED_USER = 'deleted_users';
38
+    /**
39
+     * @const DELETED_USER type and id for a user that has been deleted
40
+     * @see deleteReferencesOfActor
41
+     * @since 9.0.0
42
+     *
43
+     * To be used as replacement for user type actors in deleteReferencesOfActor().
44
+     *
45
+     * User interfaces shall show "Deleted user" as display name, if needed.
46
+     */
47
+    const DELETED_USER = 'deleted_users';
48 48
 
49
-	/**
50
-	 * returns a comment instance
51
-	 *
52
-	 * @param string $id the ID of the comment
53
-	 * @return IComment
54
-	 * @throws NotFoundException
55
-	 * @since 9.0.0
56
-	 */
57
-	public function get($id);
49
+    /**
50
+     * returns a comment instance
51
+     *
52
+     * @param string $id the ID of the comment
53
+     * @return IComment
54
+     * @throws NotFoundException
55
+     * @since 9.0.0
56
+     */
57
+    public function get($id);
58 58
 
59
-	/**
60
-	 * returns the comment specified by the id and all it's child comments
61
-	 *
62
-	 * @param string $id
63
-	 * @param int $limit max number of entries to return, 0 returns all
64
-	 * @param int $offset the start entry
65
-	 * @return array
66
-	 * @since 9.0.0
67
-	 *
68
-	 * The return array looks like this
69
-	 * [
70
-	 * 	 'comment' => IComment, // root comment
71
-	 *   'replies' =>
72
-	 *   [
73
-	 *     0 =>
74
-	 *     [
75
-	 *       'comment' => IComment,
76
-	 *       'replies' =>
77
-	 *       [
78
-	 *         0 =>
79
-	 *         [
80
-	 *           'comment' => IComment,
81
-	 *           'replies' => [ … ]
82
-	 *         ],
83
-	 *         …
84
-	 *       ]
85
-	 *     ]
86
-	 *     1 =>
87
-	 *     [
88
-	 *       'comment' => IComment,
89
-	 *       'replies'=> [ … ]
90
-	 *     ],
91
-	 *     …
92
-	 *   ]
93
-	 * ]
94
-	 */
95
-	public function getTree($id, $limit = 0, $offset = 0);
59
+    /**
60
+     * returns the comment specified by the id and all it's child comments
61
+     *
62
+     * @param string $id
63
+     * @param int $limit max number of entries to return, 0 returns all
64
+     * @param int $offset the start entry
65
+     * @return array
66
+     * @since 9.0.0
67
+     *
68
+     * The return array looks like this
69
+     * [
70
+     * 	 'comment' => IComment, // root comment
71
+     *   'replies' =>
72
+     *   [
73
+     *     0 =>
74
+     *     [
75
+     *       'comment' => IComment,
76
+     *       'replies' =>
77
+     *       [
78
+     *         0 =>
79
+     *         [
80
+     *           'comment' => IComment,
81
+     *           'replies' => [ … ]
82
+     *         ],
83
+     *         …
84
+     *       ]
85
+     *     ]
86
+     *     1 =>
87
+     *     [
88
+     *       'comment' => IComment,
89
+     *       'replies'=> [ … ]
90
+     *     ],
91
+     *     …
92
+     *   ]
93
+     * ]
94
+     */
95
+    public function getTree($id, $limit = 0, $offset = 0);
96 96
 
97
-	/**
98
-	 * returns comments for a specific object (e.g. a file).
99
-	 *
100
-	 * The sort order is always newest to oldest.
101
-	 *
102
-	 * @param string $objectType the object type, e.g. 'files'
103
-	 * @param string $objectId the id of the object
104
-	 * @param int $limit optional, number of maximum comments to be returned. if
105
-	 * not specified, all comments are returned.
106
-	 * @param int $offset optional, starting point
107
-	 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
108
-	 * that may be returned
109
-	 * @return IComment[]
110
-	 * @since 9.0.0
111
-	 */
112
-	public function getForObject(
113
-			$objectType,
114
-			$objectId,
115
-			$limit = 0,
116
-			$offset = 0,
117
-			\DateTime $notOlderThan = null
118
-	);
97
+    /**
98
+     * returns comments for a specific object (e.g. a file).
99
+     *
100
+     * The sort order is always newest to oldest.
101
+     *
102
+     * @param string $objectType the object type, e.g. 'files'
103
+     * @param string $objectId the id of the object
104
+     * @param int $limit optional, number of maximum comments to be returned. if
105
+     * not specified, all comments are returned.
106
+     * @param int $offset optional, starting point
107
+     * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
108
+     * that may be returned
109
+     * @return IComment[]
110
+     * @since 9.0.0
111
+     */
112
+    public function getForObject(
113
+            $objectType,
114
+            $objectId,
115
+            $limit = 0,
116
+            $offset = 0,
117
+            \DateTime $notOlderThan = null
118
+    );
119 119
 
120
-	/**
121
-	 * @param $objectType string the object type, e.g. 'files'
122
-	 * @param $objectId string the id of the object
123
-	 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
124
-	 * that may be returned
125
-	 * @return Int
126
-	 * @since 9.0.0
127
-	 */
128
-	public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null);
120
+    /**
121
+     * @param $objectType string the object type, e.g. 'files'
122
+     * @param $objectId string the id of the object
123
+     * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
124
+     * that may be returned
125
+     * @return Int
126
+     * @since 9.0.0
127
+     */
128
+    public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null);
129 129
 
130
-	/**
131
-	 * Get the number of unread comments for all files in a folder
132
-	 *
133
-	 * @param int $folderId
134
-	 * @param IUser $user
135
-	 * @return array [$fileId => $unreadCount]
136
-	 * @since 12.0.0
137
-	 */
138
-	public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user);
130
+    /**
131
+     * Get the number of unread comments for all files in a folder
132
+     *
133
+     * @param int $folderId
134
+     * @param IUser $user
135
+     * @return array [$fileId => $unreadCount]
136
+     * @since 12.0.0
137
+     */
138
+    public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user);
139 139
 
140
-	/**
141
-	 * creates a new comment and returns it. At this point of time, it is not
142
-	 * saved in the used data storage. Use save() after setting other fields
143
-	 * of the comment (e.g. message or verb).
144
-	 *
145
-	 * @param string $actorType the actor type (e.g. 'users')
146
-	 * @param string $actorId a user id
147
-	 * @param string $objectType the object type the comment is attached to
148
-	 * @param string $objectId the object id the comment is attached to
149
-	 * @return IComment
150
-	 * @since 9.0.0
151
-	 */
152
-	public function create($actorType, $actorId, $objectType, $objectId);
140
+    /**
141
+     * creates a new comment and returns it. At this point of time, it is not
142
+     * saved in the used data storage. Use save() after setting other fields
143
+     * of the comment (e.g. message or verb).
144
+     *
145
+     * @param string $actorType the actor type (e.g. 'users')
146
+     * @param string $actorId a user id
147
+     * @param string $objectType the object type the comment is attached to
148
+     * @param string $objectId the object id the comment is attached to
149
+     * @return IComment
150
+     * @since 9.0.0
151
+     */
152
+    public function create($actorType, $actorId, $objectType, $objectId);
153 153
 
154
-	/**
155
-	 * permanently deletes the comment specified by the ID
156
-	 *
157
-	 * When the comment has child comments, their parent ID will be changed to
158
-	 * the parent ID of the item that is to be deleted.
159
-	 *
160
-	 * @param string $id
161
-	 * @return bool
162
-	 * @since 9.0.0
163
-	 */
164
-	public function delete($id);
154
+    /**
155
+     * permanently deletes the comment specified by the ID
156
+     *
157
+     * When the comment has child comments, their parent ID will be changed to
158
+     * the parent ID of the item that is to be deleted.
159
+     *
160
+     * @param string $id
161
+     * @return bool
162
+     * @since 9.0.0
163
+     */
164
+    public function delete($id);
165 165
 
166
-	/**
167
-	 * saves the comment permanently
168
-	 *
169
-	 * if the supplied comment has an empty ID, a new entry comment will be
170
-	 * saved and the instance updated with the new ID.
171
-	 *
172
-	 * Otherwise, an existing comment will be updated.
173
-	 *
174
-	 * Throws NotFoundException when a comment that is to be updated does not
175
-	 * exist anymore at this point of time.
176
-	 *
177
-	 * @param IComment $comment
178
-	 * @return bool
179
-	 * @throws NotFoundException
180
-	 * @since 9.0.0
181
-	 */
182
-	public function save(IComment $comment);
166
+    /**
167
+     * saves the comment permanently
168
+     *
169
+     * if the supplied comment has an empty ID, a new entry comment will be
170
+     * saved and the instance updated with the new ID.
171
+     *
172
+     * Otherwise, an existing comment will be updated.
173
+     *
174
+     * Throws NotFoundException when a comment that is to be updated does not
175
+     * exist anymore at this point of time.
176
+     *
177
+     * @param IComment $comment
178
+     * @return bool
179
+     * @throws NotFoundException
180
+     * @since 9.0.0
181
+     */
182
+    public function save(IComment $comment);
183 183
 
184
-	/**
185
-	 * removes references to specific actor (e.g. on user delete) of a comment.
186
-	 * The comment itself must not get lost/deleted.
187
-	 *
188
-	 * A 'users' type actor (type and id) should get replaced by the
189
-	 * value of the DELETED_USER constant of this interface.
190
-	 *
191
-	 * @param string $actorType the actor type (e.g. 'users')
192
-	 * @param string $actorId a user id
193
-	 * @return boolean
194
-	 * @since 9.0.0
195
-	 */
196
-	public function deleteReferencesOfActor($actorType, $actorId);
184
+    /**
185
+     * removes references to specific actor (e.g. on user delete) of a comment.
186
+     * The comment itself must not get lost/deleted.
187
+     *
188
+     * A 'users' type actor (type and id) should get replaced by the
189
+     * value of the DELETED_USER constant of this interface.
190
+     *
191
+     * @param string $actorType the actor type (e.g. 'users')
192
+     * @param string $actorId a user id
193
+     * @return boolean
194
+     * @since 9.0.0
195
+     */
196
+    public function deleteReferencesOfActor($actorType, $actorId);
197 197
 
198
-	/**
199
-	 * deletes all comments made of a specific object (e.g. on file delete)
200
-	 *
201
-	 * @param string $objectType the object type (e.g. 'files')
202
-	 * @param string $objectId e.g. the file id
203
-	 * @return boolean
204
-	 * @since 9.0.0
205
-	 */
206
-	public function deleteCommentsAtObject($objectType, $objectId);
198
+    /**
199
+     * deletes all comments made of a specific object (e.g. on file delete)
200
+     *
201
+     * @param string $objectType the object type (e.g. 'files')
202
+     * @param string $objectId e.g. the file id
203
+     * @return boolean
204
+     * @since 9.0.0
205
+     */
206
+    public function deleteCommentsAtObject($objectType, $objectId);
207 207
 
208
-	/**
209
-	 * sets the read marker for a given file to the specified date for the
210
-	 * provided user
211
-	 *
212
-	 * @param string $objectType
213
-	 * @param string $objectId
214
-	 * @param \DateTime $dateTime
215
-	 * @param \OCP\IUser $user
216
-	 * @since 9.0.0
217
-	 */
218
-	public function setReadMark($objectType, $objectId, \DateTime $dateTime, \OCP\IUser $user);
208
+    /**
209
+     * sets the read marker for a given file to the specified date for the
210
+     * provided user
211
+     *
212
+     * @param string $objectType
213
+     * @param string $objectId
214
+     * @param \DateTime $dateTime
215
+     * @param \OCP\IUser $user
216
+     * @since 9.0.0
217
+     */
218
+    public function setReadMark($objectType, $objectId, \DateTime $dateTime, \OCP\IUser $user);
219 219
 
220
-	/**
221
-	 * returns the read marker for a given file to the specified date for the
222
-	 * provided user. It returns null, when the marker is not present, i.e.
223
-	 * no comments were marked as read.
224
-	 *
225
-	 * @param string $objectType
226
-	 * @param string $objectId
227
-	 * @param \OCP\IUser $user
228
-	 * @return \DateTime|null
229
-	 * @since 9.0.0
230
-	 */
231
-	public function getReadMark($objectType, $objectId, \OCP\IUser $user);
220
+    /**
221
+     * returns the read marker for a given file to the specified date for the
222
+     * provided user. It returns null, when the marker is not present, i.e.
223
+     * no comments were marked as read.
224
+     *
225
+     * @param string $objectType
226
+     * @param string $objectId
227
+     * @param \OCP\IUser $user
228
+     * @return \DateTime|null
229
+     * @since 9.0.0
230
+     */
231
+    public function getReadMark($objectType, $objectId, \OCP\IUser $user);
232 232
 
233
-	/**
234
-	 * deletes the read markers for the specified user
235
-	 *
236
-	 * @param \OCP\IUser $user
237
-	 * @return bool
238
-	 * @since 9.0.0
239
-	 */
240
-	public function deleteReadMarksFromUser(\OCP\IUser $user);
233
+    /**
234
+     * deletes the read markers for the specified user
235
+     *
236
+     * @param \OCP\IUser $user
237
+     * @return bool
238
+     * @since 9.0.0
239
+     */
240
+    public function deleteReadMarksFromUser(\OCP\IUser $user);
241 241
 
242
-	/**
243
-	 * deletes the read markers on the specified object
244
-	 *
245
-	 * @param string $objectType
246
-	 * @param string $objectId
247
-	 * @return bool
248
-	 * @since 9.0.0
249
-	 */
250
-	public function deleteReadMarksOnObject($objectType, $objectId);
242
+    /**
243
+     * deletes the read markers on the specified object
244
+     *
245
+     * @param string $objectType
246
+     * @param string $objectId
247
+     * @return bool
248
+     * @since 9.0.0
249
+     */
250
+    public function deleteReadMarksOnObject($objectType, $objectId);
251 251
 
252
-	/**
253
-	 * registers an Entity to the manager, so event notifications can be send
254
-	 * to consumers of the comments infrastructure
255
-	 *
256
-	 * @param \Closure $closure
257
-	 * @since 11.0.0
258
-	 */
259
-	public function registerEventHandler(\Closure $closure);
252
+    /**
253
+     * registers an Entity to the manager, so event notifications can be send
254
+     * to consumers of the comments infrastructure
255
+     *
256
+     * @param \Closure $closure
257
+     * @since 11.0.0
258
+     */
259
+    public function registerEventHandler(\Closure $closure);
260 260
 
261
-	/**
262
-	 * registers a method that resolves an ID to a display name for a given type
263
-	 *
264
-	 * @param string $type
265
-	 * @param \Closure $closure
266
-	 * @throws \OutOfBoundsException
267
-	 * @since 11.0.0
268
-	 *
269
-	 * Only one resolver shall be registered per type. Otherwise a
270
-	 * \OutOfBoundsException has to thrown.
271
-	 */
272
-	public function registerDisplayNameResolver($type, \Closure $closure);
261
+    /**
262
+     * registers a method that resolves an ID to a display name for a given type
263
+     *
264
+     * @param string $type
265
+     * @param \Closure $closure
266
+     * @throws \OutOfBoundsException
267
+     * @since 11.0.0
268
+     *
269
+     * Only one resolver shall be registered per type. Otherwise a
270
+     * \OutOfBoundsException has to thrown.
271
+     */
272
+    public function registerDisplayNameResolver($type, \Closure $closure);
273 273
 
274
-	/**
275
-	 * resolves a given ID of a given Type to a display name.
276
-	 *
277
-	 * @param string $type
278
-	 * @param string $id
279
-	 * @return string
280
-	 * @throws \OutOfBoundsException
281
-	 * @since 11.0.0
282
-	 *
283
-	 * If a provided type was not registered, an \OutOfBoundsException shall
284
-	 * be thrown. It is upon the resolver discretion what to return of the
285
-	 * provided ID is unknown. It must be ensured that a string is returned.
286
-	 */
287
-	public function resolveDisplayName($type, $id);
274
+    /**
275
+     * resolves a given ID of a given Type to a display name.
276
+     *
277
+     * @param string $type
278
+     * @param string $id
279
+     * @return string
280
+     * @throws \OutOfBoundsException
281
+     * @since 11.0.0
282
+     *
283
+     * If a provided type was not registered, an \OutOfBoundsException shall
284
+     * be thrown. It is upon the resolver discretion what to return of the
285
+     * provided ID is unknown. It must be ensured that a string is returned.
286
+     */
287
+    public function resolveDisplayName($type, $id);
288 288
 
289 289
 }
Please login to merge, or discard this patch.
lib/private/DB/QueryBuilder/QuoteHelper.php 2 patches
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -27,55 +27,55 @@
 block discarded – undo
27 27
 use OCP\DB\QueryBuilder\IQueryFunction;
28 28
 
29 29
 class QuoteHelper {
30
-	/**
31
-	 * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter
32
-	 * @return array|string
33
-	 */
34
-	public function quoteColumnNames($strings) {
35
-		if (!is_array($strings)) {
36
-			return $this->quoteColumnName($strings);
37
-		}
30
+    /**
31
+     * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter
32
+     * @return array|string
33
+     */
34
+    public function quoteColumnNames($strings) {
35
+        if (!is_array($strings)) {
36
+            return $this->quoteColumnName($strings);
37
+        }
38 38
 
39
-		$return = [];
40
-		foreach ($strings as $string) {
41
-			$return[] = $this->quoteColumnName($string);
42
-		}
39
+        $return = [];
40
+        foreach ($strings as $string) {
41
+            $return[] = $this->quoteColumnName($string);
42
+        }
43 43
 
44
-		return $return;
45
-	}
44
+        return $return;
45
+    }
46 46
 
47
-	/**
48
-	 * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter
49
-	 * @return string
50
-	 */
51
-	public function quoteColumnName($string) {
52
-		if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) {
53
-			return (string) $string;
54
-		}
47
+    /**
48
+     * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter
49
+     * @return string
50
+     */
51
+    public function quoteColumnName($string) {
52
+        if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) {
53
+            return (string) $string;
54
+        }
55 55
 
56
-		if ($string === null || $string === 'null' || $string === '*') {
57
-			return $string;
58
-		}
56
+        if ($string === null || $string === 'null' || $string === '*') {
57
+            return $string;
58
+        }
59 59
 
60
-		if (!is_string($string)) {
61
-			throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
62
-		}
60
+        if (!is_string($string)) {
61
+            throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
62
+        }
63 63
 
64
-		$string = str_replace(' AS ', ' as ', $string);
65
-		if (substr_count($string, ' as ')) {
66
-			return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2)));
67
-		}
64
+        $string = str_replace(' AS ', ' as ', $string);
65
+        if (substr_count($string, ' as ')) {
66
+            return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2)));
67
+        }
68 68
 
69
-		if (substr_count($string, '.')) {
70
-			list($alias, $columnName) = explode('.', $string, 2);
69
+        if (substr_count($string, '.')) {
70
+            list($alias, $columnName) = explode('.', $string, 2);
71 71
 
72
-			if ($columnName === '*') {
73
-				return $string;
74
-			}
72
+            if ($columnName === '*') {
73
+                return $string;
74
+            }
75 75
 
76
-			return '`' . $alias . '`.`' . $columnName . '`';
77
-		}
76
+            return '`' . $alias . '`.`' . $columnName . '`';
77
+        }
78 78
 
79
-		return '`' . $string . '`';
80
-	}
79
+        return '`' . $string . '`';
80
+    }
81 81
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -73,9 +73,9 @@
 block discarded – undo
73 73
 				return $string;
74 74
 			}
75 75
 
76
-			return '`' . $alias . '`.`' . $columnName . '`';
76
+			return '`'.$alias.'`.`'.$columnName.'`';
77 77
 		}
78 78
 
79
-		return '`' . $string . '`';
79
+		return '`'.$string.'`';
80 80
 	}
81 81
 }
Please login to merge, or discard this patch.
lib/private/Comments/Manager.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -25,7 +25,6 @@
 block discarded – undo
25 25
 namespace OC\Comments;
26 26
 
27 27
 use Doctrine\DBAL\Exception\DriverException;
28
-use Doctrine\DBAL\Platforms\MySqlPlatform;
29 28
 use OCP\Comments\CommentsEvent;
30 29
 use OCP\Comments\IComment;
31 30
 use OCP\Comments\ICommentsEventHandler;
Please login to merge, or discard this patch.
Indentation   +838 added lines, -838 removed lines patch added patch discarded remove patch
@@ -39,842 +39,842 @@
 block discarded – undo
39 39
 
40 40
 class Manager implements ICommentsManager {
41 41
 
42
-	/** @var  IDBConnection */
43
-	protected $dbConn;
44
-
45
-	/** @var  ILogger */
46
-	protected $logger;
47
-
48
-	/** @var IConfig */
49
-	protected $config;
50
-
51
-	/** @var IComment[] */
52
-	protected $commentsCache = [];
53
-
54
-	/** @var  \Closure[] */
55
-	protected $eventHandlerClosures = [];
56
-
57
-	/** @var  ICommentsEventHandler[] */
58
-	protected $eventHandlers = [];
59
-
60
-	/** @var \Closure[] */
61
-	protected $displayNameResolvers = [];
62
-
63
-	/**
64
-	 * Manager constructor.
65
-	 *
66
-	 * @param IDBConnection $dbConn
67
-	 * @param ILogger $logger
68
-	 * @param IConfig $config
69
-	 */
70
-	public function __construct(
71
-		IDBConnection $dbConn,
72
-		ILogger $logger,
73
-		IConfig $config
74
-	) {
75
-		$this->dbConn = $dbConn;
76
-		$this->logger = $logger;
77
-		$this->config = $config;
78
-	}
79
-
80
-	/**
81
-	 * converts data base data into PHP native, proper types as defined by
82
-	 * IComment interface.
83
-	 *
84
-	 * @param array $data
85
-	 * @return array
86
-	 */
87
-	protected function normalizeDatabaseData(array $data) {
88
-		$data['id'] = strval($data['id']);
89
-		$data['parent_id'] = strval($data['parent_id']);
90
-		$data['topmost_parent_id'] = strval($data['topmost_parent_id']);
91
-		$data['creation_timestamp'] = new \DateTime($data['creation_timestamp']);
92
-		if (!is_null($data['latest_child_timestamp'])) {
93
-			$data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']);
94
-		}
95
-		$data['children_count'] = intval($data['children_count']);
96
-		return $data;
97
-	}
98
-
99
-	/**
100
-	 * prepares a comment for an insert or update operation after making sure
101
-	 * all necessary fields have a value assigned.
102
-	 *
103
-	 * @param IComment $comment
104
-	 * @return IComment returns the same updated IComment instance as provided
105
-	 *                  by parameter for convenience
106
-	 * @throws \UnexpectedValueException
107
-	 */
108
-	protected function prepareCommentForDatabaseWrite(IComment $comment) {
109
-		if (!$comment->getActorType()
110
-			|| !$comment->getActorId()
111
-			|| !$comment->getObjectType()
112
-			|| !$comment->getObjectId()
113
-			|| !$comment->getVerb()
114
-		) {
115
-			throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
116
-		}
117
-
118
-		if ($comment->getId() === '') {
119
-			$comment->setChildrenCount(0);
120
-			$comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC')));
121
-			$comment->setLatestChildDateTime(null);
122
-		}
123
-
124
-		if (is_null($comment->getCreationDateTime())) {
125
-			$comment->setCreationDateTime(new \DateTime());
126
-		}
127
-
128
-		if ($comment->getParentId() !== '0') {
129
-			$comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId()));
130
-		} else {
131
-			$comment->setTopmostParentId('0');
132
-		}
133
-
134
-		$this->cache($comment);
135
-
136
-		return $comment;
137
-	}
138
-
139
-	/**
140
-	 * returns the topmost parent id of a given comment identified by ID
141
-	 *
142
-	 * @param string $id
143
-	 * @return string
144
-	 * @throws NotFoundException
145
-	 */
146
-	protected function determineTopmostParentId($id) {
147
-		$comment = $this->get($id);
148
-		if ($comment->getParentId() === '0') {
149
-			return $comment->getId();
150
-		} else {
151
-			return $this->determineTopmostParentId($comment->getId());
152
-		}
153
-	}
154
-
155
-	/**
156
-	 * updates child information of a comment
157
-	 *
158
-	 * @param string $id
159
-	 * @param \DateTime $cDateTime the date time of the most recent child
160
-	 * @throws NotFoundException
161
-	 */
162
-	protected function updateChildrenInformation($id, \DateTime $cDateTime) {
163
-		$qb = $this->dbConn->getQueryBuilder();
164
-		$query = $qb->select($qb->createFunction('COUNT(`id`)'))
165
-			->from('comments')
166
-			->where($qb->expr()->eq('parent_id', $qb->createParameter('id')))
167
-			->setParameter('id', $id);
168
-
169
-		$resultStatement = $query->execute();
170
-		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
171
-		$resultStatement->closeCursor();
172
-		$children = intval($data[0]);
173
-
174
-		$comment = $this->get($id);
175
-		$comment->setChildrenCount($children);
176
-		$comment->setLatestChildDateTime($cDateTime);
177
-		$this->save($comment);
178
-	}
179
-
180
-	/**
181
-	 * Tests whether actor or object type and id parameters are acceptable.
182
-	 * Throws exception if not.
183
-	 *
184
-	 * @param string $role
185
-	 * @param string $type
186
-	 * @param string $id
187
-	 * @throws \InvalidArgumentException
188
-	 */
189
-	protected function checkRoleParameters($role, $type, $id) {
190
-		if (
191
-			!is_string($type) || empty($type)
192
-			|| !is_string($id) || empty($id)
193
-		) {
194
-			throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
195
-		}
196
-	}
197
-
198
-	/**
199
-	 * run-time caches a comment
200
-	 *
201
-	 * @param IComment $comment
202
-	 */
203
-	protected function cache(IComment $comment) {
204
-		$id = $comment->getId();
205
-		if (empty($id)) {
206
-			return;
207
-		}
208
-		$this->commentsCache[strval($id)] = $comment;
209
-	}
210
-
211
-	/**
212
-	 * removes an entry from the comments run time cache
213
-	 *
214
-	 * @param mixed $id the comment's id
215
-	 */
216
-	protected function uncache($id) {
217
-		$id = strval($id);
218
-		if (isset($this->commentsCache[$id])) {
219
-			unset($this->commentsCache[$id]);
220
-		}
221
-	}
222
-
223
-	/**
224
-	 * returns a comment instance
225
-	 *
226
-	 * @param string $id the ID of the comment
227
-	 * @return IComment
228
-	 * @throws NotFoundException
229
-	 * @throws \InvalidArgumentException
230
-	 * @since 9.0.0
231
-	 */
232
-	public function get($id) {
233
-		if (intval($id) === 0) {
234
-			throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.');
235
-		}
236
-
237
-		if (isset($this->commentsCache[$id])) {
238
-			return $this->commentsCache[$id];
239
-		}
240
-
241
-		$qb = $this->dbConn->getQueryBuilder();
242
-		$resultStatement = $qb->select('*')
243
-			->from('comments')
244
-			->where($qb->expr()->eq('id', $qb->createParameter('id')))
245
-			->setParameter('id', $id, IQueryBuilder::PARAM_INT)
246
-			->execute();
247
-
248
-		$data = $resultStatement->fetch();
249
-		$resultStatement->closeCursor();
250
-		if (!$data) {
251
-			throw new NotFoundException();
252
-		}
253
-
254
-		$comment = new Comment($this->normalizeDatabaseData($data));
255
-		$this->cache($comment);
256
-		return $comment;
257
-	}
258
-
259
-	/**
260
-	 * returns the comment specified by the id and all it's child comments.
261
-	 * At this point of time, we do only support one level depth.
262
-	 *
263
-	 * @param string $id
264
-	 * @param int $limit max number of entries to return, 0 returns all
265
-	 * @param int $offset the start entry
266
-	 * @return array
267
-	 * @since 9.0.0
268
-	 *
269
-	 * The return array looks like this
270
-	 * [
271
-	 *   'comment' => IComment, // root comment
272
-	 *   'replies' =>
273
-	 *   [
274
-	 *     0 =>
275
-	 *     [
276
-	 *       'comment' => IComment,
277
-	 *       'replies' => []
278
-	 *     ]
279
-	 *     1 =>
280
-	 *     [
281
-	 *       'comment' => IComment,
282
-	 *       'replies'=> []
283
-	 *     ],
284
-	 *     …
285
-	 *   ]
286
-	 * ]
287
-	 */
288
-	public function getTree($id, $limit = 0, $offset = 0) {
289
-		$tree = [];
290
-		$tree['comment'] = $this->get($id);
291
-		$tree['replies'] = [];
292
-
293
-		$qb = $this->dbConn->getQueryBuilder();
294
-		$query = $qb->select('*')
295
-			->from('comments')
296
-			->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id')))
297
-			->orderBy('creation_timestamp', 'DESC')
298
-			->setParameter('id', $id);
299
-
300
-		if ($limit > 0) {
301
-			$query->setMaxResults($limit);
302
-		}
303
-		if ($offset > 0) {
304
-			$query->setFirstResult($offset);
305
-		}
306
-
307
-		$resultStatement = $query->execute();
308
-		while ($data = $resultStatement->fetch()) {
309
-			$comment = new Comment($this->normalizeDatabaseData($data));
310
-			$this->cache($comment);
311
-			$tree['replies'][] = [
312
-				'comment' => $comment,
313
-				'replies' => []
314
-			];
315
-		}
316
-		$resultStatement->closeCursor();
317
-
318
-		return $tree;
319
-	}
320
-
321
-	/**
322
-	 * returns comments for a specific object (e.g. a file).
323
-	 *
324
-	 * The sort order is always newest to oldest.
325
-	 *
326
-	 * @param string $objectType the object type, e.g. 'files'
327
-	 * @param string $objectId the id of the object
328
-	 * @param int $limit optional, number of maximum comments to be returned. if
329
-	 * not specified, all comments are returned.
330
-	 * @param int $offset optional, starting point
331
-	 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
332
-	 * that may be returned
333
-	 * @return IComment[]
334
-	 * @since 9.0.0
335
-	 */
336
-	public function getForObject(
337
-		$objectType,
338
-		$objectId,
339
-		$limit = 0,
340
-		$offset = 0,
341
-		\DateTime $notOlderThan = null
342
-	) {
343
-		$comments = [];
344
-
345
-		$qb = $this->dbConn->getQueryBuilder();
346
-		$query = $qb->select('*')
347
-			->from('comments')
348
-			->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
349
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
350
-			->orderBy('creation_timestamp', 'DESC')
351
-			->setParameter('type', $objectType)
352
-			->setParameter('id', $objectId);
353
-
354
-		if ($limit > 0) {
355
-			$query->setMaxResults($limit);
356
-		}
357
-		if ($offset > 0) {
358
-			$query->setFirstResult($offset);
359
-		}
360
-		if (!is_null($notOlderThan)) {
361
-			$query
362
-				->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
363
-				->setParameter('notOlderThan', $notOlderThan, 'datetime');
364
-		}
365
-
366
-		$resultStatement = $query->execute();
367
-		while ($data = $resultStatement->fetch()) {
368
-			$comment = new Comment($this->normalizeDatabaseData($data));
369
-			$this->cache($comment);
370
-			$comments[] = $comment;
371
-		}
372
-		$resultStatement->closeCursor();
373
-
374
-		return $comments;
375
-	}
376
-
377
-	/**
378
-	 * @param $objectType string the object type, e.g. 'files'
379
-	 * @param $objectId string the id of the object
380
-	 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
381
-	 * that may be returned
382
-	 * @return Int
383
-	 * @since 9.0.0
384
-	 */
385
-	public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {
386
-		$qb = $this->dbConn->getQueryBuilder();
387
-		$query = $qb->select($qb->createFunction('COUNT(`id`)'))
388
-			->from('comments')
389
-			->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
390
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
391
-			->setParameter('type', $objectType)
392
-			->setParameter('id', $objectId);
393
-
394
-		if (!is_null($notOlderThan)) {
395
-			$query
396
-				->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
397
-				->setParameter('notOlderThan', $notOlderThan, 'datetime');
398
-		}
399
-
400
-		$resultStatement = $query->execute();
401
-		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
402
-		$resultStatement->closeCursor();
403
-		return intval($data[0]);
404
-	}
405
-
406
-	/**
407
-	 * Get the number of unread comments for all files in a folder
408
-	 *
409
-	 * @param int $folderId
410
-	 * @param IUser $user
411
-	 * @return array [$fileId => $unreadCount]
412
-	 */
413
-	public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
414
-		$qb = $this->dbConn->getQueryBuilder();
415
-		$query = $qb->select('fileid', $qb->createFunction(
416
-			'COUNT(' . $qb->getColumnName('c.id') . ')')
417
-		)->from('comments', 'c')
418
-			->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
419
-				$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
420
-				$qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT))
421
-			))
422
-			->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
423
-				$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
424
-				$qb->expr()->eq('m.object_id', 'c.object_id'),
425
-				$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID()))
426
-			))
427
-			->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)))
428
-			->andWhere($qb->expr()->orX(
429
-				$qb->expr()->gt('c.creation_timestamp', 'marker_datetime'),
430
-				$qb->expr()->isNull('marker_datetime')
431
-			))
432
-			->groupBy('f.fileid');
433
-
434
-		$resultStatement = $query->execute();
435
-		return array_map(function ($count) {
436
-			return (int)$count;
437
-		}, $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR));
438
-	}
439
-
440
-	/**
441
-	 * creates a new comment and returns it. At this point of time, it is not
442
-	 * saved in the used data storage. Use save() after setting other fields
443
-	 * of the comment (e.g. message or verb).
444
-	 *
445
-	 * @param string $actorType the actor type (e.g. 'users')
446
-	 * @param string $actorId a user id
447
-	 * @param string $objectType the object type the comment is attached to
448
-	 * @param string $objectId the object id the comment is attached to
449
-	 * @return IComment
450
-	 * @since 9.0.0
451
-	 */
452
-	public function create($actorType, $actorId, $objectType, $objectId) {
453
-		$comment = new Comment();
454
-		$comment
455
-			->setActor($actorType, $actorId)
456
-			->setObject($objectType, $objectId);
457
-		return $comment;
458
-	}
459
-
460
-	/**
461
-	 * permanently deletes the comment specified by the ID
462
-	 *
463
-	 * When the comment has child comments, their parent ID will be changed to
464
-	 * the parent ID of the item that is to be deleted.
465
-	 *
466
-	 * @param string $id
467
-	 * @return bool
468
-	 * @throws \InvalidArgumentException
469
-	 * @since 9.0.0
470
-	 */
471
-	public function delete($id) {
472
-		if (!is_string($id)) {
473
-			throw new \InvalidArgumentException('Parameter must be string');
474
-		}
475
-
476
-		try {
477
-			$comment = $this->get($id);
478
-		} catch (\Exception $e) {
479
-			// Ignore exceptions, we just don't fire a hook then
480
-			$comment = null;
481
-		}
482
-
483
-		$qb = $this->dbConn->getQueryBuilder();
484
-		$query = $qb->delete('comments')
485
-			->where($qb->expr()->eq('id', $qb->createParameter('id')))
486
-			->setParameter('id', $id);
487
-
488
-		try {
489
-			$affectedRows = $query->execute();
490
-			$this->uncache($id);
491
-		} catch (DriverException $e) {
492
-			$this->logger->logException($e, ['app' => 'core_comments']);
493
-			return false;
494
-		}
495
-
496
-		if ($affectedRows > 0 && $comment instanceof IComment) {
497
-			$this->sendEvent(CommentsEvent::EVENT_DELETE, $comment);
498
-		}
499
-
500
-		return ($affectedRows > 0);
501
-	}
502
-
503
-	/**
504
-	 * saves the comment permanently
505
-	 *
506
-	 * if the supplied comment has an empty ID, a new entry comment will be
507
-	 * saved and the instance updated with the new ID.
508
-	 *
509
-	 * Otherwise, an existing comment will be updated.
510
-	 *
511
-	 * Throws NotFoundException when a comment that is to be updated does not
512
-	 * exist anymore at this point of time.
513
-	 *
514
-	 * @param IComment $comment
515
-	 * @return bool
516
-	 * @throws NotFoundException
517
-	 * @since 9.0.0
518
-	 */
519
-	public function save(IComment $comment) {
520
-		if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') {
521
-			$result = $this->insert($comment);
522
-		} else {
523
-			$result = $this->update($comment);
524
-		}
525
-
526
-		if ($result && !!$comment->getParentId()) {
527
-			$this->updateChildrenInformation(
528
-				$comment->getParentId(),
529
-				$comment->getCreationDateTime()
530
-			);
531
-			$this->cache($comment);
532
-		}
533
-
534
-		return $result;
535
-	}
536
-
537
-	/**
538
-	 * inserts the provided comment in the database
539
-	 *
540
-	 * @param IComment $comment
541
-	 * @return bool
542
-	 */
543
-	protected function insert(IComment &$comment) {
544
-		$qb = $this->dbConn->getQueryBuilder();
545
-		$affectedRows = $qb
546
-			->insert('comments')
547
-			->values([
548
-				'parent_id' => $qb->createNamedParameter($comment->getParentId()),
549
-				'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
550
-				'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
551
-				'actor_type' => $qb->createNamedParameter($comment->getActorType()),
552
-				'actor_id' => $qb->createNamedParameter($comment->getActorId()),
553
-				'message' => $qb->createNamedParameter($comment->getMessage()),
554
-				'verb' => $qb->createNamedParameter($comment->getVerb()),
555
-				'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
556
-				'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
557
-				'object_type' => $qb->createNamedParameter($comment->getObjectType()),
558
-				'object_id' => $qb->createNamedParameter($comment->getObjectId()),
559
-			])
560
-			->execute();
561
-
562
-		if ($affectedRows > 0) {
563
-			$comment->setId(strval($qb->getLastInsertId()));
564
-			$this->sendEvent(CommentsEvent::EVENT_ADD, $comment);
565
-		}
566
-
567
-		return $affectedRows > 0;
568
-	}
569
-
570
-	/**
571
-	 * updates a Comment data row
572
-	 *
573
-	 * @param IComment $comment
574
-	 * @return bool
575
-	 * @throws NotFoundException
576
-	 */
577
-	protected function update(IComment $comment) {
578
-		// for properly working preUpdate Events we need the old comments as is
579
-		// in the DB and overcome caching. Also avoid that outdated information stays.
580
-		$this->uncache($comment->getId());
581
-		$this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId()));
582
-		$this->uncache($comment->getId());
583
-
584
-		$qb = $this->dbConn->getQueryBuilder();
585
-		$affectedRows = $qb
586
-			->update('comments')
587
-			->set('parent_id', $qb->createNamedParameter($comment->getParentId()))
588
-			->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId()))
589
-			->set('children_count', $qb->createNamedParameter($comment->getChildrenCount()))
590
-			->set('actor_type', $qb->createNamedParameter($comment->getActorType()))
591
-			->set('actor_id', $qb->createNamedParameter($comment->getActorId()))
592
-			->set('message', $qb->createNamedParameter($comment->getMessage()))
593
-			->set('verb', $qb->createNamedParameter($comment->getVerb()))
594
-			->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'))
595
-			->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
596
-			->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
597
-			->set('object_id', $qb->createNamedParameter($comment->getObjectId()))
598
-			->where($qb->expr()->eq('id', $qb->createParameter('id')))
599
-			->setParameter('id', $comment->getId())
600
-			->execute();
601
-
602
-		if ($affectedRows === 0) {
603
-			throw new NotFoundException('Comment to update does ceased to exist');
604
-		}
605
-
606
-		$this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment);
607
-
608
-		return $affectedRows > 0;
609
-	}
610
-
611
-	/**
612
-	 * removes references to specific actor (e.g. on user delete) of a comment.
613
-	 * The comment itself must not get lost/deleted.
614
-	 *
615
-	 * @param string $actorType the actor type (e.g. 'users')
616
-	 * @param string $actorId a user id
617
-	 * @return boolean
618
-	 * @since 9.0.0
619
-	 */
620
-	public function deleteReferencesOfActor($actorType, $actorId) {
621
-		$this->checkRoleParameters('Actor', $actorType, $actorId);
622
-
623
-		$qb = $this->dbConn->getQueryBuilder();
624
-		$affectedRows = $qb
625
-			->update('comments')
626
-			->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
627
-			->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
628
-			->where($qb->expr()->eq('actor_type', $qb->createParameter('type')))
629
-			->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id')))
630
-			->setParameter('type', $actorType)
631
-			->setParameter('id', $actorId)
632
-			->execute();
633
-
634
-		$this->commentsCache = [];
635
-
636
-		return is_int($affectedRows);
637
-	}
638
-
639
-	/**
640
-	 * deletes all comments made of a specific object (e.g. on file delete)
641
-	 *
642
-	 * @param string $objectType the object type (e.g. 'files')
643
-	 * @param string $objectId e.g. the file id
644
-	 * @return boolean
645
-	 * @since 9.0.0
646
-	 */
647
-	public function deleteCommentsAtObject($objectType, $objectId) {
648
-		$this->checkRoleParameters('Object', $objectType, $objectId);
649
-
650
-		$qb = $this->dbConn->getQueryBuilder();
651
-		$affectedRows = $qb
652
-			->delete('comments')
653
-			->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
654
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
655
-			->setParameter('type', $objectType)
656
-			->setParameter('id', $objectId)
657
-			->execute();
658
-
659
-		$this->commentsCache = [];
660
-
661
-		return is_int($affectedRows);
662
-	}
663
-
664
-	/**
665
-	 * deletes the read markers for the specified user
666
-	 *
667
-	 * @param \OCP\IUser $user
668
-	 * @return bool
669
-	 * @since 9.0.0
670
-	 */
671
-	public function deleteReadMarksFromUser(IUser $user) {
672
-		$qb = $this->dbConn->getQueryBuilder();
673
-		$query = $qb->delete('comments_read_markers')
674
-			->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
675
-			->setParameter('user_id', $user->getUID());
676
-
677
-		try {
678
-			$affectedRows = $query->execute();
679
-		} catch (DriverException $e) {
680
-			$this->logger->logException($e, ['app' => 'core_comments']);
681
-			return false;
682
-		}
683
-		return ($affectedRows > 0);
684
-	}
685
-
686
-	/**
687
-	 * sets the read marker for a given file to the specified date for the
688
-	 * provided user
689
-	 *
690
-	 * @param string $objectType
691
-	 * @param string $objectId
692
-	 * @param \DateTime $dateTime
693
-	 * @param IUser $user
694
-	 * @since 9.0.0
695
-	 */
696
-	public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) {
697
-		$this->checkRoleParameters('Object', $objectType, $objectId);
698
-
699
-		$qb = $this->dbConn->getQueryBuilder();
700
-		$values = [
701
-			'user_id' => $qb->createNamedParameter($user->getUID()),
702
-			'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'),
703
-			'object_type' => $qb->createNamedParameter($objectType),
704
-			'object_id' => $qb->createNamedParameter($objectId),
705
-		];
706
-
707
-		// Strategy: try to update, if this does not return affected rows, do an insert.
708
-		$affectedRows = $qb
709
-			->update('comments_read_markers')
710
-			->set('user_id', $values['user_id'])
711
-			->set('marker_datetime', $values['marker_datetime'])
712
-			->set('object_type', $values['object_type'])
713
-			->set('object_id', $values['object_id'])
714
-			->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
715
-			->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
716
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
717
-			->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
718
-			->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
719
-			->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
720
-			->execute();
721
-
722
-		if ($affectedRows > 0) {
723
-			return;
724
-		}
725
-
726
-		$qb->insert('comments_read_markers')
727
-			->values($values)
728
-			->execute();
729
-	}
730
-
731
-	/**
732
-	 * returns the read marker for a given file to the specified date for the
733
-	 * provided user. It returns null, when the marker is not present, i.e.
734
-	 * no comments were marked as read.
735
-	 *
736
-	 * @param string $objectType
737
-	 * @param string $objectId
738
-	 * @param IUser $user
739
-	 * @return \DateTime|null
740
-	 * @since 9.0.0
741
-	 */
742
-	public function getReadMark($objectType, $objectId, IUser $user) {
743
-		$qb = $this->dbConn->getQueryBuilder();
744
-		$resultStatement = $qb->select('marker_datetime')
745
-			->from('comments_read_markers')
746
-			->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
747
-			->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
748
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
749
-			->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
750
-			->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
751
-			->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
752
-			->execute();
753
-
754
-		$data = $resultStatement->fetch();
755
-		$resultStatement->closeCursor();
756
-		if (!$data || is_null($data['marker_datetime'])) {
757
-			return null;
758
-		}
759
-
760
-		return new \DateTime($data['marker_datetime']);
761
-	}
762
-
763
-	/**
764
-	 * deletes the read markers on the specified object
765
-	 *
766
-	 * @param string $objectType
767
-	 * @param string $objectId
768
-	 * @return bool
769
-	 * @since 9.0.0
770
-	 */
771
-	public function deleteReadMarksOnObject($objectType, $objectId) {
772
-		$this->checkRoleParameters('Object', $objectType, $objectId);
773
-
774
-		$qb = $this->dbConn->getQueryBuilder();
775
-		$query = $qb->delete('comments_read_markers')
776
-			->where($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
777
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
778
-			->setParameter('object_type', $objectType)
779
-			->setParameter('object_id', $objectId);
780
-
781
-		try {
782
-			$affectedRows = $query->execute();
783
-		} catch (DriverException $e) {
784
-			$this->logger->logException($e, ['app' => 'core_comments']);
785
-			return false;
786
-		}
787
-		return ($affectedRows > 0);
788
-	}
789
-
790
-	/**
791
-	 * registers an Entity to the manager, so event notifications can be send
792
-	 * to consumers of the comments infrastructure
793
-	 *
794
-	 * @param \Closure $closure
795
-	 */
796
-	public function registerEventHandler(\Closure $closure) {
797
-		$this->eventHandlerClosures[] = $closure;
798
-		$this->eventHandlers = [];
799
-	}
800
-
801
-	/**
802
-	 * registers a method that resolves an ID to a display name for a given type
803
-	 *
804
-	 * @param string $type
805
-	 * @param \Closure $closure
806
-	 * @throws \OutOfBoundsException
807
-	 * @since 11.0.0
808
-	 *
809
-	 * Only one resolver shall be registered per type. Otherwise a
810
-	 * \OutOfBoundsException has to thrown.
811
-	 */
812
-	public function registerDisplayNameResolver($type, \Closure $closure) {
813
-		if (!is_string($type)) {
814
-			throw new \InvalidArgumentException('String expected.');
815
-		}
816
-		if (isset($this->displayNameResolvers[$type])) {
817
-			throw new \OutOfBoundsException('Displayname resolver for this type already registered');
818
-		}
819
-		$this->displayNameResolvers[$type] = $closure;
820
-	}
821
-
822
-	/**
823
-	 * resolves a given ID of a given Type to a display name.
824
-	 *
825
-	 * @param string $type
826
-	 * @param string $id
827
-	 * @return string
828
-	 * @throws \OutOfBoundsException
829
-	 * @since 11.0.0
830
-	 *
831
-	 * If a provided type was not registered, an \OutOfBoundsException shall
832
-	 * be thrown. It is upon the resolver discretion what to return of the
833
-	 * provided ID is unknown. It must be ensured that a string is returned.
834
-	 */
835
-	public function resolveDisplayName($type, $id) {
836
-		if (!is_string($type)) {
837
-			throw new \InvalidArgumentException('String expected.');
838
-		}
839
-		if (!isset($this->displayNameResolvers[$type])) {
840
-			throw new \OutOfBoundsException('No Displayname resolver for this type registered');
841
-		}
842
-		return (string)$this->displayNameResolvers[$type]($id);
843
-	}
844
-
845
-	/**
846
-	 * returns valid, registered entities
847
-	 *
848
-	 * @return \OCP\Comments\ICommentsEventHandler[]
849
-	 */
850
-	private function getEventHandlers() {
851
-		if (!empty($this->eventHandlers)) {
852
-			return $this->eventHandlers;
853
-		}
854
-
855
-		$this->eventHandlers = [];
856
-		foreach ($this->eventHandlerClosures as $name => $closure) {
857
-			$entity = $closure();
858
-			if (!($entity instanceof ICommentsEventHandler)) {
859
-				throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface');
860
-			}
861
-			$this->eventHandlers[$name] = $entity;
862
-		}
863
-
864
-		return $this->eventHandlers;
865
-	}
866
-
867
-	/**
868
-	 * sends notifications to the registered entities
869
-	 *
870
-	 * @param $eventType
871
-	 * @param IComment $comment
872
-	 */
873
-	private function sendEvent($eventType, IComment $comment) {
874
-		$entities = $this->getEventHandlers();
875
-		$event = new CommentsEvent($eventType, $comment);
876
-		foreach ($entities as $entity) {
877
-			$entity->handle($event);
878
-		}
879
-	}
42
+    /** @var  IDBConnection */
43
+    protected $dbConn;
44
+
45
+    /** @var  ILogger */
46
+    protected $logger;
47
+
48
+    /** @var IConfig */
49
+    protected $config;
50
+
51
+    /** @var IComment[] */
52
+    protected $commentsCache = [];
53
+
54
+    /** @var  \Closure[] */
55
+    protected $eventHandlerClosures = [];
56
+
57
+    /** @var  ICommentsEventHandler[] */
58
+    protected $eventHandlers = [];
59
+
60
+    /** @var \Closure[] */
61
+    protected $displayNameResolvers = [];
62
+
63
+    /**
64
+     * Manager constructor.
65
+     *
66
+     * @param IDBConnection $dbConn
67
+     * @param ILogger $logger
68
+     * @param IConfig $config
69
+     */
70
+    public function __construct(
71
+        IDBConnection $dbConn,
72
+        ILogger $logger,
73
+        IConfig $config
74
+    ) {
75
+        $this->dbConn = $dbConn;
76
+        $this->logger = $logger;
77
+        $this->config = $config;
78
+    }
79
+
80
+    /**
81
+     * converts data base data into PHP native, proper types as defined by
82
+     * IComment interface.
83
+     *
84
+     * @param array $data
85
+     * @return array
86
+     */
87
+    protected function normalizeDatabaseData(array $data) {
88
+        $data['id'] = strval($data['id']);
89
+        $data['parent_id'] = strval($data['parent_id']);
90
+        $data['topmost_parent_id'] = strval($data['topmost_parent_id']);
91
+        $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']);
92
+        if (!is_null($data['latest_child_timestamp'])) {
93
+            $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']);
94
+        }
95
+        $data['children_count'] = intval($data['children_count']);
96
+        return $data;
97
+    }
98
+
99
+    /**
100
+     * prepares a comment for an insert or update operation after making sure
101
+     * all necessary fields have a value assigned.
102
+     *
103
+     * @param IComment $comment
104
+     * @return IComment returns the same updated IComment instance as provided
105
+     *                  by parameter for convenience
106
+     * @throws \UnexpectedValueException
107
+     */
108
+    protected function prepareCommentForDatabaseWrite(IComment $comment) {
109
+        if (!$comment->getActorType()
110
+            || !$comment->getActorId()
111
+            || !$comment->getObjectType()
112
+            || !$comment->getObjectId()
113
+            || !$comment->getVerb()
114
+        ) {
115
+            throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
116
+        }
117
+
118
+        if ($comment->getId() === '') {
119
+            $comment->setChildrenCount(0);
120
+            $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC')));
121
+            $comment->setLatestChildDateTime(null);
122
+        }
123
+
124
+        if (is_null($comment->getCreationDateTime())) {
125
+            $comment->setCreationDateTime(new \DateTime());
126
+        }
127
+
128
+        if ($comment->getParentId() !== '0') {
129
+            $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId()));
130
+        } else {
131
+            $comment->setTopmostParentId('0');
132
+        }
133
+
134
+        $this->cache($comment);
135
+
136
+        return $comment;
137
+    }
138
+
139
+    /**
140
+     * returns the topmost parent id of a given comment identified by ID
141
+     *
142
+     * @param string $id
143
+     * @return string
144
+     * @throws NotFoundException
145
+     */
146
+    protected function determineTopmostParentId($id) {
147
+        $comment = $this->get($id);
148
+        if ($comment->getParentId() === '0') {
149
+            return $comment->getId();
150
+        } else {
151
+            return $this->determineTopmostParentId($comment->getId());
152
+        }
153
+    }
154
+
155
+    /**
156
+     * updates child information of a comment
157
+     *
158
+     * @param string $id
159
+     * @param \DateTime $cDateTime the date time of the most recent child
160
+     * @throws NotFoundException
161
+     */
162
+    protected function updateChildrenInformation($id, \DateTime $cDateTime) {
163
+        $qb = $this->dbConn->getQueryBuilder();
164
+        $query = $qb->select($qb->createFunction('COUNT(`id`)'))
165
+            ->from('comments')
166
+            ->where($qb->expr()->eq('parent_id', $qb->createParameter('id')))
167
+            ->setParameter('id', $id);
168
+
169
+        $resultStatement = $query->execute();
170
+        $data = $resultStatement->fetch(\PDO::FETCH_NUM);
171
+        $resultStatement->closeCursor();
172
+        $children = intval($data[0]);
173
+
174
+        $comment = $this->get($id);
175
+        $comment->setChildrenCount($children);
176
+        $comment->setLatestChildDateTime($cDateTime);
177
+        $this->save($comment);
178
+    }
179
+
180
+    /**
181
+     * Tests whether actor or object type and id parameters are acceptable.
182
+     * Throws exception if not.
183
+     *
184
+     * @param string $role
185
+     * @param string $type
186
+     * @param string $id
187
+     * @throws \InvalidArgumentException
188
+     */
189
+    protected function checkRoleParameters($role, $type, $id) {
190
+        if (
191
+            !is_string($type) || empty($type)
192
+            || !is_string($id) || empty($id)
193
+        ) {
194
+            throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
195
+        }
196
+    }
197
+
198
+    /**
199
+     * run-time caches a comment
200
+     *
201
+     * @param IComment $comment
202
+     */
203
+    protected function cache(IComment $comment) {
204
+        $id = $comment->getId();
205
+        if (empty($id)) {
206
+            return;
207
+        }
208
+        $this->commentsCache[strval($id)] = $comment;
209
+    }
210
+
211
+    /**
212
+     * removes an entry from the comments run time cache
213
+     *
214
+     * @param mixed $id the comment's id
215
+     */
216
+    protected function uncache($id) {
217
+        $id = strval($id);
218
+        if (isset($this->commentsCache[$id])) {
219
+            unset($this->commentsCache[$id]);
220
+        }
221
+    }
222
+
223
+    /**
224
+     * returns a comment instance
225
+     *
226
+     * @param string $id the ID of the comment
227
+     * @return IComment
228
+     * @throws NotFoundException
229
+     * @throws \InvalidArgumentException
230
+     * @since 9.0.0
231
+     */
232
+    public function get($id) {
233
+        if (intval($id) === 0) {
234
+            throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.');
235
+        }
236
+
237
+        if (isset($this->commentsCache[$id])) {
238
+            return $this->commentsCache[$id];
239
+        }
240
+
241
+        $qb = $this->dbConn->getQueryBuilder();
242
+        $resultStatement = $qb->select('*')
243
+            ->from('comments')
244
+            ->where($qb->expr()->eq('id', $qb->createParameter('id')))
245
+            ->setParameter('id', $id, IQueryBuilder::PARAM_INT)
246
+            ->execute();
247
+
248
+        $data = $resultStatement->fetch();
249
+        $resultStatement->closeCursor();
250
+        if (!$data) {
251
+            throw new NotFoundException();
252
+        }
253
+
254
+        $comment = new Comment($this->normalizeDatabaseData($data));
255
+        $this->cache($comment);
256
+        return $comment;
257
+    }
258
+
259
+    /**
260
+     * returns the comment specified by the id and all it's child comments.
261
+     * At this point of time, we do only support one level depth.
262
+     *
263
+     * @param string $id
264
+     * @param int $limit max number of entries to return, 0 returns all
265
+     * @param int $offset the start entry
266
+     * @return array
267
+     * @since 9.0.0
268
+     *
269
+     * The return array looks like this
270
+     * [
271
+     *   'comment' => IComment, // root comment
272
+     *   'replies' =>
273
+     *   [
274
+     *     0 =>
275
+     *     [
276
+     *       'comment' => IComment,
277
+     *       'replies' => []
278
+     *     ]
279
+     *     1 =>
280
+     *     [
281
+     *       'comment' => IComment,
282
+     *       'replies'=> []
283
+     *     ],
284
+     *     …
285
+     *   ]
286
+     * ]
287
+     */
288
+    public function getTree($id, $limit = 0, $offset = 0) {
289
+        $tree = [];
290
+        $tree['comment'] = $this->get($id);
291
+        $tree['replies'] = [];
292
+
293
+        $qb = $this->dbConn->getQueryBuilder();
294
+        $query = $qb->select('*')
295
+            ->from('comments')
296
+            ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id')))
297
+            ->orderBy('creation_timestamp', 'DESC')
298
+            ->setParameter('id', $id);
299
+
300
+        if ($limit > 0) {
301
+            $query->setMaxResults($limit);
302
+        }
303
+        if ($offset > 0) {
304
+            $query->setFirstResult($offset);
305
+        }
306
+
307
+        $resultStatement = $query->execute();
308
+        while ($data = $resultStatement->fetch()) {
309
+            $comment = new Comment($this->normalizeDatabaseData($data));
310
+            $this->cache($comment);
311
+            $tree['replies'][] = [
312
+                'comment' => $comment,
313
+                'replies' => []
314
+            ];
315
+        }
316
+        $resultStatement->closeCursor();
317
+
318
+        return $tree;
319
+    }
320
+
321
+    /**
322
+     * returns comments for a specific object (e.g. a file).
323
+     *
324
+     * The sort order is always newest to oldest.
325
+     *
326
+     * @param string $objectType the object type, e.g. 'files'
327
+     * @param string $objectId the id of the object
328
+     * @param int $limit optional, number of maximum comments to be returned. if
329
+     * not specified, all comments are returned.
330
+     * @param int $offset optional, starting point
331
+     * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
332
+     * that may be returned
333
+     * @return IComment[]
334
+     * @since 9.0.0
335
+     */
336
+    public function getForObject(
337
+        $objectType,
338
+        $objectId,
339
+        $limit = 0,
340
+        $offset = 0,
341
+        \DateTime $notOlderThan = null
342
+    ) {
343
+        $comments = [];
344
+
345
+        $qb = $this->dbConn->getQueryBuilder();
346
+        $query = $qb->select('*')
347
+            ->from('comments')
348
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
349
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
350
+            ->orderBy('creation_timestamp', 'DESC')
351
+            ->setParameter('type', $objectType)
352
+            ->setParameter('id', $objectId);
353
+
354
+        if ($limit > 0) {
355
+            $query->setMaxResults($limit);
356
+        }
357
+        if ($offset > 0) {
358
+            $query->setFirstResult($offset);
359
+        }
360
+        if (!is_null($notOlderThan)) {
361
+            $query
362
+                ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
363
+                ->setParameter('notOlderThan', $notOlderThan, 'datetime');
364
+        }
365
+
366
+        $resultStatement = $query->execute();
367
+        while ($data = $resultStatement->fetch()) {
368
+            $comment = new Comment($this->normalizeDatabaseData($data));
369
+            $this->cache($comment);
370
+            $comments[] = $comment;
371
+        }
372
+        $resultStatement->closeCursor();
373
+
374
+        return $comments;
375
+    }
376
+
377
+    /**
378
+     * @param $objectType string the object type, e.g. 'files'
379
+     * @param $objectId string the id of the object
380
+     * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
381
+     * that may be returned
382
+     * @return Int
383
+     * @since 9.0.0
384
+     */
385
+    public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {
386
+        $qb = $this->dbConn->getQueryBuilder();
387
+        $query = $qb->select($qb->createFunction('COUNT(`id`)'))
388
+            ->from('comments')
389
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
390
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
391
+            ->setParameter('type', $objectType)
392
+            ->setParameter('id', $objectId);
393
+
394
+        if (!is_null($notOlderThan)) {
395
+            $query
396
+                ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
397
+                ->setParameter('notOlderThan', $notOlderThan, 'datetime');
398
+        }
399
+
400
+        $resultStatement = $query->execute();
401
+        $data = $resultStatement->fetch(\PDO::FETCH_NUM);
402
+        $resultStatement->closeCursor();
403
+        return intval($data[0]);
404
+    }
405
+
406
+    /**
407
+     * Get the number of unread comments for all files in a folder
408
+     *
409
+     * @param int $folderId
410
+     * @param IUser $user
411
+     * @return array [$fileId => $unreadCount]
412
+     */
413
+    public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
414
+        $qb = $this->dbConn->getQueryBuilder();
415
+        $query = $qb->select('fileid', $qb->createFunction(
416
+            'COUNT(' . $qb->getColumnName('c.id') . ')')
417
+        )->from('comments', 'c')
418
+            ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
419
+                $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
420
+                $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT))
421
+            ))
422
+            ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
423
+                $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
424
+                $qb->expr()->eq('m.object_id', 'c.object_id'),
425
+                $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID()))
426
+            ))
427
+            ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)))
428
+            ->andWhere($qb->expr()->orX(
429
+                $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'),
430
+                $qb->expr()->isNull('marker_datetime')
431
+            ))
432
+            ->groupBy('f.fileid');
433
+
434
+        $resultStatement = $query->execute();
435
+        return array_map(function ($count) {
436
+            return (int)$count;
437
+        }, $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR));
438
+    }
439
+
440
+    /**
441
+     * creates a new comment and returns it. At this point of time, it is not
442
+     * saved in the used data storage. Use save() after setting other fields
443
+     * of the comment (e.g. message or verb).
444
+     *
445
+     * @param string $actorType the actor type (e.g. 'users')
446
+     * @param string $actorId a user id
447
+     * @param string $objectType the object type the comment is attached to
448
+     * @param string $objectId the object id the comment is attached to
449
+     * @return IComment
450
+     * @since 9.0.0
451
+     */
452
+    public function create($actorType, $actorId, $objectType, $objectId) {
453
+        $comment = new Comment();
454
+        $comment
455
+            ->setActor($actorType, $actorId)
456
+            ->setObject($objectType, $objectId);
457
+        return $comment;
458
+    }
459
+
460
+    /**
461
+     * permanently deletes the comment specified by the ID
462
+     *
463
+     * When the comment has child comments, their parent ID will be changed to
464
+     * the parent ID of the item that is to be deleted.
465
+     *
466
+     * @param string $id
467
+     * @return bool
468
+     * @throws \InvalidArgumentException
469
+     * @since 9.0.0
470
+     */
471
+    public function delete($id) {
472
+        if (!is_string($id)) {
473
+            throw new \InvalidArgumentException('Parameter must be string');
474
+        }
475
+
476
+        try {
477
+            $comment = $this->get($id);
478
+        } catch (\Exception $e) {
479
+            // Ignore exceptions, we just don't fire a hook then
480
+            $comment = null;
481
+        }
482
+
483
+        $qb = $this->dbConn->getQueryBuilder();
484
+        $query = $qb->delete('comments')
485
+            ->where($qb->expr()->eq('id', $qb->createParameter('id')))
486
+            ->setParameter('id', $id);
487
+
488
+        try {
489
+            $affectedRows = $query->execute();
490
+            $this->uncache($id);
491
+        } catch (DriverException $e) {
492
+            $this->logger->logException($e, ['app' => 'core_comments']);
493
+            return false;
494
+        }
495
+
496
+        if ($affectedRows > 0 && $comment instanceof IComment) {
497
+            $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment);
498
+        }
499
+
500
+        return ($affectedRows > 0);
501
+    }
502
+
503
+    /**
504
+     * saves the comment permanently
505
+     *
506
+     * if the supplied comment has an empty ID, a new entry comment will be
507
+     * saved and the instance updated with the new ID.
508
+     *
509
+     * Otherwise, an existing comment will be updated.
510
+     *
511
+     * Throws NotFoundException when a comment that is to be updated does not
512
+     * exist anymore at this point of time.
513
+     *
514
+     * @param IComment $comment
515
+     * @return bool
516
+     * @throws NotFoundException
517
+     * @since 9.0.0
518
+     */
519
+    public function save(IComment $comment) {
520
+        if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') {
521
+            $result = $this->insert($comment);
522
+        } else {
523
+            $result = $this->update($comment);
524
+        }
525
+
526
+        if ($result && !!$comment->getParentId()) {
527
+            $this->updateChildrenInformation(
528
+                $comment->getParentId(),
529
+                $comment->getCreationDateTime()
530
+            );
531
+            $this->cache($comment);
532
+        }
533
+
534
+        return $result;
535
+    }
536
+
537
+    /**
538
+     * inserts the provided comment in the database
539
+     *
540
+     * @param IComment $comment
541
+     * @return bool
542
+     */
543
+    protected function insert(IComment &$comment) {
544
+        $qb = $this->dbConn->getQueryBuilder();
545
+        $affectedRows = $qb
546
+            ->insert('comments')
547
+            ->values([
548
+                'parent_id' => $qb->createNamedParameter($comment->getParentId()),
549
+                'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
550
+                'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
551
+                'actor_type' => $qb->createNamedParameter($comment->getActorType()),
552
+                'actor_id' => $qb->createNamedParameter($comment->getActorId()),
553
+                'message' => $qb->createNamedParameter($comment->getMessage()),
554
+                'verb' => $qb->createNamedParameter($comment->getVerb()),
555
+                'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
556
+                'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
557
+                'object_type' => $qb->createNamedParameter($comment->getObjectType()),
558
+                'object_id' => $qb->createNamedParameter($comment->getObjectId()),
559
+            ])
560
+            ->execute();
561
+
562
+        if ($affectedRows > 0) {
563
+            $comment->setId(strval($qb->getLastInsertId()));
564
+            $this->sendEvent(CommentsEvent::EVENT_ADD, $comment);
565
+        }
566
+
567
+        return $affectedRows > 0;
568
+    }
569
+
570
+    /**
571
+     * updates a Comment data row
572
+     *
573
+     * @param IComment $comment
574
+     * @return bool
575
+     * @throws NotFoundException
576
+     */
577
+    protected function update(IComment $comment) {
578
+        // for properly working preUpdate Events we need the old comments as is
579
+        // in the DB and overcome caching. Also avoid that outdated information stays.
580
+        $this->uncache($comment->getId());
581
+        $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId()));
582
+        $this->uncache($comment->getId());
583
+
584
+        $qb = $this->dbConn->getQueryBuilder();
585
+        $affectedRows = $qb
586
+            ->update('comments')
587
+            ->set('parent_id', $qb->createNamedParameter($comment->getParentId()))
588
+            ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId()))
589
+            ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount()))
590
+            ->set('actor_type', $qb->createNamedParameter($comment->getActorType()))
591
+            ->set('actor_id', $qb->createNamedParameter($comment->getActorId()))
592
+            ->set('message', $qb->createNamedParameter($comment->getMessage()))
593
+            ->set('verb', $qb->createNamedParameter($comment->getVerb()))
594
+            ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'))
595
+            ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
596
+            ->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
597
+            ->set('object_id', $qb->createNamedParameter($comment->getObjectId()))
598
+            ->where($qb->expr()->eq('id', $qb->createParameter('id')))
599
+            ->setParameter('id', $comment->getId())
600
+            ->execute();
601
+
602
+        if ($affectedRows === 0) {
603
+            throw new NotFoundException('Comment to update does ceased to exist');
604
+        }
605
+
606
+        $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment);
607
+
608
+        return $affectedRows > 0;
609
+    }
610
+
611
+    /**
612
+     * removes references to specific actor (e.g. on user delete) of a comment.
613
+     * The comment itself must not get lost/deleted.
614
+     *
615
+     * @param string $actorType the actor type (e.g. 'users')
616
+     * @param string $actorId a user id
617
+     * @return boolean
618
+     * @since 9.0.0
619
+     */
620
+    public function deleteReferencesOfActor($actorType, $actorId) {
621
+        $this->checkRoleParameters('Actor', $actorType, $actorId);
622
+
623
+        $qb = $this->dbConn->getQueryBuilder();
624
+        $affectedRows = $qb
625
+            ->update('comments')
626
+            ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
627
+            ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
628
+            ->where($qb->expr()->eq('actor_type', $qb->createParameter('type')))
629
+            ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id')))
630
+            ->setParameter('type', $actorType)
631
+            ->setParameter('id', $actorId)
632
+            ->execute();
633
+
634
+        $this->commentsCache = [];
635
+
636
+        return is_int($affectedRows);
637
+    }
638
+
639
+    /**
640
+     * deletes all comments made of a specific object (e.g. on file delete)
641
+     *
642
+     * @param string $objectType the object type (e.g. 'files')
643
+     * @param string $objectId e.g. the file id
644
+     * @return boolean
645
+     * @since 9.0.0
646
+     */
647
+    public function deleteCommentsAtObject($objectType, $objectId) {
648
+        $this->checkRoleParameters('Object', $objectType, $objectId);
649
+
650
+        $qb = $this->dbConn->getQueryBuilder();
651
+        $affectedRows = $qb
652
+            ->delete('comments')
653
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
654
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
655
+            ->setParameter('type', $objectType)
656
+            ->setParameter('id', $objectId)
657
+            ->execute();
658
+
659
+        $this->commentsCache = [];
660
+
661
+        return is_int($affectedRows);
662
+    }
663
+
664
+    /**
665
+     * deletes the read markers for the specified user
666
+     *
667
+     * @param \OCP\IUser $user
668
+     * @return bool
669
+     * @since 9.0.0
670
+     */
671
+    public function deleteReadMarksFromUser(IUser $user) {
672
+        $qb = $this->dbConn->getQueryBuilder();
673
+        $query = $qb->delete('comments_read_markers')
674
+            ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
675
+            ->setParameter('user_id', $user->getUID());
676
+
677
+        try {
678
+            $affectedRows = $query->execute();
679
+        } catch (DriverException $e) {
680
+            $this->logger->logException($e, ['app' => 'core_comments']);
681
+            return false;
682
+        }
683
+        return ($affectedRows > 0);
684
+    }
685
+
686
+    /**
687
+     * sets the read marker for a given file to the specified date for the
688
+     * provided user
689
+     *
690
+     * @param string $objectType
691
+     * @param string $objectId
692
+     * @param \DateTime $dateTime
693
+     * @param IUser $user
694
+     * @since 9.0.0
695
+     */
696
+    public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) {
697
+        $this->checkRoleParameters('Object', $objectType, $objectId);
698
+
699
+        $qb = $this->dbConn->getQueryBuilder();
700
+        $values = [
701
+            'user_id' => $qb->createNamedParameter($user->getUID()),
702
+            'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'),
703
+            'object_type' => $qb->createNamedParameter($objectType),
704
+            'object_id' => $qb->createNamedParameter($objectId),
705
+        ];
706
+
707
+        // Strategy: try to update, if this does not return affected rows, do an insert.
708
+        $affectedRows = $qb
709
+            ->update('comments_read_markers')
710
+            ->set('user_id', $values['user_id'])
711
+            ->set('marker_datetime', $values['marker_datetime'])
712
+            ->set('object_type', $values['object_type'])
713
+            ->set('object_id', $values['object_id'])
714
+            ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
715
+            ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
716
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
717
+            ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
718
+            ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
719
+            ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
720
+            ->execute();
721
+
722
+        if ($affectedRows > 0) {
723
+            return;
724
+        }
725
+
726
+        $qb->insert('comments_read_markers')
727
+            ->values($values)
728
+            ->execute();
729
+    }
730
+
731
+    /**
732
+     * returns the read marker for a given file to the specified date for the
733
+     * provided user. It returns null, when the marker is not present, i.e.
734
+     * no comments were marked as read.
735
+     *
736
+     * @param string $objectType
737
+     * @param string $objectId
738
+     * @param IUser $user
739
+     * @return \DateTime|null
740
+     * @since 9.0.0
741
+     */
742
+    public function getReadMark($objectType, $objectId, IUser $user) {
743
+        $qb = $this->dbConn->getQueryBuilder();
744
+        $resultStatement = $qb->select('marker_datetime')
745
+            ->from('comments_read_markers')
746
+            ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
747
+            ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
748
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
749
+            ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
750
+            ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
751
+            ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
752
+            ->execute();
753
+
754
+        $data = $resultStatement->fetch();
755
+        $resultStatement->closeCursor();
756
+        if (!$data || is_null($data['marker_datetime'])) {
757
+            return null;
758
+        }
759
+
760
+        return new \DateTime($data['marker_datetime']);
761
+    }
762
+
763
+    /**
764
+     * deletes the read markers on the specified object
765
+     *
766
+     * @param string $objectType
767
+     * @param string $objectId
768
+     * @return bool
769
+     * @since 9.0.0
770
+     */
771
+    public function deleteReadMarksOnObject($objectType, $objectId) {
772
+        $this->checkRoleParameters('Object', $objectType, $objectId);
773
+
774
+        $qb = $this->dbConn->getQueryBuilder();
775
+        $query = $qb->delete('comments_read_markers')
776
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
777
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
778
+            ->setParameter('object_type', $objectType)
779
+            ->setParameter('object_id', $objectId);
780
+
781
+        try {
782
+            $affectedRows = $query->execute();
783
+        } catch (DriverException $e) {
784
+            $this->logger->logException($e, ['app' => 'core_comments']);
785
+            return false;
786
+        }
787
+        return ($affectedRows > 0);
788
+    }
789
+
790
+    /**
791
+     * registers an Entity to the manager, so event notifications can be send
792
+     * to consumers of the comments infrastructure
793
+     *
794
+     * @param \Closure $closure
795
+     */
796
+    public function registerEventHandler(\Closure $closure) {
797
+        $this->eventHandlerClosures[] = $closure;
798
+        $this->eventHandlers = [];
799
+    }
800
+
801
+    /**
802
+     * registers a method that resolves an ID to a display name for a given type
803
+     *
804
+     * @param string $type
805
+     * @param \Closure $closure
806
+     * @throws \OutOfBoundsException
807
+     * @since 11.0.0
808
+     *
809
+     * Only one resolver shall be registered per type. Otherwise a
810
+     * \OutOfBoundsException has to thrown.
811
+     */
812
+    public function registerDisplayNameResolver($type, \Closure $closure) {
813
+        if (!is_string($type)) {
814
+            throw new \InvalidArgumentException('String expected.');
815
+        }
816
+        if (isset($this->displayNameResolvers[$type])) {
817
+            throw new \OutOfBoundsException('Displayname resolver for this type already registered');
818
+        }
819
+        $this->displayNameResolvers[$type] = $closure;
820
+    }
821
+
822
+    /**
823
+     * resolves a given ID of a given Type to a display name.
824
+     *
825
+     * @param string $type
826
+     * @param string $id
827
+     * @return string
828
+     * @throws \OutOfBoundsException
829
+     * @since 11.0.0
830
+     *
831
+     * If a provided type was not registered, an \OutOfBoundsException shall
832
+     * be thrown. It is upon the resolver discretion what to return of the
833
+     * provided ID is unknown. It must be ensured that a string is returned.
834
+     */
835
+    public function resolveDisplayName($type, $id) {
836
+        if (!is_string($type)) {
837
+            throw new \InvalidArgumentException('String expected.');
838
+        }
839
+        if (!isset($this->displayNameResolvers[$type])) {
840
+            throw new \OutOfBoundsException('No Displayname resolver for this type registered');
841
+        }
842
+        return (string)$this->displayNameResolvers[$type]($id);
843
+    }
844
+
845
+    /**
846
+     * returns valid, registered entities
847
+     *
848
+     * @return \OCP\Comments\ICommentsEventHandler[]
849
+     */
850
+    private function getEventHandlers() {
851
+        if (!empty($this->eventHandlers)) {
852
+            return $this->eventHandlers;
853
+        }
854
+
855
+        $this->eventHandlers = [];
856
+        foreach ($this->eventHandlerClosures as $name => $closure) {
857
+            $entity = $closure();
858
+            if (!($entity instanceof ICommentsEventHandler)) {
859
+                throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface');
860
+            }
861
+            $this->eventHandlers[$name] = $entity;
862
+        }
863
+
864
+        return $this->eventHandlers;
865
+    }
866
+
867
+    /**
868
+     * sends notifications to the registered entities
869
+     *
870
+     * @param $eventType
871
+     * @param IComment $comment
872
+     */
873
+    private function sendEvent($eventType, IComment $comment) {
874
+        $entities = $this->getEventHandlers();
875
+        $event = new CommentsEvent($eventType, $comment);
876
+        foreach ($entities as $entity) {
877
+            $entity->handle($event);
878
+        }
879
+    }
880 880
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -191,7 +191,7 @@  discard block
 block discarded – undo
191 191
 			!is_string($type) || empty($type)
192 192
 			|| !is_string($id) || empty($id)
193 193
 		) {
194
-			throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
194
+			throw new \InvalidArgumentException($role.' parameters must be string and not empty');
195 195
 		}
196 196
 	}
197 197
 
@@ -413,7 +413,7 @@  discard block
 block discarded – undo
413 413
 	public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
414 414
 		$qb = $this->dbConn->getQueryBuilder();
415 415
 		$query = $qb->select('fileid', $qb->createFunction(
416
-			'COUNT(' . $qb->getColumnName('c.id') . ')')
416
+			'COUNT('.$qb->getColumnName('c.id').')')
417 417
 		)->from('comments', 'c')
418 418
 			->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
419 419
 				$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
@@ -432,8 +432,8 @@  discard block
 block discarded – undo
432 432
 			->groupBy('f.fileid');
433 433
 
434 434
 		$resultStatement = $query->execute();
435
-		return array_map(function ($count) {
436
-			return (int)$count;
435
+		return array_map(function($count) {
436
+			return (int) $count;
437 437
 		}, $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR));
438 438
 	}
439 439
 
@@ -540,7 +540,7 @@  discard block
 block discarded – undo
540 540
 	 * @param IComment $comment
541 541
 	 * @return bool
542 542
 	 */
543
-	protected function insert(IComment &$comment) {
543
+	protected function insert(IComment & $comment) {
544 544
 		$qb = $this->dbConn->getQueryBuilder();
545 545
 		$affectedRows = $qb
546 546
 			->insert('comments')
@@ -839,7 +839,7 @@  discard block
 block discarded – undo
839 839
 		if (!isset($this->displayNameResolvers[$type])) {
840 840
 			throw new \OutOfBoundsException('No Displayname resolver for this type registered');
841 841
 		}
842
-		return (string)$this->displayNameResolvers[$type]($id);
842
+		return (string) $this->displayNameResolvers[$type]($id);
843 843
 	}
844 844
 
845 845
 	/**
Please login to merge, or discard this patch.