2 * Behavior related to the temporary user banner, specifically
3 * the tooltip. Design: https://phabricator.wikimedia.org/T330510
10 const config = require( './config.json' ),
11 $tempUserBannerEl = $( '.mw-temp-user-banner ' ),
12 $tempUserBannerTooltipEl = $( '.mw-temp-user-banner-tooltip ' ),
13 $tempUserBannerTooltipButtonEl = $( '#mw-temp-user-banner-tooltip-button' ),
14 TTL_DAY_MS = 86400000;
17 * Get the time since the account was created
20 * @return {number} The duration since it was created until "now" in milliseconds
22 function getTemporaryAccountDurationMs() {
23 return Date.now() - mw.user.getFirstRegistration().getTime();
27 * Whether the account will expire within the period defined
28 * by notifyBeforeExpirationDays in AutoCreateTempUser configuration
33 function isWithinExpirationNotificationPeriod() {
34 const expirationDurationMs = TTL_DAY_MS * config.AutoCreateTempUser.expireAfterDays;
35 const notificationDurationMs = TTL_DAY_MS * config.AutoCreateTempUser.notifyBeforeExpirationDays;
36 return getTemporaryAccountDurationMs() > ( expirationDurationMs - notificationDurationMs );
40 * Get the time until the account will expire
43 * @return {number} The duration from "now" until the account expiration in milliseconds
45 function getTimeToExpirationMs() {
46 return ( TTL_DAY_MS * config.AutoCreateTempUser.expireAfterDays ) - getTemporaryAccountDurationMs();
50 * Whether the expiration tooltip should be shown. The following criteria
52 * - Expiration is set in AutoCreateTempUser configuration (expireAfterDays)
53 * - The evaluated temporary account expiration is within the notification period
54 * as defined in AutoCreateTempUser configuration (notifyBeforeExpirationDays)
55 * - The user didn't proactively dismiss the tooltip clicking on the close button,
61 function shouldShowExpirationAlert() {
62 const tempUserExpirationAlertDismissed = localStorage.getItem( 'tempUserExpirationAlertDismissed' );
63 const expirationIsSet = typeof config.AutoCreateTempUser.expireAfterDays === 'number';
64 const notifyBeforeExpirationIsSet = typeof config.AutoCreateTempUser.notifyBeforeExpirationDays === 'number';
65 return expirationIsSet &&
66 notifyBeforeExpirationIsSet &&
67 !tempUserExpirationAlertDismissed &&
68 isWithinExpirationNotificationPeriod();
72 * Retrieve the expiration descriptive text
77 function getExpirationDescriptionText() {
78 const timeToExpirationDays = Math.floor( getTimeToExpirationMs() / TTL_DAY_MS );
79 const params = [ timeToExpirationDays ];
80 let key = 'temp-user-banner-tooltip-description-expiration-soon';
81 if ( timeToExpirationDays < 1 ) {
85 // Messages that can be used here:
86 // * temp-user-banner-tooltip-description-expiration-soon
87 // * temp-user-banner-tooltip-description-expiration-soon-day
88 return mw.message( key, params ).parseDom();
92 * Retrieve the tooltip content
95 * @param {boolean} shouldShowExpiration
98 function getTooltipContent( shouldShowExpiration ) {
99 const descriptionText = shouldShowExpiration ?
100 getExpirationDescriptionText() :
101 mw.message( 'temp-user-banner-tooltip-description-learn-more' ).parseDom();
102 return $( '<div>' ).append(
103 $( '<p>' ).append( descriptionText ),
104 $( '<p>' ).append( mw.message( 'temp-user-banner-tooltip-description-login' ).parseDom() )
109 * Builds a tooltip which is part of a banner for temporary account (IP masking) users.
112 * @param {jQuery} $bannerEl
113 * @param {jQuery} $tooltipEl
114 * @param {jQuery} $buttonEl
116 function initTempUserBannerTooltip( $bannerEl, $tooltipEl, $buttonEl ) {
117 if ( !$bannerEl.length || !$tooltipEl.length || !$buttonEl.length ) {
124 * Creates the tooltip if it doesn't already exist and toggles it.
128 function showTooltip() {
129 return mw.loader.using( [
133 'oojs-ui.styles.icons-interactions'
135 const shouldShowExpiration = shouldShowExpirationAlert();
137 popup = new OO.ui.PopupWidget( {
141 label: mw.msg( 'temp-user-banner-tooltip-title' ),
142 $content: getTooltipContent( shouldShowExpiration ),
143 autoClose: !shouldShowExpiration,
144 $autoCloseIgnore: $buttonEl,
145 $floatableContainer: $tooltipEl,
148 $tooltipEl.append( popup.$element );
150 if ( shouldShowExpiration ) {
151 popup.on( 'toggle', ( visible ) => {
153 localStorage.setItem( 'tempUserExpirationAlertDismissed', true );
163 $buttonEl.on( 'click', showTooltip );
165 if ( shouldShowExpirationAlert() ) {
170 initTempUserBannerTooltip( $tempUserBannerEl, $tempUserBannerTooltipEl, $tempUserBannerTooltipButtonEl );