-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathClanService.java
More file actions
136 lines (111 loc) · 4.61 KB
/
ClanService.java
File metadata and controls
136 lines (111 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.faforever.api.clan;
import com.faforever.api.clan.result.ClanResult;
import com.faforever.api.clan.result.InvitationResult;
import com.faforever.api.clan.result.PlayerResult;
import com.faforever.api.config.FafApiProperties;
import com.faforever.api.data.domain.Clan;
import com.faforever.api.data.domain.ClanMembership;
import com.faforever.api.data.domain.Player;
import com.faforever.api.error.ApiException;
import com.faforever.api.error.Error;
import com.faforever.api.error.ErrorCode;
import com.faforever.api.player.PlayerService;
import com.faforever.api.security.JwtService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class ClanService {
private final ClanRepository clanRepository;
private final FafApiProperties fafApiProperties;
private final JwtService jwtService;
private final ObjectMapper objectMapper;
private final PlayerService playerService;
private final ClanMembershipRepository clanMembershipRepository;
@Transactional
public void preCreate(Clan clan) {
Assert.isNull(clan.getId(), "Clan payload with id can not be used for creation.");
Player player = playerService.getCurrentPlayer();
if (player.getClanMembership() != null) {
throw ApiException.of(ErrorCode.CLAN_CREATE_FOUNDER_IS_IN_A_CLAN);
}
if (!player.equals(clan.getFounder())) {
throw ApiException.of(ErrorCode.CLAN_INVALID_FOUNDER);
}
clanRepository.findOneByName(clan.getName()).ifPresent(c -> {
throw ApiException.of(ErrorCode.CLAN_NAME_EXISTS, clan.getName());
});
clanRepository.findOneByTag(clan.getTag()).ifPresent(c -> {
throw ApiException.of(ErrorCode.CLAN_TAG_EXISTS, clan.getTag());
});
clan.setLeader(player);
final ClanMembership clanMembership = new ClanMembership();
clanMembership.setClan(clan);
clanMembership.setPlayer(player);
clan.setMemberships(Set.of(clanMembership));
}
@SneakyThrows
@Transactional
@Deprecated
// use POST via Elide instead
public Clan create(String name, String tag, String description) {
Clan clan = new Clan();
clan.setName(name);
clan.setTag(tag);
clan.setDescription(description);
clan.setRequiresInvitation(true);
Player currentPlayer = playerService.getCurrentPlayer();
clan.setFounder(currentPlayer);
clan.setLeader(currentPlayer);
// validation is done at preCreate() called by ClanListener
clanRepository.save(clan);
return clan;
}
@SneakyThrows
@Transactional
public String generatePlayerInvitationToken(int newMemberId, int clanId) {
Player requester = playerService.getCurrentPlayer();
Clan clan = clanRepository.findById(clanId)
.orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
if (!requester.getId().equals(clan.getLeader().getId())) {
throw ApiException.of(ErrorCode.CLAN_NOT_LEADER, clanId);
}
Player newMember = playerService.getById(newMemberId);
long expire = Instant.now()
.plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES)
.toEpochMilli();
InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
return jwtService.sign(result);
}
@SneakyThrows
void acceptPlayerInvitationToken(String stringToken) {
String decodedToken = jwtService.decodeAndVerify(stringToken);
InvitationResult invitation = objectMapper.readValue(decodedToken, InvitationResult.class);
if (invitation.isExpired()) {
throw ApiException.of(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE);
}
final Integer clanId = invitation.clan().id();
Player player = playerService.getCurrentPlayer();
Clan clan = clanRepository.findById(clanId)
.orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
Player newMember = playerService.getById(invitation.newMember().id());
if (!Objects.equals(player.getId(), newMember.getId())) {
throw ApiException.of(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER);
}
if (newMember.getClan() != null) {
throw ApiException.of(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN);
}
ClanMembership membership = new ClanMembership();
membership.setClan(clan);
membership.setPlayer(newMember);
clanMembershipRepository.save(membership);
}
}