aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Event/Activity/ActivitySubscriber.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Event/Activity/ActivitySubscriber.php')
-rw-r--r--src/Wallabag/CoreBundle/Event/Activity/ActivitySubscriber.php224
1 files changed, 224 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Event/Activity/ActivitySubscriber.php b/src/Wallabag/CoreBundle/Event/Activity/ActivitySubscriber.php
new file mode 100644
index 00000000..81379ff2
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Event/Activity/ActivitySubscriber.php
@@ -0,0 +1,224 @@
1<?php
2
3namespace Wallabag\CoreBundle\Event\Activity;
4
5use Doctrine\ORM\EntityManager;
6use FOS\UserBundle\Event\UserEvent;
7use FOS\UserBundle\FOSUserEvents;
8use Psr\Log\LoggerInterface;
9use Symfony\Component\EventDispatcher\Event;
10use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11use Wallabag\CoreBundle\Entity\Activity;
12use Wallabag\CoreBundle\Event\Activity\Actions\Annotation\AnnotationCreatedEvent;
13use Wallabag\CoreBundle\Event\Activity\Actions\Annotation\AnnotationDeletedEvent;
14use Wallabag\CoreBundle\Event\Activity\Actions\Annotation\AnnotationEditedEvent;
15use Wallabag\CoreBundle\Event\Activity\Actions\Annotation\AnnotationEvent;
16use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntryDeletedEvent;
17use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntryEditedEvent;
18use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntryEvent;
19use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntryFavouriteEvent;
20use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntryReadEvent;
21use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntrySavedEvent;
22use Wallabag\CoreBundle\Event\Activity\Actions\Entry\EntryTaggedEvent;
23use Wallabag\CoreBundle\Event\Activity\Actions\Federation\FollowEvent;
24use Wallabag\CoreBundle\Event\Activity\Actions\Federation\RecommendedEntryEvent;
25use Wallabag\CoreBundle\Event\Activity\Actions\Federation\UnfollowEvent;
26use Wallabag\CoreBundle\Event\Activity\Actions\Share\ShareAcceptedEvent;
27use Wallabag\CoreBundle\Event\Activity\Actions\Share\ShareCancelledEvent;
28use Wallabag\CoreBundle\Event\Activity\Actions\Share\ShareCreatedEvent;
29use Wallabag\CoreBundle\Event\Activity\Actions\Share\ShareDeniedEvent;
30use Wallabag\CoreBundle\Event\Activity\Actions\Share\ShareEvent;
31use Wallabag\CoreBundle\Event\Activity\Actions\User\UserDeletedEvent;
32use Wallabag\CoreBundle\Event\Activity\Actions\User\UserEditedEvent;
33use Wallabag\CoreBundle\Notifications\ActionInterface;
34
35/**
36 * This listener will create the associated configuration when a user register.
37 * This configuration will be created right after the registration (no matter if it needs an email validation).
38 */
39class ActivitySubscriber implements EventSubscriberInterface
40{
41
42 /**
43 * @var EntityManager
44 */
45 private $em;
46
47 /**
48 * @var LoggerInterface $logger
49 */
50 private $logger;
51
52 public function __construct(EntityManager $em, LoggerInterface $logger)
53 {
54 $this->em = $em;
55 $this->logger = $logger;
56 }
57
58 public static function getSubscribedEvents()
59 {
60 return [
61 EntrySavedEvent::NAME => 'entryActivity',
62 EntryDeletedEvent::NAME => 'entryActivity',
63 EntryEditedEvent::NAME => 'entryActivity',
64 EntryTaggedEvent::NAME => 'taggedEntry',
65 EntryFavouriteEvent::NAME => 'entryActivity',
66 EntryReadEvent::NAME => 'entryActivity',
67
68 AnnotationCreatedEvent::NAME => 'annotationActivity',
69 AnnotationEditedEvent::NAME => 'annotationActivity',
70 AnnotationDeletedEvent::NAME => 'annotationActivity',
71
72 FollowEvent::NAME => 'followedAccount',
73 UnfollowEvent::NAME => 'unfollowedAccount',
74 RecommendedEntryEvent::NAME => 'recommendedEntry',
75
76 ShareCreatedEvent::NAME => 'shareActivity',
77 ShareAcceptedEvent::NAME => 'shareActivity',
78 ShareDeniedEvent::NAME => 'shareActivity',
79 ShareCancelledEvent::NAME => 'shareActivity',
80
81 // when a user register using the normal form
82 FOSUserEvents::REGISTRATION_COMPLETED => 'userActivity',
83 // when we manually create a user using the command line
84 // OR when we create it from the config UI
85 FOSUserEvents::USER_CREATED => 'userActivity',
86 UserEditedEvent::NAME => 'userActivity',
87 UserDeletedEvent::NAME => 'userActivity',
88 ];
89 }
90
91 public function userActivity(Event $event)
92 {
93 $activityType = 0;
94 if ($event instanceof UserEvent) {
95 $activityType = Activity::USER_CREATE;
96 } elseif ($event instanceof UserEditedEvent) {
97 $activityType = Activity::USER_EDIT;
98 } elseif ($event instanceof UserDeletedEvent) {
99 $activityType = Activity::USER_REMOVE;
100 }
101
102 $user = $event->getUser();
103 $activity = new Activity($activityType, Activity::USER_OBJECT, $user->getId());
104 $activity->setUser($user->getAccount());
105 $this->em->persist($activity);
106 $this->em->flush();
107 }
108
109 public function entryActivity(EntryEvent $event)
110 {
111 $entry = $event->getEntry();
112
113 $activityType = 0;
114 if ($event instanceof EntrySavedEvent) {
115 $activityType = Activity::ENTRY_ADD;
116 } elseif ($event instanceof EntryDeletedEvent) {
117 $activityType = Activity::ENTRY_DELETE;
118 } elseif ($event instanceof EntryEditedEvent) {
119 $activityType = Activity::ENTRY_EDIT;
120 } elseif ($event instanceof EntryFavouriteEvent) {
121 if ($entry->isStarred()) {
122 $activityType = Activity::ENTRY_FAVOURITE;
123 } else {
124 $activityType = Activity::ENTRY_UNFAVOURITE;
125 }
126 } elseif ($event instanceof EntryReadEvent) {
127 if ($entry->isArchived()) {
128 $activityType = Activity::ENTRY_READ;
129 } else {
130 $activityType = Activity::ENTRY_UNREAD;
131 }
132 }
133
134 $activity = new Activity($activityType, Activity::ENTRY_OBJECT, $entry->getId());
135 $activity->setUser($entry->getUser()->getAccount());
136 $this->em->persist($activity);
137 $this->em->flush();
138 }
139
140 public function taggedEntry(EntryTaggedEvent $event)
141 {
142 $entry = $event->getEntry();
143 $activity = new Activity($event->isRemove() ? Activity::ENTRY_REMOVE_TAG : Activity::ENTRY_ADD_TAG, Activity::ENTRY_OBJECT, $entry->getId());
144 $activity->setUser($entry->getUser()->getAccount());
145 $activity->setSecondaryObjectType(Activity::TAG_OBJECT)
146 ->setSecondaryObjectId($event->getTags()[0]->getId());
147 $this->em->persist($activity);
148 $this->em->flush();
149 }
150
151 public function annotationActivity(AnnotationEvent $event)
152 {
153 $annotation = $event->getAnnotation();
154
155 $activityType = 0;
156 if ($event instanceof AnnotationCreatedEvent) {
157 $activityType = Activity::ANNOTATION_ADD;
158 } elseif ($event instanceof AnnotationEditedEvent) {
159 $activityType = Activity::ANNOTATION_EDIT;
160 } elseif ($event instanceof AnnotationDeletedEvent) {
161 $activityType = Activity::ANNOTATION_REMOVE;
162 }
163
164 $activity = new Activity($activityType, Activity::ANNOTATION_OBJECT, $annotation->getId());
165 $activity->setUser($annotation->getUser()->getAccount());
166 $this->em->persist($activity);
167 $this->em->flush();
168 }
169
170 public function followedAccount(FollowEvent $event)
171 {
172 $activity = new Activity(Activity::FOLLOW_ACCOUNT, Activity::ACCOUNT_OBJECT, $event->getAccount()->getId());
173 $activity->setUser($event->getAccount());
174 $activity->setSecondaryObjectType(Activity::ACCOUNT_OBJECT)
175 ->setSecondaryObjectId($event->getFollower()->getId());
176 $this->em->persist($activity);
177 $this->em->flush();
178 }
179
180 public function unfollowedAccount(UnfollowEvent $event)
181 {
182 $activity = new Activity(Activity::UNFOLLOW_ACCOUNT, Activity::ACCOUNT_OBJECT, $event->getAccount()->getId());
183 $activity->setUser($event->getAccount());
184 $activity->setSecondaryObjectType(Activity::ACCOUNT_OBJECT)
185 ->setSecondaryObjectId($event->getFollower()->getId());
186 $this->em->persist($activity);
187 $this->em->flush();
188 }
189
190 public function recommendedEntry(RecommendedEntryEvent $event)
191 {
192 $entry = $event->getEntry();
193 $account = $entry->getUser()->getAccount();
194 $activity = new Activity(Activity::RECOMMEND_ENTRY, Activity::ACCOUNT_OBJECT, $account->getId());
195 $activity->setUser($account);
196 $activity->setSecondaryObjectType(Activity::ENTRY_OBJECT)
197 ->setSecondaryObjectId($entry->getId());
198 $this->em->persist($activity);
199 $this->em->flush();
200 }
201
202 public function shareActivity(ShareEvent $event)
203 {
204 $share = $event->getShare();
205
206 $activityType = 0;
207 if ($event instanceof ShareCreatedEvent) {
208 $activityType = Activity::USER_SHARE_CREATED;
209 } elseif ($event instanceof ShareAcceptedEvent) {
210 $activityType = Activity::USER_SHARE_ACCEPTED;
211 } elseif ($event instanceof ShareDeniedEvent) {
212 $activityType = Activity::USER_SHARE_REFUSED;
213 } elseif ($event instanceof ShareCancelledEvent) {
214 $activityType = Activity::USER_SHARE_CANCELLED;
215 }
216
217 $activity = new Activity($activityType, Activity::SHARE_OBJECT, $share->getId());
218 $activity->setUser($share->getUserOrigin());
219 $activity->setSecondaryObjectType(Activity::ACCOUNT_OBJECT)
220 ->setSecondaryObjectId($share->getUserDestination()->getId());
221 $this->em->persist($activity);
222 $this->em->flush();
223 }
224}