Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add admin photo moderation endpoints #621

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Photos.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using Refresh.GameServer.Types.Activity;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.Photos;
using Refresh.GameServer.Types.UserData;
Expand Down Expand Up @@ -57,6 +58,13 @@ public void RemovePhoto(GamePhoto photo)
{
this.Write(() =>
{
IQueryable<Event> photoEvents = this.Events
.Where(e => e._StoredDataType == (int)EventDataType.Photo && e.StoredSequentialId == photo.PhotoId);

// Remove all events referencing the photo
this.Events.RemoveRange(photoEvents);

// Remove the photo
this.GamePhotos.Remove(photo);
});
}
Expand Down Expand Up @@ -106,4 +114,17 @@ public DatabaseList<GamePhoto> GetPhotosInLevel(GameLevel level, int count, int
[Pure]
public int GetTotalPhotosInLevel(GameLevel level)
=> this.GamePhotos.Count(p => p.LevelId == level.LevelId);

public void DeletePhotosPostedByUser(GameUser user)
{
IEnumerable<GamePhoto> photos = this.GamePhotos.Where(s => s.Publisher == user);

this.Write(() =>
{
foreach (GamePhoto photo in photos)
{
this.RemovePhoto(photo);
}
});
}
}
53 changes: 53 additions & 0 deletions Refresh.GameServer/Endpoints/ApiV3/Admin/AdminPhotoApiEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using AttribDoc.Attributes;
using Bunkum.Core;
using Bunkum.Core.Endpoints;
using Bunkum.Protocols.Http;
using Refresh.GameServer.Database;
using Refresh.GameServer.Endpoints.ApiV3.ApiTypes;
using Refresh.GameServer.Endpoints.ApiV3.ApiTypes.Errors;
using Refresh.GameServer.Types.Photos;
using Refresh.GameServer.Types.Roles;
using Refresh.GameServer.Types.UserData;

namespace Refresh.GameServer.Endpoints.ApiV3.Admin;

public class AdminPhotoApiEndpoints : EndpointGroup
{
[ApiV3Endpoint("admin/users/uuid/{uuid}/photos", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)]
[DocSummary("Deletes all photos posted by a user. Gets user by their UUID.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
public ApiOkResponse DeletePhotosPostedByUuid(RequestContext context, GameDatabaseContext database,
[DocSummary("The UUID of the user")] string uuid)
{
GameUser? user = database.GetUserByUuid(uuid);
if (user == null) return ApiNotFoundError.UserMissingError;

database.DeletePhotosPostedByUser(user);
return new ApiOkResponse();
}

[ApiV3Endpoint("admin/users/name/{username}/photos", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)]
[DocSummary("Deletes all photos posted by a user. Gets user by their username.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.UserMissingErrorWhen)]
public ApiOkResponse DeletePhotosPostedByUsername(RequestContext context, GameDatabaseContext database,
[DocSummary("The username of the user")] string username)
{
GameUser? user = database.GetUserByUsername(username);
if (user == null) return ApiNotFoundError.UserMissingError;

database.DeletePhotosPostedByUser(user);
return new ApiOkResponse();
}

[ApiV3Endpoint("admin/photos/id/{id}", HttpMethods.Delete), MinimumRole(GameUserRole.Admin)]
[DocSummary("Deletes a photo.")]
[DocError(typeof(ApiNotFoundError), ApiNotFoundError.PhotoMissingErrorWhen)]
public ApiOkResponse DeletePhoto(RequestContext context, GameDatabaseContext database, int id)
{
GamePhoto? photo = database.GetPhotoById(id);
if (photo == null) return ApiNotFoundError.PhotoMissingError;

database.RemovePhoto(photo);
return new ApiOkResponse();
}
}
Loading