Adding profile pictures to leaderboards. adapting privacy visibility to leaderboards.
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-29 20:48:08 +01:00
parent fb94421247
commit 53cfce5359
5 changed files with 63 additions and 47 deletions
@@ -3,9 +3,11 @@ package SuMoC.backend.gamification.scoretable;
import SuMoC.backend.gamification.scoretable.models.Leaderboard;
import SuMoC.backend.gamification.Timeframe;
import SuMoC.backend.gamification.scoretable.models.LeaderboardEntry;
import SuMoC.backend.images.ImageBase64Util;
import SuMoC.backend.user.models.User;
import SuMoC.backend.user.service.CurrentUserService;
import SuMoC.backend.user.service.FriendshipService;
import SuMoC.backend.user.service.PrivacyService;
import SuMoC.backend.user.stats.*;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@@ -15,24 +17,25 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class LeaderboardService {
private final FriendshipService friendshipService;
private final StatsListRepository statsListRepository;
private final CurrentUserService currentUser;
private final PrivacyService privacyService;
public LeaderboardService(
FriendshipService friendshipService,
StatsListRepository statsListRepository,
CurrentUserService currentUser
) {
CurrentUserService currentUser,
PrivacyService privacyService) {
this.friendshipService = friendshipService;
this.statsListRepository = statsListRepository;
this.currentUser = currentUser;
this.privacyService = privacyService;
}
/**
@@ -56,30 +59,21 @@ public class LeaderboardService {
User user = currentUser.get();
List<StatsList> topStats = getTopStats(timeframe, field, limit);
List<Long> topUserIds = topStats.stream()
.map(stats -> stats.getUser().getId())
.toList();
Map<Long, FriendshipService.NameWithFriendship> nameMap =
friendshipService.getPrivateNameIfFriendshipAccepted(user, topUserIds)
.stream()
.collect(Collectors.toMap(FriendshipService.NameWithFriendship::id, n -> n));
Leaderboard leaderboard = new Leaderboard();
leaderboard.setEntries(new ArrayList<>());
List<User> friendships = friendshipService.getAcceptedFriends(user);
for (StatsList statsList : topStats) {
User topUser = statsList.getUser();
FriendshipService.NameWithFriendship nwf = nameMap.get(topUser.getId());
String displayName = (nwf != null) ? nwf.name() : topUser.getPublicName();
boolean friendship = (nwf != null) && nwf.friendship();
boolean ps = privacyService.checkVisibility(user, topUser.getId());
LeaderboardEntry entry = new LeaderboardEntry(
topUser.getId(),
displayName,
friendship,
statsList.getStatValue(timeframe, field)
ps ? topUser.getPrivateName() : topUser.getPublicName(),
friendships
.stream()
.anyMatch(o -> o.getId().equals(topUser.getId())),
statsList.getStatValue(timeframe, field),
ps ? ImageBase64Util.encode(topUser.getProfileImage().getImageData()) : null
);
leaderboard.getEntries().add(entry);
@@ -89,8 +83,9 @@ public class LeaderboardService {
LeaderboardEntry userEntry = new LeaderboardEntry(
user.getId(),
user.getPrivateName(),
true,
user.getStats().getStatValue(timeframe, field)
false,
user.getStats().getStatValue(timeframe, field),
ImageBase64Util.encode(user.getProfileImage().getImageData())
);
leaderboard.getEntries().add(userEntry);
}
@@ -127,30 +122,32 @@ public class LeaderboardService {
List<User> friends = friendshipService.getAcceptedFriends(user);
Leaderboard leaderboard = new Leaderboard();
leaderboard.setEntries(new ArrayList<>()); // Initialize list
leaderboard.setEntries(new ArrayList<>());
for (User friend : friends) {
StatsList statsList = friend.getStats();
if (statsList != null) {
Long statValue = statsList.getStatValue(timeframe, field);
if (statValue != null) {
LeaderboardEntry entry = new LeaderboardEntry(
friend.getId(),
friend.getPrivateName(),
true,
statValue
);
leaderboard.getEntries().add(entry);
}
}
boolean ps = privacyService.checkVisibility(user, friend.getId());
Long statValue = statsList.getStatValue(timeframe, field);
LeaderboardEntry entry = new LeaderboardEntry(
friend.getId(),
ps ? friend.getPrivateName() : friend.getPublicName(),
true,
statValue,
ps ? ImageBase64Util.encode(friend.getProfileImage().getImageData()): null
);
leaderboard.getEntries().add(entry);
}
if (leaderboard.getEntries().stream().noneMatch(e -> e.getUserId().equals(user.getId()))) {
leaderboard.getEntries().add(new LeaderboardEntry(
user.getId(),
user.getPrivateName(),
true,
user.getStats().getStatValue(timeframe, field)
false,
user.getStats().getStatValue(timeframe, field),
ImageBase64Util.encode(user.getProfileImage().getImageData())
));
}
@@ -5,12 +5,15 @@ public class LeaderboardEntry {
private String username;
private Boolean isFriend;
private Long score;
private String base64ProfilePicture;
public LeaderboardEntry(Long userId, String username, Boolean isFriend, Long score) {
public LeaderboardEntry(Long userId, String username, Boolean isFriend, Long score, String base64ProfilePicture) {
this.userId = userId;
this.username = username;
this.isFriend = isFriend;
this.score = score;
this.base64ProfilePicture = base64ProfilePicture;
}
public LeaderboardEntry() {}
@@ -27,4 +30,12 @@ public class LeaderboardEntry {
public Long getScore() { return score; }
public void setScore(Long score) { this.score = score; }
public String getBase64ProfilePicture() {
return base64ProfilePicture;
}
public void setBase64ProfilePicture(String base64ProfilePicture) {
this.base64ProfilePicture = base64ProfilePicture;
}
}
@@ -37,14 +37,14 @@ public class TripController {
@Operation(summary = "User uploads a new trip from the App.")
@PostMapping("/uploadTrip")
public ResponseEntity<Void> uploadTrip(@Valid @RequestBody NewTripDTO newTripDTO) {
public ResponseEntity<Long> uploadTrip(@Valid @RequestBody NewTripDTO newTripDTO) {
User user = getAuthenticatedUser();
Trip savedTrip = tripService.addTrip(user, newTripDTO);
// let the ad hoc service suggest an alternative more ecofriendly route if appropriate
adHocSuggestionService.checkNewTripForNotifiedAlternativeSuggestion(savedTrip);
return ResponseEntity.status(HttpStatus.CREATED).build();
return ResponseEntity.status(HttpStatus.CREATED).body(savedTrip.getId());
}
@Operation(summary = "Retrieve all trips of the authenticated user.")
@@ -28,6 +28,8 @@ public class PrivacyService {
public boolean checkVisibility(User sender, User requested) {
if(sender.equals(requested)) { return true; }
if(requested.getVisibilityPrivacy().equals(VisibilityPrivacy.NONE)) { return false; }
if(friendshipService.getBlockedUsers(requested.getId()).stream()