Merge pull request #779 from addo37/access-privacy

Ease the access modifier on getPrivacy and other auxiliary methods
This commit is contained in:
Ruben Bermudez 2020-07-31 01:12:06 +01:00 committed by GitHub
commit 4a4267768c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,6 @@ package org.telegram.abilitybots.api.bot;
import com.google.common.collect.*; import com.google.common.collect.*;
import com.google.common.collect.ImmutableList.Builder; import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.telegram.abilitybots.api.db.DBContext; import org.telegram.abilitybots.api.db.DBContext;
@ -132,35 +131,35 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
/** /**
* @return the map of <ID,User> * @return the map of <ID,User>
*/ */
protected Map<Integer, User> users() { public Map<Integer, User> users() {
return db.getMap(USERS); return db.getMap(USERS);
} }
/** /**
* @return the map of <Username,ID> * @return the map of <Username,ID>
*/ */
protected Map<String, Integer> userIds() { public Map<String, Integer> userIds() {
return db.getMap(USER_ID); return db.getMap(USER_ID);
} }
/** /**
* @return a blacklist containing all the IDs of the banned users * @return a blacklist containing all the IDs of the banned users
*/ */
protected Set<Integer> blacklist() { public Set<Integer> blacklist() {
return db.getSet(BLACKLIST); return db.getSet(BLACKLIST);
} }
/** /**
* @return an admin set of all the IDs of bot administrators * @return an admin set of all the IDs of bot administrators
*/ */
protected Set<Integer> admins() { public Set<Integer> admins() {
return db.getSet(ADMINS); return db.getSet(ADMINS);
} }
/** /**
* @return a mapping of ability and reply names to their corresponding statistics * @return a mapping of ability and reply names to their corresponding statistics
*/ */
protected Map<String, Stats> stats() { public Map<String, Stats> stats() {
return stats; return stats;
} }
@ -223,6 +222,32 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
return botUsername; return botUsername;
} }
public Privacy getPrivacy(Update update, int id) {
return isCreator(id) ?
CREATOR : isAdmin(id) ?
ADMIN : (isGroupUpdate(update) || isSuperGroupUpdate(update)) && isGroupAdmin(update, id) ?
GROUP_ADMIN : PUBLIC;
}
public boolean isGroupAdmin(Update update, int id) {
return isGroupAdmin(getChatId(update), id);
}
public boolean isGroupAdmin(long chatId, int id) {
GetChatAdministrators admins = new GetChatAdministrators().setChatId(chatId);
return silent.execute(admins)
.orElse(new ArrayList<>()).stream()
.anyMatch(member -> member.getUser().getId() == id);
}
public boolean isCreator(int id) {
return id == creatorId();
}
public boolean isAdmin(Integer id) {
return admins().contains(id);
}
/** /**
* Test the update against the provided global flags. The default implementation is a passthrough to all updates. * Test the update against the provided global flags. The default implementation is a passthrough to all updates.
* <p> * <p>
@ -465,30 +490,6 @@ public abstract class BaseAbilityBot extends DefaultAbsSender implements Ability
return isOk; return isOk;
} }
@NotNull
Privacy getPrivacy(Update update, int id) {
return isCreator(id) ?
CREATOR : isAdmin(id) ?
ADMIN : (isGroupUpdate(update) || isSuperGroupUpdate(update)) && isGroupAdmin(update, id) ?
GROUP_ADMIN : PUBLIC;
}
private boolean isGroupAdmin(Update update, int id) {
GetChatAdministrators admins = new GetChatAdministrators().setChatId(getChatId(update));
return silent.execute(admins)
.orElse(new ArrayList<>()).stream()
.anyMatch(member -> member.getUser().getId() == id);
}
private boolean isCreator(int id) {
return id == creatorId();
}
private boolean isAdmin(Integer id) {
return admins().contains(id);
}
boolean validateAbility(Trio<Update, Ability, String[]> trio) { boolean validateAbility(Trio<Update, Ability, String[]> trio) {
return trio.b() != null; return trio.b() != null;
} }