Projects (GanjooRazor -> DiwanRazor, GanjoorService.sln -> DiwanService.sln), files, folders, classes, namespaces, settings, API routes (/api/diwan) and DB tables (Diwan*). External addresses (ganjoor.net, github.com/ganjoor) unchanged. Migrations renamed consistently (no pending model changes); requires a fresh database. Upstream RUNNING_LOCALLY/SEMANTIC_SEARCH docs archived unmodified under docs/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
223 lines
9.2 KiB
C#
223 lines
9.2 KiB
C#
using DiwanRazor.Utils;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Newtonsoft.Json;
|
|
using RMuseum.Models.Diwan.ViewModels;
|
|
using RSecurityBackend.Models.Generic;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Linq;
|
|
using System;
|
|
using RMuseum.Models.Diwan;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Text;
|
|
using RMuseum.Models.Auth.ViewModel;
|
|
|
|
namespace DiwanRazor.Areas.Admin.Pages
|
|
{
|
|
public class ReviewQuotedsModel : PageModel
|
|
{
|
|
/// <summary>
|
|
/// moderation model
|
|
/// </summary>
|
|
[BindProperty]
|
|
public DiwanQuotedPoemModerationViewModel ModerationModel { get; set; }
|
|
|
|
/// <summary>
|
|
/// suggestion
|
|
/// </summary>
|
|
public DiwanQuotedPoemViewModel DiwanQuotedPoem { get; set; }
|
|
/// <summary>
|
|
/// fatal error
|
|
/// </summary>
|
|
public string FatalError { get; set; }
|
|
|
|
/// <summary>
|
|
/// skip
|
|
/// </summary>
|
|
public int Skip { get; set; }
|
|
|
|
/// <summary>
|
|
/// total count
|
|
/// </summary>
|
|
public int TotalCount { get; set; }
|
|
|
|
public Tuple<int, string>[] Couplets { get; set; }
|
|
|
|
public Tuple<int, string>[] RelatedCouplets { get; set; }
|
|
|
|
public DiwanPoemCompleteViewModel Poem { get; set; }
|
|
|
|
public DiwanPoemCompleteViewModel RelatedPoem { get; set; }
|
|
|
|
public DiwanUserPublicProfile SuggestedBy { get; set; }
|
|
|
|
private Tuple<int, string>[] GetCouplets(DiwanVerseViewModel[] verses)
|
|
{
|
|
int coupetIndex = -1;
|
|
string coupletText = "";
|
|
List<Tuple<int, string>> couplets = new List<Tuple<int, string>>();
|
|
int verseIndex = 0;
|
|
bool incompleteCouplet = false;
|
|
while (verseIndex < verses.Length)
|
|
{
|
|
switch (verses[verseIndex].VersePosition)
|
|
{
|
|
case VersePosition.Comment:
|
|
incompleteCouplet = false;
|
|
break;
|
|
case VersePosition.Paragraph:
|
|
case VersePosition.Single:
|
|
incompleteCouplet = false;
|
|
if (!string.IsNullOrEmpty(coupletText))
|
|
{
|
|
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
|
|
coupletText = "";
|
|
}
|
|
coupetIndex++;
|
|
couplets.Add(new Tuple<int, string>(coupetIndex, verses[verseIndex].Text));
|
|
break;
|
|
case VersePosition.Right:
|
|
case VersePosition.CenteredVerse1:
|
|
incompleteCouplet = false;
|
|
if (!string.IsNullOrEmpty(coupletText))
|
|
{
|
|
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
|
|
}
|
|
coupetIndex++;
|
|
coupletText = verses[verseIndex].Text;
|
|
break;
|
|
case VersePosition.Left:
|
|
case VersePosition.CenteredVerse2:
|
|
incompleteCouplet = true;
|
|
coupletText += $" - {verses[verseIndex].Text}";
|
|
break;
|
|
}
|
|
verseIndex++;
|
|
}
|
|
|
|
|
|
if (incompleteCouplet && !string.IsNullOrEmpty(coupletText))
|
|
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
|
|
|
|
return couplets.ToArray();
|
|
}
|
|
public async Task<IActionResult> OnGetAsync()
|
|
{
|
|
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
|
|
return Redirect("/");
|
|
|
|
FatalError = "";
|
|
TotalCount = 0;
|
|
Skip = string.IsNullOrEmpty(Request.Query["skip"]) ? 0 : int.Parse(Request.Query["skip"]);
|
|
RelatedCouplets = [];
|
|
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
|
|
{
|
|
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
var nextResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/diwan/quoted/suggestion/next?skip={Skip}");
|
|
if (!nextResponse.IsSuccessStatusCode)
|
|
{
|
|
FatalError = JsonConvert.DeserializeObject<string>(await nextResponse.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
|
|
string paginnationMetadata = nextResponse.Headers.GetValues("paging-headers").FirstOrDefault();
|
|
if (!string.IsNullOrEmpty(paginnationMetadata))
|
|
{
|
|
TotalCount = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata).totalCount;
|
|
}
|
|
DiwanQuotedPoem = JsonConvert.DeserializeObject<DiwanQuotedPoemViewModel>(await nextResponse.Content.ReadAsStringAsync());
|
|
|
|
if(DiwanQuotedPoem != null)
|
|
{
|
|
var responseUserProfile = await secureClient.GetAsync($"{APIRoot.Url}/api/diwan/user/profile/{DiwanQuotedPoem.SuggestedById}");
|
|
if (!responseUserProfile.IsSuccessStatusCode)
|
|
{
|
|
FatalError = JsonConvert.DeserializeObject<string>(await responseUserProfile.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
|
|
SuggestedBy = JsonConvert.DeserializeObject<DiwanUserPublicProfile>(await responseUserProfile.Content.ReadAsStringAsync());
|
|
|
|
var poemQuery = await secureClient.GetAsync($"{APIRoot.Url}/api/diwan/poem/{DiwanQuotedPoem.PoemId}");
|
|
if (!poemQuery.IsSuccessStatusCode)
|
|
{
|
|
FatalError = JsonConvert.DeserializeObject<string>(await poemQuery.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
Poem = JObject.Parse(await poemQuery.Content.ReadAsStringAsync()).ToObject<DiwanPoemCompleteViewModel>();
|
|
|
|
Couplets = GetCouplets(Poem.Verses);
|
|
|
|
if (DiwanQuotedPoem.RelatedPoemId != null)
|
|
{
|
|
var relPoemQuery = await secureClient.GetAsync($"{APIRoot.Url}/api/diwan/poem/{DiwanQuotedPoem.RelatedPoemId}");
|
|
if (!relPoemQuery.IsSuccessStatusCode)
|
|
{
|
|
FatalError = JsonConvert.DeserializeObject<string>(await relPoemQuery.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
RelatedPoem = JObject.Parse(await relPoemQuery.Content.ReadAsStringAsync()).ToObject<DiwanPoemCompleteViewModel>();
|
|
|
|
RelatedCouplets = GetCouplets(RelatedPoem.Verses);
|
|
|
|
|
|
|
|
}
|
|
|
|
ModerationModel = new DiwanQuotedPoemModerationViewModel()
|
|
{
|
|
Id = DiwanQuotedPoem.Id,
|
|
Approved = true,
|
|
ReviewNote = ""
|
|
};
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
FatalError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
|
|
}
|
|
}
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostAsync(DiwanQuotedPoemModerationViewModel ModerationModel)
|
|
{
|
|
FatalError = "";
|
|
try
|
|
{
|
|
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
|
|
{
|
|
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
var url = $"{APIRoot.Url}/api/diwan/quoted/moderate";
|
|
var payload = new StringContent(JsonConvert.SerializeObject(ModerationModel), Encoding.UTF8, "application/json");
|
|
HttpResponseMessage response = await secureClient.PutAsync(url, payload);
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
FatalError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
|
|
}
|
|
else
|
|
{
|
|
Skip = string.IsNullOrEmpty(Request.Query["skip"]) ? 0 : int.Parse(Request.Query["skip"]);
|
|
Response.Redirect($"/Admin/ReviewQuoteds?skip={Skip}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FatalError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
FatalError = e.ToString();
|
|
}
|
|
return Page();
|
|
}
|
|
}
|
|
}
|