Skip to content

Commit

Permalink
Working on fixing tests that broke + added bungieAPIService
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Villavicencio authored and Daniel Villavicencio committed Feb 9, 2024
1 parent d171faf commit 3248795
Show file tree
Hide file tree
Showing 41 changed files with 1,045 additions and 410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@EnableCaching
@EnableAsync
@SpringBootApplication
@EnableAspectJAutoProxy
public class Destiny2botApplication {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.danielvm.destiny2bot.enums.ManifestEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

Expand All @@ -19,13 +18,10 @@
public class BungieManifestClient {

private final BungieClient reactiveClient;
private final BungieClient imperativeClient;

public BungieManifestClient(
BungieClient reactiveBungieClient,
BungieClient imperativeBungieClient) {
BungieClient reactiveBungieClient) {
this.reactiveClient = reactiveBungieClient;
this.imperativeClient = imperativeBungieClient;
}

/**
Expand All @@ -40,18 +36,4 @@ public Mono<BungieResponse<ManifestFields>> getManifestEntityRx(
ManifestEntity entityType, String hashIdentifier) {
return reactiveClient.getManifestEntityRx(entityType.getId(), hashIdentifier).cache();
}

/**
* Wraps the client call to the Manifest with a Cacheable method with the imperative Rest Client
*
* @param entityType The entity type (see {@link ManifestEntity})
* @param hashIdentifier The hash identifier
* @return {@link BungieResponse} of {@link ManifestFields}
*/
@Cacheable(cacheNames = "entityImperative", cacheManager = "inMemoryCacheManager")
public ResponseEntity<BungieResponse<ManifestFields>> getManifestEntity(
ManifestEntity entityType, Long hashIdentifier) {
return imperativeClient.getManifestEntity(entityType.getId(), hashIdentifier);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import com.danielvm.destiny2bot.dto.discord.DmMessageRequest;
import java.util.HashSet;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class IndexingStateContext {

private static final ThreadLocal<IndexingStateContext> STATE_CONTEXT = ThreadLocal.withInitial(
IndexingStateContext::new);

private final Set<Long> seenCharacters = new HashSet<>();

private DmMessageRequest payload;
private DmMessageRequest currentDmMessage;
private Long channelId;
private Long messageId;

Expand All @@ -26,36 +30,8 @@ public static IndexingStateContext getContext() {
public static void clear() {
IndexingStateContext context = STATE_CONTEXT.get();
context.seenCharacters.clear();
context.payload = null;
context.currentDmMessage = null;
context.channelId = null;
context.messageId = null;
}

public DmMessageRequest getCurrentPayload() {
return this.payload;
}

public void setPayload(DmMessageRequest payload) {
this.payload = payload;
}

public Long getChannelId() {
return channelId;
}

public void setChannelId(Long channelId) {
this.channelId = channelId;
}

public Long getMessageId() {
return messageId;
}

public void setMessageId(Long messageId) {
this.messageId = messageId;
}

public Set<Long> getSeenCharacters() {
return seenCharacters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DestinyCharacter {
/**
* The characterId
*/
private String characterId;
private Long characterId;

/**
* The character class of this character, e.g., Titan, Warlock, or Hunter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
@NoArgsConstructor
public class CharacterInfo {

private String membershipId;
private Long membershipId;
private Integer membershipType;
private String CharacterId;
private Long CharacterId;
private Integer light;
private Integer raceType;
private Integer genderType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@AllArgsConstructor
public class Characters {

private Map<String, CharacterInfo> data;
private Map<Long, CharacterInfo> data;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.danielvm.destiny2bot.dto.destiny.manifest;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ManifestFields {

private DisplayProperties displayProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class CharacterRaid {
@Column(name = "raid_duration")
private String raidDuration;

@Column(name = "is_hard_mode")
private Boolean isHardMode;

@Column(name = "user_character_id")
private Long userCharacterId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.danielvm.destiny2bot.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Builder
@Table(name = "user_character_indexing_information")
public class UserCharacterIndexing {

@Id
@Column(name = "character_id")
private Long characterId;

@Column(name = "last_page")
private Integer lastPage;

@ManyToOne
@JoinColumn(name = "bot_user_id")
private UserIndexing userIndexing;
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package com.danielvm.destiny2bot.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "user_indexing_information")
public class UserIndexingInformation {
public class UserIndexing {

@Id
@Column(name = "user_discord_id")
private Long userDiscordId;

@Column(name = "number_of_pages")
private Integer numberOfPages;

@Column(name = "last_page")
private Integer lastPage;
private Long discordUserId;

@Column(name = "is_indexing")
private Boolean isIndexing;

@OneToMany(mappedBy = "userIndexing", cascade = CascadeType.ALL)
private List<UserCharacterIndexing> currentCharacters;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.danielvm.destiny2bot.enums;

public enum IndexingStatus {
INDEXING,
SUCCESS,
ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.danielvm.destiny2bot.exception.ResourceNotFoundException;
import com.danielvm.destiny2bot.factory.creator.MessageComponentSource;
import com.danielvm.destiny2bot.factory.creator.WIRDButtonMessageCreator;
import com.danielvm.destiny2bot.factory.creator.WhyAuthorizeButtonMessageCreator;
import com.danielvm.destiny2bot.util.MessageComponentUtil;
import java.util.Map;
import java.util.Objects;
import org.springframework.stereotype.Component;
Expand All @@ -12,9 +14,12 @@ public class MessageComponentFactory {

private final Map<String, MessageComponentSource> messageComponentFactory;

public MessageComponentFactory(WIRDButtonMessageCreator wirdButtonMessageCreator) {
public MessageComponentFactory(
WIRDButtonMessageCreator wirdButtonMessageCreator,
WhyAuthorizeButtonMessageCreator whyAuthorizeButtonMessageCreator) {
this.messageComponentFactory = Map.of(
"WIRD", wirdButtonMessageCreator
MessageComponentUtil.WIRD_BUTTON_ID, wirdButtonMessageCreator,
MessageComponentUtil.WHY_AUTHORIZE_BUTTON_ID, whyAuthorizeButtonMessageCreator
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public class AuthorizeMessageCreator implements ApplicationCommandSource {

public static final String MESSAGE_TITLE = "**Link Bungie and Discord accounts here**";
public static final String MESSAGE_DESCRIPTION = """
Riven can grant you wishes unique to your Destiny 2 characters.
However, in order for her to do that you must authorize her to read a sub-set of your Destiny 2 data beforehand.
Riven can fulfill commands that are unique to your Destiny 2 characters.
However, in order for her to do that we must first link your Discord account with your Destiny 2 account.
""";
private static final Integer EPHEMERAL_BYTE = 1000000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.danielvm.destiny2bot.dto.discord.Interaction;
import com.danielvm.destiny2bot.dto.discord.InteractionResponse;
import com.danielvm.destiny2bot.dto.discord.InteractionResponseData;
import com.danielvm.destiny2bot.service.DestinyCharacterService;
import com.danielvm.destiny2bot.service.UserCharacterService;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

Expand All @@ -13,11 +13,11 @@ public class RaidStatsMessageCreator implements ApplicationCommandSource,
AutocompleteSource {

private static final String CHOICE_FORMAT = "[%s] %s - %s";
private final DestinyCharacterService destinyCharacterService;
private final UserCharacterService userCharacterService;

public RaidStatsMessageCreator(
DestinyCharacterService destinyCharacterService) {
this.destinyCharacterService = destinyCharacterService;
UserCharacterService userCharacterService) {
this.userCharacterService = userCharacterService;
}

@Override
Expand All @@ -28,7 +28,7 @@ public Mono<InteractionResponse> createResponse(Interaction interaction) {
@Override
public Mono<InteractionResponse> autocompleteResponse(Interaction interaction) {
String userId = interaction.getMember().getUser().getId();
return destinyCharacterService.getCharactersForUser(userId)
return userCharacterService.getCharactersForUser(userId)
.map(character -> new Choice(CHOICE_FORMAT.formatted(
character.getLightLevel(), character.getCharacterRace(), character.getCharacterClass()),
character.getCharacterId()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.danielvm.destiny2bot.factory.creator;

import com.danielvm.destiny2bot.dto.discord.Interaction;
import com.danielvm.destiny2bot.dto.discord.InteractionResponse;
import com.danielvm.destiny2bot.dto.discord.InteractionResponseData;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
public class WhyAuthorizeButtonMessageCreator implements MessageComponentSource {

private static final Integer EPHEMERAL_BYTE = 1000000;

@Override
public Mono<InteractionResponse> messageComponentResponse(Interaction interaction) {
return Mono.just(InteractionResponse.builder()
.type(4)
.data(InteractionResponseData.builder()
.content(
"Without going into technical details, in order to retrieve interesting and relevant "
+ "information regarding your Destiny 2 characters, e.g., like Raid statistics, "
+ "**through Discord**, we need to link both your Bungie and Discord account together. "
+ "This command allows you to do just that. In the consent screen for both Discord "
+ "and Bungie you will be able to review all the permissions the Riven will have with "
+ "both your accounts. Be sure to read and review them well!")
.flags(EPHEMERAL_BYTE)
.build())
.build());
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.danielvm.destiny2bot.model.state;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
public class ErrorState {
@Component
public class ErrorState implements NotificationState {

private static final String ERROR_EMOJI_CODE = ":white_check_mark:";

public static void updateState(MutableMessagePart statefulContent) {
@Override
public void updateState(MutableMessagePart statefulContent) {
statefulContent.setEmoji(ERROR_EMOJI_CODE);
}

public static void logStatus(Long user, String character, Long characterId) {
@Override
public void logStatus(String user, String character, Long characterId) {
log.error("There was an error trying to index character [{}] with Id [{}] for user [{}]",
character, character, user);
}
Expand Down
Loading

0 comments on commit 3248795

Please sign in to comment.