using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using Shared; using System.IO; using System.Threading.Tasks; namespace TelegramBot.Controllers { public class TgNotifyController : BaseController { [HttpGet] [AllowAnonymous] [Route("tg-notify.js")] public ActionResult TgNotifyPlugin() { // Ищем плагин в возможных расположениях модуля (NextGen: module/ или mods/) string file = null; foreach (var p in new[] { "module/Tg-notify.bot/tg-notify.js", "module/Community/Tg-notify.bot/tg-notify.js", "mods/TelegramBot/tg-notify.js", "module/TelegramBot/tg-notify.js", "plugins/tg-notify.js", "wwwroot/plugins/tg-notify.js" }) { if (System.IO.File.Exists(p)) { file = p; break; } } if (file == null) return Content(string.Empty, "application/javascript; charset=utf-8"); var js = System.IO.File.ReadAllText(file); js = js.Replace("{localhost}", host); return Content(js, "application/javascript; charset=utf-8"); } [HttpPost] [Route("/api/tg/subscribe")] public async Task Subscribe() { if (!ModInit.IsRunning) return Json(new { success = false, msg = "bot_not_running" }); var uid = ModInit.ResolveUid(requestInfo.user_uid, Request.Query["uid"].ToString()); if (string.IsNullOrEmpty(uid)) return Json(new { success = false, msg = "no_uid" }); using var reader = new StreamReader(HttpContext.Request.Body); var json = JObject.Parse(await reader.ReadToEndAsync()); var result = await ModInit.SubscribeApi( uid, json.Value("tmdb_id"), json.Value("title") ?? "", json.Value("voice") ?? "", json.Value("season"), json.Value("episode"), json.Value("mirage_orid") ?? "", json.Value("mirage_voice_id"), json.Value("voice_episode"), json.Value("collaps_orid") ?? "", json.Value("voice_source") ?? "", json.Value("kp_id") ); return Json(result); } [HttpPost] [Route("/api/tg/unsubscribe")] public async Task Unsubscribe() { if (!ModInit.IsRunning) return Json(new { success = false, msg = "bot_not_running" }); var uid = ModInit.ResolveUid(requestInfo.user_uid, Request.Query["uid"].ToString()); if (string.IsNullOrEmpty(uid)) return Json(new { success = false, msg = "no_uid" }); using var reader = new StreamReader(HttpContext.Request.Body); var json = JObject.Parse(await reader.ReadToEndAsync()); return Json(ModInit.UnsubscribeApi(uid, json.Value("tmdb_id"), json.Value("voice"))); } [HttpGet] [Route("/api/tg/status")] public ActionResult Status(int tmdb_id) { var uid = ModInit.ResolveUid(requestInfo.user_uid, Request.Query["uid"].ToString()); if (string.IsNullOrEmpty(uid)) return Json(new { success = false, msg = "no_uid" }); return Json(ModInit.StatusApi(uid, tmdb_id)); } [HttpGet] [Route("/api/tg/voices")] public async Task Voices(string title, int year, int season, int tmdb_id) { return Json(await ModInit.GetVoicesApi(title ?? "", year, season, tmdb_id)); } [HttpGet] [Route("/api/tg/link")] public async Task GetLink() { var uid = requestInfo.user_uid; if (string.IsNullOrEmpty(uid)) return Json(new { success = false, msg = "no_uid" }); return Json(await ModInit.LinkApi(uid)); } /// /// Список всех подписок пользователя с метаданными (для страницы подписок в Lampa) /// [HttpGet] [Route("/api/tg/subscriptions")] public ActionResult Subscriptions() { var uid = ModInit.ResolveUid(requestInfo.user_uid, Request.Query["uid"].ToString()); if (string.IsNullOrEmpty(uid)) return Json(new { success = false, msg = "no_uid" }); return Json(ModInit.SubscriptionsApi(uid)); } } }