Browse Source

Seq holes improved

master
Igor Zhukov 11 years ago
parent
commit
ec4a2b55ce
  1. 62
      app/js/controllers.js
  2. 257
      app/js/services.js

62
app/js/controllers.js

@ -1575,16 +1575,17 @@ angular.module('myApp.controllers', [])
}); });
function onStatedMessage (statedMessage) { function onStatedMessage (statedMessage) {
AppUsersManager.saveApiUsers(statedMessage.users); ApiUpdatesManager.processUpdateMessage({
AppChatsManager.saveApiChats(statedMessage.chats); _: 'updates',
users: statedMessage.users,
if (ApiUpdatesManager.saveSeq(statedMessage.seq)) { chats: statedMessage.chats,
ApiUpdatesManager.saveUpdate({ seq: statedMessage.seq,
updates: [{
_: 'updateNewMessage', _: 'updateNewMessage',
message: statedMessage.message, message: statedMessage.message,
pts: statedMessage.pts pts: statedMessage.pts
}]
}); });
}
$rootScope.$broadcast('history_focus', {peerString: $scope.chatFull.peerString}); $rootScope.$broadcast('history_focus', {peerString: $scope.chatFull.peerString});
} }
@ -1618,16 +1619,17 @@ angular.module('myApp.controllers', [])
user_id: {_: 'inputUserContact', user_id: userID}, user_id: {_: 'inputUserContact', user_id: userID},
fwd_limit: 100 fwd_limit: 100
}).then(function (addResult) { }).then(function (addResult) {
AppUsersManager.saveApiUsers(addResult.users); ApiUpdatesManager.processUpdateMessage({
AppChatsManager.saveApiChats(addResult.chats); _: 'updates',
seq: addResult.seq,
if (ApiUpdatesManager.saveSeq(addResult.seq)) { users: addResult.users,
ApiUpdatesManager.saveUpdate({ chats: addResult.chats,
updates: [{
_: 'updateNewMessage', _: 'updateNewMessage',
message: addResult.message, message: addResult.message,
pts: addResult.pts pts: addResult.pts
}]
}); });
}
}); });
}); });
@ -1748,12 +1750,15 @@ angular.module('myApp.controllers', [])
}).then(function (updateResult) { }).then(function (updateResult) {
AppUsersManager.saveApiUsers(updateResult.users); AppUsersManager.saveApiUsers(updateResult.users);
MtpApiManager.getUserID().then(function (id) { MtpApiManager.getUserID().then(function (id) {
ApiUpdatesManager.saveUpdate({ ApiUpdatesManager.processUpdateMessage({
_: 'updateShort',
update: {
_: 'updateUserPhoto', _: 'updateUserPhoto',
user_id: id, user_id: id,
date: tsNow(true), date: tsNow(true),
photo: AppUsersManager.getUser(id).photo, photo: AppUsersManager.getUser(id).photo,
previous: true previous: true
}
}); });
$scope.photo = AppUsersManager.getUserPhoto(id, 'User'); $scope.photo = AppUsersManager.getUserPhoto(id, 'User');
}); });
@ -1770,12 +1775,15 @@ angular.module('myApp.controllers', [])
crop: {_: 'inputPhotoCropAuto'} crop: {_: 'inputPhotoCropAuto'}
}).then(function (updateResult) { }).then(function (updateResult) {
MtpApiManager.getUserID().then(function (id) { MtpApiManager.getUserID().then(function (id) {
ApiUpdatesManager.saveUpdate({ ApiUpdatesManager.processUpdateMessage({
_: 'updateShort',
update: {
_: 'updateUserPhoto', _: 'updateUserPhoto',
user_id: id, user_id: id,
date: tsNow(true), date: tsNow(true),
photo: updateResult, photo: updateResult,
previous: true previous: true
}
}); });
$scope.photo = AppUsersManager.getUserPhoto(id, 'User'); $scope.photo = AppUsersManager.getUserPhoto(id, 'User');
}); });
@ -2024,16 +2032,17 @@ angular.module('myApp.controllers', [])
title: $scope.group.name, title: $scope.group.name,
users: inputUsers users: inputUsers
}).then(function (createdResult) { }).then(function (createdResult) {
AppUsersManager.saveApiUsers(createdResult.users); ApiUpdatesManager.processUpdateMessage({
AppChatsManager.saveApiChats(createdResult.chats); _: 'updates',
seq: createdResult.seq,
if (ApiUpdatesManager.saveSeq(createdResult.seq)) { users: createdResult.users,
ApiUpdatesManager.saveUpdate({ chats: createdResult.chats,
updates: [{
_: 'updateNewMessage', _: 'updateNewMessage',
message: createdResult.message, message: createdResult.message,
pts: createdResult.pts pts: createdResult.pts
}]
}); });
}
var peerString = AppChatsManager.getChatString(createdResult.message.to_id.chat_id); var peerString = AppChatsManager.getChatString(createdResult.message.to_id.chat_id);
$rootScope.$broadcast('history_focus', {peerString: peerString}); $rootScope.$broadcast('history_focus', {peerString: peerString});
@ -2063,16 +2072,17 @@ angular.module('myApp.controllers', [])
chat_id: $scope.chatID, chat_id: $scope.chatID,
title: $scope.group.name title: $scope.group.name
}).then(function (editResult) { }).then(function (editResult) {
AppUsersManager.saveApiUsers(editResult.users); ApiUpdatesManager.processUpdateMessage({
AppChatsManager.saveApiChats(editResult.chats); _: 'updates',
seq: editResult.seq,
if (ApiUpdatesManager.saveSeq(editResult.seq)) { users: editResult.users,
ApiUpdatesManager.saveUpdate({ chats: editResult.chats,
updates: [{
_: 'updateNewMessage', _: 'updateNewMessage',
message: editResult.message, message: editResult.message,
pts: editResult.pts pts: editResult.pts
}]
}); });
}
var peerString = AppChatsManager.getChatString($scope.chatID); var peerString = AppChatsManager.getChatString($scope.chatID);
$rootScope.$broadcast('history_focus', {peerString: peerString}); $rootScope.$broadcast('history_focus', {peerString: peerString});

257
app/js/services.js

@ -1170,12 +1170,13 @@ angular.module('myApp.services', [])
return MtpApiManager.invokeApi('messages.deleteMessages', { return MtpApiManager.invokeApi('messages.deleteMessages', {
id: messageIDs id: messageIDs
}).then(function (deletedMessageIDs) { }).then(function (deletedMessageIDs) {
ApiUpdatesManager.processUpdateMessage({
ApiUpdatesManager.saveUpdate({ _: 'updateShort',
update: {
_: 'updateDeleteMessages', _: 'updateDeleteMessages',
messages: deletedMessageIDs messages: deletedMessageIDs
}
}); });
return deletedMessageIDs; return deletedMessageIDs;
}); });
} }
@ -1361,22 +1362,25 @@ angular.module('myApp.services', [])
peer: inputPeer, peer: inputPeer,
message: text, message: text,
random_id: randomID random_id: randomID
}, sentRequestOptions).then(function (result) { }, sentRequestOptions).then(function (sentMessage) {
if (ApiUpdatesManager.saveSeq(result.seq)) { message.date = sentMessage.date;
ApiUpdatesManager.saveUpdate({ message.id = sentMessage.id;
ApiUpdatesManager.processUpdateMessage({
_: 'updates',
users: [],
chats: [],
seq: sentMessage.seq,
updates: [{
_: 'updateMessageID', _: 'updateMessageID',
random_id: randomIDS, random_id: randomIDS,
id: result.id id: sentMessage.id
}); }, {
message.date = result.date;
message.id = result.id;
ApiUpdatesManager.saveUpdate({
_: 'updateNewMessage', _: 'updateNewMessage',
message: message, message: message,
pts: result.pts pts: sentMessage.pts
}]
}); });
}
}, function (error) { }, function (error) {
toggleError(true); toggleError(true);
})['finally'](function () { })['finally'](function () {
@ -1503,25 +1507,26 @@ angular.module('myApp.services', [])
peer: inputPeer, peer: inputPeer,
media: inputMedia, media: inputMedia,
random_id: randomID random_id: randomID
}).then(function (result) { }).then(function (statedMessage) {
if (ApiUpdatesManager.saveSeq(result.seq)) { message.date = statedMessage.message.date;
ApiUpdatesManager.saveUpdate({ message.id = statedMessage.message.id;
message.media = statedMessage.message.media;
ApiUpdatesManager.processUpdateMessage({
_: 'updates',
users: statedMessage.users,
chats: statedMessage.chats,
seq: statedMessage.seq,
updates: [{
_: 'updateMessageID', _: 'updateMessageID',
random_id: randomIDS, random_id: randomIDS,
id: result.message.id id: statedMessage.message.id
}); }, {
message.date = result.message.date;
message.id = result.message.id;
message.media = result.message.media;
ApiUpdatesManager.saveUpdate({
_: 'updateNewMessage', _: 'updateNewMessage',
message: message, message: message,
pts: result.pts pts: statedMessage.pts
}]
}); });
}
}, function (error) { }, function (error) {
toggleError(true); toggleError(true);
}); });
@ -1617,24 +1622,26 @@ angular.module('myApp.services', [])
peer: inputPeer, peer: inputPeer,
media: inputMedia, media: inputMedia,
random_id: randomID random_id: randomID
}).then(function (result) { }).then(function (statedMessage) {
if (ApiUpdatesManager.saveSeq(result.seq)) { message.date = statedMessage.message.date;
ApiUpdatesManager.saveUpdate({ message.id = statedMessage.message.id;
message.media = statedMessage.message.media;
ApiUpdatesManager.processUpdateMessage({
_: 'updates',
users: statedMessage.users,
chats: statedMessage.chats,
seq: statedMessage.seq,
updates: [{
_: 'updateMessageID', _: 'updateMessageID',
random_id: randomIDS, random_id: randomIDS,
id: result.message.id id: statedMessage.message.id
}); }, {
message.date = result.message.date;
message.id = result.message.id;
message.media = result.message.media;
ApiUpdatesManager.saveUpdate({
_: 'updateNewMessage', _: 'updateNewMessage',
message: message, message: message,
pts: result.pts pts: statedMessage.pts
}]
}); });
}
}, function (error) { }, function (error) {
toggleError(true); toggleError(true);
}); });
@ -1656,22 +1663,23 @@ angular.module('myApp.services', [])
return MtpApiManager.invokeApi('messages.forwardMessages', { return MtpApiManager.invokeApi('messages.forwardMessages', {
peer: AppPeersManager.getInputPeerByID(peerID), peer: AppPeersManager.getInputPeerByID(peerID),
id: msgIDs id: msgIDs
}).then(function (forwardResult) { }).then(function (statedMessages) {
AppUsersManager.saveApiUsers(forwardResult.users); var updates = [];
AppChatsManager.saveApiChats(forwardResult.chats); angular.forEach(statedMessages.messages, function(apiMessage) {
updates.push({
if (ApiUpdatesManager.saveSeq(forwardResult.seq)) {
angular.forEach(forwardResult.messages, function(apiMessage) {
ApiUpdatesManager.saveUpdate({
_: 'updateNewMessage', _: 'updateNewMessage',
message: apiMessage, message: apiMessage,
pts: forwardResult.pts pts: statedMessages.pts
}); });
}); });
}
ApiUpdatesManager.processUpdateMessage({
_: 'updates',
users: statedMessages.users,
chats: statedMessages.chats,
seq: statedMessages.seq,
updates: updates
});
}); });
}; };
@ -1686,9 +1694,12 @@ angular.module('myApp.services', [])
historyStorage = historiesStorage[peerID], historyStorage = historiesStorage[peerID],
i; i;
ApiUpdatesManager.saveUpdate({ ApiUpdatesManager.processUpdateMessage({
_: 'updateShort',
update: {
_: 'updateDeleteMessages', _: 'updateDeleteMessages',
messages: [tempID] messages: [tempID]
}
}); });
for (i = 0; i < historyStorage.pending.length; i++) { for (i = 0; i < historyStorage.pending.length; i++) {
@ -2828,15 +2839,28 @@ angular.module('myApp.services', [])
.service('ApiUpdatesManager', function ($rootScope, MtpNetworkerFactory, AppUsersManager, AppChatsManager, AppPeersManager, MtpApiManager) { .service('ApiUpdatesManager', function ($rootScope, MtpNetworkerFactory, AppUsersManager, AppChatsManager, AppPeersManager, MtpApiManager) {
var curState = {invalid: true}; var isSynchronizing = true,
getDifferencePending = false,
curState = {},
pendingUpdates = {};
function processUpdateMessage (updateMessage) { function popPendingUpdate () {
if (curState.invalid) { var updateMessage = pendingUpdates[curState.seq + 1];
return false; if (updateMessage && processUpdateMessage(updateMessage)) {
delete pendingUpdates[curState.seq + 1];
}
} }
function forceGetDifference () {
if (!isSynchronizing) {
getDifference();
}
}
function processUpdateMessage (updateMessage) {
if (updateMessage.seq) { if (updateMessage.seq) {
if (!saveSeq(updateMessage.seq, updateMessage.seq_start)) { if (!saveSeq(updateMessage.seq, updateMessage.seq_start)) {
pendingUpdates[updateMessage.seq_start || updateMessage.seq] = updateMessage;
return false; return false;
} }
if (updateMessage.date) { if (updateMessage.date) {
@ -2847,47 +2871,18 @@ angular.module('myApp.services', [])
switch (updateMessage._) { switch (updateMessage._) {
case 'updatesTooLong': case 'updatesTooLong':
getDifference(); forceGetDifference();
break; break;
case 'updateShort': case 'updateShort':
saveUpdate(updateMessage.update); saveUpdate(updateMessage.update);
break; break;
case 'updatesCombined':
case 'updates':
AppUsersManager.saveApiUsers(updateMessage.users);
AppChatsManager.saveApiChats(updateMessage.chats);
var i, update, message;
for (var i = 0; i < updateMessage.updates.length; i++) {
update = updateMessage.updates[i];
switch (update._) {
case 'updateNewMessage':
message = update.message;
if (message.from_id && !AppUsersManager.hasUser(message.from_id)) {
console.log('User not found', message.from_id, 'getDiff');
getDifference();
return false;
}
if (message.to_id.chat_id && !AppChatsManager.hasChat(message.to_id.chat_id)) {
console.log('Chat not found', message.to_id.chat_id, 'getDiff');
getDifference();
return false;
}
break;
}
}
angular.forEach(updateMessage.updates, function (update) {
saveUpdate(update);
});
break;
case 'updateShortMessage': case 'updateShortMessage':
if (!AppUsersManager.hasUser(updateMessage.from_id)) { if (!AppUsersManager.hasUser(updateMessage.from_id)) {
console.log('User not found', updateMessage.from_id, 'getDiff'); console.log('User not found', updateMessage.from_id, 'getDiff');
getDifference(); forceGetDifference();
break; break;
} }
saveUpdate({ saveUpdate({
@ -2911,7 +2906,7 @@ angular.module('myApp.services', [])
if (!AppUsersManager.hasUser(updateMessage.from_id) || if (!AppUsersManager.hasUser(updateMessage.from_id) ||
!AppChatsManager.hasChat(updateMessage.chat_id)) { !AppChatsManager.hasChat(updateMessage.chat_id)) {
console.log('User or chat not found', updateMessage.from_id, updateMessage.chat_id, 'getDiff'); console.log('User or chat not found', updateMessage.from_id, updateMessage.chat_id, 'getDiff');
getDifference(); forceGetDifference();
break; break;
} }
saveUpdate({ saveUpdate({
@ -2930,22 +2925,63 @@ angular.module('myApp.services', [])
pts: updateMessage.pts pts: updateMessage.pts
}); });
break; break;
case 'updatesCombined':
case 'updates':
default:
AppUsersManager.saveApiUsers(updateMessage.users);
AppChatsManager.saveApiChats(updateMessage.chats);
var i, update, message;
for (var i = 0; i < updateMessage.updates.length; i++) {
update = updateMessage.updates[i];
switch (update._) {
case 'updateNewMessage':
message = update.message;
if (message.from_id && !AppUsersManager.hasUser(message.from_id)) {
console.log('User not found', message.from_id, 'getDiff');
forceGetDifference();
return false;
}
if (message.to_id.chat_id && !AppChatsManager.hasChat(message.to_id.chat_id)) {
console.log('Chat not found', message.to_id.chat_id, 'getDiff');
forceGetDifference();
return false;
}
break;
}
}
angular.forEach(updateMessage.updates, function (update) {
saveUpdate(update);
});
break;
}
popPendingUpdate();
if (getDifferencePending && curState.seq >= getDifferencePending.seqAwaiting) {
clearTimeout(getDifferencePending.timeout);
getDifferencePending = false;
} }
return true; return true;
} }
function getDifference (force) { function getDifference () {
if (curState.invalid && !force) { isSynchronizing = true;
return false;
if (getDifferencePending) {
clearTimeout(getDifferencePending.timeout);
getDifferencePending = false;
} }
curState.invalid = true;
MtpApiManager.invokeApi('updates.getDifference', {pts: curState.pts, date: curState.date, qts: 0}).then(function (differenceResult) { MtpApiManager.invokeApi('updates.getDifference', {pts: curState.pts, date: curState.date, qts: 0}).then(function (differenceResult) {
if (differenceResult._ == 'updates.differenceEmpty') { if (differenceResult._ == 'updates.differenceEmpty') {
curState.date = differenceResult.date; curState.date = differenceResult.date;
curState.seq = differenceResult.seq; curState.seq = differenceResult.seq;
delete curState.invalid; isSynchronizing = false;
popPendingUpdate();
return false; return false;
} }
@ -2954,7 +2990,7 @@ angular.module('myApp.services', [])
// Should be first because of updateMessageID // Should be first because of updateMessageID
angular.forEach(differenceResult.other_updates, function(update){ angular.forEach(differenceResult.other_updates, function(update){
saveUpdate(update, true); saveUpdate(update);
}); });
angular.forEach(differenceResult.new_messages, function (apiMessage) { angular.forEach(differenceResult.new_messages, function (apiMessage) {
@ -2962,7 +2998,7 @@ angular.module('myApp.services', [])
_: 'updateNewMessage', _: 'updateNewMessage',
message: apiMessage, message: apiMessage,
pts: curState.pts pts: curState.pts
}, true); });
}); });
var nextState = differenceResult.intermediate_state || differenceResult.state; var nextState = differenceResult.intermediate_state || differenceResult.state;
@ -2973,15 +3009,12 @@ angular.module('myApp.services', [])
if (differenceResult._ == 'updates.differenceSlice') { if (differenceResult._ == 'updates.differenceSlice') {
getDifference(true); getDifference(true);
} else { } else {
delete curState.invalid; isSynchronizing = false;
} }
}); });
} }
function saveUpdate (update, force) { function saveUpdate (update) {
if (curState.invalid && !force) {
return false;
}
if (update.pts) { if (update.pts) {
curState.pts = update.pts; curState.pts = update.pts;
} }
@ -2989,23 +3022,31 @@ angular.module('myApp.services', [])
$rootScope.$broadcast('apiUpdate', update); $rootScope.$broadcast('apiUpdate', update);
} }
function saveSeq (seq, seqStart) {
// console.log('saving seq', curState.invalid, seq, seqStart, curState.seq);
if (curState.invalid) {
return false;
}
function saveSeq (seq, seqStart) {
// console.log('saving seq', curState.invalid, seq, seqStart, curState.seq);
seqStart = seqStart || seq; seqStart = seqStart || seq;
if (!seqStart) { if (!seqStart) {
return true; return true;
} }
if (isSynchronizing) {
return false;
}
if (seqStart != curState.seq + 1) { if (seqStart != curState.seq + 1) {
if (seqStart > curState.seq) { if (seqStart > curState.seq) {
console.warn('Seq hole', seqStart, curState.seq); console.warn('Seq hole', seqStart, curState.seq);
if (!getDifferencePending) {
getDifferencePending = {
seqAwaiting: seqStart,
timeout: setTimeout(function () {
getDifference(); getDifference();
}, 5000)
};
}
} }
return false; return false;
} }
@ -3021,13 +3062,13 @@ angular.module('myApp.services', [])
curState.seq = stateResult.seq; curState.seq = stateResult.seq;
curState.pts = stateResult.pts; curState.pts = stateResult.pts;
curState.date = stateResult.date; curState.date = stateResult.date;
delete curState.invalid; isSynchronizing = false;
}) })
} }
return { return {
saveUpdate: saveUpdate, processUpdateMessage: processUpdateMessage,
saveSeq: saveSeq, saveSeq: saveSeq,
attach: attach attach: attach
} }

Loading…
Cancel
Save