added media class

This commit is contained in:
2026-03-28 23:14:51 -04:00
parent 3a5dde9ee3
commit b8d01b567c
6 changed files with 47 additions and 17 deletions
+8
View File
@@ -20,6 +20,7 @@ public class Media
public string[] Tags { get; set; } = [];
public Size? Dimensions { get; set; }
public Dictionary<ThumbnailSize, ObjectId> Thumbnails { get; set; } = [];
public MediaClass Class { get; set; }
public static readonly Dictionary<string, MediaType> KnownTypes = new()
@@ -117,3 +118,10 @@ public enum MediaType
Code,
Raw
}
public enum MediaClass
{
Standard,
NSFW,
Secret
}
+17 -7
View File
@@ -25,25 +25,28 @@ public class AobaService(IMongoDatabase db)
return await _media.Find(m => m.MediaId == id).FirstOrDefaultAsync(cancellationToken);
}
public async Task<PagedResult<Media>> FindMediaAsync(string? query, ObjectId userId, int page = 1, int pageSize = 100)
public async Task<PagedResult<Media>> FindMediaAsync(string? query, ObjectId userId, int page = 1, int pageSize = 100, CancellationToken cancellationToken = default)
{
var filters = new List<FilterDefinition<Media>>()
{
var filter = Builders<Media>.Filter.And([
string.IsNullOrWhiteSpace(query) ? "{}" : Builders<Media>.Filter.Text(query),
Builders<Media>.Filter.Eq(m => m.Owner, userId)
]);
};
if (string.IsNullOrWhiteSpace(query))
filters.Add(Builders<Media>.Filter.Ne(m => m.Class, MediaClass.Secret));
var sort = Builders<Media>.Sort.Descending(m => m.UploadDate);
var find = _media.Find(filter);
var find = _media.Find(Builders<Media>.Filter.And(filters));
var total = await find.CountDocumentsAsync();
var total = await find.CountDocumentsAsync(cancellationToken);
page -= 1;
var items = await find.Sort(sort).Skip(page * pageSize).Limit(pageSize).ToListAsync();
var items = await find.Sort(sort).Skip(page * pageSize).Limit(pageSize).ToListAsync(cancellationToken);
return new PagedResult<Media>(items, page, pageSize, (int)total);
}
public async Task<List<Media>> FindMediaWithExtAsync(string ext, CancellationToken cancellationToken = default)
{
var filter = Builders<Media>.Filter.Eq(m => m.Ext, ext);
return await _media.Find(filter).ToListAsync();
return await _media.Find(filter).ToListAsync(cancellationToken);
}
public Task AddMediaAsync(Media media, CancellationToken cancellationToken = default)
@@ -51,6 +54,13 @@ public class AobaService(IMongoDatabase db)
return _media.InsertOneAsync(media, null, cancellationToken);
}
public async Task SetMediaClassAsync(ObjectId mediaId, MediaClass mediaClass, CancellationToken cancellationToken = default)
{
var update = Builders<Media>.Update
.Set(m => m.Class, mediaClass);
await _media.UpdateOneAsync(m => m.MediaId == mediaId, update, cancellationToken: cancellationToken);
}
public async Task AddThumbnailAsync(ObjectId mediaId, ObjectId thumbId, ThumbnailSize size, CancellationToken cancellationToken = default)
{
var upate = Builders<Media>.Update.Set(m => m.Thumbnails[size], thumbId);
+1
View File
@@ -12,5 +12,6 @@ service AobaRpc {
rpc ListMedia(PageFilter) returns (ListResponse);
rpc GetUser(Id) returns (UserResponse);
rpc GetShareXDestination(google.protobuf.Empty) returns (ShareXResponse);
rpc SetMediaClass(SetMediaClassRequest) returns(google.protobuf.Empty);
}
+11 -1
View File
@@ -9,7 +9,10 @@ message Credentials{
string password = 2;
}
message SetMediaClassRequest{
Id id = 1;
MediaClass class = 2;
}
message Jwt{
@@ -81,6 +84,7 @@ message MediaModel {
Id owner = 6;
string thumbUrl = 7;
string mediaUrl = 8;
MediaClass class = 9;
}
enum MediaType {
@@ -92,6 +96,12 @@ enum MediaType {
Raw = 5;
}
enum MediaClass {
Standard = 0;
NSFW = 1;
Secret = 2;
}
message ShareXResponse {
oneof dstResult {
string destination = 1;
+8 -8
View File
@@ -9,10 +9,7 @@ using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using MongoDB.Bson.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AobaServer.Services;
@@ -20,7 +17,7 @@ public class AobaRpcService(AobaService aobaService, AccountsService accountsSer
{
public override async Task<MediaResponse> GetMedia(Id request, ServerCallContext context)
{
var media = await aobaService.GetMediaFromLegacyIdAsync(request.ToObjectId());
var media = await aobaService.GetMediaFromLegacyIdAsync(request.ToObjectId(), context.CancellationToken);
return media.ToResponse();
}
@@ -31,6 +28,12 @@ public class AobaRpcService(AobaService aobaService, AccountsService accountsSer
return result.ToResponse();
}
public override async Task<Empty> SetMediaClass(SetMediaClassRequest request, ServerCallContext context)
{
await aobaService.SetMediaClassAsync(request.Id.ToObjectId(), (AobaCore.Models.MediaClass)request.Class, context.CancellationToken);
return new Empty();
}
public override async Task<ShareXResponse> GetShareXDestination(Empty request, ServerCallContext context)
{
var userId = context.GetHttpContext().User.GetId();
@@ -50,10 +53,7 @@ public class AobaRpcService(AobaService aobaService, AccountsService accountsSer
};
return new ShareXResponse
{
Destination = JsonSerializer.Serialize(dest, new JsonSerializerOptions
{
WriteIndented = true
})
Destination = JsonSerializer.Serialize(dest)
};
}
+2 -1
View File
@@ -56,7 +56,8 @@ public static class ProtoExtensions
Owner = media.Owner.ToId(),
ViewCount = media.ViewCount,
ThumbUrl = thumbUrl,
MediaUrl = media.GetMediaUrl()
MediaUrl = media.GetMediaUrl(),
Class = (Aoba.RPC.MediaClass)media.Class,
};
}