Skip to content

Commit

Permalink
Abstract away from amb to more general slotbot
Browse files Browse the repository at this point in the history
  • Loading branch information
Alf-Melmac committed Oct 10, 2021
1 parent 0df4dc1 commit f67be66
Show file tree
Hide file tree
Showing 58 changed files with 1,082 additions and 467 deletions.
8 changes: 5 additions & 3 deletions src/main/java/de/webalf/slotbot/assembler/EventAssembler.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static Event fromDto(EventDto dto) {
.missionLength(dto.getMissionLength())
.reserveParticipating(dto.getReserveParticipating())
.details(EventFieldAssembler.fromDtoIterable(dto.getDetails()))
.discordInformation(EventDiscordInformationAssembler.fromDto(dto.getDiscordInformation()))
.discordInformation(EventDiscordInformationAssembler.fromDtoIterable(dto.getDiscordInformation()))
.build();
}

Expand All @@ -48,6 +48,7 @@ public static EventReferencelessDto toReferencelessDto(Event event) {
.date(dateTime.toLocalDate())
.startTime(dateTime.toLocalTime())
.creator(event.getCreator())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.hidden(event.isHidden())
.squadList(SquadAssembler.toReferencelessDtoList(event.getSquadList()))
.description(event.getDescription())
Expand All @@ -56,7 +57,7 @@ public static EventReferencelessDto toReferencelessDto(Event event) {
.missionLength(event.getMissionLength())
.reserveParticipating(event.getReserveParticipating())
.details(EventFieldAssembler.toReferencelessDtoList(event.getDetails()))
.discordInformation(EventDiscordInformationAssembler.toDto(event.getDiscordInformation()))
.discordInformation(EventDiscordInformationAssembler.toDtoSet(event.getDiscordInformation()))
.build();
}

Expand All @@ -72,13 +73,14 @@ static EventDto toAbstractDto(Event event) {
.date(dateTime.toLocalDate())
.startTime(dateTime.toLocalTime())
.creator(event.getCreator())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.hidden(event.isHidden())
.description(event.getDescription())
.pictureUrl(event.getPictureUrl())
.missionType(event.getMissionType())
.missionLength(event.getMissionLength())
.reserveParticipating(event.getReserveParticipating())
.discordInformation(EventDiscordInformationAssembler.toDto(event.getDiscordInformation()))
.discordInformation(EventDiscordInformationAssembler.toDtoSet(event.getDiscordInformation()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import de.webalf.slotbot.model.EventDiscordInformation;
import de.webalf.slotbot.model.dtos.EventDiscordInformationDto;
import de.webalf.slotbot.util.LongUtils;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* @author Alf
* @since 04.07.2021
Expand All @@ -18,22 +24,45 @@ public static EventDiscordInformation fromDto(EventDiscordInformationDto dto) {

return EventDiscordInformation.builder()
.channel(Long.parseLong(dto.getChannel()))
.guild(Long.parseLong(dto.getGuild()))
.infoMsg(LongUtils.parseLongWrapper(dto.getInfoMsg()))
.slotListMsgPartOne(LongUtils.parseLongWrapper(dto.getSlotListMsgPartOne()))
.slotListMsgPartTwo(LongUtils.parseLongWrapper(dto.getSlotListMsgPartTwo()))
.build();
}

public static EventDiscordInformationDto toDto(EventDiscordInformation discordInformation) {
static Set<EventDiscordInformation> fromDtoIterable(Iterable<? extends EventDiscordInformationDto> dtos) {
if (dtos == null) {
return Collections.emptySet();
}

return StreamSupport.stream(dtos.spliterator(), true)
.map(EventDiscordInformationAssembler::fromDto)
.collect(Collectors.toUnmodifiableSet());
}

private static EventDiscordInformationDto toDto(EventDiscordInformation discordInformation) {
if (discordInformation == null) {
return null;
}

return EventDiscordInformationDto.builder()
.channel(LongUtils.toString(discordInformation.getChannel()))
.guild(LongUtils.toString(discordInformation.getGuild()))
.infoMsg(LongUtils.toString(discordInformation.getInfoMsg()))
.slotListMsgPartOne(LongUtils.toString(discordInformation.getSlotListMsgPartOne()))
.slotListMsgPartTwo(LongUtils.toString(discordInformation.getSlotListMsgPartTwo()))
.channelUrl(getChannelUrl(discordInformation))
.build();
}

static Set<EventDiscordInformationDto> toDtoSet(Iterable<? extends EventDiscordInformation> eventFields) {
return StreamSupport.stream(eventFields.spliterator(), true)
.map(EventDiscordInformationAssembler::toDto)
.collect(Collectors.toUnmodifiableSet());
}

private String getChannelUrl(@NonNull EventDiscordInformation discordInformation) {
return "discord://discordapp.com/channels/" + discordInformation.getGuild() + "/" + discordInformation.getChannel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static EventApiDto toDto(@NonNull Event event) {
.date(dateTime.toLocalDate())
.startTime(dateTime.toLocalTime())
.creator(event.getCreator())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.hidden(event.isHidden())
.squadList(SquadApiAssembler.toDtoList(event.getSquadList()))
.description(event.getDescription())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private SlotApiViewDto toViewDto(Slot slot) {
return SlotApiViewDto.builder()
.name(slot.getName())
.number(slot.getNumber())
.user(userService.toUserNameDto(slot.getUser()))
.user(userService.toUserNameDto(slot.getUser(), slot.getEvent().getOwnerGuild()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.webalf.slotbot.assembler.EventFieldAssembler;
import de.webalf.slotbot.assembler.EventTypeAssembler;
import de.webalf.slotbot.configuration.properties.DiscordProperties;
import de.webalf.slotbot.model.Event;
import de.webalf.slotbot.model.EventField;
import de.webalf.slotbot.model.Slot;
Expand Down Expand Up @@ -32,7 +31,6 @@
@Component
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EventDetailsAssembler {
private final DiscordProperties discordProperties;
private final DiscordApiService discordApiService;

public EventDetailsDto toDto(@NonNull Event event) {
Expand All @@ -46,6 +44,7 @@ public EventDetailsDto toDto(@NonNull Event event) {
.date(dateTime.toLocalDate())
.startTime(dateTime.toLocalTime())
.creator(event.getCreator())
.ownerGuild(Long.toString(event.getOwnerGuild()))
.hidden(event.isHidden())
.squadList(toEventDetailsDtoList(event.getSquadList()))
.description(event.getDescription())
Expand All @@ -61,7 +60,6 @@ public EventEditDto toEditDto(@NonNull Event event) {
final LocalDateTime dateTime = event.getDateTime();

return EventEditDto.builder()
.channelUrl(getChannelUrl(event))
.id(event.getId())
.eventType(EventTypeAssembler.toDto(event.getEventType()))
.name(event.getName())
Expand All @@ -80,11 +78,10 @@ public EventEditDto toEditDto(@NonNull Event event) {
}

private String getChannelUrl(@NonNull Event event) {
String channelUrl = null;
if (event.isAssigned()) {
channelUrl = "discord://discordapp.com/channels/" + discordProperties.getGuild() + "/" + LongUtils.toString(event.getDiscordInformation().getChannel());
}
return channelUrl;
final long ownerGuild = event.getOwnerGuild();
return event.getDiscordInformation(ownerGuild)
.map(eventDiscordInformation -> "discord://discordapp.com/channels/" + ownerGuild + "/" + LongUtils.toString(eventDiscordInformation.getChannel()))
.orElse(null);
}

private List<EventFieldReferencelessDto> getDetails(List<EventField> details) {
Expand Down Expand Up @@ -133,7 +130,7 @@ private EventDetailsSlotDto toEventDetailsSlotDto(@NonNull Slot slot) {
}
blocked = true;
} else {
text = discordApiService.getName(LongUtils.toString(slot.getUser().getId()));
text = discordApiService.getName(LongUtils.toString(slot.getUser().getId()), slot.getSquad().getEvent().getOwnerGuild());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.webalf.slotbot.configuration.authentication.website;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.webalf.slotbot.service.external.DiscordApiService;
import de.webalf.slotbot.service.external.DiscordApiService.User;
import de.webalf.slotbot.service.external.DiscordAuthenticationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,7 +26,7 @@
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Slf4j
public class CustomOAuth2UserService extends DefaultOAuth2UserService {
private final DiscordApiService discordApiService;
private final DiscordAuthenticationService discordAuthenticationService;

@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) {
Expand Down Expand Up @@ -94,7 +94,7 @@ private Set<GrantedAuthority> mapAuthorities(Map<String, Object> attributes, Use
private Set<GrantedAuthority> getAuthorities(User discordUser, Map<String, Object> attributes) {
final Set<GrantedAuthority> grantedAuthorities = new HashSet<>();

discordApiService.getRoles(discordUser)
discordAuthenticationService.getRoles(discordUser.getId())
.forEach(role -> grantedAuthorities.add(new OAuth2UserAuthority(role, attributes)));

return grantedAuthorities;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.webalf.slotbot.configuration.authentication.website;

import de.webalf.slotbot.service.external.DiscordApiService;
import de.webalf.slotbot.service.external.DiscordAuthenticationService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -30,7 +30,7 @@
@EnableWebSecurity
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OAuth2EndpointConfig extends WebSecurityConfigurerAdapter {
private final DiscordApiService discordApiService;
private final DiscordAuthenticationService discordAuthenticationService;

@Override
protected void configure(HttpSecurity http) throws Exception {
Expand Down Expand Up @@ -61,7 +61,7 @@ public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest oauth2Reques

@Bean
public OAuth2UserService<OAuth2UserRequest, OAuth2User> oAuthUserService() {
DefaultOAuth2UserService service = new CustomOAuth2UserService(discordApiService);
DefaultOAuth2UserService service = new CustomOAuth2UserService(discordAuthenticationService);

service.setRequestEntityConverter(new OAuth2UserRequestEntityConverter() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
* @author Alf
Expand All @@ -18,14 +18,18 @@ public class DiscordProperties {
@NotBlank
private String token;

@NotBlank
private String guild;

@NotBlank
private String prefix = "!";

@NotNull
private long id;
private Map<Long, Long> archive;

private Long archive;
/**
* Returns the configured archive channel for the given guild
*
* @param guildId in which the event will be archived
* @return channel id of the archive channel
*/
public Long getArchive(long guildId) {
return archive.get(guildId);
}
}
21 changes: 15 additions & 6 deletions src/main/java/de/webalf/slotbot/controller/EventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import de.webalf.slotbot.model.dtos.referenceless.EventReferencelessDto;
import de.webalf.slotbot.model.dtos.website.CalendarEventDto;
import de.webalf.slotbot.service.EventService;
import de.webalf.slotbot.util.GuildUtils;
import de.webalf.slotbot.util.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,7 +22,8 @@
import java.util.List;

import static de.webalf.slotbot.util.eventfield.EventFieldUtils.getDefault;
import static de.webalf.slotbot.util.permissions.ApplicationPermissionHelper.HAS_ROLE_EVENT_MANAGE;
import static de.webalf.slotbot.util.permissions.ApplicationPermissionHelper.HAS_POTENTIALLY_ROLE_EVENT_MANAGE;
import static de.webalf.slotbot.util.permissions.PermissionHelper.assertEventManagePermission;

/**
* @author Alf
Expand All @@ -36,25 +39,31 @@ public class EventController {
@GetMapping(value = "/list")
public List<CalendarEventDto> getBetween(@RequestParam LocalDateTime start,
@RequestParam LocalDateTime end) {
return CalendarEventAssembler.toDtoList(eventService.findAllBetween(start, end));
return CalendarEventAssembler.toDtoList(eventService.findAllBetweenOfGuild(start, end, GuildUtils.getCurrentOwnerGuild()));
}

@PostMapping
@PreAuthorize(HAS_ROLE_EVENT_MANAGE)
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public EventReferencelessDto postEvent(@Valid @RequestBody EventDto event) {
assertEventManagePermission(LongUtils.parseLongWrapper(event.getOwnerGuild()));

return EventAssembler.toReferencelessDto(eventService.createEvent(event));
}

@PutMapping("/{id}")
@PreAuthorize(HAS_ROLE_EVENT_MANAGE)
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public EventReferencelessDto updateEvent(@PathVariable(value = "id") long eventId, @RequestBody EventDto event) {
assertEventManagePermission(eventService.getGuildIdByEventId(eventId));

event.setId(eventId);
return EventAssembler.toReferencelessDto(eventService.updateEvent(event));
}

@PostMapping("/editable")
@PreAuthorize(HAS_ROLE_EVENT_MANAGE)
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public EventReferencelessDto updateEventEditable(long pk, String name, String value) {
assertEventManagePermission(eventService.getGuildIdByEventId(pk));

EventDto dto = EventDto.builder().id(pk).build();
try {
ReflectionUtils.setField(dto.getClass().getSuperclass().getDeclaredField(name), dto, value);
Expand All @@ -66,7 +75,7 @@ public EventReferencelessDto updateEventEditable(long pk, String name, String va
}

@PutMapping("/fields")
@PreAuthorize(HAS_ROLE_EVENT_MANAGE)
@PreAuthorize(HAS_POTENTIALLY_ROLE_EVENT_MANAGE)
public List<EventFieldDefaultDto> getEventFieldDefaults(@RequestBody String eventTypeName) {
return getDefault(eventTypeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.web.servlet.ModelAndView;

import static de.webalf.slotbot.constant.Urls.ADMIN;
import static de.webalf.slotbot.util.ControllerUtils.addLayoutSettings;
import static de.webalf.slotbot.util.permissions.ApplicationPermissionHelper.HAS_ROLE_SYS_ADMIN;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
Expand All @@ -37,14 +38,13 @@ public class AdminUtilsWebController {
public ModelAndView getAdminUtilsHtml() {
ModelAndView mav = new ModelAndView("adminUtils");

mav.addObject("startUrl", linkTo(methodOn(StartWebController.class).getStart()).toUri().toString());
mav.addObject("adminUrl", linkTo(methodOn(AdminWebController.class).getAdminHtml()).toUri().toString());

mav.addObject("postActionUrl", linkTo(methodOn(AdminUtilsWebController.class)
.postAction("null"))
.toUri().toString()
.replace("null", "{action}"));

addLayoutSettings(mav);
return mav;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Set;

import static de.webalf.slotbot.constant.Urls.ADMIN;
import static de.webalf.slotbot.util.ControllerUtils.addLayoutSettings;
import static de.webalf.slotbot.util.permissions.ApplicationPermissionHelper.HAS_ROLE_ADMIN;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;
Expand All @@ -40,7 +41,6 @@ public class AdminWebController {
public ModelAndView getAdminHtml() {
ModelAndView mav = new ModelAndView("admin");

mav.addObject("startUrl", linkTo(methodOn(StartWebController.class).getStart()).toUri().toString());
mav.addObject("logsUrl", linkTo(methodOn(LogWebController.class).getLogsHtml()).toUri().toString());
mav.addObject("utilsUrl", linkTo(methodOn(AdminUtilsWebController.class).getAdminUtilsHtml()).toUri().toString());
mav.addObject("serverToggleUrl", linkTo(methodOn(AdminWebController.class).postServerRestart(null)).toUri().toString());
Expand All @@ -55,6 +55,7 @@ public ModelAndView getAdminHtml() {
.build());
mav.addObject("servers", servers);

addLayoutSettings(mav);
return mav;
}

Expand Down

0 comments on commit f67be66

Please sign in to comment.