tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.17k stars 1.45k forks source link

TDLib 1.7: Value of 'is_pinned' attribute in messages not reliable? #1343

Closed Wunderfitz closed 3 years ago

Wunderfitz commented 3 years ago

As I'm porting my app to TDLib 1.7, I'm using the new function getChatPinnedMessage to get the latest pinned message for a given chat. The function itself is absolutely reliable, but it seems that the message that is returned afterwards, doesn't always contain the correct value for the is_pinned attribute. I've seen that in several chats, no matter if channels or supergroups - some work properly, some don't. One example is the official COVID-19 information channel by the German Federal Ministry of Health (https://t.me/corona_infokanal_bmg). Below you see the JSON request and response for the getChatPinnedMessage function - invoked for this channel. Do you have any explanation for is_pinned being false here?


Request

{
    "@extra": "getChatPinnedMessage:-1001446651076",
    "@type": "getChatPinnedMessage",
    "chat_id": -1001446651076
}

Response

{
    "@extra": "getChatPinnedMessage:-1001446651076",
    "@type": "message",
    "author_signature": "",
    "can_be_deleted_for_all_users": false,
    "can_be_deleted_only_for_self": false,
    "can_be_edited": false,
    "can_be_forwarded": true,
    "can_get_message_thread": false,
    "can_get_statistics": false,
    "chat_id": -1001446651076,
    "contains_unread_mention": false,
    "content": {
        "@type": "messageText",
        "text": {
            "@type": "formattedText",
            "entities": [
                {
                    "@type": "textEntity",
                    "length": 7,
                    "offset": 0,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 37,
                    "offset": 44,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 22,
                    "offset": 200,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 21,
                    "offset": 227,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 26,
                    "offset": 482,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 35,
                    "offset": 533,
                    "type": {
                        "@type": "textEntityTypeUrl"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 26,
                    "offset": 572,
                    "type": {
                        "@type": "textEntityTypeUrl"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 19,
                    "offset": 602,
                    "type": {
                        "@type": "textEntityTypeUrl"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 8,
                    "offset": 632,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 8,
                    "offset": 645,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 9,
                    "offset": 700,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 33,
                    "offset": 710,
                    "type": {
                        "@type": "textEntityTypeUrl"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 9,
                    "offset": 744,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 38,
                    "offset": 754,
                    "type": {
                        "@type": "textEntityTypeUrl"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 10,
                    "offset": 938,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 2,
                    "offset": 958,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 7,
                    "offset": 991,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 1,
                    "offset": 1020,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 16,
                    "offset": 1027,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 9,
                    "offset": 1077,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 1,
                    "offset": 1088,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 12,
                    "offset": 1160,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 33,
                    "offset": 1228,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                },
                {
                    "@type": "textEntity",
                    "length": 68,
                    "offset": 1282,
                    "type": {
                        "@type": "textEntityTypeBold"
                    }
                }
            ],
            "text": "Achtung ⚠️ \n\nAuf Telegram und WhatsApp sind zahlreiche Falschmeldungen und Mythen in Bezug auf das Coronavirus im Umlauf, die Ihrer Gesundheit schaden können. Achten Sie bei Nachrichten genau auf die Quelle der Information und überprüfen Sie diese. 🔍  \n\nHelfen Sie dabei, die Ausbreitung von Falschmeldungen zu reduzieren. Leiten Sie keine Infos weiter, von denen Sie sich nicht sicher sind, ob diese stimmen. 🤔\n\nTeilen Sie diese Bitte mit Ihrer Familie, Freunden und Bekannten.\n\nVerlässliche Informationen finden Sie u.a. auf:\n🌐 www.infektionsschutz.de/coronavirus\n🌐 www.zusammengegencorona.de\n🌐 www.rki.de/covid-19\n\nAuch auf Telegram und WhatsApp informiert das Bundesgesundheitsministerium:\n\nTelegram: https://t.me/corona_infokanal_bmg\nWhatsApp: https://wa.me/4915162875183?text=Hallo\nTeilen Sie diese Information mit Ihren Liebsten.\n\nUm weiterhin die Lockerungen aufrechtzuerhalten, ist es sehr wichtig, dass wir uns alle an die AHA-Regeln halten:\n\nA ✋ - Halten Sie mindestens 1,5m Abstand zu Ihren Mitmenschen\nH 🧼 - Waschen Sie sich regelmäßig für mind. 20 Sekunden die Hände \nA 😷 - Tragen Sie beim Einkauf und in öffentlichen Verkehrsmitteln eine Alltagsmaske\n\nSo schützen Sie sich selbst und andere vor dem Virus.\nLeiten Sie diese Nachricht weiter und helfen Sie mit, Ihre Familie, Freunde und Bekannten vor Falschmeldungen zu schützen! ↪️"
        }
    },
    "date": 1590769213,
    "edit_date": 0,
    "id": 44040192,
    "interaction_info": {
        "@type": "messageInteractionInfo",
        "forward_count": 0,
        "view_count": 658336
    },
    "is_channel_post": true,
    "is_outgoing": false,
    "is_pinned": false,
    "media_album_id": "0",
    "message_thread_id": 0,
    "reply_in_chat_id": 0,
    "reply_to_message_id": 0,
    "restriction_reason": "",
    "sender": {
        "@type": "messageSenderChat",
        "chat_id": -1001446651076
    },
    "ttl": 0,
    "ttl_expires_in": 0,
    "via_bot_user_id": 0
}
levlam commented 3 years ago

I expect that the message was loaded from the database. It has no flag is_pinned, because it was received before client update.

Official Telegram clients show as pinned the first message older than currently shown messages (if there is such a message) and use searchChatMessages with filter searchMessagesFilterPinned to find it. Messages returned by searchChatMessages should always have up-to-date is_pinned flag.

Wunderfitz commented 3 years ago

Thanks for the hint how the official clients do it! And yes, I'm using the message database and the ones I found so far are rather old.

However, if you include a dedicated function such as getChatPinnedMessage maybe it would be a good idea to make sure the returned message is consistent as well... Of course, it's not a showstopper, I've implemented a proper workaround myself... ;)

levlam commented 3 years ago

I pushed a fix, which should update the flag of the message after a getSupergroupFullInfo call for the channel, or the chat is opened.

Wunderfitz commented 3 years ago

Thank you very much - did a cherry-pick onto v1.7 and it worked! OK, the first call still resulted in the wrong is_pinned information, but afterwards the information was updated properly. I'm closing this issue!