Skip to content

Commit

Permalink
Implement APIv3/Game endpoints for getting events by a user (#673)
Browse files Browse the repository at this point in the history
Also cleans up some naming to make things more clear.
  • Loading branch information
jvyden authored Oct 16, 2024
2 parents c70bd6a + 9f1e25b commit add6077
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using JetBrains.Annotations;
using MongoDB.Bson;
using Refresh.GameServer.Types.Activity;
Expand Down Expand Up @@ -92,6 +93,15 @@ ActivityQueryParameters parameters
.Where(e => e._StoredDataType == 1 && e.StoredSequentialId == level.LevelId)
.OrderByDescending(e => e.Timestamp), parameters.Skip, parameters.Count);
}

[Pure]
public DatabaseList<Event> GetRecentActivityFromUser(ActivityQueryParameters parameters)
{
Debug.Assert(parameters.User != null);
return new DatabaseList<Event>(this.GetRecentActivity(parameters)
.Where(e => e.User?.UserId == parameters.User.UserId)
.OrderByDescending(e => e.Timestamp), parameters.Skip, parameters.Count);
}

public int GetTotalEventCount() => this.Events.Count();
}
60 changes: 59 additions & 1 deletion Refresh.GameServer/Endpoints/ApiV3/ActivityApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,65 @@ public ApiResponse<ApiActivityPageResponse> GetRecentActivityForLevel(RequestCon

(int skip, int count) = context.GetPageData();

ActivityPage page = ActivityPage.ApiLevelActivity(database, level, new ActivityQueryParameters
ActivityPage page = ActivityPage.ApiForLevelActivity(database, level, new ActivityQueryParameters
{
Timestamp = timestamp,
Skip = skip,
Count = count,
User = user,
}, dataContext, false);
return ApiActivityPageResponse.FromOld(page, dataContext);
}

[ApiV3Endpoint("users/uuid/{uuid}/activity"), Authentication(false)]
[DocUsesPageData, DocSummary("Fetch a list of recent happenings for a particular user")]
[DocQueryParam("timestamp", "A timestamp in unix seconds, used to search backwards")]
[DocError(typeof(ApiValidationError), ApiValidationError.NumberParseErrorWhen)]
[DocError(typeof(ApiNotFoundError), "The user could not be found")]
public ApiResponse<ApiActivityPageResponse> GetRecentActivityForUserUuid(RequestContext context,
GameDatabaseContext database, IDataStore dataStore,
[DocSummary("The UUID of the user")] string uuid, DataContext dataContext)
{
long timestamp = 0;

string? tsStr = context.QueryString["timestamp"];
if (tsStr != null && !long.TryParse(tsStr, out timestamp)) return ApiValidationError.NumberParseError;

GameUser? user = database.GetUserByUuid(uuid);
if (user == null) return ApiNotFoundError.Instance;

(int skip, int count) = context.GetPageData();

ActivityPage page = ActivityPage.ApiFromUserActivity(database, new ActivityQueryParameters
{
Timestamp = timestamp,
Skip = skip,
Count = count,
User = user,
}, dataContext, false);
return ApiActivityPageResponse.FromOld(page, dataContext);
}

[ApiV3Endpoint("users/name/{username}/activity"), Authentication(false)]
[DocUsesPageData, DocSummary("Fetch a list of recent happenings for a particular user")]
[DocQueryParam("timestamp", "A timestamp in unix seconds, used to search backwards")]
[DocError(typeof(ApiValidationError), ApiValidationError.NumberParseErrorWhen)]
[DocError(typeof(ApiNotFoundError), "The user could not be found")]
public ApiResponse<ApiActivityPageResponse> GetRecentActivityForUserUsername(RequestContext context,
GameDatabaseContext database, IDataStore dataStore,
[DocSummary("The username of the user")] string username, DataContext dataContext)
{
long timestamp = 0;

string? tsStr = context.QueryString["timestamp"];
if (tsStr != null && !long.TryParse(tsStr, out timestamp)) return ApiValidationError.NumberParseError;

GameUser? user = database.GetUserByUsername(username);
if (user == null) return ApiNotFoundError.Instance;

(int skip, int count) = context.GetPageData();

ActivityPage page = ActivityPage.ApiFromUserActivity(database, new ActivityQueryParameters
{
Timestamp = timestamp,
Skip = skip,
Expand Down
8 changes: 4 additions & 4 deletions Refresh.GameServer/Endpoints/Game/ActivityEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ActivityEndpoints : EndpointGroup

if (endTimestamp == 0) endTimestamp = timestamp - 86400000 * 7; // 1 week

return ActivityPage.UserActivity(database, new ActivityQueryParameters
return ActivityPage.GameUserActivity(database, new ActivityQueryParameters
{
Timestamp = timestamp,
EndTimestamp = endTimestamp,
Expand Down Expand Up @@ -77,7 +77,7 @@ public Response GetRecentActivityForLevel(RequestContext context, GameDatabaseCo

if (endTimestamp == 0) endTimestamp = timestamp - 86400000 * 7; // 1 week

ActivityPage page = ActivityPage.GameLevelActivity(database, level, new ActivityQueryParameters
ActivityPage page = ActivityPage.GameForLevelActivity(database, level, new ActivityQueryParameters
{
Count = 20,
Skip = 0,
Expand All @@ -95,7 +95,7 @@ public Response GetRecentActivityForLevel(RequestContext context, GameDatabaseCo
[GameEndpoint("stream/user2/{username}", ContentType.Xml)]
[NullStatusCode(BadRequest)]
[MinimumRole(GameUserRole.Restricted)]
public Response GetRecentActivityForUser(RequestContext context, GameDatabaseContext database, string username,
public Response GetRecentActivityFromUser(RequestContext context, GameDatabaseContext database, string username,
DataContext dataContext)
{
GameUser? user = database.GetUserByUsername(username);
Expand All @@ -119,7 +119,7 @@ public Response GetRecentActivityForUser(RequestContext context, GameDatabaseCon

if (endTimestamp == 0) endTimestamp = timestamp - 86400000 * 7; // 1 week

return new Response(ActivityPage.UserActivity(database, new ActivityQueryParameters
return new Response(ActivityPage.GameFromUserActivity(database, new ActivityQueryParameters
{
Timestamp = timestamp,
EndTimestamp = endTimestamp,
Expand Down
40 changes: 37 additions & 3 deletions Refresh.GameServer/Types/Activity/ActivityPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void FillInInfo(GameDatabaseContext database, bool generateGroups, Activ
}
}

public static ActivityPage GameLevelActivity(GameDatabaseContext database,
public static ActivityPage GameForLevelActivity(GameDatabaseContext database,
GameLevel level,
ActivityQueryParameters parameters, DataContext dataContext)
{
Expand All @@ -137,7 +137,7 @@ public static ActivityPage GameLevelActivity(GameDatabaseContext database,
return page;
}

public static ActivityPage ApiLevelActivity(GameDatabaseContext database,
public static ActivityPage ApiForLevelActivity(GameDatabaseContext database,
GameLevel level,
ActivityQueryParameters parameters,
DataContext dataContext,
Expand All @@ -155,7 +155,41 @@ public static ActivityPage ApiLevelActivity(GameDatabaseContext database,
return page;
}

public static ActivityPage UserActivity(GameDatabaseContext database,
public static ActivityPage ApiFromUserActivity(GameDatabaseContext database,
ActivityQueryParameters parameters,
DataContext dataContext,
bool generateGroups = true)
{
DatabaseList<Event> events = database.GetRecentActivityFromUser(parameters);

ActivityPage page = new()
{
Events = events.Items,
};

page.FillInInfo(database, generateGroups, parameters, dataContext);

return page;
}

public static ActivityPage GameFromUserActivity(GameDatabaseContext database,
ActivityQueryParameters parameters, DataContext dataContext)
{
DatabaseList<Event> events = database.GetRecentActivityFromUser(parameters);

ActivityPage page = new()
{
Events = events.Items,
};

page.FillInInfo(database, true, parameters, dataContext);

page.Groups.Groups = page.Groups.Groups.SelectMany(group => group.Subgroups?.Items ?? []).ToList();

return page;
}

public static ActivityPage GameUserActivity(GameDatabaseContext database,
ActivityQueryParameters parameters,
DataContext dataContext,
bool generateGroups = true)
Expand Down

0 comments on commit add6077

Please sign in to comment.