Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into jwt
  • Loading branch information
Daggerpov committed Feb 14, 2025
2 parents bda1de5 + 9d15835 commit 1339379
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ResponseEntity<FriendRequestDTO> createFriendRequest(@RequestBody FriendR

// full path: /api/v1/friend-requests/{friendRequestId}?friendRequestAction={accept/reject}
@PutMapping("{friendRequestId}")
public ResponseEntity<Void> acceptFriendRequest(@PathVariable UUID friendRequestId, @RequestParam FriendRequestAction friendRequestAction) {
public ResponseEntity<Void> friendRequestAction(@PathVariable UUID friendRequestId, @RequestParam FriendRequestAction friendRequestAction) {
try {
if (friendRequestAction == FriendRequestAction.accept){
friendRequestService.acceptFriendRequest(friendRequestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.danielagapov.spawn.DTOs.RecommendedFriendUserDTO;
import com.danielagapov.spawn.DTOs.UserDTO;
import com.danielagapov.spawn.Exceptions.Base.BaseNotFoundException;
import com.danielagapov.spawn.Services.FriendRequestService.IFriendRequestService;
import com.danielagapov.spawn.Services.S3.IS3Service;
import com.danielagapov.spawn.Services.User.IUserService;
import org.springframework.http.HttpStatus;
Expand All @@ -20,12 +19,10 @@
@RequestMapping("api/v1/users")
public class UserController {
private final IUserService userService;
private final IFriendRequestService friendRequestService;
private final IS3Service s3Service;

public UserController(IUserService userService, IFriendRequestService friendRequestService, IS3Service s3Service) {
public UserController(IUserService userService, IS3Service s3Service) {
this.userService = userService;
this.friendRequestService = friendRequestService;
this.s3Service = s3Service;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.UUID;

@Service
public class OAuthService implements IOAuthService {
Expand Down Expand Up @@ -63,16 +64,40 @@ public FullUserDTO makeUser(UserDTO userDTO, String externalUserId, byte[] profi

@Override
public FullUserDTO getUserIfExistsbyExternalId(String externalUserId, String email) {
UserIdExternalIdMap mapping;
try {
UserIdExternalIdMap mapping = getMapping(externalUserId);
return mapping == null ? userService.getFullUserByEmail(email) : getFullUserDTO(mapping);
mapping = getMapping(externalUserId);
} catch (DataAccessException e) {
logger.log("Database error while fetching user by external ID: " + e.getMessage());
logger.log("Database error while fetching external user id <> spawn user id by externalUserId(" + externalUserId + ") :" + e.getMessage());
throw e;
} catch (Exception e) {
logger.log("Unexpected error while fetching user by external ID: " + e.getMessage());
logger.log("Unexpected error while fetching user by externalUserId (" + externalUserId + ") : " + e.getMessage());
throw e;
}

if (mapping == null) {
// if not (signed in through external provider, but no external id <> user id mapping -> try finding by email
try {
return userService.getFullUserByEmail(email);
} catch (DataAccessException e) {
logger.log("Database error while fetching user by email(" + email + "): " + e.getMessage());
throw e;
} catch (Exception e) {
logger.log("Unexpected error while fetching user by email(" + email + "): " + e.getMessage());
throw e;
}
} else {
// if there is already a mapping (Spawn account exists, given externalUserId) -> get the associated `FullUserDTO`
try {
return getFullUserDTO(mapping.getUser().getId());
} catch (DataAccessException e) {
logger.log("Database error while fetching user by externalUserId(" + externalUserId + "): " + e.getMessage());
throw e;
} catch (Exception e) {
logger.log("Unexpected error while fetching user by externalUserId(" + externalUserId + "): " + e.getMessage());
throw e;
}
}
}

/**
Expand Down Expand Up @@ -141,9 +166,9 @@ private UserDTO getUserDTO(UserIdExternalIdMap mapping) {
}
}

private FullUserDTO getFullUserDTO(UserIdExternalIdMap mapping) {
private FullUserDTO getFullUserDTO(UUID externalUserId) {
try {
return userService.getFullUserById(mapping.getUser().getId());
return userService.getFullUserById(externalUserId);
} catch (BaseNotFoundException e) {
logger.log("User not found while fetching full user DTO: " + e.getMessage());
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,13 @@ public List<UserDTO> getFriendsByUserId(UUID userId) {

// Retrieve the friends for each FriendTag and return as a flattened list
List<UserDTO> friends = getFriendsByFriendTagId(everyoneTag.getId());
System.out.printf("friends: %s%n", friends);
return friends;

// Filter out the friend whose ID matches the userId
List<UserDTO> filteredFriends = friends.stream()
.filter(friend -> !friend.id().equals(userId)) // Exclude the user themselves
.collect(Collectors.toList());

return filteredFriends;
} catch (Exception e) {
logger.log(e.getMessage());
throw e;
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/danielagapov/spawn/SpawnApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ public static void main(String[] args) {
} catch (NullPointerException e) {
System.err.println("Error: MYSQL_PASSWORD environment variable not set. Consider setting, or adding a .env file.");
}
try {
System.setProperty("GOOGLE_CLIENT_SECRET",
System.getenv("GOOGLE_CLIENT_SECRET") != null ? System.getenv("GOOGLE_CLIENT_SECRET") : dotenv.get("GOOGLE_CLIENT_SECRET"));
} catch (NullPointerException e) {
System.err.println("Error: GOOGLE_CLIENT_SECRET environment variable not set.");
}
SpringApplication.run(SpawnApplication.class, args);
}
}

0 comments on commit 1339379

Please sign in to comment.