Skip to content

Instantly share code, notes, and snippets.

@stek29 stek29/all_dialogs.js
Last active Feb 16, 2019

Embed
What would you like to do?
Webogram Dialog loader
const angularGet = name => angular.element(document).injector().get(name);
const _invokeApi = angularGet('MtpApiManager').invokeApi;
const saveApiChats = angularGet('AppChatsManager').saveApiChats;
const saveApiUsers = angularGet('AppUsersManager').saveApiUsers;
const getInputPeerByID = angularGet('AppPeersManager').getInputPeerByID;
const getChat = angularGet('AppChatsManager').getChat;
const getUser = angularGet('AppUsersManager').getUser;
const invokeApi = function() {
let args = arguments;
return new Promise((res, rej) => _invokeApi.apply(null, args).then(res).catch(rej));
}
const getPeerID = function(peer) {
if (peer._ === 'peerUser') {
return peer.user_id;
} else if (peer._ === 'peerChat') {
return -peer.chat_id;
} else if (peer._ === 'peerChannel') {
return -peer.channel_id;
} else {
throw '';
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var offset_peer = {"_": "inputPeerEmpty"};
var offset_date = 0;
var offset_id = 0;
var dialogArrays = [];
while (true) {
let dialogSlice = await invokeApi('messages.getDialogs', {
"flags": 1,
"offset_peer": offset_peer,
"offset_date": offset_date,
"offset_id": offset_id,
"limit": 100,
});
saveApiUsers(dialogSlice.users);
saveApiChats(dialogSlice.chats);
dialogArrays.push(dialogSlice.dialogs);
let lastDialog = dialogSlice.dialogs[dialogSlice.dialogs.length - 1];
let lastDialogID = getPeerID(lastDialog.peer);
offset_peer = getInputPeerByID(lastDialogID);
let new_offset_date = Math.min.apply(null, dialogSlice.messages.map(m=>m.date));
if (new_offset_date >= offset_date && offset_date != 0) {
console.error(`offset_date grows: ${offset_date} => ${new_offset_date} (${new Date(offset_date*1000)} => ${new Date(new_offset_date)})`);
break;
}
offset_date = new_offset_date;
// would eventually crash here when all dialogs are loaded
let new_offset_id = dialogSlice.messages.filter(m=>m.to_id._ != 'peerChannel').pop().id;
if (new_offset_id == offset_id) {
console.error('offset_id didnt change!');
break;
}
offset_id = new_offset_id;
await sleep(parseInt(500 + Math.random()*500));
}
// Example of analysis:
// myChannels = dialogArrays.flat().filter(d=>d.peer._ == 'peerChannel').filter(d=>d.notify_settings.mute_until==0).map(d=>getChat(-getPeerID(d.peer))).filter(c=>c.pFlags.broadcast&&c.pFlags.creator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.