using System.Security.Claims; using System.Text; using System.Text.Json; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.SqlClient; using PlotLine.Data; using PlotLine.Services.NativeCompanion; using PlotDirector.Companion.Protocol; namespace PlotLine.Controllers; [Authorize] public sealed class WritingActivityController(WritingActivityRepository activity,IBookRepository books):Controller { private int Actor => int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!); [HttpGet("Writer/Activity")] public async Task Index(int bookId,long? before=null,DateTime? day=null) { try { var page=await activity.Read(Actor,bookId,before,day);page.Day=day;page.Book=await books.GetAsync(bookId);return View(page); } catch(SqlException e)when(e.Number==51000){return NotFound();} } [HttpPost("Writer/Activity/Baseline"),ValidateAntiForgeryToken] public async Task Baseline(int bookId) { try{await activity.Baseline(Actor,bookId);return RedirectToAction(nameof(Index),new{bookId});} catch(SqlException e)when(e.Number==51000){return NotFound();}catch(InvalidOperationException e){return BadRequest(e.Message);} } [HttpPost("Writer/Activity/Settings"),ValidateAntiForgeryToken] public async Task Settings(int bookId,bool enabled,int retentionDays) { if(retentionDays is <30 or >3650)return BadRequest("Choose retention between 30 and 3650 days."); try{await activity.Configure(Actor,bookId,enabled,retentionDays);return RedirectToAction(nameof(Index),new{bookId});}catch(SqlException e)when(e.Number==51000){return NotFound();} } [HttpPost("Writer/Activity/Erase"),ValidateAntiForgeryToken] public async Task Erase(int bookId,bool confirm) { if(!confirm)return BadRequest("Confirm deletion of activity history."); try{await activity.Erase(Actor,bookId);return RedirectToAction(nameof(Index),new{bookId});}catch(SqlException e)when(e.Number==51000){return NotFound();} } [HttpGet("Writer/Activity/Export")] public async Task Export(int bookId,long? before=null) { try{var page=await activity.Read(Actor,bookId,before);return File(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(new{coverage="Up to 500 retained records; use before cursor for earlier pages",page.BookID,page.Events})),"application/json","writing-activity.json");}catch(SqlException e)when(e.Number==51000){return NotFound();} } [HttpPost("api/native-companion/writing-activity"),Authorize(AuthenticationSchemes=DesktopPairing.Scheme),RequestSizeLimit(4096)] public async Task Observe([FromBody]WritingObservation observation) { if(observation==null||observation.RequestID==Guid.Empty||observation.ManuscriptID==Guid.Empty||observation.BookID<=0||observation.PayloadHash==null||observation.PayloadHash.Length!=64||!observation.PayloadHash.All(Uri.IsHexDigit))return BadRequest(); try{await activity.Observe(Actor,observation);return Ok(new{observation.RequestID});}catch(SqlException e)when(e.Number==51000){return Conflict(new{code="activity_scope_or_clock",message="Observation could not be accepted for this book and clock window."});} } }