diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/users/User.java b/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/users/User.java index 06fa68b..cfdd902 100644 --- a/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/users/User.java +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/data/models/users/User.java @@ -22,16 +22,12 @@ */ package io.github.jwdeveloper.tiktok.data.models.users; -import io.github.jwdeveloper.tiktok.data.models.badges.Badge; import io.github.jwdeveloper.tiktok.data.models.Picture; +import io.github.jwdeveloper.tiktok.data.models.badges.Badge; import io.github.jwdeveloper.tiktok.messages.webcast.*; -import lombok.AccessLevel; -import lombok.Getter; +import lombok.*; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; @Getter public class User { @@ -49,12 +45,12 @@ public class User { return attributes.stream().toList(); } - public boolean hasAttribute(UserAttribute userFlag) { - return attributes.contains(userFlag); + public boolean hasAttribute(UserAttribute attribute) { + return attributes.contains(attribute); } - public void addAttribute(UserAttribute... flags) { - this.attributes.addAll(Arrays.stream(flags).toList()); + public void addAttribute(UserAttribute... attributes) { + this.attributes.addAll(List.of(attributes)); } public boolean isGiftGiver() { @@ -168,11 +164,11 @@ public class User { } public static User EMPTY = new User(0L, - "", - Picture.empty(), - 0, - 0, - List.of(Badge.empty())); + "", + Picture.empty(), + 0, + 0, + List.of(Badge.empty())); public static User map(io.github.jwdeveloper.tiktok.messages.data.User user) { return new User(user); @@ -209,12 +205,12 @@ public class User { public static User map(WebcastEnvelopeMessage.EnvelopeInfo envelopeInfo) { return new User(0L, - //envelopeInfo.getSendUserId(), - envelopeInfo.getSendUserName(), - Picture.map(envelopeInfo.getSendUserAvatar()), - 0, - 0, - List.of(Badge.empty())); + //envelopeInfo.getSendUserId(), + envelopeInfo.getSendUserName(), + Picture.map(envelopeInfo.getSendUserAvatar()), + 0, + 0, + List.of(Badge.empty())); } @Override diff --git a/API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokLiveUnknownHostException.java b/API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokLiveUnknownHostException.java new file mode 100644 index 0000000..2f21399 --- /dev/null +++ b/API/src/main/java/io/github/jwdeveloper/tiktok/exceptions/TikTokLiveUnknownHostException.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023-2023 jwdeveloper jacekwoln@gmail.com + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package io.github.jwdeveloper.tiktok.exceptions; + +public class TikTokLiveUnknownHostException extends TikTokLiveException +{ + public TikTokLiveUnknownHostException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java index 98fc3f9..c418fa5 100644 --- a/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java +++ b/Client/src/main/java/io/github/jwdeveloper/tiktok/TikTokLiveClient.java @@ -78,15 +78,10 @@ public class TikTokLiveClient implements LiveClient this.logger = logger; } - public void connectAsync(Consumer onConnection) { - CompletableFuture.runAsync(() -> { - connect(); - onConnection.accept(this); - }); + connectAsync().thenAccept(onConnection); } - public CompletableFuture connectAsync() { return CompletableFuture.supplyAsync(() -> { connect(); @@ -131,14 +126,11 @@ public class TikTokLiveClient implements LiveClient roomInfo.setStartTime(userData.getStartTime()); roomInfo.setRoomId(userData.getRoomId()); - if (clientSettings.isFetchGifts()) - giftManager.attachGiftsList(httpClient.fetchRoomGiftsData(userData.getRoomId()).getGifts()); - if (userData.getUserStatus() == LiveUserData.UserStatus.Offline) throw new TikTokLiveOfflineHostException("User is offline: " + roomInfo.getHostName()); if (userData.getUserStatus() == LiveUserData.UserStatus.NotFound) - throw new TikTokLiveOfflineHostException("User not found: " + roomInfo.getHostName()); + throw new TikTokLiveUnknownHostException("User not found: " + roomInfo.getHostName()); var liveDataRequest = new LiveData.Request(userData.getRoomId()); var liveData = httpClient.fetchLiveData(liveDataRequest); @@ -147,7 +139,7 @@ public class TikTokLiveClient implements LiveClient throw new TikTokLiveException("Livestream for " + roomInfo.getHostName() + " is 18+ or age restricted!"); if (liveData.getLiveStatus() == LiveData.LiveStatus.HostNotFound) - throw new TikTokLiveOfflineHostException("LiveStream for " + roomInfo.getHostName() + " could not be found."); + throw new TikTokLiveUnknownHostException("LiveStream for " + roomInfo.getHostName() + " could not be found."); if (liveData.getLiveStatus() == LiveData.LiveStatus.HostOffline) throw new TikTokLiveOfflineHostException("LiveStream for " + roomInfo.getHostName() + " not found, is the Host offline?"); @@ -165,6 +157,9 @@ public class TikTokLiveClient implements LiveClient if (preconnectEvent.isCancelConnection()) throw new TikTokLiveException("TikTokPreConnectionEvent cancelled connection!"); + if (clientSettings.isFetchGifts()) + giftManager.attachGiftsList(httpClient.fetchRoomGiftsData(userData.getRoomId()).getGifts()); + var liveConnectionRequest = new LiveConnectionData.Request(userData.getRoomId()); var liveConnectionData = httpClient.fetchLiveConnectionData(liveConnectionRequest); webSocketClient.start(liveConnectionData, this);