2828 *
2929 */
3030
31+ use OCA \Files \Activity \FavoriteProvider ;
32+ use OCP \Activity \IManager ;
33+ use OCP \EventDispatcher \IEventDispatcher ;
34+ use OCP \Files \Events \NodeAddedToFavorite ;
35+ use OCP \Files \Events \NodeRemovedFromFavorite ;
36+ use OCP \IUser ;
37+ use OCP \IUserSession ;
3138use Sabre \DAV \PropFind ;
3239use Sabre \DAV \PropPatch ;
3340
@@ -51,6 +58,15 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin {
5158 */
5259 private $ tagManager ;
5360
61+ /** @var \OCP\IUserSession */
62+ private $ userSession ;
63+
64+ /** @var IEventDispatcher */
65+ private $ dispatcher ;
66+
67+ /** @var IManager */
68+ private $ activityManager ;
69+
5470 /**
5571 * @var \OCP\ITags
5672 */
@@ -73,11 +89,20 @@ class TagsPlugin extends \Sabre\DAV\ServerPlugin {
7389 * @param \Sabre\DAV\Tree $tree tree
7490 * @param \OCP\ITagManager $tagManager tag manager
7591 */
76- public function __construct (\Sabre \DAV \Tree $ tree , \OCP \ITagManager $ tagManager ) {
92+ public function __construct (
93+ \Sabre \DAV \Tree $ tree ,
94+ \OCP \ITagManager $ tagManager ,
95+ IUserSession $ userSession ,
96+ IEventDispatcher $ dispatcher ,
97+ IManager $ activityManager ,
98+ ) {
7799 $ this ->tree = $ tree ;
78100 $ this ->tagManager = $ tagManager ;
79101 $ this ->tagger = null ;
80102 $ this ->cachedTags = [];
103+ $ this ->userSession = $ userSession ;
104+ $ this ->dispatcher = $ dispatcher ;
105+ $ this ->activityManager = $ activityManager ;
81106 }
82107
83108 /**
@@ -248,7 +273,7 @@ public function handleGetProperties(
248273 *
249274 * @return void
250275 */
251- public function handleUpdateProperties ($ path , PropPatch $ propPatch ) {
276+ public function handleUpdateProperties (string $ path , PropPatch $ propPatch ) {
252277 $ node = $ this ->tree ->getNodeForPath ($ path );
253278 if (!($ node instanceof \OCA \DAV \Connector \Sabre \Node)) {
254279 return ;
@@ -259,10 +284,12 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
259284 return true ;
260285 });
261286
262- $ propPatch ->handle (self ::FAVORITE_PROPERTYNAME , function ($ favState ) use ($ node ) {
287+ $ propPatch ->handle (self ::FAVORITE_PROPERTYNAME , function ($ favState ) use ($ node, $ path ) {
263288 if ((int )$ favState === 1 || $ favState === 'true ' ) {
289+ $ this ->addActivity (true , $ node ->getId (), $ path );
264290 $ this ->getTagger ()->tagAs ($ node ->getId (), self ::TAG_FAVORITE );
265291 } else {
292+ $ this ->addActivity (false , $ node ->getId (), $ path );
266293 $ this ->getTagger ()->unTag ($ node ->getId (), self ::TAG_FAVORITE );
267294 }
268295
@@ -274,4 +301,40 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
274301 return 200 ;
275302 });
276303 }
304+
305+ /**
306+ * @param bool $addToFavorite
307+ * @param int $fileId
308+ * @param string $path
309+ */
310+ protected function addActivity ($ addToFavorite , $ fileId , $ path ) {
311+ $ user = $ this ->userSession ->getUser ();
312+ if (!$ user instanceof IUser) {
313+ return ;
314+ }
315+
316+ if ($ addToFavorite ) {
317+ $ event = new NodeAddedToFavorite ($ user , $ fileId , $ path );
318+ } else {
319+ $ event = new NodeRemovedFromFavorite ($ user , $ fileId , $ path );
320+ }
321+ $ this ->dispatcher ->dispatchTyped ($ event );
322+
323+ $ event = $ this ->activityManager ->generateEvent ();
324+ try {
325+ $ event ->setApp ('files ' )
326+ ->setObject ('files ' , $ fileId , $ path )
327+ ->setType ('favorite ' )
328+ ->setAuthor ($ user ->getUID ())
329+ ->setAffectedUser ($ user ->getUID ())
330+ ->setTimestamp (time ())
331+ ->setSubject (
332+ $ addToFavorite ? FavoriteProvider::SUBJECT_ADDED : FavoriteProvider::SUBJECT_REMOVED ,
333+ ['id ' => $ fileId , 'path ' => $ path ]
334+ );
335+ $ this ->activityManager ->publish ($ event );
336+ } catch (\InvalidArgumentException $ e ) {
337+ } catch (\BadMethodCallException $ e ) {
338+ }
339+ }
277340}
0 commit comments