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

Fixed the detection of supergroups and channels. Fixes #69.

This commit is contained in:
Fabian Schlenz 2017-12-16 23:21:06 +01:00
parent ab6a30c48e
commit c83de2ed00
2 changed files with 65 additions and 19 deletions

View File

@ -271,7 +271,7 @@ class Database private constructor(var client: TelegramClient) {
} }
@Synchronized @Synchronized
fun saveMessages(all: TLVector<TLAbsMessage>, api_layer: Int) { fun saveMessages(all: TLVector<TLAbsMessage>, api_layer: Int, source_type: MessageSource = MessageSource.NORMAL) {
try { try {
//"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " + //"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " +
//"VALUES " + //"VALUES " +
@ -283,9 +283,8 @@ class Database private constructor(var client: TelegramClient) {
val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO messages " + columns) val ps = conn!!.prepareStatement("INSERT OR REPLACE INTO messages " + columns)
val ps_insert_or_ignore = conn!!.prepareStatement("INSERT OR IGNORE INTO messages " + columns) val ps_insert_or_ignore = conn!!.prepareStatement("INSERT OR IGNORE INTO messages " + columns)
for (abs in all) { for (msg in all) {
if (abs is TLMessage) { if (msg is TLMessage) {
val msg = abs
ps.setInt(1, msg.getId()) ps.setInt(1, msg.getId())
ps.setString(2, "message") ps.setString(2, "message")
val peer = msg.getToId() val peer = msg.getToId()
@ -300,14 +299,19 @@ class Database private constructor(var client: TelegramClient) {
ps.setString(3, "dialog") ps.setString(3, "dialog")
ps.setInt(4, id) ps.setInt(4, id)
} else if (peer is TLPeerChannel) { } else if (peer is TLPeerChannel) {
ps.setString(3, "channel") if (source_type == MessageSource.CHANNEL) {
ps.setString(3, "channel")
} else if (source_type == MessageSource.SUPERGROUP) {
ps.setString(3, "supergroup")
} else {
throw RuntimeException("Got a TLPeerChannel, but were expecting $source_type")
}
ps.setInt(4, peer.getChannelId()) ps.setInt(4, peer.getChannelId())
} else { } else {
throw RuntimeException("Unexpected Peer type: " + peer.javaClass) throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
} }
if (peer is TLPeerChannel) { if (peer is TLPeerChannel && msg.getFromId() == null) {
// Message in a channel don't have a sender -> insert a null
ps.setNull(5, Types.INTEGER) ps.setNull(5, Types.INTEGER)
} else { } else {
ps.setInt(5, msg.getFromId()) ps.setInt(5, msg.getFromId())
@ -347,11 +351,33 @@ class Database private constructor(var client: TelegramClient) {
ps.setBytes(13, stream.toByteArray()) ps.setBytes(13, stream.toByteArray())
ps.setInt(14, api_layer) ps.setInt(14, api_layer)
ps.addBatch() ps.addBatch()
} else if (abs is TLMessageService) { } else if (msg is TLMessageService) {
ps_insert_or_ignore.setInt(1, abs.getId()) ps_insert_or_ignore.setInt(1, msg.getId())
ps_insert_or_ignore.setString(2, "service_message") ps_insert_or_ignore.setString(2, "service_message")
ps_insert_or_ignore.setNull(3, Types.INTEGER)
ps_insert_or_ignore.setNull(4, Types.INTEGER) val peer = msg.getToId()
if (peer is TLPeerChat) {
ps.setString(3, "group")
ps.setInt(4, peer.getChatId())
} else if (peer is TLPeerUser) {
var id = peer.getUserId()
if (id == this.user_manager.user!!.getId()) {
id = msg.getFromId()
}
ps.setString(3, "dialog")
ps.setInt(4, id)
} else if (peer is TLPeerChannel) {
// Messages in channels don't have a sender.
if (msg.getFromId() == null) {
ps.setString(3, "channel")
} else {
ps.setString(3, "supergroup")
}
ps.setInt(4, peer.getChannelId())
} else {
throw RuntimeException("Unexpected Peer type: " + peer.javaClass)
}
ps_insert_or_ignore.setNull(5, Types.INTEGER) ps_insert_or_ignore.setNull(5, Types.INTEGER)
ps_insert_or_ignore.setNull(6, Types.INTEGER) ps_insert_or_ignore.setNull(6, Types.INTEGER)
ps_insert_or_ignore.setNull(7, Types.VARCHAR) ps_insert_or_ignore.setNull(7, Types.VARCHAR)
@ -363,8 +389,8 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_ignore.setNull(13, Types.BLOB) ps_insert_or_ignore.setNull(13, Types.BLOB)
ps_insert_or_ignore.setInt(14, api_layer) ps_insert_or_ignore.setInt(14, api_layer)
ps_insert_or_ignore.addBatch() ps_insert_or_ignore.addBatch()
} else if (abs is TLMessageEmpty) { } else if (msg is TLMessageEmpty) {
ps_insert_or_ignore.setInt(1, abs.getId()) ps_insert_or_ignore.setInt(1, msg.getId())
ps_insert_or_ignore.setString(2, "empty_message") ps_insert_or_ignore.setString(2, "empty_message")
ps_insert_or_ignore.setNull(3, Types.INTEGER) ps_insert_or_ignore.setNull(3, Types.INTEGER)
ps_insert_or_ignore.setNull(4, Types.INTEGER) ps_insert_or_ignore.setNull(4, Types.INTEGER)
@ -380,7 +406,7 @@ class Database private constructor(var client: TelegramClient) {
ps_insert_or_ignore.setInt(14, api_layer) ps_insert_or_ignore.setInt(14, api_layer)
ps_insert_or_ignore.addBatch() ps_insert_or_ignore.addBatch()
} else { } else {
throw RuntimeException("Unexpected Message type: " + abs.javaClass) throw RuntimeException("Unexpected Message type: " + msg.javaClass)
} }
} }
conn!!.setAutoCommit(false) conn!!.setAutoCommit(false)

View File

@ -52,6 +52,12 @@ import java.nio.file.StandardCopyOption
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
enum class MessageSource(val descr: String) {
NORMAL(""),
CHANNEL("channel"),
SUPERGROUP("supergroup")
}
class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressInterface) { class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressInterface) {
internal var user: UserManager? = null internal var user: UserManager? = null
internal var db: Database? = null internal var db: Database? = null
@ -138,7 +144,7 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
val end_id = max_message_id val end_id = max_message_id
val ids = makeIdList(start_id, end_id) val ids = makeIdList(start_id, end_id)
downloadMessages(ids, null, null) downloadMessages(ids, null)
} }
logger.info("Searching for missing messages in the db") logger.info("Searching for missing messages in the db")
@ -214,7 +220,14 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
channel_name = "?" channel_name = "?"
} }
val channel = TLInputChannel(channel_id, access_hash) val channel = TLInputChannel(channel_id, access_hash)
downloadMessages(ids, channel, "channel $channel_name") val source_type = if (supergroups.contains(channel_id)) {
MessageSource.SUPERGROUP
} else if (channels.contains(channel_id)) {
MessageSource.CHANNEL
} else {
throw RuntimeException("chat is neither in channels nor in supergroups...")
}
downloadMessages(ids, channel, source_type=source_type, source_name=channel_name)
} }
} }
} }
@ -222,7 +235,14 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
} }
@Throws(RpcErrorException::class, IOException::class) @Throws(RpcErrorException::class, IOException::class)
private fun downloadMessages(ids: MutableList<Int>, channel: TLInputChannel?, source_string: String?) { private fun downloadMessages(ids: MutableList<Int>, channel: TLInputChannel?, source_type: MessageSource = MessageSource.NORMAL, source_name: String? = null) {
val source_string = if (source_type == MessageSource.NORMAL) {
null
} else if (source_name == null) {
"unknown ${source_type.descr}"
} else {
"${source_type.descr} $source_name"
}
prog!!.onMessageDownloadStart(ids.size, source_string) prog!!.onMessageDownloadStart(ids.size, source_string)
logger.debug("Entering download loop") logger.debug("Entering download loop")
@ -269,7 +289,7 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
} }
prog!!.onMessageDownloaded(response.getMessages().size) prog!!.onMessageDownloaded(response.getMessages().size)
db!!.saveMessages(response.getMessages(), Kotlogram.API_LAYER) db!!.saveMessages(response.getMessages(), Kotlogram.API_LAYER, source_type=source_type)
db!!.saveChats(response.getChats()) db!!.saveChats(response.getChats())
db!!.saveUsers(response.getUsers()) db!!.saveUsers(response.getUsers())
logger.trace("Sleeping") logger.trace("Sleeping")
@ -320,7 +340,7 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
if (ids.size > 0) { if (ids.size > 0) {
System.out.println("You have ${ids.size} messages in your db that need an update. Doing that now.") System.out.println("You have ${ids.size} messages in your db that need an update. Doing that now.")
logger.debug("Found {} messages", ids.size) logger.debug("Found {} messages", ids.size)
downloadMessages(ids, null, null) downloadMessages(ids, null, source_type=MessageSource.NORMAL)
} }
val messages = this.db!!.getMessagesWithMedia() val messages = this.db!!.getMessagesWithMedia()