using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using RadicadeServer.DB; using RadicadeServer.RecNet.Auth; using RadicadeServer.RecNet.Rooms; using RadicadeServer.Radicade.Public; namespace RadicadeServer.API.Public { [Route("p/photos")] public class PublicPhotosAPI(RecDBContext db) : Controller { [HttpGet("latest")] public async Task>> GetLatestPhotos([FromQuery] int? amount) { if (!amount.HasValue) amount = 3; if (amount.Value > 6) return BadRequest("Can't go above 6 pictures."); var images = await db.Images.AsNoTracking().Where(x => x.Accessibility == RecNet.CDN.SavedImageAccessibility.Public).OrderByDescending(x => x.CreatedAt).Take(amount.Value).ToListAsync(); List converted = new(); foreach (var image in images) { if (image == null) continue; RoomData? roomData = null; Profile? profile = null; int cheers = 0; if (image.RoomId.HasValue) roomData = await db.Rooms.AsNoTracking().FirstOrDefaultAsync(x => x.RoomId == image.RoomId && x.Accessibility >= RecNet.Enums.Accessibility.Public); profile = await db.Profiles.AsNoTracking().FirstOrDefaultAsync(x => x.accountId == image.OwnerId); cheers = await db.ImageInteractions.AsNoTracking().Where(x => x.SavedImageId == image.Id).CountAsync(); if (profile == null) continue; PublicPhotoData data = new PublicPhotoData { Cheers = cheers, ImageCdnUrl = $"{DBConfig.NameServer.Images}/img/{image.Name}?width=512", LocationName = (roomData == null) ? "[PRIVATE]" : "^" + roomData.Name, OwnerName = "@" + profile.username }; converted.Add(data); } return converted; } } }