Skip to content

Commit

Permalink
some handy features
Browse files Browse the repository at this point in the history
* extended admin rights to restrict gifs, stickers, games or inline bots for users in chat
  * the idea and main code from @arslan and his Cherrygram
* custom error messages if you got resticted for gifs only or stickers only
  * actually i track well strickers forbidden and let you to use gifs, but if gifs are forbidden (but stickers are allowed) you can use both (but gifs will not pass). Thats somewhere in the vanilla code i need to debug later.
* channels and groups have edit icon now, so you can list all members (if not hidden) and admins.
  * the code which display admin numbers is from @arslan, but i adapted it for channels only.
* added english & russian translations for new options
  • Loading branch information
nikitasius committed Jan 22, 2023
1 parent b1aa607 commit 583efe5
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 53 deletions.
72 changes: 59 additions & 13 deletions TMessagesProj/src/main/java/org/telegram/ui/ChatEditActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;

public class ChatEditActivity extends BaseFragment implements ImageUpdater.ImageUpdaterDelegate, NotificationCenter.NotificationCenterDelegate {
Expand Down Expand Up @@ -152,6 +153,8 @@ public class ChatEditActivity extends BaseFragment implements ImageUpdater.Image

private final static int done_button = 1;

private int realAdminCount = 0;

private PhotoViewer.PhotoViewerProvider provider = new PhotoViewer.EmptyPhotoViewerProvider() {

@Override
Expand Down Expand Up @@ -842,7 +845,7 @@ public void afterTextChanged(Editable editable) {

blockCell = new TextCell(context);
blockCell.setBackground(Theme.getSelectorDrawable(false));
blockCell.setVisibility(ChatObject.isChannel(currentChat) || currentChat.creator || ChatObject.hasAdminRights(currentChat) && ChatObject.canChangeChatInfo(currentChat) ? View.VISIBLE : View.GONE);
blockCell.setVisibility(View.VISIBLE);
blockCell.setOnClickListener(v -> {
Bundle args = new Bundle();
args.putLong("chat_id", chatId);
Expand Down Expand Up @@ -901,9 +904,9 @@ public void afterTextChanged(Editable editable) {
});
}

if (ChatObject.isChannel(currentChat) || currentChat.gigagroup) {
if ((ChatObject.isChannel(currentChat) || currentChat.gigagroup) && ChatObject.hasAdminRights(currentChat)) {
logCell = new TextCell(context);
logCell.setTextAndIcon(LocaleController.getString("EventLog", R.string.EventLog), R.drawable.msg_log, false);
logCell.setTextAndIcon(LocaleController.getString("EventLog", R.string.EventLog), R.drawable.msg_log, !isChannel && info != null && info.can_set_stickers);
logCell.setBackground(Theme.getSelectorDrawable(false));
logCell.setOnClickListener(v -> presentFragment(new ChannelAdminLogActivity(currentChat)));
}
Expand Down Expand Up @@ -942,13 +945,10 @@ public void afterTextChanged(Editable editable) {
groupStickersActivity.setInfo(info);
presentFragment(groupStickersActivity);
});
} else if (logCell != null) {
infoContainer.addView(logCell, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
}

if (!ChatObject.hasAdminRights(currentChat)) {
infoContainer.setVisibility(View.GONE);
settingsTopSectionCell.setVisibility(View.GONE);
if (logCell != null) {
infoContainer.addView(logCell, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
}

if (stickersCell == null) {
Expand Down Expand Up @@ -1187,6 +1187,34 @@ private int getAdminCount() {
return count;
}

private int getChannelAdminCount() {
if (info == null) {
return 1;
}
int count = 0;
for (int a = 0, N = info.participants.participants.size(); a < N; a++) {
TLRPC.ChatParticipant chatParticipant = info.participants.participants.get(a);
TLRPC.ChannelParticipant channelParticipant = ((TLRPC.TL_chatChannelParticipant) chatParticipant).channelParticipant;
if (channelParticipant instanceof TLRPC.TL_channelParticipantAdmin ||
channelParticipant instanceof TLRPC.TL_channelParticipantCreator) {
count++;
}
}
return count;
}

private void getRealChannelAdminCount() {
TLRPC.TL_channels_getParticipants req = new TLRPC.TL_channels_getParticipants();
req.channel = getMessagesController().getInputChannel(chatId);
req.filter = new TLRPC.TL_channelParticipantsAdmins();
int reqId = getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> {
TLRPC.TL_channels_channelParticipants res = (TLRPC.TL_channels_channelParticipants) response;
realAdminCount = res == null ? 0 : res.count;
adminCell.setTextAndValueAndIcon(LocaleController.getString("ChannelAdministrators", R.string.ChannelAdministrators), String.format(Locale.getDefault(), "%d", realAdminCount), R.drawable.msg_admin_add, true);
}));
getConnectionsManager().bindRequestToGuid(reqId, classGuid);
}

private void processDone() {
if (donePressed || nameTextView == null) {
return;
Expand Down Expand Up @@ -1370,7 +1398,7 @@ private void updateFields(boolean updateChat, boolean animated) {
}

if (logCell != null) {
logCell.setVisibility(!currentChat.megagroup || currentChat.gigagroup || info != null && info.participants_count > 200 ? View.VISIBLE : View.GONE);
logCell.setVisibility(!currentChat.megagroup || currentChat.gigagroup || info != null ? View.VISIBLE : View.GONE);
}

if (linkedCell != null) {
Expand Down Expand Up @@ -1476,6 +1504,15 @@ private void updateFields(boolean updateChat, boolean animated) {
if (!currentChat.default_banned_rights.send_stickers) {
count++;
}
if (!currentChat.default_banned_rights.send_gifs) {
count++;
}
if (!currentChat.default_banned_rights.send_games) {
count++;
}
if (!currentChat.default_banned_rights.send_inline) {
count++;
}
if (!currentChat.default_banned_rights.send_media) {
count++;
}
Expand All @@ -1501,15 +1538,24 @@ private void updateFields(boolean updateChat, boolean animated) {
count++;
}
} else {
count = forum ? 9 : 8;
count = forum ? 12 : 11;
}
blockCell.setTextAndValueAndIcon(LocaleController.getString("ChannelPermissions", R.string.ChannelPermissions), String.format("%d/%d", count, forum ? 9 : 8), animated, R.drawable.msg_permissions, true);
blockCell.setTextAndValueAndIcon(LocaleController.getString("ChannelPermissions", R.string.ChannelPermissions), String.format(Locale.getDefault(),"%d/%d", count, forum ? 12 : 11), animated, R.drawable.msg_permissions, true);
}
if (memberRequestsCell != null) {
memberRequestsCell.setTextAndValueAndIcon(LocaleController.getString("MemberRequests", R.string.MemberRequests), String.format("%d", info.requests_pending), R.drawable.msg_requests, logCell != null && logCell.getVisibility() == View.VISIBLE);
memberRequestsCell.setTextAndValueAndIcon(LocaleController.getString("MemberRequests", R.string.MemberRequests), String.format(Locale.getDefault(),"%d", info.requests_pending), R.drawable.msg_requests, logCell != null && logCell.getVisibility() == View.VISIBLE);
}
}
if (ChatObject.hasAdminRights(currentChat)) {
adminCell.setTextAndValueAndIcon(LocaleController.getString("ChannelAdministrators", R.string.ChannelAdministrators), String.format(Locale.getDefault(),"%d", ChatObject.isChannel(currentChat) ? info.admins_count : getAdminCount()), R.drawable.msg_admins, true);
} else if (info != null && info.participants != null) {
if (ChatObject.isChannel(currentChat)) {
adminCell.setTextAndIcon(LocaleController.getString("ChannelAdministrators", R.string.ChannelAdministrators), R.drawable.msg_admins, true);
getRealChannelAdminCount();
} else {
adminCell.setTextAndValueAndIcon(LocaleController.getString("ChannelAdministrators", R.string.ChannelAdministrators), String.format(Locale.getDefault(),"%d", ChatObject.isChannel(currentChat) ? realAdminCount == 0 ? getChannelAdminCount() : realAdminCount : getAdminCount()), R.drawable.msg_admins, true);
}
}
adminCell.setTextAndValueAndIcon(LocaleController.getString("ChannelAdministrators", R.string.ChannelAdministrators), String.format("%d", ChatObject.isChannel(currentChat) ? info.admins_count : getAdminCount()), R.drawable.msg_admins, true);
} else {
if (isChannel) {
membersCell.setTextAndIcon(LocaleController.getString("ChannelSubscribers", R.string.ChannelSubscribers), R.drawable.msg_groups, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public class ChatRightsEditActivity extends BaseFragment {

private int sendMessagesRow;
private int sendMediaRow;
private int sendGifsRow;
private int sendGamesRow;
private int useInlineBotRow;
private int sendStickersRow;
private int sendPollsRow;
private int embedLinksRow;
Expand Down Expand Up @@ -709,7 +712,13 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
} else if (position == sendMediaRow) {
value = bannedRights.send_media = !bannedRights.send_media;
} else if (position == sendStickersRow) {
value = bannedRights.send_stickers = bannedRights.send_games = bannedRights.send_gifs = bannedRights.send_inline = !bannedRights.send_stickers;
value = bannedRights.send_stickers = !bannedRights.send_stickers;
} else if (position == sendGifsRow) {
value = bannedRights.send_gifs = !bannedRights.send_gifs;
} else if (position == sendGamesRow) {
value = bannedRights.send_games = !bannedRights.send_games;
}else if (position == useInlineBotRow) {
value = bannedRights.send_inline = !bannedRights.send_inline;
} else if (position == embedLinksRow) {
value = bannedRights.embed_links = !bannedRights.embed_links;
} else if (position == sendPollsRow) {
Expand Down Expand Up @@ -738,12 +747,33 @@ public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
}
if ((bannedRights.view_messages || bannedRights.send_messages) && !bannedRights.send_stickers) {
bannedRights.send_stickers = bannedRights.send_games = bannedRights.send_gifs = bannedRights.send_inline = true;
bannedRights.send_stickers = true;
RecyclerListView.ViewHolder holder = listView.findViewHolderForAdapterPosition(sendStickersRow);
if (holder != null) {
((TextCheckCell2) holder.itemView).setChecked(false);
}
}
if ((bannedRights.view_messages || bannedRights.send_messages) && !bannedRights.send_gifs) {
bannedRights.send_gifs = true;
RecyclerListView.ViewHolder holder = listView.findViewHolderForAdapterPosition(sendGifsRow);
if (holder != null) {
((TextCheckCell2) holder.itemView).setChecked(false);
}
}
if ((bannedRights.view_messages || bannedRights.send_messages) && !bannedRights.send_games) {
bannedRights.send_games = true;
RecyclerListView.ViewHolder holder = listView.findViewHolderForAdapterPosition(sendGamesRow);
if (holder != null) {
((TextCheckCell2) holder.itemView).setChecked(false);
}
}
if ((bannedRights.view_messages || bannedRights.send_messages) && !bannedRights.send_inline) {
bannedRights.send_inline = true;
RecyclerListView.ViewHolder holder = listView.findViewHolderForAdapterPosition(useInlineBotRow);
if (holder != null) {
((TextCheckCell2) holder.itemView).setChecked(false);
}
}
if ((bannedRights.view_messages || bannedRights.send_messages) && !bannedRights.embed_links) {
bannedRights.embed_links = true;
RecyclerListView.ViewHolder holder = listView.findViewHolderForAdapterPosition(embedLinksRow);
Expand Down Expand Up @@ -983,6 +1013,9 @@ private void updateRows(boolean update) {
sendMessagesRow = -1;
sendMediaRow = -1;
sendStickersRow = -1;
sendGifsRow = -1;
sendGamesRow = -1;
useInlineBotRow = -1;
sendPollsRow = -1;
embedLinksRow = -1;
startVoiceChatRow = -1;
Expand Down Expand Up @@ -1022,6 +1055,9 @@ private void updateRows(boolean update) {
sendMessagesRow = rowCount++;
sendMediaRow = rowCount++;
sendStickersRow = rowCount++;
sendGifsRow = rowCount++;
sendGamesRow = rowCount++;
useInlineBotRow = rowCount++;
sendPollsRow = rowCount++;
embedLinksRow = rowCount++;
addUsersRow = rowCount++;
Expand Down Expand Up @@ -1646,8 +1682,17 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
checkCell.setTextAndCheck(LocaleController.getString("UserRestrictionsSendMedia", R.string.UserRestrictionsSendMedia), !bannedRights.send_media && !defaultBannedRights.send_media, true);
checkCell.setIcon(defaultBannedRights.send_media ? R.drawable.permission_locked : 0);
} else if (position == sendStickersRow) {
checkCell.setTextAndCheck(LocaleController.getString("UserRestrictionsSendStickers", R.string.UserRestrictionsSendStickers), !bannedRights.send_stickers && !defaultBannedRights.send_stickers, true);
checkCell.setTextAndCheck(LocaleController.getString("THUserRestrictionsSendStickers", R.string.THUserRestrictionsSendStickers), !bannedRights.send_stickers && !defaultBannedRights.send_stickers, true);
checkCell.setIcon(defaultBannedRights.send_stickers ? R.drawable.permission_locked : 0);
} else if (position == sendGifsRow) {
checkCell.setTextAndCheck(LocaleController.getString("THUserRestrictionsSendGifs", R.string.THUserRestrictionsSendGifs), !bannedRights.send_gifs && !defaultBannedRights.send_gifs, true);
checkCell.setIcon(defaultBannedRights.send_gifs ? R.drawable.permission_locked : 0);
} else if (position == sendGamesRow) {
checkCell.setTextAndCheck(LocaleController.getString("THUserRestrictionsSendGames", R.string.THUserRestrictionsSendGames), !bannedRights.send_games && !defaultBannedRights.send_games, true);
checkCell.setIcon(defaultBannedRights.send_games ? R.drawable.permission_locked : 0);
} else if (position == useInlineBotRow) {
checkCell.setTextAndCheck(LocaleController.getString("THUserRestrictionsUseInlineBots", R.string.THUserRestrictionsUseInlineBots), !bannedRights.send_inline && !defaultBannedRights.send_inline, true);
checkCell.setIcon(defaultBannedRights.send_inline ? R.drawable.permission_locked : 0);
} else if (position == embedLinksRow) {
checkCell.setTextAndCheck(LocaleController.getString("UserRestrictionsEmbedLinks", R.string.UserRestrictionsEmbedLinks), !bannedRights.embed_links && !defaultBannedRights.embed_links, true);
checkCell.setIcon(defaultBannedRights.embed_links ? R.drawable.permission_locked : 0);
Expand All @@ -1659,7 +1704,7 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (currentType == TYPE_ADD_BOT) {
// checkCell.setEnabled((asAdmin || position == manageRow) && !checkCell.hasIcon(), false);
} else {
if (position == sendMediaRow || position == sendStickersRow || position == embedLinksRow || position == sendPollsRow) {
if (position == sendMediaRow || position == sendStickersRow || position == sendGifsRow || position == sendGamesRow || position == useInlineBotRow || position == embedLinksRow || position == sendPollsRow) {
checkCell.setEnabled(!bannedRights.send_messages && !bannedRights.view_messages && !defaultBannedRights.send_messages && !defaultBannedRights.view_messages);
} else if (position == sendMessagesRow) {
checkCell.setEnabled(!bannedRights.view_messages && !defaultBannedRights.view_messages);
Expand Down Expand Up @@ -1736,9 +1781,10 @@ public int getItemViewType(int position) {
} else if (position == 2 || position == rankHeaderRow) {
return VIEW_TYPE_HEADER_CELL;
} else if (position == changeInfoRow || position == postMessagesRow || position == editMesagesRow || position == deleteMessagesRow ||
position == addAdminsRow || position == banUsersRow || position == addUsersRow || position == pinMessagesRow ||
position == sendMessagesRow || position == sendMediaRow || position == sendStickersRow || position == embedLinksRow ||
position == sendPollsRow || position == anonymousRow || position == startVoiceChatRow || position == manageRow || position == manageTopicsRow) {
position == addAdminsRow || position == banUsersRow || position == addUsersRow || position == pinMessagesRow ||
position == sendMessagesRow || position == sendMediaRow || position == sendStickersRow || position == sendGifsRow ||
position == sendGamesRow || position == useInlineBotRow || position == embedLinksRow || position == sendPollsRow ||
position == anonymousRow || position == startVoiceChatRow || position == manageRow || position == manageTopicsRow) {
return VIEW_TYPE_SWITCH_CELL;
} else if (position == cantEditInfoRow || position == rankInfoRow) {
return VIEW_TYPE_INFO_CELL;
Expand Down
Loading

0 comments on commit 583efe5

Please sign in to comment.