Browse Source

Supported stickers real-time update

master
Igor Zhukov 9 years ago
parent
commit
64efe88880
  1. 5
      app/js/directives.js
  2. 8
      app/js/message_composer.js
  3. 182
      app/js/services.js

5
app/js/directives.js

@ -1493,6 +1493,11 @@ angular.module('myApp.directives', ['myApp.filters']) @@ -1493,6 +1493,11 @@ angular.module('myApp.directives', ['myApp.filters'])
}
});
$scope.$on('stickers_changed', function () {
emojiTooltip.onStickersChanged();
});
var composerEmojiPanel;
if (emojiPanel) {
composerEmojiPanel = new EmojiPanel(emojiPanel, {

8
app/js/message_composer.js

@ -364,7 +364,7 @@ EmojiTooltip.prototype.createTooltip = function () { @@ -364,7 +364,7 @@ EmojiTooltip.prototype.createTooltip = function () {
EmojiTooltip.prototype.selectCategory = function (cat, force) {
if (this.cat === cat && !force) {
if (!this.tab && this.cat === cat && !force) {
return false;
}
$('.active', this.categoriesEl).removeClass('active');
@ -566,6 +566,12 @@ EmojiTooltip.prototype.onStickersScroll = function (scrollable, scrollTop) { @@ -566,6 +566,12 @@ EmojiTooltip.prototype.onStickersScroll = function (scrollable, scrollTop) {
this.activateStickerCategory();
};
EmojiTooltip.prototype.onStickersChanged = function () {
if (this.tab) {
this.updateStickersContents(true);
}
};
EmojiTooltip.prototype.activateStickerCategory = function () {
var categoriesEl = this.categoriesEl[1];
var categoryEl = categoriesEl.childNodes[this.cat];

182
app/js/services.js

@ -2285,26 +2285,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2285,26 +2285,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
var currentStickerSets = [];
$rootScope.$on('apiUpdate', function (e, update) {
var rewriteCached = false;
if (update._ == 'updateStickerSets') {
getStickers(true);
return true;
}
if (update._ != 'updateNewStickerSet' &&
if (update._ != 'updateStickerSets' &&
update._ != 'updateNewStickerSet' &&
update._ != 'updateDelStickerSet' &&
update._ != 'updateStickerSetsOrder') {
return false;
}
return Storage.get('all_stickers').then(function (stickers) {
if (stickers &&
stickers.layer == Config.Schema.API.layer) {
if (!stickers ||
stickers.layer != Config.Schema.API.layer) {
$rootScope.$broadcast('stickers_changed');
}
switch (update._) {
case 'updateNewStickerSet':
var fullSet = update.stickerset;
var set = fullSet.set;
set.pFlags.installed = true;
stickers.fullSets[set.id] = fullSet;
var pos = false;
for (var i = 0, len = stickers.sets.length; i < len; i++) {
if (stickers.sets[i].id == set.id) {
@ -2315,12 +2311,17 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2315,12 +2311,17 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
if (pos !== false) {
stickers.sets.splice(pos, 1);
}
set.pFlags.installed = true;
stickers.sets.unshift(set);
stickers.fullSets[set.id] = fullSet;
break;
case 'updateDelStickerSet':
var set;
for (var i = 0, len = stickers.sets.length; i < len; i++) {
if (stickers.sets[i].id == update.id) {
set = stickers.sets[i];
if (set.id == update.id) {
set.pFlags.installed = false;
stickers.sets.splice(i, 1);
break;
}
@ -2335,21 +2336,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2335,21 +2336,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
});
break;
}
stickers.hash = getStickerSetsHash(stickers.sets);
stickers.date = 0;
Storage.set({all_stickers: stickers}).then(function () {
getStickers(true)
$rootScope.$broadcast('stickers_changed');
});
}
});
});
return {
start: start,
getStickers: getStickers,
openStickersetLink: openStickersetLink,
openStickerset: openStickerset,
installStickerset: installStickerset,
pushPopularSticker: pushPopularSticker,
getStickers: getStickers,
getStickerset: getStickerset
};
@ -2360,46 +2362,38 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2360,46 +2362,38 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
}
}
function getPopularStickers () {
return Storage.get('stickers_popular').then(function (popStickers) {
var result = [];
var i, len, docID;
if (popStickers && popStickers.length) {
for (i = 0, len = popStickers.length; i < len; i++) {
docID = popStickers[i][0];
if (AppDocsManager.hasDoc(docID)) {
result.push({id: docID, rate: popStickers[i][1]});
function getStickers (force) {
return Storage.get('all_stickers').then(function (stickers) {
var layer = Config.Schema.API.layer;
if (stickers.layer != layer) {
stickers = false;
}
if (stickers && stickers.date > tsNow(true) && !force) {
return processRawStickers(stickers);
}
};
return result;
});
return MtpApiManager.invokeApi('messages.getAllStickers', {
hash: stickers && stickers.hash || ''
}).then(function (newStickers) {
var notModified = newStickers._ == 'messages.allStickersNotModified';
if (notModified) {
newStickers = stickers;
}
newStickers.date = tsNow(true) + 3600;
newStickers.layer = layer;
delete newStickers._;
function pushPopularSticker (id) {
getPopularStickers().then(function (popularStickers) {
var exists = false;
var count = popularStickers.length;
var result = [];
for (var i = 0; i < count; i++) {
if (popularStickers[i].id == id) {
exists = true;
popularStickers[i].rate++;
}
result.push([popularStickers[i].id, popularStickers[i].rate]);
if (notModified) {
Storage.set({all_stickers: newStickers});
return processRawStickers(newStickers);
}
if (exists) {
result.sort(function (a, b) {
return b[1] - a[1];
return getStickerSets(newStickers, stickers && stickers.fullSets).then(function () {
Storage.set({all_stickers: newStickers});
return processRawStickers(newStickers);
});
} else {
if (result.length > 15) {
result = result.slice(0, 15);
}
result.push([id, 1]);
}
ConfigStorage.set({stickers_popular: result});
});
})
}
function processRawStickers(stickers) {
@ -2443,49 +2437,13 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2443,49 +2437,13 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
docIDs: docIDs
});
}
console.log('stickers', resultStickersets);
return resultStickersets;
});
}
function getStickers (force) {
return Storage.get('all_stickers').then(function (stickers) {
var layer = Config.Schema.API.layer;
if (stickers.layer != layer) {
stickers = false;
}
if (stickers && stickers.date > tsNow(true) && !force) {
return processRawStickers(stickers);
}
return MtpApiManager.invokeApi('messages.getAllStickers', {
hash: stickers && stickers.hash || ''
}).then(function (newStickers) {
var notModified = newStickers._ == 'messages.allStickersNotModified';
if (notModified) {
newStickers = stickers;
}
newStickers.date = tsNow(true) + 3600;
newStickers.layer = layer;
delete newStickers._;
if (notModified) {
Storage.set({all_stickers: newStickers});
return processRawStickers(newStickers);
}
return getStickerSets(newStickers).then(function () {
Storage.set({all_stickers: newStickers});
return processRawStickers(newStickers);
});
});
})
}
function getStickerSets (allStickers) {
function getStickerSets (allStickers, prevCachedSets) {
var promises = [];
var cachedSets = allStickers.fullSets || {};
var cachedSets = prevCachedSets || allStickers.fullSets || {};
allStickers.fullSets = {};
angular.forEach(allStickers.sets, function (shortSet) {
var fullSet = cachedSets[shortSet.id];
@ -2507,6 +2465,48 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2507,6 +2465,48 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
return $q.all(promises);
}
function getPopularStickers () {
return Storage.get('stickers_popular').then(function (popStickers) {
var result = [];
var i, len, docID;
if (popStickers && popStickers.length) {
for (i = 0, len = popStickers.length; i < len; i++) {
docID = popStickers[i][0];
if (AppDocsManager.hasDoc(docID)) {
result.push({id: docID, rate: popStickers[i][1]});
}
}
};
return result;
});
}
function pushPopularSticker (id) {
getPopularStickers().then(function (popularStickers) {
var exists = false;
var count = popularStickers.length;
var result = [];
for (var i = 0; i < count; i++) {
if (popularStickers[i].id == id) {
exists = true;
popularStickers[i].rate++;
}
result.push([popularStickers[i].id, popularStickers[i].rate]);
}
if (exists) {
result.sort(function (a, b) {
return b[1] - a[1];
});
} else {
if (result.length > 15) {
result = result.slice(0, 15);
}
result.push([id, 1]);
}
ConfigStorage.set({stickers_popular: result});
});
}
function getStickerset (inputStickerset) {
return MtpApiManager.invokeApi('messages.getStickerSet', {
stickerset: inputStickerset
@ -2561,6 +2561,18 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) @@ -2561,6 +2561,18 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
windowClass: 'stickerset_modal_window mobile_modal'
});
}
function getStickerSetsHash (stickerSets) {
var acc = 0, set;
for (var i = 0; i < stickerSets.length; i++) {
set = stickerSets[i];
if (set.pFlags.disabled || !set.pFlags.installed) {
continue;
}
acc = ((acc * 20261) + 0x80000000 + set.hash) % 0x80000000;
}
return acc;
}
})
.service('ApiUpdatesManager', function ($rootScope, MtpNetworkerFactory, AppUsersManager, AppChatsManager, AppPeersManager, MtpApiManager) {

Loading…
Cancel
Save