summaryrefslogtreecommitdiff
blob: 58eec3a14eee4390a67b02af2af1a64304397976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php

/**
 * Utility class that formats a notification in the format specified
 */
class EchoDataOutputFormatter {

	/**
	 * @var array type => class
	 */
	protected static $formatters = [
		'flyout' => 'EchoFlyoutFormatter',
		'model' => 'EchoModelFormatter',
		'special' => 'SpecialNotificationsFormatter',
		'html' => 'SpecialNotificationsFormatter',
	];

	/**
	 * Format a notification for a user in the format specified
	 *
	 * @param EchoNotification $notification
	 * @param string|bool $format output format, false to not format any notifications
	 * @param User $user the target user viewing the notification
	 * @param Language $lang Language to format the notification in
	 * @return array|bool false if it could not be formatted
	 */
	public static function formatOutput( EchoNotification $notification, $format = false, User $user, Language $lang ) {
		$event = $notification->getEvent();
		$timestamp = $notification->getTimestamp();
		$utcTimestampIso8601 = wfTimestamp( TS_ISO_8601, $timestamp );
		$utcTimestampUnix = wfTimestamp( TS_UNIX, $timestamp );
		$utcTimestampMW = wfTimestamp( TS_MW, $timestamp );
		$bundledIds = null;

		$bundledNotifs = $notification->getBundledNotifications();
		if ( $bundledNotifs ) {
			$bundledEvents = array_map( function ( EchoNotification $notif ) {
				return $notif->getEvent();
			}, $bundledNotifs );
			$event->setBundledEvents( $bundledEvents );

			$bundledIds = array_map( function ( $event ) {
				return (int)$event->getId();
			}, $bundledEvents );
		}

		$timestampMw = self::getUserLocalTime( $user, $timestamp );

		// Start creating date section header
		$now = wfTimestamp();
		$dateFormat = substr( $timestampMw, 0, 8 );
		$timeDiff = $now - $utcTimestampUnix;
		// Most notifications would be more than two days ago, check this
		// first instead of checking 'today' then 'yesterday'
		if ( $timeDiff > 172800 ) {
			$date = self::getDateHeader( $user, $timestampMw );
		// 'Today'
		} elseif ( substr( self::getUserLocalTime( $user, $now ), 0, 8 ) === $dateFormat ) {
			$date = wfMessage( 'echo-date-today' )->escaped();
		// 'Yesterday'
		} elseif ( substr( self::getUserLocalTime( $user, $now - 86400 ), 0, 8 ) === $dateFormat ) {
			$date = wfMessage( 'echo-date-yesterday' )->escaped();
		} else {
			$date = self::getDateHeader( $user, $timestampMw );
		}
		// End creating date section header

		$output = [
			'wiki' => wfWikiID(),
			'id' => $event->getId(),
			'type' => $event->getType(),
			'category' => $event->getCategory(),
			'timestamp' => [
				// ISO 8601 is supposed to be the *only* format used for
				// date output, but back-compat...
				'utciso8601' => $utcTimestampIso8601,

				// UTC timestamp in UNIX format used for loading more notification
				'utcunix' => $utcTimestampUnix,
				'unix' => self::getUserLocalTime( $user, $timestamp, TS_UNIX ),
				'utcmw' => $utcTimestampMW,
				'mw' => $timestampMw,
				'date' => $date
			],
		];

		if ( $bundledIds ) {
			$output['bundledIds'] = $bundledIds;
		}

		if ( $event->getVariant() ) {
			$output['variant'] = $event->getVariant();
		}

		$title = $event->getTitle();
		if ( $title ) {
			$output['title'] = [
				'full' => $title->getPrefixedText(),
				'namespace' => $title->getNSText(),
				'namespace-key' => $title->getNamespace(),
				'text' => $title->getText(),
			];
		}

		$agent = $event->getAgent();
		if ( $agent ) {
			if ( $event->userCan( Revision::DELETED_USER, $user ) ) {
				$output['agent'] = [
					'id' => $agent->getId(),
					'name' => $agent->getName(),
				];
			} else {
				$output['agent'] = [ 'userhidden' => '' ];
			}
		}

		if ( $event->getRevision() ) {
			$output['revid'] = $event->getRevision()->getId();
		}

		if ( $notification->getReadTimestamp() ) {
			$output['read'] = $notification->getReadTimestamp();
		}

		// This is only meant for unread notifications, if a notification has a target
		// page, then it shouldn't be auto marked as read unless the user visits
		// the target page or a user marks it as read manually ( coming soon )
		$output['targetpages'] = [];
		if ( $notification->getTargetPages() ) {
			foreach ( $notification->getTargetPages() as $targetPage ) {
				$output['targetpages'][] = $targetPage->getPageId();
			}
		}

		if ( $format ) {
			$formatted = self::formatNotification( $event, $user, $format, $lang );
			if ( $formatted === false ) {
				// Can't display it, so mark it as read
				EchoDeferredMarkAsDeletedUpdate::add( $event );
				return false;
			}
			$output['*'] = $formatted;

			if ( $notification->getBundledNotifications() && self::isBundleExpandable( $event->getType() ) ) {
				$output['bundledNotifications'] = array_values( array_filter( array_map( function ( EchoNotification $notification ) use ( $format, $user, $lang ) {
					// remove nested notifications to
					// - ensure they are formatted as single notifications (not bundled)
					// - prevent further re-entrance on the current notification
					$notification->setBundledNotifications( [] );
					$notification->getEvent()->setBundledEvents( [] );
					return self::formatOutput( $notification, $format, $user, $lang );
				}, array_merge( [ $notification ], $notification->getBundledNotifications() ) ) ) );
			}
		}

		return $output;
	}

	/**
	 * @param EchoEvent $event
	 * @param User $user
	 * @param string $format
	 * @param Language $lang
	 * @return string|bool false if it could not be formatted
	 */
	protected static function formatNotification( EchoEvent $event, User $user, $format, $lang ) {
		if ( isset( self::$formatters[$format] ) ) {
			$class = self::$formatters[$format];
			/** @var EchoEventFormatter $formatter */
			$formatter = new $class( $user, $lang );
			return $formatter->format( $event );
		} else {
			return false;
		}
	}

	/**
	 * Get the date header in user's format, 'May 10' or '10 May', depending
	 * on user's date format preference
	 * @param User $user
	 * @param string $timestampMw
	 * @return string
	 */
	protected static function getDateHeader( User $user, $timestampMw ) {
		$lang = RequestContext::getMain()->getLanguage();
		$dateFormat = $lang->getDateFormatString( 'pretty', $user->getDatePreference() ?: 'default' );

		return $lang->sprintfDate( $dateFormat, $timestampMw );
	}

	/**
	 * Helper function for converting UTC timezone to a user's timezone
	 *
	 * @param User
	 * @param string
	 * @param int $format output format
	 *
	 * @return string
	 */
	public static function getUserLocalTime( User $user, $ts, $format = TS_MW ) {
		$timestamp = new MWTimestamp( $ts );
		$timestamp->offsetForUser( $user );

		return $timestamp->getTimestamp( $format );
	}

	/**
	 * @param string $type
	 * @return bool Whether a notification type can be an expandable bundle
	 */
	public static function isBundleExpandable( $type ) {
		global $wgEchoNotifications;
		return isset( $wgEchoNotifications[$type]['bundle']['expandable'] ) && $wgEchoNotifications[$type]['bundle']['expandable'];
	}

}