divan/GanjooRazor/Areas/User/Pages/Edits.cshtml.cs
Hamid Reza Mohammadi 5ce8973ca3 people tags #387
2026-10-03 08:10:12 +03:30

357 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using DNTPersianUtils.Core;
using GanjooRazor.Utils;
using GSpotifyProxy.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Auth.ViewModel;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.Ganjoor.ViewModels;
using RSecurityBackend.Models.Generic;
namespace GanjooRazor.Areas.User.Pages
{
public class EditsModel : PageModel
{
/// <summary>
/// Last Error
/// </summary>
public string LastError { get; set; }
/// <summary>
/// pagination links
/// </summary>
public List<NameIdUrlImage> PaginationLinks { get; set; }
/// <summary>
/// can edit
/// </summary>
public bool CanEdit { get; set; }
/// <summary>
/// all users edits
/// </summary>
public bool AllUsersEdits { get; set; }
/// <summary>
/// filteredUserId
/// </summary>
public Guid? filteredUserId { get; set; }
public GanjoorUserPublicProfile Profile { get; set; }
/// <summary>
/// Corrections
/// </summary>
public List<GanjoorPoemCorrectionViewModel> Corrections { get; set; }
public GanjoorLanguage[] Languages { get; set; }
private async Task ReadLanguagesAsync(HttpClient secureClient)
{
HttpResponseMessage response = await secureClient.GetAsync($"{APIRoot.Url}/api/translations/languages");
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return;
}
Languages = JsonConvert.DeserializeObject<GanjoorLanguage[]>(await response.Content.ReadAsStringAsync());
}
public async Task<IActionResult> OnGetAsync()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
CanEdit = Request.Cookies["CanEdit"] == "True";
AllUsersEdits = false;
LastError = "";
using (HttpClient secureClient = new HttpClient(new GanjoorReloginHandler(Request, Response)))
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
{
{
int pageNumber = 1;
if (!string.IsNullOrEmpty(Request.Query["page"]))
{
pageNumber = int.Parse(Request.Query["page"]);
}
string url = $"{APIRoot.Url}/api/ganjoor/corrections/mine?PageNumber={pageNumber}&PageSize=20";
if (CanEdit)
{
AllUsersEdits = Request.Query["AllUsers"] == "1";
if (AllUsersEdits)
{
url = $"{APIRoot.Url}/api/ganjoor/corrections/all?PageNumber={pageNumber}&PageSize=20";
}
filteredUserId = string.IsNullOrEmpty(Request.Query["UserId"]) ? null : Guid.Parse(Request.Query["UserId"]);
if (filteredUserId != null)
{
url += $"&userId={filteredUserId}";
var responseUserProfile = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/user/profile/{filteredUserId}");
if (!responseUserProfile.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await responseUserProfile.Content.ReadAsStringAsync());
return Page();
}
Profile = JsonConvert.DeserializeObject<GanjoorUserPublicProfile>(await responseUserProfile.Content.ReadAsStringAsync());
}
}
var response = await secureClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return Page();
}
Corrections = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoemCorrectionViewModel>>();
string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault();
if (!string.IsNullOrEmpty(paginnationMetadata))
{
PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata);
PaginationLinks = new List<NameIdUrlImage>();
if (paginationMetadata.totalPages > 1)
{
if (paginationMetadata.currentPage > 3)
{
PaginationLinks.Add
(
new NameIdUrlImage()
{
Name = "صفحهٔ اول",
Url = AllUsersEdits ? "/User/Edits/?page=1&AllUsers=1" : "/User/Edits/?page=1"
}
);
if(filteredUserId != null)
{
PaginationLinks[PaginationLinks.Count - 1].Url += $"&UserId={filteredUserId}";
}
}
for (int i = (paginationMetadata.currentPage - 2); i <= (paginationMetadata.currentPage + 2); i++)
{
if (i >= 1 && i <= paginationMetadata.totalPages)
{
if (i == paginationMetadata.currentPage)
{
PaginationLinks.Add
(
new NameIdUrlImage()
{
Name = i.ToPersianNumbers(),
}
);
}
else
{
PaginationLinks.Add
(
new NameIdUrlImage()
{
Name = i.ToPersianNumbers(),
Url = AllUsersEdits ? $"/User/Edits/?page={i}&AllUsers=1" : $"/User/Edits/?page={i}"
}
);
if (filteredUserId != null)
{
PaginationLinks[PaginationLinks.Count - 1].Url += $"&UserId={filteredUserId}";
}
}
}
}
if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2))
{
PaginationLinks.Add
(
new NameIdUrlImage()
{
Name = "... ",
}
);
PaginationLinks.Add
(
new NameIdUrlImage()
{
Name = "صفحهٔ آخر",
Url = AllUsersEdits ? $"/User/Edits/?page={paginationMetadata.totalPages}&AllUsers=1" : $"/User/Edits/?page={paginationMetadata.totalPages}"
}
);
if (filteredUserId != null)
{
PaginationLinks[PaginationLinks.Count - 1].Url += $"&UserId={filteredUserId}";
}
}
}
}
await ReadLanguagesAsync(secureClient);
}
}
else
{
LastError = "لطفاً از گنجور خارج و مجددا به آن وارد شوید.";
}
return Page();
}
public async Task<IActionResult> OnPostRollBackCorrectionAsync(int correctionId)
{
using (HttpClient secureClient = new HttpClient(new GanjoorReloginHandler(Request, Response)))
{
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
{
var correctionResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/correction/{correctionId}");
if (!correctionResponse.IsSuccessStatusCode)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await correctionResponse.Content.ReadAsStringAsync()));
}
var currentCorrection = JsonConvert.DeserializeObject<GanjoorPoemCorrectionViewModel>(await correctionResponse.Content.ReadAsStringAsync());
GanjoorPoemCorrectionViewModel correction = new GanjoorPoemCorrectionViewModel();
correction.PoemId = currentCorrection.PoemId;
if (currentCorrection.Title != null && currentCorrection.Result == CorrectionReviewResult.Approved)
{
correction.OriginalTitle = currentCorrection.Title;
correction.Title = currentCorrection.OriginalTitle;
}
List<GanjoorVerseVOrderText> vOrderTexts = new List<GanjoorVerseVOrderText>();
if (currentCorrection.VerseOrderText != null)
{
foreach (var verseOrderText in currentCorrection.VerseOrderText)
{
if (
verseOrderText.Result == CorrectionReviewResult.Approved
||
verseOrderText.VersePositionResult == CorrectionReviewResult.Approved
||
verseOrderText.LanguageReviewResult == CorrectionReviewResult.Approved
||
verseOrderText.SummaryReviewResult == CorrectionReviewResult.Approved
)
{
vOrderTexts.Add(
new GanjoorVerseVOrderText()
{
VORder = verseOrderText.VORder,
OriginalText = verseOrderText.Text,
Text = verseOrderText.OriginalText,
VersePosition = verseOrderText.VersePositionResult == CorrectionReviewResult.Approved && verseOrderText.VersePosition != null ? verseOrderText.OriginalVersePosition : null,
OriginalVersePosition = verseOrderText.VersePositionResult == CorrectionReviewResult.Approved && verseOrderText.VersePosition != null ? verseOrderText.VersePosition : null,
LanguageId = verseOrderText.LanguageReviewResult == CorrectionReviewResult.Approved && verseOrderText.LanguageId != null ? verseOrderText.OriginalLanguageId : null,
OriginalLanguageId = verseOrderText.LanguageReviewResult == CorrectionReviewResult.Approved && verseOrderText.LanguageId != null ? verseOrderText.LanguageId : null,
CoupletIndex = verseOrderText.CoupletIndex,
CoupletSummary = verseOrderText.SummaryReviewResult == CorrectionReviewResult.Approved && verseOrderText.CoupletSummary != null ? verseOrderText.OriginalCoupletSummary ?? "" : null,
OriginalCoupletSummary = verseOrderText.SummaryReviewResult == CorrectionReviewResult.Approved && verseOrderText.CoupletSummary != null ? verseOrderText.CoupletSummary : null,
});
}
}
}
correction.VerseOrderText = vOrderTexts.ToArray();
correction.Rhythm = currentCorrection.RhythmResult == CorrectionReviewResult.Approved && currentCorrection.Rhythm != null ? currentCorrection.OriginalRhythm ?? "" : null;
correction.OriginalRhythm = currentCorrection.RhythmResult == CorrectionReviewResult.Approved && currentCorrection.Rhythm != null ? currentCorrection.Rhythm : null;
correction.Rhythm2 = currentCorrection.Rhythm2Result == CorrectionReviewResult.Approved && currentCorrection.Rhythm2 != null ? currentCorrection.OriginalRhythm2 ?? "" : null;
correction.OriginalRhythm2 = currentCorrection.Rhythm2Result == CorrectionReviewResult.Approved && currentCorrection.Rhythm2 != null ? currentCorrection.Rhythm2 : null;
correction.Rhythm3 = currentCorrection.Rhythm3Result == CorrectionReviewResult.Approved && currentCorrection.Rhythm3 != null ? currentCorrection.OriginalRhythm3 ?? "" : null;
correction.OriginalRhythm3 = currentCorrection.Rhythm3Result == CorrectionReviewResult.Approved && currentCorrection.Rhythm3 != null ? currentCorrection.Rhythm3 : null;
correction.Rhythm4 = currentCorrection.Rhythm4Result == CorrectionReviewResult.Approved && currentCorrection.Rhythm4 != null ? currentCorrection.OriginalRhythm4 ?? "" : null;
correction.OriginalRhythm4 = currentCorrection.Rhythm4Result == CorrectionReviewResult.Approved && currentCorrection.Rhythm4 != null ? currentCorrection.Rhythm4 : null;
correction.PoemFormat = currentCorrection.PoemFormatReviewResult == CorrectionReviewResult.Approved && currentCorrection.PoemFormat != null ? currentCorrection.OriginalPoemFormat : null;
correction.OriginalPoemFormat = currentCorrection.PoemFormatReviewResult == CorrectionReviewResult.Approved && currentCorrection.PoemFormat != null ? currentCorrection.PoemFormat : null;
correction.RhymeLetters = currentCorrection.RhymeLettersReviewResult == CorrectionReviewResult.Approved && currentCorrection.RhymeLetters != null ? currentCorrection.OriginalRhymeLetters ?? "" : null;
correction.OriginalRhymeLetters = currentCorrection.RhymeLettersReviewResult == CorrectionReviewResult.Approved && currentCorrection.RhymeLetters != null ? currentCorrection.RhymeLetters : null;
correction.PoemSummary = currentCorrection.SummaryReviewResult == CorrectionReviewResult.Approved && currentCorrection.PoemSummary != null ? currentCorrection.OriginalPoemSummary ?? "" : null;
correction.OriginalPoemSummary = currentCorrection.SummaryReviewResult == CorrectionReviewResult.Approved && currentCorrection.PoemSummary != null ? currentCorrection.PoemSummary : null;
if (currentCorrection.GeoDateTags != null)
{
var geoDateTagsRollback = new List<GanjoorPoemGeoDateTagCorrection>();
foreach (var geoTag in currentCorrection.GeoDateTags.Where(g => g.Result == CorrectionReviewResult.Approved))
{
// PersonId (if the deleted tag was a person tag) is part of what got snapshotted
// onto this same record when the delete request was approved, so it's already
// available here for GeoDateTagCorrectionDisplay.IsPersonTag to tell the two kinds
// of tag apart for the note text below.
var geoTagLabel = GeoDateTagCorrectionDisplay.IsPersonTag(geoTag) ? "نامبرده" : "برچسب جغرافیایی/تاریخی";
if (geoTag.MarkForDelete)
{
// the original request deleted an existing tag - undo that by re-adding a tag with
// the same data, which was snapshotted onto this same record when it was approved
// (LocationId/LunarYear/etc. are otherwise unused for a pure delete-request)
geoDateTagsRollback.Add(new GanjoorPoemGeoDateTagCorrection()
{
CoupletIndex = geoTag.CoupletIndex,
MarkForDelete = false,
LocationId = geoTag.LocationId,
LunarYear = geoTag.LunarYear,
LunarMonth = geoTag.LunarMonth,
LunarDay = geoTag.LunarDay,
PersonId = geoTag.PersonId,
IgnoreInCategory = geoTag.IgnoreInCategory,
SuggestionNote = $"برگشت حذف {geoTagLabel} با کد {correctionId}"
});
}
else if (geoTag.ExistingTagId != null)
{
// the original request added a new tag - undo that by requesting deletion of the
// live tag it produced (its id was recorded onto ExistingTagId when approved)
geoDateTagsRollback.Add(new GanjoorPoemGeoDateTagCorrection()
{
CoupletIndex = geoTag.CoupletIndex,
MarkForDelete = true,
ExistingTagId = geoTag.ExistingTagId,
SuggestionNote = $"برگشت افزودن {geoTagLabel} با کد {correctionId}"
});
}
}
if (geoDateTagsRollback.Count > 0)
{
correction.GeoDateTags = geoDateTagsRollback.ToArray();
}
}
correction.Note = $"برگشت تصحیح با کد {correctionId}";
if(!string.IsNullOrEmpty(currentCorrection.Note))
{
correction.Note += " - یادداشت تصحیح قبلی - ";
correction.Note += currentCorrection.Note;
}
HttpResponseMessage response = await secureClient.PostAsync(
$"{APIRoot.Url}/api/ganjoor/poem/correction",
new StringContent(JsonConvert.SerializeObject(correction),
Encoding.UTF8,
"application/json"));
if (!response.IsSuccessStatusCode)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
return new OkObjectResult(true);
}
}
return new BadRequestObjectResult("لطفاً از گنجور خارج و مجددا به آن وارد شوید.");
}
}
}