Passed
Push — master ( e2d0b8...cfcce6 )
by Roeland
19:40 queued 08:57
created
apps/dav/lib/Connector/Sabre/CommentPropertiesPlugin.php 1 patch
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -30,132 +30,132 @@
 block discarded – undo
30 30
 
31 31
 class CommentPropertiesPlugin extends ServerPlugin {
32 32
 
33
-	const PROPERTY_NAME_HREF   = '{http://owncloud.org/ns}comments-href';
34
-	const PROPERTY_NAME_COUNT  = '{http://owncloud.org/ns}comments-count';
35
-	const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread';
36
-
37
-	/** @var  \Sabre\DAV\Server */
38
-	protected $server;
39
-
40
-	/** @var ICommentsManager */
41
-	private $commentsManager;
42
-
43
-	/** @var IUserSession */
44
-	private $userSession;
45
-
46
-	private $cachedUnreadCount = [];
47
-
48
-	private $cachedFolders = [];
49
-
50
-	public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
51
-		$this->commentsManager = $commentsManager;
52
-		$this->userSession = $userSession;
53
-	}
54
-
55
-	/**
56
-	 * This initializes the plugin.
57
-	 *
58
-	 * This function is called by Sabre\DAV\Server, after
59
-	 * addPlugin is called.
60
-	 *
61
-	 * This method should set up the required event subscriptions.
62
-	 *
63
-	 * @param \Sabre\DAV\Server $server
64
-	 * @return void
65
-	 */
66
-	function initialize(\Sabre\DAV\Server $server) {
67
-		$this->server = $server;
68
-		$this->server->on('propFind', array($this, 'handleGetProperties'));
69
-	}
70
-
71
-	/**
72
-	 * Adds tags and favorites properties to the response,
73
-	 * if requested.
74
-	 *
75
-	 * @param PropFind $propFind
76
-	 * @param \Sabre\DAV\INode $node
77
-	 * @return void
78
-	 */
79
-	public function handleGetProperties(
80
-		PropFind $propFind,
81
-		\Sabre\DAV\INode $node
82
-	) {
83
-		if (!($node instanceof File) && !($node instanceof Directory)) {
84
-			return;
85
-		}
86
-
87
-		// need prefetch ?
88
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
89
-			&& $propFind->getDepth() !== 0
90
-			&& !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
91
-		) {
92
-			$unreadCounts = $this->commentsManager->getNumberOfUnreadCommentsForFolder($node->getId(), $this->userSession->getUser());
93
-			$this->cachedFolders[] = $node->getPath();
94
-			foreach ($unreadCounts as $id => $count) {
95
-				$this->cachedUnreadCount[$id] = $count;
96
-			}
97
-		}
98
-
99
-		$propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) {
100
-			return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
101
-		});
102
-
103
-		$propFind->handle(self::PROPERTY_NAME_HREF, function() use ($node) {
104
-			return $this->getCommentsLink($node);
105
-		});
106
-
107
-		$propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) {
108
-			if (isset($this->cachedUnreadCount[$node->getId()])) {
109
-				return $this->cachedUnreadCount[$node->getId()];
110
-			} else {
111
-				list($parentPath,) = \Sabre\Uri\split($node->getPath());
112
-				if ($parentPath === '') {
113
-					$parentPath = '/';
114
-				}
115
-				// if we already cached the folder this file is in we know there are no comments for this file
116
-				if (array_search($parentPath, $this->cachedFolders) === false) {
117
-					return 0;
118
-				} else {
119
-					return $this->getUnreadCount($node);
120
-				}
121
-			}
122
-		});
123
-	}
124
-
125
-	/**
126
-	 * returns a reference to the comments node
127
-	 *
128
-	 * @param Node $node
129
-	 * @return mixed|string
130
-	 */
131
-	public function getCommentsLink(Node $node) {
132
-		$href =  $this->server->getBaseUri();
133
-		$entryPoint = strpos($href, '/remote.php/');
134
-		if($entryPoint === false) {
135
-			// in case we end up somewhere else, unexpectedly.
136
-			return null;
137
-		}
138
-		$commentsPart = 'dav/comments/files/' . rawurldecode($node->getId());
139
-		$href = substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
140
-		return $href;
141
-	}
142
-
143
-	/**
144
-	 * returns the number of unread comments for the currently logged in user
145
-	 * on the given file or directory node
146
-	 *
147
-	 * @param Node $node
148
-	 * @return Int|null
149
-	 */
150
-	public function getUnreadCount(Node $node) {
151
-		$user = $this->userSession->getUser();
152
-		if(is_null($user)) {
153
-			return null;
154
-		}
155
-
156
-		$lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
157
-
158
-		return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
159
-	}
33
+    const PROPERTY_NAME_HREF   = '{http://owncloud.org/ns}comments-href';
34
+    const PROPERTY_NAME_COUNT  = '{http://owncloud.org/ns}comments-count';
35
+    const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread';
36
+
37
+    /** @var  \Sabre\DAV\Server */
38
+    protected $server;
39
+
40
+    /** @var ICommentsManager */
41
+    private $commentsManager;
42
+
43
+    /** @var IUserSession */
44
+    private $userSession;
45
+
46
+    private $cachedUnreadCount = [];
47
+
48
+    private $cachedFolders = [];
49
+
50
+    public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
51
+        $this->commentsManager = $commentsManager;
52
+        $this->userSession = $userSession;
53
+    }
54
+
55
+    /**
56
+     * This initializes the plugin.
57
+     *
58
+     * This function is called by Sabre\DAV\Server, after
59
+     * addPlugin is called.
60
+     *
61
+     * This method should set up the required event subscriptions.
62
+     *
63
+     * @param \Sabre\DAV\Server $server
64
+     * @return void
65
+     */
66
+    function initialize(\Sabre\DAV\Server $server) {
67
+        $this->server = $server;
68
+        $this->server->on('propFind', array($this, 'handleGetProperties'));
69
+    }
70
+
71
+    /**
72
+     * Adds tags and favorites properties to the response,
73
+     * if requested.
74
+     *
75
+     * @param PropFind $propFind
76
+     * @param \Sabre\DAV\INode $node
77
+     * @return void
78
+     */
79
+    public function handleGetProperties(
80
+        PropFind $propFind,
81
+        \Sabre\DAV\INode $node
82
+    ) {
83
+        if (!($node instanceof File) && !($node instanceof Directory)) {
84
+            return;
85
+        }
86
+
87
+        // need prefetch ?
88
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
89
+            && $propFind->getDepth() !== 0
90
+            && !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
91
+        ) {
92
+            $unreadCounts = $this->commentsManager->getNumberOfUnreadCommentsForFolder($node->getId(), $this->userSession->getUser());
93
+            $this->cachedFolders[] = $node->getPath();
94
+            foreach ($unreadCounts as $id => $count) {
95
+                $this->cachedUnreadCount[$id] = $count;
96
+            }
97
+        }
98
+
99
+        $propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) {
100
+            return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
101
+        });
102
+
103
+        $propFind->handle(self::PROPERTY_NAME_HREF, function() use ($node) {
104
+            return $this->getCommentsLink($node);
105
+        });
106
+
107
+        $propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) {
108
+            if (isset($this->cachedUnreadCount[$node->getId()])) {
109
+                return $this->cachedUnreadCount[$node->getId()];
110
+            } else {
111
+                list($parentPath,) = \Sabre\Uri\split($node->getPath());
112
+                if ($parentPath === '') {
113
+                    $parentPath = '/';
114
+                }
115
+                // if we already cached the folder this file is in we know there are no comments for this file
116
+                if (array_search($parentPath, $this->cachedFolders) === false) {
117
+                    return 0;
118
+                } else {
119
+                    return $this->getUnreadCount($node);
120
+                }
121
+            }
122
+        });
123
+    }
124
+
125
+    /**
126
+     * returns a reference to the comments node
127
+     *
128
+     * @param Node $node
129
+     * @return mixed|string
130
+     */
131
+    public function getCommentsLink(Node $node) {
132
+        $href =  $this->server->getBaseUri();
133
+        $entryPoint = strpos($href, '/remote.php/');
134
+        if($entryPoint === false) {
135
+            // in case we end up somewhere else, unexpectedly.
136
+            return null;
137
+        }
138
+        $commentsPart = 'dav/comments/files/' . rawurldecode($node->getId());
139
+        $href = substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
140
+        return $href;
141
+    }
142
+
143
+    /**
144
+     * returns the number of unread comments for the currently logged in user
145
+     * on the given file or directory node
146
+     *
147
+     * @param Node $node
148
+     * @return Int|null
149
+     */
150
+    public function getUnreadCount(Node $node) {
151
+        $user = $this->userSession->getUser();
152
+        if(is_null($user)) {
153
+            return null;
154
+        }
155
+
156
+        $lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
157
+
158
+        return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
159
+    }
160 160
 
161 161
 }
Please login to merge, or discard this patch.