Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
MessageGroupSubscriptionPresentationModel | |
0.00% |
0 / 36 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
getIconType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPrimaryLink | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
getHeaderMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getCompactHeaderMessageKey | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getCompactHeaderMessage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getBodyMessage | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
getNumberOfChangedMessages | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageGroupProcessing; |
5 | |
6 | use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel; |
7 | use MediaWiki\Extension\Notifications\Model\Event; |
8 | use MediaWiki\SpecialPage\SpecialPage; |
9 | |
10 | /** |
11 | * Presentation model for Echo notifications sent out for message group subscriptions |
12 | * @since 2024.04 |
13 | * @author Abijeet Patro |
14 | * @license GPL-2.0-or-later |
15 | */ |
16 | class MessageGroupSubscriptionPresentationModel extends EchoEventPresentationModel { |
17 | public function getIconType() { |
18 | return 'translate-mgs-icon'; |
19 | } |
20 | |
21 | public function getPrimaryLink() { |
22 | if ( $this->isBundled() ) { |
23 | return false; |
24 | } |
25 | |
26 | $groupId = $this->event->getExtraParam( 'groupId' ); |
27 | return [ |
28 | 'url' => SpecialPage::getTitleFor( 'Translate', $groupId )->getFullURL(), |
29 | 'label' => $this->msg( 'notification-link-mgs-group-translate' ) |
30 | ->params( $this->event->getExtraParam( 'groupLabel' ) ) |
31 | ]; |
32 | } |
33 | |
34 | public function getHeaderMessage() { |
35 | $msg = $this->msg( 'notification-header-message-group-subscription' ); |
36 | $msg->params( $this->event->getExtraParam( 'groupLabel' ) ); |
37 | return $msg; |
38 | } |
39 | |
40 | public function getCompactHeaderMessageKey(): string { |
41 | $type = $this->event->getType(); |
42 | if ( $type === 'translate-mgs-message-added' ) { |
43 | return 'notification-body-translate-mgs-message-added'; |
44 | } |
45 | |
46 | return parent::getCompactHeaderMessageKey(); |
47 | } |
48 | |
49 | public function getCompactHeaderMessage() { |
50 | $msg = $this->msg( $this->getCompactHeaderMessageKey() ); |
51 | $msg->params( $this->getNumberOfChangedMessages( $this->event ) ); |
52 | return $msg; |
53 | } |
54 | |
55 | public function getBodyMessage() { |
56 | $type = $this->event->getType(); |
57 | if ( $type === 'translate-mgs-message-added' ) { |
58 | if ( $this->isBundled() ) { |
59 | $events = $this->getBundledEvents(); |
60 | $events[] = $this->event; |
61 | } else { |
62 | $events = [ $this->event ]; |
63 | } |
64 | |
65 | $addedOrUpdatedMessages = 0; |
66 | foreach ( $events as $event ) { |
67 | $addedOrUpdatedMessages += $this->getNumberOfChangedMessages( $event ); |
68 | } |
69 | |
70 | $msg = $this->msg( 'notification-body-translate-mgs-message-added' ); |
71 | $msg->params( $addedOrUpdatedMessages ); |
72 | return $msg; |
73 | } |
74 | } |
75 | |
76 | private function getNumberOfChangedMessages( Event $event ): int { |
77 | $changes = $event->getExtraParam( 'changes' ); |
78 | $messageCount = 0; |
79 | $messageCount += count( $changes[MessageGroupSubscription::STATE_ADDED] ?? [] ); |
80 | $messageCount += count( $changes[MessageGroupSubscription::STATE_UPDATED] ?? [] ); |
81 | |
82 | return $messageCount; |
83 | } |
84 | } |