Added customization class

This commit is contained in:
Andrea Cavalli 2020-11-03 22:48:10 +01:00
parent 951765e3c8
commit ec3b788a72
4 changed files with 65 additions and 29 deletions

View File

@ -45,7 +45,7 @@ public class App extends Application {
scene = new Scene(loadFXML("primary"), 800, 600);
stage.setScene(scene);
stage.setTitle("TransferBot");
stage.setTitle(StaticSettings.BRANDING_NAME);
stage.show();
logService.append(Level.INFO, "Initialized");
}

View File

@ -1,5 +1,8 @@
package it.cavallium;
import static it.cavallium.StaticSettings.REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP;
import static it.cavallium.StaticSettings.requiresAdminPrivilegesOnSourceSupergroup;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
@ -59,7 +62,7 @@ public class PrimaryController {
var srcItems = sourceGroupCombo.getItems();
srcItems.clear();
for (BaseChatInfo originSupergroups : App.getTransferService().getAdminSupergroups(true, false)) {
for (BaseChatInfo originSupergroups : App.getTransferService().getAdminSupergroups(REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP, false)) {
srcItems.add(originSupergroups);
}

View File

@ -0,0 +1,11 @@
package it.cavallium;
public class StaticSettings {
public static final String BRANDING_NAME = "TransferBot";
public static final boolean REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP = true;
public static boolean requiresAdminPrivilegesOnSourceSupergroup() {
return REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP;
}
}

View File

@ -1,6 +1,8 @@
package it.cavallium;
import static it.cavallium.PrimaryController.getUserbotPhoneNumber;
import static it.cavallium.StaticSettings.REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP;
import static it.cavallium.StaticSettings.requiresAdminPrivilegesOnSourceSupergroup;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
@ -17,6 +19,7 @@ import it.tdlight.jni.TdApi.AuthorizationStateReady;
import it.tdlight.jni.TdApi.ChatMemberStatusAdministrator;
import it.tdlight.jni.TdApi.ChatMemberStatusCreator;
import it.tdlight.jni.TdApi.ChatMemberStatusLeft;
import it.tdlight.jni.TdApi.ChatMemberStatusMember;
import it.tdlight.jni.TdApi.GetMe;
import it.tdlight.jni.TdApi.GetUserPrivacySettingRules;
import it.tdlight.jni.TdApi.Ok;
@ -309,7 +312,7 @@ public class TransferServiceImpl implements TransferService {
.then(phaseDescriptionConsumer.apply("Transfer from " + sourceGroup.getTitle() + " to " + destGroup.getTitle()))
// Check and get the set of userbots that can transfer users from group X to group Y
.then(phaseDescriptionConsumer.apply("Checking available userbots for removing users in the source group"))
.then(phaseDescriptionConsumer.apply("Checking available userbots for " + (REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP ? "removing" : "managing") + " users in the source group"))
.thenMany(Flux.fromIterable(sourceSupergroupClients))
.flatMap(client -> client
.send(new TdApi.GetMe())
@ -322,19 +325,32 @@ public class TransferServiceImpl implements TransferService {
.flatMap(MonoUtils::orElseThrow)
.timeout(Duration.ofSeconds(5))
.filter(sourceGroupFullInfo -> {
if (sourceGroupFullInfo.status.getConstructor() == ChatMemberStatusAdministrator.CONSTRUCTOR) {
var statusAdmin = (ChatMemberStatusAdministrator) sourceGroupFullInfo.status;
if (statusAdmin.canRestrictMembers) {
if (requiresAdminPrivilegesOnSourceSupergroup()) {
if (sourceGroupFullInfo.status.getConstructor() == ChatMemberStatusAdministrator.CONSTRUCTOR) {
var statusAdmin = (ChatMemberStatusAdministrator) sourceGroupFullInfo.status;
if (statusAdmin.canRestrictMembers) {
return true;
} else {
App.getLogService().append(Level.WARN, "Userbot " + client + " failed: Can't restrict members of group " + sourceGroup.getTitle());
return false;
}
} else if (sourceGroupFullInfo.status.getConstructor() == ChatMemberStatusCreator.CONSTRUCTOR) {
return true;
} else {
App.getLogService().append(Level.WARN, "Userbot " + client + " failed: Can't restrict members of group " + sourceGroup.getTitle());
App.getLogService().append(Level.WARN, "Userbot " + client + " failed: Can't administer group " + sourceGroup.getTitle());
return false;
}
} else if (sourceGroupFullInfo.status.getConstructor() == ChatMemberStatusCreator.CONSTRUCTOR) {
return true;
} else {
App.getLogService().append(Level.WARN, "Userbot " + client + " failed: Can't administer group " + sourceGroup.getTitle());
switch (sourceGroupFullInfo.status.getConstructor()) {
case ChatMemberStatusAdministrator.CONSTRUCTOR:
case ChatMemberStatusCreator.CONSTRUCTOR:
case ChatMemberStatusMember.CONSTRUCTOR:
return true;
default:
App.getLogService().append(Level.WARN, "Userbot " + client + " failed: Can't access group " + sourceGroup.getTitle() + " (probably restricted, left or banned)");
return false;
}
}
return false;
})
.map(_v -> client)
.onErrorResume(e -> {
@ -537,25 +553,31 @@ public class TransferServiceImpl implements TransferService {
});
})
.<User>flatMap(user -> {
return client.<Ok>send(new TdApi.SetChatMemberStatus(sourceGroup.getSupergroupId(), user.id, new ChatMemberStatusLeft()))
.flatMap(result -> {
if (result.failed()) {
if (TdLightUtils.errorEquals(new TdError(result.cause().code, result.cause().message), 3, "Can't remove chat owner")) {
return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.CANT_REMOVE_CHAT_OWNER, "")).then(Mono.empty());
if (REMOVE_MEMBERS_FROM_SOURCE_SUPERGROUP) {
// Remove the user from the source supergroup
return client.<Ok>send(new TdApi.SetChatMemberStatus(sourceGroup.getSupergroupId(), user.id, new ChatMemberStatusLeft()))
.flatMap(result -> {
if (result.failed()) {
if (TdLightUtils.errorEquals(new TdError(result.cause().code, result.cause().message), 3, "Can't remove chat owner")) {
return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.CANT_REMOVE_CHAT_OWNER, "")).then(Mono.empty());
}
}
}
return Mono.just(result);
})
.flatMap(MonoUtils::orElseThrow)
.timeout(Duration.ofMinutes(2))
.onErrorResume((error) -> {
App.getLogService().append(Level.WARN, "Can't remove user \"" + getName(user) + "\" from supergroup \"" + sourceGroup.getSupergroupIdInt() + " " + sourceGroup.getTitle() + "\"");
return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.CANT_REMOVE, "Can't remove from source supergroup: " + error.getLocalizedMessage())).then(Mono.empty());
})
.flatMap(_v -> {
transferredSuccessfullyUsersStats.incrementAndGet();
return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.DONE, "")).then(Mono.empty()).thenReturn(user);
});
return Mono.just(result);
})
.flatMap(MonoUtils::orElseThrow)
.timeout(Duration.ofMinutes(2))
.onErrorResume((error) -> {
App.getLogService().append(Level.WARN, "Can't remove user \"" + getName(user) + "\" from supergroup \"" + sourceGroup.getSupergroupIdInt() + " " + sourceGroup.getTitle() + "\"");
return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.CANT_REMOVE, "Can't remove from source supergroup: " + error.getLocalizedMessage())).then(Mono.empty());
})
.flatMap(_v -> {
transferredSuccessfullyUsersStats.incrementAndGet();
return userStatusConsumer.apply(new UserStatus(getName(user), user.id, UserStatusType.DONE, "")).then(Mono.empty()).thenReturn(user);
});
} else {
// Don't remove the user from the source supergroup
return Mono.just(user);
}
})
.delayElements(App.getSettingsService().getDelayBetweenAdds())
.collect(Collectors.toSet())