1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

DownloadManager now looks at all dialogs, not just the last 100.

This commit is contained in:
Fabian Schlenz 2018-04-12 06:46:42 +02:00
parent c5f901f4a2
commit 96d5b5c77b

View File

@ -28,6 +28,7 @@ import com.github.badoualy.telegram.tl.core.TLIntVector
import com.github.badoualy.telegram.tl.core.TLObject import com.github.badoualy.telegram.tl.core.TLObject
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs
import com.github.badoualy.telegram.tl.api.messages.TLDialogsSlice
import com.github.badoualy.telegram.tl.api.* import com.github.badoualy.telegram.tl.api.*
import com.github.badoualy.telegram.tl.api.upload.TLFile import com.github.badoualy.telegram.tl.api.upload.TLFile
import com.github.badoualy.telegram.tl.exception.RpcErrorException import com.github.badoualy.telegram.tl.exception.RpcErrorException
@ -283,34 +284,57 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
fun getChats(): ChatList { fun getChats(): ChatList {
val cl = ChatList() val cl = ChatList()
logger.trace("Calling messagesGetDialogs") logger.debug("Getting list of chats...")
val dialogs = client.messagesGetDialogs(0, 0, TLInputPeerEmpty(), 100) val limit = 100
logger.trace("Got {} dialogs back", dialogs.getDialogs().size) var offset = 0
while (true) {
// Add dialogs var temp: TLAbsDialogs? = null
cl.dialogs.addAll(dialogs.getDialogs().filter{it.getPeer() !is TLPeerChannel}) logger.trace("Calling messagesGetDialogs with offset {}", offset)
Utils.obeyFloodWait {
// Add supergoups and channels temp = client.messagesGetDialogs(offset, 0, TLInputPeerEmpty(), limit)
for (tl_channel in dialogs.getChats().filter{it is TLChannel}.map{it as TLChannel}) {
val tl_peer_channel = dialogs.getDialogs().find{var p = it.getPeer() ; p is TLPeerChannel && p.getChannelId()==tl_channel.getId()}
if (tl_peer_channel == null) continue
var download = true
if (settings.whitelist_channels.isNotEmpty()) {
download = settings.whitelist_channels.contains(tl_channel.getId().toString())
} else if (settings.blacklist_channels.isNotEmpty()) {
download = !settings.blacklist_channels.contains(tl_channel.getId().toString())
} }
val channel = Channel(id=tl_channel.getId(), access_hash=tl_channel.getAccessHash(), title=tl_channel.getTitle(), obj=tl_peer_channel, download=download) val dialogs = temp!!
if (tl_channel.getMegagroup()) { val last_message = dialogs.messages.filter{ it is TLMessage || it is TLMessageService }.last()
channel.message_source = MessageSource.SUPERGROUP offset = when(last_message) {
cl.supergroups.add(channel) is TLMessage -> last_message.date
} else { is TLMessageService -> last_message.date
channel.message_source = MessageSource.CHANNEL else -> throw RuntimeException("Unexpected last_message type ${last_message.javaClass}")
cl.channels.add(channel) }
logger.trace("Got {} dialogs back", dialogs.dialogs.size)
logger.trace("New offset will be {}", offset)
// Add dialogs
cl.dialogs.addAll(dialogs.getDialogs().filter{it.getPeer() !is TLPeerChannel})
// Add supergoups and channels
for (tl_channel in dialogs.getChats().filter{it is TLChannel}.map{it as TLChannel}) {
val tl_peer_channel = dialogs.getDialogs().find{var p = it.getPeer() ; p is TLPeerChannel && p.getChannelId()==tl_channel.getId()}
if (tl_peer_channel == null) continue
var download = true
if (settings.whitelist_channels.isNotEmpty()) {
download = settings.whitelist_channels.contains(tl_channel.getId().toString())
} else if (settings.blacklist_channels.isNotEmpty()) {
download = !settings.blacklist_channels.contains(tl_channel.getId().toString())
}
val channel = Channel(id=tl_channel.getId(), access_hash=tl_channel.getAccessHash(), title=tl_channel.getTitle(), obj=tl_peer_channel, download=download)
if (tl_channel.getMegagroup()) {
channel.message_source = MessageSource.SUPERGROUP
cl.supergroups.add(channel)
} else {
channel.message_source = MessageSource.CHANNEL
cl.channels.add(channel)
}
}
if (dialogs.dialogs.size < limit) {
logger.debug("Got only ${dialogs.dialogs.size} back instead of ${limit}. Stopping the loop.")
logger.debug("Got ${cl.dialogs.size} groups, ${cl.channels.size} channels and ${cl.supergroups.size} supergroups.")
break;
} }
} }
return cl return cl
} }