javastrava.util.PrivacyUtils Maven / Gradle / Ivy
Show all versions of javastrava-api Show documentation
/**
*
*/
package javastrava.util;
import java.util.ArrayList;
import java.util.List;
import javastrava.api.v3.auth.model.Token;
import javastrava.api.v3.model.StravaActivity;
import javastrava.api.v3.model.StravaClub;
import javastrava.api.v3.model.StravaGear;
import javastrava.api.v3.model.StravaSegment;
import javastrava.api.v3.model.StravaSegmentEffort;
import javastrava.api.v3.model.StravaSegmentLeaderboard;
import javastrava.api.v3.model.StravaSegmentLeaderboardEntry;
import javastrava.api.v3.model.reference.StravaResourceState;
/**
* @author Dan Shannon
*
*/
public class PrivacyUtils {
/**
*
* Check that an activity belongs to the authenticated user
*
* @param activity The activity to check
* @param token The access token in use
* @return true
if the activity belongs to the authenticated user, false
otherwise
*/
private static boolean activityBelongsToAuthenticatedUser(final StravaActivity activity, final Token token) {
return activity.getAthlete().getId().equals(token.getAthlete().getId());
}
/**
* Checks if an activity is private
* @param activity The activity to check
* @return true
if and only if the privateActivity flag is set to Boolean.TRUE
*/
private static boolean activityIsPrivate(final StravaActivity activity) {
return ((activity.getPrivateActivity() != null) && activity.getPrivateActivity().equals(Boolean.TRUE));
}
/**
*
* Removes private activities from the list (by changing private ones to activities with resourceState=PRIVATE)
*
* @param activities The list of activities to process
* @param token The access token in use (so we can check if it has view_private acess)
* @return Modified list of activities
*/
public static List handlePrivateActivities(final List activities, final Token token) {
if (activities == null) {
return null;
}
final List returnedActivities = new ArrayList();
for (final StravaActivity activity : activities) {
// If the activity is not private then it's OK to include
if (!activityIsPrivate(activity)) {
returnedActivities.add(activity);
}
// Else if it *is* private, and there's no view_private access or it belongs to someone else, then add as the private activity
else {
if (activityBelongsToAuthenticatedUser(activity, token) && token.hasViewPrivate()) {
returnedActivities.add(activity);
} else {
returnedActivities.add(PrivacyUtils.privateActivity(activity.getId()));
}
}
}
return returnedActivities;
}
/**
*
* Returns the list of segments with any that should be flagged as private having their data cleared and resource state set to {@link StravaResourceState#PRIVATE}
*
* @param efforts List of efforts to be 'privatised'
* @param token The access token in use
* @return Modified list of efforts
*/
public static List handlePrivateSegmentEfforts(final List efforts, final Token token) {
return efforts;
// if (efforts == null) {
// return null;
// }
//
// // Build a set of segments and activities to check for privacy
// Set activities = new HashSet();
// Set segments = new HashSet();
// for (StravaSegmentEffort effort : efforts) {
// activities.add(effort.getActivity());
// segments.add(effort.getSegment());
// }
//
// // Now make a list of the activities that are private
// Set privateActivities = new HashSet();
// for (StravaActivity activity : activities) {
// StravaActivity stravaActivity = token.getService(ActivityService.class).getActivity(activity.getId());
// if (stravaActivity.getResourceState() == StravaResourceState.PRIVATE) {
// privateActivities.add(activity);
// }
// }
//
// // Now make a list of the segments that are private
// Set privateSegments = new HashSet();
// for (StravaSegment segment : segments) {
// StravaSegment stravaSegment = token.getService(SegmentService.class).getSegment(segment.getId());
// if (stravaSegment.getResourceState() == StravaResourceState.PRIVATE) {
// privateSegments.add(segment);
// }
// }
//
// // Now run through all the efforts and fix the ones for private segments/activities
// List returnedEfforts = new ArrayList();
// for (StravaSegmentEffort effort : efforts) {
// if (privateActivities.contains(effort.getActivity()) || privateSegments.contains(effort.getSegment())) {
// returnedEfforts.add(privateSegmentEffort(effort.getId()));
// } else {
// returnedEfforts.add(effort);
// }
// }
//
// // That's it
// return returnedEfforts;
}
/**
*
* Removes private segments from the given list (by replacing them with segments with resourceState = {@link StravaResourceState#PRIVATE}
*
* @param segments The list of segments to be 'privatised'
* @param token The access token being used; will be checked for view_private access for segments belonging to the authenticated user
* @return The modified list of segments
*/
public static List handlePrivateSegments(final List segments, final Token token) {
if (segments == null) {
return null;
}
final List returnedSegments = new ArrayList();
for (final StravaSegment segment : segments) {
// If the segment is not flagged as private then its ok to include
if (!segmentIsPrivate(segment)) {
returnedSegments.add(segment);
}
// Otherwise if it belongs to the authenticated user and the token has view_private scope, then it's OK to return
else {
if (token.hasViewPrivate()) {
returnedSegments.add(segment);
} else {
returnedSegments.add(PrivacyUtils.privateSegment(segment.getId()));
}
}
}
return returnedSegments;
}
/**
* Creates an activity with the given id and resourceState = {@link StravaResourceState#PRIVATE}
* @param activityId the activity id to be given to the private activity
* @return The private activity
*/
public static StravaActivity privateActivity(final Long activityId) {
final StravaActivity activity = new StravaActivity();
activity.setId(activityId);
activity.setResourceState(StravaResourceState.PRIVATE);
return activity;
}
/**
* Creates a {@link StravaClub} with resourceState = {@link StravaResourceState#PRIVATE}
* @param id The id of the club to create
* @return The private club
*/
public static StravaClub privateClubRepresentation(final Integer id) {
final StravaClub club = new StravaClub();
club.setId(id);
club.setResourceState(StravaResourceState.PRIVATE);
return club;
}
/**
* Creates a {@link StravaGear} with resourceState = {@link StravaResourceState#PRIVATE}
* @param id The id of the gear to create
* @return The private gear
*/
public static StravaGear privateGear(final String id) {
final StravaGear gear = new StravaGear();
gear.setId(id);
gear.setResourceState(StravaResourceState.PRIVATE);
return gear;
}
/**
* Creates a {@link StravaSegment} with resourceState = {@link StravaResourceState#PRIVATE}
* @param id The id of the segment to create
* @return The private segment
*/
public static StravaSegment privateSegment(final Integer id) {
final StravaSegment segment = new StravaSegment();
segment.setId(id);
segment.setResourceState(StravaResourceState.PRIVATE);
return segment;
}
/**
* Creates a {@link StravaSegmentEffort} with resourceState = {@link StravaResourceState#PRIVATE}
* @param id The id of the effort to create
* @return The private effort
*/
public static StravaSegmentEffort privateSegmentEffort(final Long id) {
final StravaSegmentEffort effort = new StravaSegmentEffort();
effort.setId(id);
effort.setResourceState(StravaResourceState.PRIVATE);
return effort;
}
/**
*
* Creates a {@link StravaSegmentLeaderboard} with resourceState = {@link StravaResourceState#PRIVATE}
*
* @return The private leaderboard
*/
public static StravaSegmentLeaderboard privateSegmentLeaderboard() {
StravaSegmentLeaderboard leaderboard = new StravaSegmentLeaderboard();
leaderboard = new StravaSegmentLeaderboard();
leaderboard.setNeighborhoodCount(Integer.valueOf(1));
leaderboard.setAthleteEntries(new ArrayList());
leaderboard.setEntries(new ArrayList());
leaderboard.setEffortCount(Integer.valueOf(0));
leaderboard.setEntryCount(Integer.valueOf(0));
leaderboard.setResourceState(StravaResourceState.PRIVATE);
return leaderboard;
}
/**
*
* Checks if a segment is flagged as private
*
* @param segment the segment to check
* @return true
if the segment is flagged as private, false
otherwise
*/
private static boolean segmentIsPrivate(final StravaSegment segment) {
return ((segment.getPrivateSegment() != null) && segment.getPrivateSegment().equals(Boolean.TRUE));
}
}