EXPORTED_SYMBOLS = ["module"];
var module = function (application) {
var log = function (message) {
application.logger.trace("[Notifications] " + message);
}
;
const OWNER_NAME = "yb-vkontakte";
const TYPES = {
"new_message": 0,
"friend_request": 1};
const NOTIFIER_MESSAGES = {
ADD: "yb:notification:add",
GET: "yb:notification:message:" + OWNER_NAME};
const MAILMAN_GROUP = "vk-notifications";
var Notifications = function () {
this._observer = null;
this.init();
}
;
Notifications.prototype = {
constructor: Notifications,
init: function vk_Notifications_init() {
var self = this;
var mailman = application.mailman;
mailman.addSubscriber({
subject: "new-messages",
group: MAILMAN_GROUP,
subscriber: {
receive: function ({body: data}) {
if (application.api.Settings.getValue("disable-notifications"))
{
return;
}

if (0 === data.total)
return;
self.notifyAboutNewMessages(data);
}
}});
mailman.addSubscriber({
subject: "new-friends",
group: MAILMAN_GROUP,
subscriber: {
receive: function ({body: data}) {
if (application.api.Settings.getValue("disable-notifications"))
{
return;
}

if (0 === data.total)
return;
self.notifyAboutFriendRequests(data);
}
}});
this._observerService.addObserver(this._observer = {
observe: function (subject, topic, data) {
log("Caught message w/ topic " + topic + " and data\n" + data);
data = JSON.parse(data);
self.handleNotificationEvent(data.subject,data.body);
}
},NOTIFIER_MESSAGES.GET,false);
}
,
finalize: function vk_Notifications_finalize() {
application.mailman.removeSubscribersGroup(MAILMAN_GROUP);
if (this._observer)
{
this._observerService.removeObserver(this._observer,NOTIFIER_MESSAGES.GET);
this._observer = null;
}

}
,
handleNotificationEvent: function vk_Notifications_handleNotificationEvent(subject, body) {
var mailman = application.mailman;
switch (subject) {
case "open_widget_settings":
mailman.send("please-open-settings");
break;
case "open_page":
let url = body;
mailman.send({
subject: "please-open-url",
body: url});
break;
case "mark_read":
let uid = body;
break;
default:
log("Unexpected message subject.");
break;
}

}
,
notifyAboutNewMessages: function vk_Notifications_notifyAboutNewMessages(data) {
var messages = data.data;
messages = messages.filter(this._isNewMessage);
if (0 === messages.length)
{
return;
}

for (let i = messages.length - 1;i >= 0;i--) {
let message = messages[i];
this.notifyAboutSingleNewMessage(message,message._profile);
}

}
,
notifyAboutSingleNewMessage: function vk_Notifications_notifyAboutSingleNewMessage(messageData, userData) {
var username = userData.first_name + " " + userData.last_name;
this.notify("message_" + messageData.mid,{
type: TYPES.new_message,
uid: messageData.uid,
url: "http://vk.com/im?sel=" + messageData.uid,
name: username,
icon: userData.photo,
text: messageData.body,
status: 1 === userData.online,
locals: this._localizedStrings});
}
,
notifyAboutFriendRequests: function vk_Notifications_notifyAboutFriendRequests(data) {
var users = data.data;
for (let i = 0;i < data.received;i++) {
let user = users[i];
this.notifyAboutSingleFriendRequest(user,user._profile);
}

}
,
notifyAboutSingleFriendRequest: function vk_Notifications_notifyAboutSingleFriendRequest(requestData, userData) {
var localizedStrings = this._localizedStrings;
var requestTextEntityName = 1 === userData.sex ? "vkontakte.notification.friend_request.female" : "vkontakte.notification.friend_request.male";
localizedStrings.friend_request = application._templater.getString(requestTextEntityName);
var uid = requestData.uid;
var username = userData.first_name + " " + userData.last_name;
this.notify("request_" + uid,{
type: TYPES.friend_request,
url: "http://vk.com/id" + uid,
name: username,
icon: userData.photo,
status: 1 === userData.online,
locals: localizedStrings});
}
,
notify: function vk_Notifications_notify(eventId, eventData) {
var alreadyNotifiedAbout = this._cache.get("ids");
if (null === alreadyNotifiedAbout)
{
alreadyNotifiedAbout = [];
}
 else
{
alreadyNotifiedAbout = JSON.parse(alreadyNotifiedAbout);
if (- 1 !== alreadyNotifiedAbout.indexOf(eventId))
{
return;
}

}

alreadyNotifiedAbout.push(eventId);
var data = {
owner: OWNER_NAME,
id: OWNER_NAME + "_" + Date.now(),
page: this._pagePath,
data: eventData};
this._observerService.notifyObservers(null,NOTIFIER_MESSAGES.ADD,JSON.stringify(data));
this._cache.put("ids",JSON.stringify(alreadyNotifiedAbout));
}
,
_isNewMessage: function vk_Notifications__isNewMessage(message) {
const MAX_AGE = 24 * 60 * 60;
return Math.ceil(Date.now() / 1000) - Number(message.date) < MAX_AGE;
}
,
__pagePath: null,
get _pagePath() {
return this.__pagePath ? this.__pagePath : (this.__pagePath = application.api.Package.resolvePath("/content/notification/index.html"));
}
,
__observerService: null,
get _observerService() {
return this.__observerService ? this.__observerService : (this.__observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService));
}
,
__cache: null,
get _cache() {
return this.__cache ? this.__cache : (this.__cache = new application.Cache("notifications.json"));
}
,
__localizedStrings: null,
get _localizedStrings() {
return this.__localizedStrings ? this.__localizedStrings : (this.__localizedStrings = {
settings: application._templater.getString("vkontakte.notification.open_settings"),
close: application._templater.getString("vkontakte.notification.close")});
}
};
return new Notifications();
}
;
