Skip to content

Commit

Permalink
feat: Group Profile Show Linked Channel Info
Browse files Browse the repository at this point in the history
  • Loading branch information
omg-xtao committed Apr 16, 2024
1 parent c24d909 commit 0fffac6
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 4 deletions.
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 @@ -370,6 +371,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
Expand Up @@ -533,6 +533,8 @@ public void openProfile(boolean byAvatar, boolean fromChatAnimation, boolean rem
}
ProfileActivity fragment = new ProfileActivity(args, sharedMediaPreloader);
fragment.setChatInfo(parentFragment.getCurrentChatInfo());
// na: Group Profile Show Linked Channel Info
fragment.setChatInfoChannelMsg(parentFragment.profileChannelMessageFetcher);
if (fromChatAnimation) {
fragment.setPlayProfileAnimation(byAvatar ? 2 : 1);
}
Expand Down
48 changes: 44 additions & 4 deletions TMessagesProj/src/main/java/org/telegram/ui/ProfileActivity.java
Expand Up @@ -4093,6 +4093,11 @@ protected boolean disablePermissionCheck() {
// }
}
} else if (position == channelRow) {
// na: Group Profile Show Linked Channel Info
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 @@ -7771,6 +7776,12 @@ public void didReceivedNotification(int id, int account, final Object... args) {
if (sharedMediaLayout != null) {
sharedMediaLayout.setChatInfo(chatInfo);
}
// na: Group Profile Show Linked Channel Info
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 @@ -8558,6 +8569,18 @@ private void updateOnlineCount(boolean notify) {
}
}

public void setChatInfoChannelMsg(ProfileChannelCell.ChannelMessageFetcher channelMessageFetcher) {
// na: Group Profile Show Linked Channel Info
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 @@ -8952,6 +8975,16 @@ private void updateRowsIds() {
sharedMediaRow = rowCount++;
}
} else if (chatId != 0) {

// na: Group Profile Show Linked Channel Info
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)) {
if (LocaleController.isRTL && ChatObject.isChannel(currentChat) && chatInfo != null && !currentChat.megagroup && chatInfo.linked_chat_id != 0) {
emptyRow = rowCount++;
Expand Down Expand Up @@ -11607,10 +11640,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
Expand Up @@ -1446,6 +1446,8 @@ private void openProfile(boolean byAvatar) {
args.putLong("chat_id", chatId);
ProfileActivity fragment = new ProfileActivity(args, avatarContainer.getSharedMediaPreloader());
fragment.setChatInfo(chatFull);
// na: Group Profile Show Linked Channel Info
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 0fffac6

Please sign in to comment.