<commit_before>package com.faforever.api.user;

import com.faforever.api.data.domain.Player;
import com.faforever.api.data.domain.UserGroup;
import com.faforever.api.player.PlayerService;
import com.faforever.api.security.FafUserDetails;
import com.yahoo.elide.jsonapi.models.Data;
import com.yahoo.elide.jsonapi.models.JsonApiDocument;
import com.yahoo.elide.jsonapi.models.Resource;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Provides the route {@code /me} which returns the currently logged in user's information.
 */
@RestController
@RequiredArgsConstructor
public class MeController {
  private final PlayerService playerService;

  @RequestMapping(method = RequestMethod.GET, value = "/me")
  @ApiOperation("Returns the authentication object of the current user")
  @ApiResponse(code = 200, message = "Success with JsonApi compliant MeResult")
  @Secured("ROLE_USER")
  public JsonApiDocument me(@AuthenticationPrincipal FafUserDetails authentication) {

    Player player = playerService.getById(authentication.getId());
    Set<String> grantedAuthorities = authentication.getAuthorities().stream()
      .map(GrantedAuthority::getAuthority)
      .collect(Collectors.toSet());

    Set<String> groups = player.getUserGroups().stream()
      .map(UserGroup::getTechnicalName)
      .collect(Collectors.toSet());

    return new JsonApiDocument(new Data<>(
      new Resource("me",
        "me",
        Map.of(
          "userId", player.getId(),
          "userName", player.getLogin(),
          "email", player.getEmail(),
          "clan", player.getClan() == null ? Optional.empty() : Clan.builder()
            .id(player.getClan().getId())
            .membershipId(player.getClanMembership().getId())
            .tag(player.getClan().getTag())
            .name(player.getClan().getName())
            .build(),
          "lastLogin", player.getLastLogin(),
          "groups", groups,<commit_msg>Wrap last login in optionalNullable<commit_after>package com.faforever.api.user;

import com.faforever.api.data.domain.Player;
import com.faforever.api.data.domain.UserGroup;
import com.faforever.api.player.PlayerService;
import com.faforever.api.security.FafUserDetails;
import com.yahoo.elide.jsonapi.models.Data;
import com.yahoo.elide.jsonapi.models.JsonApiDocument;
import com.yahoo.elide.jsonapi.models.Resource;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Provides the route {@code /me} which returns the currently logged in user's information.
 */
@RestController
@RequiredArgsConstructor
public class MeController {
  private final PlayerService playerService;

  @RequestMapping(method = RequestMethod.GET, value = "/me")
  @ApiOperation("Returns the authentication object of the current user")
  @ApiResponse(code = 200, message = "Success with JsonApi compliant MeResult")
  @Secured("ROLE_USER")
  public JsonApiDocument me(@AuthenticationPrincipal FafUserDetails authentication) {

    Player player = playerService.getById(authentication.getId());
    Set<String> grantedAuthorities = authentication.getAuthorities().stream()
      .map(GrantedAuthority::getAuthority)
      .collect(Collectors.toSet());

    Set<String> groups = player.getUserGroups().stream()
      .map(UserGroup::getTechnicalName)
      .collect(Collectors.toSet());

    return new JsonApiDocument(new Data<>(
      new Resource("me",
        "me",
        Map.of(
          "userId", player.getId(),
          "userName", player.getLogin(),
          "email", player.getEmail(),
          "clan", player.getClan() == null ? Optional.empty() : Clan.builder()
            .id(player.getClan().getId())
            .membershipId(player.getClanMembership().getId())
            .tag(player.getClan().getTag())
            .name(player.getClan().getName())
            .build(),
          "lastLogin", Optional.ofNullable(player.getLastLogin()),
          "groups", groups,
{'fraction_non_alphanumeric': 0.12068629362448821, 'fraction_numerical': 0.0011698186781048938, 'mean_word_length': 4.377358490566038, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624798', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1436, 'n_words': 273}