Skip to content

Commit

Permalink
feat: show group linked channel info in ProfileActivity
Browse files Browse the repository at this point in the history
Co-authored-by: xtaodada <xtao@xtaolink.cn>
  • Loading branch information
NextAlone and omg-xtao committed Apr 23, 2024
1 parent 5b28a4a commit 5354ff2
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<TLRPC.User> users = new ArrayList<>();
ArrayList<TLRPC.Chat> 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<Long> usersToLoad = new ArrayList<>();
ArrayList<Long> 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<Runnable> callbacks = new ArrayList<>();
public void subscribe(Runnable callback) {
if (loaded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
43 changes: 39 additions & 4 deletions TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 5354ff2

Please sign in to comment.