1717use OCP \L10N \IFactory ;
1818use OCP \Notification \INotification ;
1919use OCP \Notification \INotifier ;
20+ use Psr \Log \LoggerInterface ;
2021
2122class Notifier implements INotifier {
2223 public const NOTIFY_POLL_DELETED_BY_OTHER = 'deletePollByOther ' ;
2324 public const NOTIFY_POLL_ARCHIVED_BY_OTHER = 'softDeletePollByOther ' ;
2425 public const NOTIFY_POLL_TAKEOVER = 'takeOverPoll ' ;
26+ public const NOTIFY_POLL_CHANGED_OWNER = 'PollChangedOwner ' ;
2527 public const NOTIFY_INVITATION = 'invitation ' ;
2628 private const SUBJECT_PARSED = 'parsedSubject ' ;
2729 private const SUBJECT_RICH = 'richSubject ' ;
@@ -33,6 +35,7 @@ public function __construct(
3335 protected PollMapper $ pollMapper ,
3436 private UserMapper $ userMapper ,
3537 private NotificationService $ notificationService ,
38+ private LoggerInterface $ logger ,
3639 ) {
3740 }
3841
@@ -50,22 +53,6 @@ public function getName(): string {
5053 return $ this ->l10nFactory ->get (AppConstants::APP_ID )->t ('Polls ' );
5154 }
5255
53- /**
54- * @return string[][]
55- *
56- * @psalm-return array{actor: array{type: 'user', id: string, name: string}}
57- */
58- private function getActor (string $ actorId ): array {
59- $ actor = $ this ->userMapper ->getUserFromUserBase ($ actorId );
60- return [
61- 'actor ' => [
62- 'type ' => 'user ' ,
63- 'id ' => $ actor ->getId (),
64- 'name ' => $ actor ->getDisplayName (),
65- ]
66- ];
67- }
68-
6956 public function prepare (INotification $ notification , string $ languageCode ): INotification {
7057 $ l = $ this ->l10nFactory ->get (AppConstants::APP_ID , $ languageCode );
7158 if ($ notification ->getApp () !== AppConstants::APP_ID ) {
@@ -81,37 +68,72 @@ public function prepare(INotification $notification, string $languageCode): INot
8168
8269 try {
8370 $ poll = $ this ->pollMapper ->get (intval ($ notification ->getObjectId ()));
84- $ actor = $ this ->getActor ($ parameters ['actor ' ] ?? $ poll ->getOwner ());
85- $ pollTitle = $ poll ->getTitle ();
86- $ notification ->setLink ($ poll ->getVoteUrl ());
8771 } catch (DoesNotExistException $ e ) {
72+ $ this ->logger ->info ('Notification silently removed, poll not found ' , [
73+ 'notification ' => $ notification ->getObjectId (),
74+ 'error ' => $ e ->getMessage (),
75+ ]);
8876 $ this ->notificationService ->removeNotification (intval ($ notification ->getObjectId ()));
8977 return $ notification ;
9078 }
9179
80+ $ actor = $ this ->userMapper ->getUserFromUserBase ($ parameters ['actor ' ] ?? $ poll ->getOwner ());
81+ $ pollTitle = $ parameters ['pollTitle ' ] ?? $ poll ->getTitle ();
82+ $ notification ->setLink ($ poll ->getVoteUrl ());
83+
84+ // TODO: tidy subjects and parameters
9285 $ subjects = match ($ notification ->getSubject ()) {
9386 self ::NOTIFY_INVITATION => [
94- self ::SUBJECT_PARSED => $ l ->t ('%s invited you to a poll ' , $ actor[ ' actor ' ][ ' name ' ] ),
87+ self ::SUBJECT_PARSED => $ l ->t ('%s invited you to a poll ' , $ actor-> getDisplayName () ),
9588 self ::SUBJECT_RICH => $ l ->t ('{actor} has invited you to the poll "%s". ' , $ pollTitle ),
9689 ],
9790 self ::NOTIFY_POLL_TAKEOVER => [
98- self ::SUBJECT_PARSED => $ l ->t ('%s took over your poll ' , $ actor[ ' actor ' ][ ' name ' ] ),
91+ self ::SUBJECT_PARSED => $ l ->t ('%s took over your poll ' , $ actor-> getDisplayName () ),
9992 self ::SUBJECT_RICH => $ l ->t ('{actor} took over your poll "%s" and is the new owner. ' , $ pollTitle ),
10093 ],
94+ self ::NOTIFY_POLL_CHANGED_OWNER => [
95+ self ::SUBJECT_PARSED => $ l ->t ('%s is the new owner of your poll. ' , $ parameters ['newOwner ' ]),
96+ self ::SUBJECT_RICH => $ l ->t ('{actor} transfered your poll "%s" to {newOwner}. You are no more the owner. ' , $ pollTitle ),
97+ ],
10198 self ::NOTIFY_POLL_DELETED_BY_OTHER => [
102- self ::SUBJECT_PARSED => $ l ->t ('%s deleted your poll ' , $ actor[ ' actor ' ][ ' name ' ] ),
99+ self ::SUBJECT_PARSED => $ l ->t ('%s deleted your poll ' , $ actor-> getDisplayName () ),
103100 self ::SUBJECT_RICH => $ l ->t ('{actor} deleted your poll "%s". ' , $ pollTitle ),
104101 ],
105102 self ::NOTIFY_POLL_ARCHIVED_BY_OTHER => [
106- self ::SUBJECT_PARSED => $ l ->t ('%s archived your poll ' , $ actor[ ' actor ' ][ ' name ' ] ),
103+ self ::SUBJECT_PARSED => $ l ->t ('%s archived your poll ' , $ actor-> getDisplayName () ),
107104 self ::SUBJECT_RICH => $ l ->t ('{actor} archived your poll "%s". ' , $ pollTitle ),
108105 ],
109106 // Unknown subject => Unknown notification => throw
110107 default => throw new \InvalidArgumentException (),
111108 };
112109
113- $ notification ->setParsedSubject ($ subjects [self ::SUBJECT_PARSED ]);
114- $ notification ->setRichSubject ($ subjects [self ::SUBJECT_RICH ], $ actor );
110+ switch ($ notification ->getSubject ()) {
111+ case self ::NOTIFY_POLL_CHANGED_OWNER :
112+ $ newOwner = $ this ->userMapper ->getUserFromUserBase ($ parameters ['newOwner ' ]);
113+ // overwrite the subject with the new owner
114+ $ notification ->setParsedSubject (
115+ $ l ->t ('%s is the new owner of your poll. ' , $ newOwner ->getDisplayName ())
116+ );
117+
118+ $ notification ->setRichSubject (
119+ $ subjects [self ::SUBJECT_RICH ],
120+ [
121+ 'actor ' => $ actor ->getRichObjectString (),
122+ 'newOwner ' => $ newOwner ->getRichObjectString (),
123+ ]
124+ );
125+ break ;
126+
127+ default :
128+ $ notification ->setParsedSubject ($ subjects [self ::SUBJECT_PARSED ]);
129+ $ notification ->setRichSubject (
130+ $ subjects [self ::SUBJECT_RICH ],
131+ [
132+ 'actor ' => $ actor ->getRichObjectString (),
133+ ]
134+ );
135+ break ;
136+ }
115137
116138 return $ notification ;
117139 }
0 commit comments