mediawiki-extensions-Echo/modules/model/mw.echo.dm.SeenTimeModel.js
Matthew Flaschen c0a464e425 BREAKING CHANGE: More ISO 8601 for seen time
* Add ISO 8601 date format to notification output

  This is actually supposed to be the only output date format used,
  per https://www.mediawiki.org/wiki/API:Data_formats#Timestamps , but
  I'm not doing anything to deprecate the others right now.

* Change wgEchoSeenTime to use ISO 8601.  mwgrep and extension grep do
  not show any usages.  However, since it is a breaking change, to
  minimize disruption, I'm also using this opportunity to change
  'message' to 'notice'.

* Remove wgEchoInitialNotifCount.  I was going to also change 'message'
  to 'notice' here too, but then I saw it was totally unused.
  (It was read in Echo to populate a JS variable, but then it was
  unused.)

* Make sure the Special:Notifications page aggregation by days is
  done by local days, even though the timestamp per item is still
  UTC. This is to make sure the days are displayed correctly in
  the local timezone.

* Change all reverse sorting callbacks to handle comparisons of
  ISO 8601.

Bug: T141413
Change-Id: I20271345c7d350dc3e7f467288e5cdc98e6250cc
2016-07-27 02:42:08 +00:00

100 lines
2.4 KiB
JavaScript

( function ( mw ) {
/**
* SeenTime model for Echo notifications
*
* @param {Object} [config] Configuration
* @cfg {string|string[]} [types='alert','message'] The types of notifications
* that this model handles
*/
mw.echo.dm.SeenTimeModel = function MwEchoSeenTimeModel( config ) {
var originalSeenTime;
config = config || {};
// Mixin constructor
OO.EventEmitter.call( this );
this.types = [ 'alert', 'message' ];
if ( config.types ) {
this.types = Array.isArray( config.types ) ? config.types : [ config.types ];
}
originalSeenTime = mw.config.get( 'wgEchoSeenTime' ) || {};
this.seenTime = {
local: {
alert: originalSeenTime.alert,
message: originalSeenTime.notice
}
};
};
/* Initialization */
OO.initClass( mw.echo.dm.SeenTimeModel );
OO.mixinClass( mw.echo.dm.SeenTimeModel, OO.EventEmitter );
/* Events */
/**
* @event update
* @param {string} source The source that updated its seenTime
* @param {string} time Seen time, as a full UTC ISO 8601 timestamp.
*
* Seen time has been updated for the given source
*/
/* Methods */
/**
* Get the seenTime value for the source
*
* @param {string} source Source name
* @return {string} Seen time, as a full UTC ISO 8601 timestamp.
*/
mw.echo.dm.SeenTimeModel.prototype.getSeenTime = function ( source ) {
source = source || 'local';
return ( this.seenTime[ source ] && this.seenTime[ source ][ this.getTypes()[ 0 ] ] ) || 0;
};
/**
* Set the seen time value for the source
*
* @private
* @param {string} [source='local'] Given source
* @param {string} time Seen time, as a full UTC ISO 8601 timestamp.
* @fires update
*/
mw.echo.dm.SeenTimeModel.prototype.setSeenTimeForSource = function ( source, time ) {
var model = this,
hasChanged = false;
source = source || 'local';
this.seenTime[ source ] = this.seenTime[ source ] || {};
this.getTypes().forEach( function ( type ) {
if ( model.seenTime[ source ][ type ] !== time ) {
model.seenTime[ source ][ type ] = time;
hasChanged = true;
}
} );
if ( hasChanged ) {
this.emit( 'update', source, time );
}
};
/**
* Get the types associated with this model
*
* @private
* @return {string[]} Types for this model; an array of 'alert', 'message' or both.
*/
mw.echo.dm.SeenTimeModel.prototype.getTypes = function () {
return this.types;
};
} )( mediaWiki );