diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Cells/ProfileChannelCell.java b/TMessagesProj/src/main/java/org/telegram/ui/Cells/ProfileChannelCell.java index 8d8e3ae13..f19d1ed6d 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Cells/ProfileChannelCell.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Cells/ProfileChannelCell.java @@ -19,6 +19,7 @@ import org.telegram.SQLite.SQLiteCursor; import org.telegram.messenger.AndroidUtilities; +import org.telegram.messenger.ChatObject; import org.telegram.messenger.DialogObject; import org.telegram.messenger.FileLog; import org.telegram.messenger.LocaleController; @@ -371,6 +372,127 @@ public void fetch(long channel_id, int message_id) { }); } + public void fetchChannelMsg(TLRPC.ChatFull chatFull) { + if (chatFull == null || chatFull.linked_chat_id == 0) { + searchId++; + loaded = true; + messageObject = null; + done(false); + return; + } + fetchChannelMsg(chatFull.linked_chat_id); + } + + public void fetchChannelMsg(long channel_id) { + if (loaded || loading) { + if (this.channel_id != channel_id) { + loaded = false; + messageObject = null; + } else { + return; + } + } + final int thisSearchId = ++this.searchId; + loading = true; + + this.channel_id = channel_id; + this.message_id = 0; + + final long selfId = UserConfig.getInstance(currentAccount).getClientUserId(); + MessagesStorage storage = MessagesStorage.getInstance(currentAccount); + storage.getStorageQueue().postRunnable(() -> { + TLRPC.Message message = null; + ArrayList users = new ArrayList<>(); + ArrayList chats = new ArrayList<>(); + SQLiteCursor cursor = null; + try { + if (message_id <= 0) { + cursor = storage.getDatabase().queryFinalized("SELECT data, mid FROM messages_v2 WHERE uid = ? ORDER BY mid DESC LIMTI 1", -channel_id); + } else { + cursor = storage.getDatabase().queryFinalized("SELECT data, mid FROM messages_v2 WHERE uid = ? AND mid = ? LIMIT 1", -channel_id, message_id); + } + ArrayList usersToLoad = new ArrayList<>(); + ArrayList chatsToLoad = new ArrayList<>(); + if (cursor.next()) { + NativeByteBuffer data = cursor.byteBufferValue(0); + if (data != null) { + message = TLRPC.Message.TLdeserialize(data, data.readInt32(false), false); + message.readAttachPath(data, selfId); + data.reuse(); + message.id = cursor.intValue(1); + message.dialog_id = -channel_id; + MessagesStorage.addUsersAndChatsFromMessage(message, usersToLoad, chatsToLoad, null); + } + } + cursor.dispose(); + + if (message != null) { + + if (!usersToLoad.isEmpty()) { + storage.getUsersInternal(TextUtils.join(",", usersToLoad), users); + } + if (!chatsToLoad.isEmpty()) { + storage.getChatsInternal(TextUtils.join(",", chatsToLoad), chats); + } + } + } catch (Exception e) { + FileLog.e(e); + } finally { + if (cursor != null) { + cursor.dispose(); + } + } + final TLRPC.Message finalMessage = message; + AndroidUtilities.runOnUIThread(() -> { + if (thisSearchId != searchId) return; + MessageObject messageObject1 = null; + if (finalMessage != null) { + messageObject1 = new MessageObject(currentAccount, finalMessage, true, true); + } + + if (messageObject1 != null) { + this.messageObject = messageObject1; + done(false); + return; + } + + TLRPC.TL_messages_search req = new TLRPC.TL_messages_search(); + req.limit = 3; + req.offset_id = 0; + req.filter = new TLRPC.TL_inputMessagesFilterEmpty(); + req.q = ""; + req.peer = MessagesController.getInstance(currentAccount).getInputPeer(-channel_id); + ConnectionsManager.getInstance(currentAccount).sendRequest(req, (response, err) -> AndroidUtilities.runOnUIThread(() -> { + if (response instanceof TLRPC.messages_Messages) { + TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; + MessagesController.getInstance(currentAccount).putUsers(res.users, false); + MessagesController.getInstance(currentAccount).putChats(res.chats, false); + storage.putUsersAndChats(res.users, res.chats, true, true); + storage.putMessages(res, -channel_id, -1, 0, false, 0, 0); + + if (thisSearchId != searchId) return; + + TLRPC.Message message1 = null; + for (TLRPC.Message m : res.messages) { + if (!(m instanceof TLRPC.TL_messageEmpty)) { + message1 = m; + break; + } + } + if (message1 != null) { + message_id = message1.id; + this.messageObject = new MessageObject(currentAccount, message1, true, true); + done(false); + } + } else { + if (thisSearchId != searchId) return; + done(true); + } + })); + }); + }); + } + private ArrayList callbacks = new ArrayList<>(); public void subscribe(Runnable callback) { if (loaded) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAvatarContainer.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAvatarContainer.java index 77ad0fca0..3f280c6af 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAvatarContainer.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAvatarContainer.java @@ -534,6 +534,7 @@ public void openProfile(boolean byAvatar, boolean fromChatAnimation, boolean rem } ProfileActivity fragment = new ProfileActivity(args, sharedMediaPreloader); fragment.setChatInfo(parentFragment.getCurrentChatInfo()); + fragment.setChatInfoChannelMsg(parentFragment.profileChannelMessageFetcher); if (fromChatAnimation) { fragment.setPlayProfileAnimation(byAvatar ? 2 : 1); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java index 45d46a3d5..c9841875b 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java @@ -3897,6 +3897,10 @@ protected boolean disablePermissionCheck() { // } } } else if (position == channelRow) { + if (chatInfo != null) { + openDiscussion(); + return; + } if (userInfo == null) return; Bundle args = new Bundle(); args.putLong("chat_id", userInfo.personal_channel_id); @@ -7692,6 +7696,11 @@ public void didReceivedNotification(int id, int account, final Object... args) { if (sharedMediaLayout != null) { sharedMediaLayout.setChatInfo(chatInfo); } + if (profileChannelMessageFetcher == null && !isSettings()) { + profileChannelMessageFetcher = new ProfileChannelCell.ChannelMessageFetcher(currentAccount); + profileChannelMessageFetcher.subscribe(() -> updateListAnimated(false)); + profileChannelMessageFetcher.fetchChannelMsg(chatInfo); + } } } else if (id == NotificationCenter.closeChats) { removeSelfFromStack(true); @@ -8475,6 +8484,17 @@ private void updateOnlineCount(boolean notify) { } } + public void setChatInfoChannelMsg(ProfileChannelCell.ChannelMessageFetcher channelMessageFetcher) { + if (profileChannelMessageFetcher == null) { + profileChannelMessageFetcher = channelMessageFetcher; + } + if (profileChannelMessageFetcher == null) { + profileChannelMessageFetcher = new ProfileChannelCell.ChannelMessageFetcher(currentAccount); + } + profileChannelMessageFetcher.subscribe(() -> updateListAnimated(false)); + profileChannelMessageFetcher.fetchChannelMsg(chatInfo); + } + public void setChatInfo(TLRPC.ChatFull value) { chatInfo = value; if (chatInfo != null && chatInfo.migrated_from_chat_id != 0 && mergeDialogId == 0) { @@ -8868,6 +8888,14 @@ private void updateRowsIds() { sharedMediaRow = rowCount++; } } else if (chatId != 0) { + if (chatInfo != null && chatInfo.linked_chat_id != 0 && (profileChannelMessageFetcher == null || !profileChannelMessageFetcher.loaded || profileChannelMessageFetcher.messageObject != null)) { + TLRPC.Chat channel = getMessagesController().getChat(chatInfo.linked_chat_id); + if (channel != null && (ChatObject.isPublic(channel) || !ChatObject.isNotInChat(channel)) && ChatObject.isChannelAndNotMegaGroup(channel)) { + channelRow = rowCount++; + channelDividerRow = rowCount++; + } + } + if (chatInfo != null && (!TextUtils.isEmpty(chatInfo.about) || chatInfo.location instanceof TLRPC.TL_channelLocation) || ChatObject.isPublic(currentChat) || !currentChat.restriction_reason.isEmpty()) { if (LocaleController.isRTL && ChatObject.isChannel(currentChat) && chatInfo != null && !currentChat.megagroup && chatInfo.linked_chat_id != 0) { emptyRow = rowCount++; @@ -11539,10 +11567,17 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { hoursCell.set(userInfo != null ? userInfo.business_work_hours : null, hoursExpanded, hoursShownMine, notificationsDividerRow < 0 || bizLocationRow >= 0); break; case VIEW_TYPE_CHANNEL: - ((ProfileChannelCell) holder.itemView).set( - getMessagesController().getChat(userInfo.personal_channel_id), - profileChannelMessageFetcher != null ? profileChannelMessageFetcher.messageObject : null - ); + if (userInfo != null) { + ((ProfileChannelCell) holder.itemView).set( + getMessagesController().getChat(userInfo.personal_channel_id), + profileChannelMessageFetcher != null ? profileChannelMessageFetcher.messageObject : null + ); + } else if (chatInfo != null) { + ((ProfileChannelCell) holder.itemView).set( + getMessagesController().getChat(chatInfo.linked_chat_id), + profileChannelMessageFetcher != null ? profileChannelMessageFetcher.messageObject : null + ); + } break; } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/TopicsFragment.java b/TMessagesProj/src/main/java/org/telegram/ui/TopicsFragment.java index 1c076fe50..c310c28bd 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/TopicsFragment.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/TopicsFragment.java @@ -1439,6 +1439,7 @@ private void openProfile(boolean byAvatar) { args.putLong("chat_id", chatId); ProfileActivity fragment = new ProfileActivity(args, avatarContainer.getSharedMediaPreloader()); fragment.setChatInfo(chatFull); + fragment.setChatInfoChannelMsg(fragment.profileChannelMessageFetcher); fragment.setPlayProfileAnimation(fragmentView.getMeasuredHeight() > fragmentView.getMeasuredWidth() && avatarContainer.getAvatarImageView().getImageReceiver().hasImageLoaded() && byAvatar ? 2 : 1); presentFragment(fragment); }