Skip to content

Commit

Permalink
Do not throw exception if LinkUser was not found. Replace field with …
Browse files Browse the repository at this point in the history
…getLinkUser() method
  • Loading branch information
bivashy committed Aug 3, 2023
1 parent 464c5e4 commit bb39ebc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.mastercapexd.auth.config.message.link.context;

import java.util.Optional;

import com.bivashy.auth.api.account.Account;
import com.bivashy.auth.api.link.LinkType;
import com.bivashy.auth.api.link.user.LinkUser;
Expand All @@ -8,11 +10,17 @@
import me.mastercapexd.auth.config.message.context.placeholder.PlaceholderProvider;

public class LinkPlaceholderContext extends BaseAccountPlaceholderContext {
protected LinkUser linkUser;
private LinkUser linkUser;

public LinkPlaceholderContext(Account account, LinkType linkType, String linkName) {
super(account);
linkUser = account.findFirstLinkUser((user) -> user.getLinkType().equals(linkType)).orElseThrow(NullPointerException::new);
linkUser = account.findFirstLinkUser((user) -> user.getLinkType().equals(linkType)).orElse(null);
if (linkUser == null)
return;
registerPlaceholderProvider(PlaceholderProvider.of(linkUser.getLinkUserInfo().getIdentificator().asString(), "%" + linkName + "_id%"));
}

public Optional<LinkUser> getLinkUser() {
return Optional.ofNullable(linkUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
public class TelegramMessagePlaceholderContext extends LinkPlaceholderContext {
public TelegramMessagePlaceholderContext(Account account) {
super(account, TelegramLinkType.getInstance(), "telegram");
if (linkUser.isIdentifierDefaultOrNull() || !linkUser.getLinkUserInfo().getIdentificator().isNumber())
return;
registerPlaceholderProvider(PlaceholderProvider.of(Long.toString(linkUser.getLinkUserInfo().getIdentificator().asNumber()), "%telegram_id%"));
getLinkUser().ifPresent(linkUser -> {
if (linkUser.isIdentifierDefaultOrNull() || !linkUser.getLinkUserInfo().getIdentificator().isNumber())
return;
registerPlaceholderProvider(PlaceholderProvider.of(Long.toString(linkUser.getLinkUserInfo().getIdentificator().asNumber()), "%telegram_id%"));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.mastercapexd.auth.config.message.vk;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import com.bivashy.auth.api.AuthPlugin;
import com.bivashy.auth.api.account.Account;
Expand All @@ -22,19 +24,34 @@ public class VKMessagePlaceholderContext extends LinkPlaceholderContext {

public VKMessagePlaceholderContext(Account account) {
super(account, VKLinkType.getInstance(), "vk");
try {
getLinkUser().ifPresent(linkUser -> {
if (linkUser.isIdentifierDefaultOrNull() || !linkUser.getLinkUserInfo().getIdentificator().isNumber())
return;
List<GetResponse> userInformationResponses =
CLIENT.users().get(ACTOR).userIds(String.valueOf(linkUser.getLinkUserInfo().getIdentificator().asNumber())).execute();
if (userInformationResponses.isEmpty())
return;
GetResponse userInformationResponse = userInformationResponses.get(0);
registerPlaceholderProvider(PlaceholderProvider.of(userInformationResponse.getScreenName(), "%vk_screen_name%"));
registerPlaceholderProvider(PlaceholderProvider.of(userInformationResponse.getFirstName(), "%vk_first_name%"));
registerPlaceholderProvider(PlaceholderProvider.of(userInformationResponse.getLastName(), "%vk_last_name%"));
} catch(ApiException | ClientException e) {
e.printStackTrace();
}
findUser(linkUser.getLinkUserInfo().getIdentificator().asNumber()).thenAccept(userOptional -> {
if (!userOptional.isPresent())
return;
GetResponse user = userOptional.get();
registerPlaceholderProvider(PlaceholderProvider.of(user.getScreenName(), "%vk_screen_name%"));
registerPlaceholderProvider(PlaceholderProvider.of(user.getFirstName(), "%vk_first_name%"));
registerPlaceholderProvider(PlaceholderProvider.of(user.getLastName(), "%vk_last_name%"));
});
});
}

private CompletableFuture<Optional<GetResponse>> findUser(long userId) {
return CompletableFuture.supplyAsync(() -> {
try {
List<GetResponse> userInformationResponses = CLIENT.users()
.get(ACTOR)
.userIds(String.valueOf(userId))
.execute();
if (userInformationResponses.isEmpty())
return Optional.empty();
return Optional.of(userInformationResponses.get(0));
} catch(ClientException | ApiException e) {
e.printStackTrace();
return Optional.empty();
}
});
}
}

0 comments on commit bb39ebc

Please sign in to comment.