[backend/api] Create endpoint to clone remote emoji
This commit is contained in:
parent
d2e7753dd1
commit
ab3f3783e8
3 changed files with 44 additions and 2 deletions
|
@ -88,6 +88,22 @@ public class EmojiController(
|
|||
return Ok(res);
|
||||
}
|
||||
|
||||
[HttpPost("clone/{name}@{host}")]
|
||||
[Authorize("role:admin")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(EmojiResponse))]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))]
|
||||
[ProducesResponseType(StatusCodes.Status409Conflict, Type = typeof(ErrorResponse))]
|
||||
public async Task<IActionResult> CloneEmoji(string name, string host)
|
||||
{
|
||||
var localEmojo = await db.Emojis.FirstOrDefaultAsync(e => e.Name == name && e.Host == null);
|
||||
if (localEmojo != null) return Conflict();
|
||||
|
||||
var emojo = await db.Emojis.FirstOrDefaultAsync(e => e.Name == name && e.Host == host);
|
||||
if (emojo == null) return NotFound();
|
||||
|
||||
return Ok(await emojiSvc.CloneEmoji(emojo));
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[Authorize("role:admin")]
|
||||
[Consumes(MediaTypeNames.Application.Json)]
|
||||
|
|
|
@ -25,7 +25,7 @@ public class DriveService(
|
|||
{
|
||||
public async Task<DriveFile?> StoreFile(
|
||||
string? uri, User user, bool sensitive, string? description = null, string? mimeType = null,
|
||||
bool logExisting = true
|
||||
bool logExisting = true, bool forceStore = false
|
||||
)
|
||||
{
|
||||
if (uri == null) return null;
|
||||
|
@ -36,7 +36,10 @@ public class DriveService(
|
|||
try
|
||||
{
|
||||
// Do we already have the file?
|
||||
var file = await db.DriveFiles.FirstOrDefaultAsync(p => p.Uri == uri);
|
||||
DriveFile? file = null;
|
||||
if (!forceStore)
|
||||
file = await db.DriveFiles.FirstOrDefaultAsync(p => p.Uri == uri);
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
// If the user matches, return the existing file
|
||||
|
|
|
@ -61,6 +61,29 @@ public partial class EmojiService(DatabaseContext db, DriveService driveSvc, Sys
|
|||
return emoji;
|
||||
}
|
||||
|
||||
public async Task<Emoji> CloneEmoji(Emoji existing)
|
||||
{
|
||||
var user = await sysUserSvc.GetInstanceActorAsync();
|
||||
var driveFile = await driveSvc.StoreFile(existing.OriginalUrl, user, sensitive: false, forceStore: true) ??
|
||||
throw new Exception("Error storing emoji file");
|
||||
|
||||
var emoji = new Emoji
|
||||
{
|
||||
Id = IdHelpers.GenerateSlowflakeId(),
|
||||
Name = existing.Name,
|
||||
UpdatedAt = DateTime.UtcNow,
|
||||
OriginalUrl = driveFile.Url,
|
||||
PublicUrl = driveFile.PublicUrl,
|
||||
Width = driveFile.Properties.Width,
|
||||
Height = driveFile.Properties.Height
|
||||
};
|
||||
|
||||
await db.AddAsync(emoji);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return emoji;
|
||||
}
|
||||
|
||||
public async Task DeleteEmoji(string id)
|
||||
{
|
||||
var emoji = await db.Emojis.FirstOrDefaultAsync(p => p.Host == null && p.Id == id);
|
||||
|
|
Loading…
Add table
Reference in a new issue