58 lines
5.4 KiB
C#
58 lines
5.4 KiB
C#
using System.Security.Claims;
|
|
using System.Text.Json;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PlotDirector.Companion.Protocol;
|
|
using PlotLine.Data;
|
|
using PlotLine.Services.NativeCompanion;
|
|
namespace PlotLine.Controllers;
|
|
[Authorize,Route("native-companion")]
|
|
public sealed class NativeWorkspaceController(IWordCompanionRepository access,IBookRepository books,INativeReviewRepository reviews,NativePresenceStore presence):Controller
|
|
{
|
|
private int Author=>int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
|
|
[HttpGet("workspace")]
|
|
public async Task<IActionResult> Workspace(int bookId,Guid? instanceId,Guid? sessionId,string view="book")
|
|
{
|
|
if(await access.GetBookStructureAsync(bookId,Author)==null)return Forbid();var book=await books.GetAsync(bookId);if(book==null)return NotFound();
|
|
return View(new NativeWorkspaceModel(bookId,book.ProjectID,book.BookDisplayTitle,instanceId,sessionId,view is "context" or "intent"?view:"book",await reviews.List(Author,bookId)));
|
|
}
|
|
[HttpGet("presence")]
|
|
public async Task<IActionResult> Presence(int bookId)
|
|
{
|
|
if(await access.GetBookStructureAsync(bookId,Author)==null)return Forbid();Response.Headers.CacheControl="no-store";
|
|
return Json(presence.Read(Author,bookId).Select(l=>new{instanceId=l.Instance,sessionId=l.Session,manuscriptId=l.Position.ManuscriptId,l.Position.BookId,l.Position.ChapterId,l.Position.SceneId,l.Position.DocumentTitle,l.Position.Active,l.Position.WordAvailable,updated=l.Updated,expires=l.Updated.AddSeconds(45)}));
|
|
}
|
|
[HttpGet("status")]
|
|
public async Task<IActionResult> Status()
|
|
{
|
|
Response.Headers.CacheControl="no-store";var leases=presence.Read(Author,0);var allowed=new HashSet<int>();
|
|
foreach(var book in leases.Select(l=>l.Position.BookId).Distinct())if(await access.GetBookStructureAsync(book,Author)!=null)allowed.Add(book);
|
|
return Json(leases.Where(l=>allowed.Contains(l.Position.BookId)).Select(l=>new{instanceId=l.Instance,sessionId=l.Session,l.Position.BookId,l.Position.DocumentTitle,l.Position.ChapterId,l.Position.SceneId,l.Position.Active,l.Position.WordAvailable,updated=l.Updated,expires=l.Updated.AddSeconds(45)}));
|
|
}
|
|
[HttpGet("reviews/{id:guid}")]
|
|
public async Task<IActionResult> Review(Guid id)
|
|
{
|
|
var row=await reviews.Get(Author,id);if(row==null)return NotFound();if(await access.GetBookStructureAsync(row.BookID,Author)==null)return Forbid();
|
|
return View(new NativeReviewModel(row,JsonSerializer.Deserialize<AiChapterResult>(row.ResultJson)!,string.IsNullOrEmpty(row.DecisionsJson)?[]:JsonSerializer.Deserialize<NativeSceneDecision[]>(row.DecisionsJson)!,presence.Read(Author,row.BookID).Where(l=>l.Position.WordAvailable&&l.Position.ManuscriptId==row.ManuscriptID&&l.Position.Revision==JsonSerializer.Deserialize<AiChapterResult>(row.ResultJson)!.DocumentRevision).ToArray()));
|
|
}
|
|
[HttpPost("reviews/{id:guid}/navigate"),ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Navigate(Guid id,int index,Guid instanceId,Guid sessionId)
|
|
{
|
|
var row=await reviews.Get(Author,id);if(row==null)return NotFound();if(await access.GetBookStructureAsync(row.BookID,Author)==null)return Forbid();var result=JsonSerializer.Deserialize<AiChapterResult>(row.ResultJson)!;
|
|
if(index<0||index>=result.Scenes.Length)return BadRequest();if(!presence.Navigate(Author,row.BookID,instanceId,sessionId,row.ManuscriptID,result.DocumentRevision,result.Scenes[index].Start))return Conflict("Word session is unavailable or this snapshot changed. Detect and review again.");
|
|
TempData["NativeReviewSaved"]="Navigation requested for the selected Word session. No text changes.";return RedirectToAction(nameof(Review),new{id});
|
|
}
|
|
[HttpPost("reviews/{id:guid}"),ValidateAntiForgeryToken,RequestSizeLimit(2*1024*1024)]
|
|
public async Task<IActionResult> Decide(Guid id,int version,List<NativeSceneDecision> scenes)
|
|
{
|
|
var row=await reviews.Get(Author,id);if(row==null)return NotFound();if(await access.GetBookStructureAsync(row.BookID,Author)==null)return Forbid();
|
|
var result=JsonSerializer.Deserialize<AiChapterResult>(row.ResultJson)!;
|
|
if(scenes.Count!=result.Scenes.Length||scenes.Select(s=>s.Index).Distinct().Count()!=scenes.Count||scenes.Any(s=>s.Index<0||s.Index>=result.Scenes.Length||s.State is not ("pending" or "approved" or "rejected")||string.IsNullOrWhiteSpace(s.Title)||s.Title.Length>200||s.Summary==null||s.Summary.Length>600))return BadRequest("Invalid review. Refresh and retry; nothing was imported.");
|
|
if(!await reviews.Decide(Author,id,version,JsonSerializer.Serialize(scenes)))return Conflict("This review changed in another window. Reload before saving; nothing was overwritten.");
|
|
TempData["NativeReviewSaved"]="Review decisions saved. No manuscript or canonical story records were changed.";return RedirectToAction(nameof(Review),new{id});
|
|
}
|
|
}
|
|
public sealed record NativeWorkspaceModel(int Book,int Project,string Title,Guid? Instance,Guid? Session,string Mode,IReadOnlyList<NativeReview> Reviews);
|
|
public sealed record NativeReviewModel(NativeReview Row,AiChapterResult Result,NativeSceneDecision[] Decisions,NativeLease[] Sessions);
|
|
public sealed class NativeSceneDecision {public int Index {get;set;}public string State {get;set;}="pending";public string Title {get;set;}="";public string Summary {get;set;}="";}
|