Skip to content

Commit

Permalink
Merge branch 'release/0.5.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Jan 6, 2019
2 parents 1b558f5 + c553cb4 commit 0969016
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 209 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.faforever</groupId>
<artifactId>faf-moderator-client</artifactId>
<version>0.5.3</version>
<version>0.5.5</version>
<packaging>jar</packaging>

<name>faf-moderator-client</name>
Expand All @@ -23,7 +23,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>

<jitpack.faf-java-commons.version>046b482571d8b29b35c68249586e41f1b9e44857</jitpack.faf-java-commons.version>
<jitpack.faf-java-commons.version>98d878df4c19853d93b2d58269ac83a5897e1145</jitpack.faf-java-commons.version>
<jitpack.nocatch.version>1.1</jitpack.nocatch.version>
<jitpack.q-builders.version>1.6</jitpack.q-builders.version>
<jitpack.jasminb.jsonapi-converter.version>0.8</jitpack.jasminb.jsonapi-converter.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import com.faforever.commons.api.dto.ApiException;
import com.github.jasminb.jsonapi.exceptions.ResourceParseException;
import com.github.jasminb.jsonapi.models.errors.Errors;
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.client.DefaultResponseErrorHandler;

import java.io.IOException;
import java.io.InputStreamReader;

@Component
@Slf4j
public class JsonApiErrorHandler extends DefaultResponseErrorHandler {
private final JsonApiMessageConverter jsonApiMessageConverter;

Expand All @@ -21,6 +26,8 @@ public JsonApiErrorHandler(JsonApiMessageConverter jsonApiMessageConverter) {

@Override
public void handleError(ClientHttpResponse response) throws IOException {
log.warn("Api call returned with error code '{}' and body '{}'", response.getStatusCode(), CharStreams.toString(new InputStreamReader(response.getBody(), Charsets.UTF_8)));

if (response.getStatusCode() == HttpStatus.UNPROCESSABLE_ENTITY) {
try {
jsonApiMessageConverter.readInternal(Errors.class, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.faforever.moderatorclient.api.domain;

import com.faforever.commons.api.dto.BanInfo;
import com.faforever.commons.api.dto.BanRevokeData;
import com.faforever.commons.api.dto.Player;
import com.faforever.commons.api.elide.ElideNavigator;
import com.faforever.commons.api.elide.ElideNavigatorOnCollection;
import com.faforever.commons.api.elide.ElideNavigatorOnId;
import com.faforever.moderatorclient.api.FafApiCommunicationService;
import com.faforever.moderatorclient.mapstruct.BanInfoMapper;
import com.faforever.moderatorclient.mapstruct.BanRevokeDataMapper;
import com.faforever.moderatorclient.ui.domain.BanInfoFX;
import com.faforever.moderatorclient.ui.domain.BanRevokeDataFX;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
Expand All @@ -24,48 +21,39 @@
public class BanService {

private final BanInfoMapper banInfoMapper;
private final BanRevokeDataMapper banRevokeDataMapper;
private final FafApiCommunicationService fafApi;

public BanService(BanInfoMapper banInfoMapper, BanRevokeDataMapper banRevokeDataMapper, FafApiCommunicationService fafApi) {
public BanService(BanInfoMapper banInfoMapper, FafApiCommunicationService fafApi) {
this.banInfoMapper = banInfoMapper;
this.banRevokeDataMapper = banRevokeDataMapper;
this.fafApi = fafApi;
}

public BanInfo patchBanInfo(@NotNull BanInfoFX banInfoFX) {
BanInfo banInfo = banInfoMapper.map(banInfoFX);
log.debug("Patching BanInfo of id: ", banInfo.getId());
banInfo.setAuthor(null);
banInfo.setPlayer(null);
return fafApi.patch(ElideNavigator.of(BanInfo.class).id(banInfo.getId()), banInfo);
}

public BanRevokeData revokeBan(@NotNull BanRevokeDataFX banRevokeDataFX) {
BanRevokeData banRevokeData = banRevokeDataMapper.map(banRevokeDataFX);
log.debug("Revoking ban with id: ", banRevokeData.getBan().getId());
banRevokeData.setAuthor(fafApi.getSelfPlayer());
ElideNavigatorOnCollection<BanRevokeData> navigator = ElideNavigator.of(Player.class)
.id(banRevokeData.getBan().getId())
.navigateRelationship(BanRevokeData.class, "banRevokeData")
.collection();

return (BanRevokeData) fafApi.post(navigator, banRevokeData);
}

public String createBan(@NotNull BanInfoFX banInfoFX) {
BanInfo banInfo = banInfoMapper.map(banInfoFX);
log.debug("Creating ban");
banInfo.setAuthor(fafApi.getSelfPlayer());
return fafApi.post(ElideNavigator.of(BanInfo.class).collection(), banInfo).getId();
}

public CompletableFuture<List<BanInfoFX>> getAllBans() {
public CompletableFuture<List<BanInfoFX>> getLatestBans() {
return CompletableFuture.supplyAsync(() -> {
List<BanInfo> banInfos = fafApi.getAll(ElideNavigator.of(BanInfo.class)
List<BanInfo> banInfos = fafApi.getPage(ElideNavigator.of(BanInfo.class)
.collection()
.addIncludeOnCollection("player")
.addIncludeOnCollection("author")
.addIncludeOnCollection("banRevokeData")
.addIncludeOnCollection("banRevokeData.author")
.addIncludeOnCollection("revokeAuthor")
.addSortingRule("createTime", false),
100,
1,
ImmutableMap.of()
);
return banInfos.stream().map(banInfoMapper::map).collect(Collectors.toList());
});
Expand All @@ -79,4 +67,21 @@ public BanInfoFX getBanInfoById(String banInfoId) {
.addIncludeOnId("author");
return banInfoMapper.map(fafApi.getOne(navigator));
}

public void updateBan(BanInfo banInfoUpdate) {
log.debug("Update for ban id: " + banInfoUpdate.getId());
ElideNavigatorOnId<BanInfo> navigator = ElideNavigator.of(BanInfo.class)
.id(banInfoUpdate.getId());

fafApi.patch(navigator, banInfoUpdate);
}

public List<BanInfoFX> getBanInfoByBannedPlayerNameContains(String name) {
ElideNavigatorOnCollection<BanInfo> navigator = ElideNavigator.of(BanInfo.class)
.collection()
.addFilter(ElideNavigator.qBuilder().string("player.login").eq("*" + name + "*"))
.addIncludeOnCollection("player")
.addIncludeOnCollection("author");
return banInfoMapper.mapToFX(fafApi.getAll(navigator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ private <T extends ElideEntity> ElideNavigatorOnCollection<T> addModeratorInclud
.addIncludeOnCollection(variablePrefix + "avatarAssignments.avatar")
.addIncludeOnCollection(variablePrefix + "bans")
.addIncludeOnCollection(variablePrefix + "bans.author")
.addIncludeOnCollection(variablePrefix + "bans.banRevokeData")
.addIncludeOnCollection(variablePrefix + "bans.banRevokeData.author");
.addIncludeOnCollection(variablePrefix + "bans.revokeAuthor");
}

public List<PlayerFX> findLatestRegistrations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import java.util.List;

@Mapper(uses = {JavaFXMapper.class, BanRevokeDataMapper.class, PlayerMapper.class, CycleAvoidingMappingContext.class})
@Mapper(uses = {JavaFXMapper.class, PlayerMapper.class, CycleAvoidingMappingContext.class})
public abstract class BanInfoMapper {
@Mapping(target = "duration", ignore = true)
@Mapping(target = "banStatus", ignore = true)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.faforever.moderatorclient.ui;

import com.faforever.commons.api.dto.BanDurationType;
import com.faforever.commons.api.dto.BanInfo;
import com.faforever.commons.api.dto.BanLevel;
import com.faforever.moderatorclient.api.FafApiCommunicationService;
import com.faforever.moderatorclient.api.domain.BanService;
import com.faforever.moderatorclient.mapstruct.PlayerMapper;
import com.faforever.moderatorclient.ui.domain.BanInfoFX;
import com.faforever.moderatorclient.ui.domain.BanRevokeDataFX;
import com.faforever.moderatorclient.ui.domain.PlayerFX;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -54,16 +58,25 @@ public class BanInfoController implements Controller<Pane> {
public RadioButton globalBanRadioButton;
public Button revokeButton;
public Label userLabel;
public Label banIsRevokedNotice;
public TextField revocationTimeTextField;
public VBox revokeOptions;
@Getter
private BanInfoFX banInfo;
private Consumer<BanInfoFX> postedListener;
private Runnable onBanRevoked;

public BanInfoController(FafApiCommunicationService fafApi, BanService banService, PlayerMapper playerMapper) {
this.fafApi = fafApi;
this.banService = banService;
this.playerMapper = playerMapper;
}

public void addRevokedListener(Runnable listener) {
this.onBanRevoked = listener;
}


public void addPostedListener(Consumer<BanInfoFX> listener) {
this.postedListener = listener;
}
Expand All @@ -75,12 +88,24 @@ public Pane getRoot() {

@FXML
public void initialize() {
banIsRevokedNotice.managedProperty().bind(banIsRevokedNotice.visibleProperty());
}

public void onRevokeTimeTextChanged() {
revocationTimeTextField.setStyle("-fx-text-fill: green");
try {
DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(revocationTimeTextField.getText());
} catch (Exception e) {
revocationTimeTextField.setStyle("-fx-text-fill: red");
}
}

public void setBanInfo(BanInfoFX banInfo) {
this.banInfo = banInfo;

if (banInfo.getId() != null) {
revokeOptions.setDisable(false);

affectedUserTextField.setText(banInfo.getPlayer().representationProperty().get());
Optional.ofNullable(banInfo.getAuthor()).ifPresent(author -> banAuthorTextField.setText(author.representationProperty().get()));
banReasonTextField.setText(banInfo.getReason());
Expand All @@ -92,14 +117,19 @@ public void setBanInfo(BanInfoFX banInfo) {
temporaryBanRadioButton.setSelected(banInfo.getDuration() == BanDurationType.TEMPORARY);
Optional.ofNullable(banInfo.getExpiresAt()).ifPresent(offsetDateTime -> untilTextField.setText(offsetDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)));

if (banInfo.getBanRevokeData() != null) {
revocationReasonTextField.setText(banInfo.getBanRevokeData().getReason());
revocationAuthorTextField.setText(banInfo.getBanRevokeData().getAuthor().toString());
if (banInfo.getRevokeTime() != null) {
banIsRevokedNotice.setVisible(true);
revocationReasonTextField.setText(banInfo.getRevokeReason());
revocationTimeTextField.setText(banInfo.getRevokeTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
revocationAuthorTextField.setText(banInfo.getRevokeAuthor() == null ? "" : banInfo.getRevokeAuthor().getLogin());
} else {
revocationTimeTextField.setText(OffsetDateTime.now().atZoneSameInstant(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
}

chatOnlyBanRadioButton.setSelected(banInfo.getLevel() == BanLevel.CHAT);
globalBanRadioButton.setSelected(banInfo.getLevel() == BanLevel.GLOBAL);
} else {

PlayerFX player = banInfo.getPlayer();
if (player != null) {
affectedUserTextField.setText(player.representationProperty().get());
Expand Down Expand Up @@ -188,23 +218,43 @@ private boolean validate() {

public void onRevoke() {
Assert.notNull(banInfo, "You can't revoke if banInfo is null.");

List<String> errors = new ArrayList<>();

String revocationReason = revocationReasonTextField.getText();

if (StringUtils.isBlank(revocationReason)) {
new Alert(Alert.AlertType.ERROR, "The reason of revocation must not be empty", ButtonType.OK).showAndWait();
errors.add("The reason of revocation must not be empty.");
}
OffsetDateTime revokeTime = null;
try {
revokeTime = OffsetDateTime.of(LocalDateTime.parse(revocationTimeTextField.getText(), DateTimeFormatter.ISO_LOCAL_DATE_TIME), ZoneOffset.UTC);
} catch (Exception e) {
log.debug("Revoke time invalid", e);
errors.add("Invalid date for revocation.");
}

if (!errors.isEmpty()) {
ViewHelper.errorDialog("Could not revoke",
errors.stream().collect(Collectors.joining("\n")));
return;
}

log.debug("Revoking ban id '{}' with reason: {}", banInfo.getId(), revocationReason);

BanRevokeDataFX banRevokeData = new BanRevokeDataFX()
.setBan(banInfo)
.setAuthor(playerMapper.map(fafApi.getSelfPlayer()))
.setReason(revocationReason);
banInfo.setRevokeAuthor(playerMapper.map(fafApi.getSelfPlayer()));
banInfo.setRevokeReason(revocationReason);
banInfo.setRevokeTime(revokeTime);
banInfo.setUpdateTime(OffsetDateTime.now());

banService.revokeBan(banRevokeData);
BanInfo banInfoUpdate = new BanInfo();
banInfoUpdate.setId(banInfo.getId());
banInfoUpdate.setRevokeReason(revocationReason);
banInfoUpdate.setRevokeTime(revokeTime);

banService.updateBan(banInfoUpdate);
if (onBanRevoked != null) {
onBanRevoked.run();
}
close();
}

Expand Down

0 comments on commit 0969016

Please sign in to comment.