Compare commits

..

4 Commits

Author SHA1 Message Date
801bb689fb Добавил ICS
Some checks failed
Create and publish a Docker image / Publish image (push) Failing after 10s
2025-09-22 23:22:13 +03:00
5b961e5b18 Перенёс Abstractions в другой namespace и небольшие улучшения 2025-09-22 23:17:59 +03:00
f9bf2a46e0 Фикс устаревших функций 2025-09-22 23:11:54 +03:00
8cb78cd208 Фикс документации 2025-09-22 22:51:22 +03:00
14 changed files with 929 additions and 94 deletions

View File

@@ -1,4 +1,4 @@
namespace SfeduSchedule.Plugin.Abstractions;
namespace SfeduSchedule.Abstractions;
// Базовый контракт плагина (общий для хоста и плагинов)
public interface IPlugin

View File

@@ -1,6 +1,6 @@
using System.ComponentModel;
namespace SfeduSchedule.Plugin.Abstractions;
namespace SfeduSchedule.Abstractions;
/// <summary>
/// DTO для запроса расписания в Modeus.

View File

@@ -0,0 +1,709 @@
// <auto-generated />
// Вот этим сайтом https://app.quicktype.io/?l=csharp
// Не является точной копией ответа, могут быть отличия
#nullable enable
#pragma warning disable CS8618
#pragma warning disable CS8601
#pragma warning disable CS8603
namespace SfeduSchedule
{
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Globalization;
public partial class Schedule
{
[JsonPropertyName("_embedded")]
public Embedded Embedded { get; set; }
[JsonPropertyName("page")]
public Page Page { get; set; }
}
public partial class Embedded
{
[JsonPropertyName("events")]
public Event[] Events { get; set; }
[JsonPropertyName("course-unit-realizations")]
public CourseUnitRealization[] CourseUnitRealizations { get; set; }
[JsonPropertyName("cycle-realizations")]
public CycleRealization[] CycleRealizations { get; set; }
[JsonPropertyName("lesson-realization-teams")]
public LessonRealizationTeam[] LessonRealizationTeams { get; set; }
[JsonPropertyName("lesson-realizations")]
public LessonRealization[] LessonRealizations { get; set; }
[JsonPropertyName("event-locations")]
public EventLocation[] EventLocations { get; set; }
[JsonPropertyName("durations")]
public Duration[] Durations { get; set; }
[JsonPropertyName("event-rooms")]
public EventRoom[] EventRooms { get; set; }
[JsonPropertyName("rooms")]
public Room[] Rooms { get; set; }
[JsonPropertyName("buildings")]
public BuildingElement[] Buildings { get; set; }
[JsonPropertyName("event-teams")]
public EventTeam[] EventTeams { get; set; }
[JsonPropertyName("event-organizers")]
public EventOrganizer[] EventOrganizers { get; set; }
[JsonPropertyName("event-attendees")]
public EventAttendee[] EventAttendees { get; set; }
[JsonPropertyName("persons")]
public Person[] Persons { get; set; }
}
public partial class BuildingElement
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("address")]
public string Address { get; set; }
[JsonPropertyName("searchableAddress")]
public string SearchableAddress { get; set; }
[JsonPropertyName("displayOrder")]
public long DisplayOrder { get; set; }
[JsonPropertyName("_links")]
public BuildingLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class BuildingLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
}
public partial class Self
{
[JsonPropertyName("href")]
public string Href { get; set; }
}
public partial class CourseUnitRealization
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("prototypeId")]
public Guid PrototypeId { get; set; }
[JsonPropertyName("_links")]
public CourseUnitRealizationLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class CourseUnitRealizationLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("planning-period")]
public Self PlanningPeriod { get; set; }
}
public partial class CycleRealization
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("code")]
public string Code { get; set; }
[JsonPropertyName("courseUnitRealizationNameShort")]
public string CourseUnitRealizationNameShort { get; set; }
[JsonPropertyName("_links")]
public CycleRealizationLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class CycleRealizationLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("course-unit-realization")]
public Self CourseUnitRealization { get; set; }
}
public partial class Duration
{
[JsonPropertyName("eventId")]
public Guid EventId { get; set; }
[JsonPropertyName("value")]
public long Value { get; set; }
[JsonPropertyName("timeUnitId")]
public string TimeUnitId { get; set; }
[JsonPropertyName("minutes")]
public long Minutes { get; set; }
[JsonPropertyName("_links")]
public DurationLinks Links { get; set; }
}
public partial class DurationLinks
{
[JsonPropertyName("self")]
public Self[] Self { get; set; }
[JsonPropertyName("time-unit")]
public Self TimeUnit { get; set; }
}
public partial class EventAttendee
{
[JsonPropertyName("roleId")]
public string RoleId { get; set; }
[JsonPropertyName("roleName")]
public string RoleName { get; set; }
[JsonPropertyName("roleNamePlural")]
public string RoleNamePlural { get; set; }
[JsonPropertyName("roleDisplayOrder")]
public long RoleDisplayOrder { get; set; }
[JsonPropertyName("_links")]
public EventAttendeeLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class EventAttendeeLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("event")]
public Self Event { get; set; }
[JsonPropertyName("person")]
public Self Person { get; set; }
}
public partial class EventLocation
{
[JsonPropertyName("eventId")]
public Guid EventId { get; set; }
[JsonPropertyName("customLocation")]
public string CustomLocation { get; set; }
[JsonPropertyName("_links")]
public EventLocationLinks Links { get; set; }
}
public partial class EventLocationLinks
{
[JsonPropertyName("self")]
public Self[] Self { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("event-rooms")]
public Self EventRooms { get; set; }
}
public partial class EventOrganizer
{
[JsonPropertyName("eventId")]
public Guid EventId { get; set; }
[JsonPropertyName("_links")]
public EventOrganizerLinks Links { get; set; }
}
public partial class EventOrganizerLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("event")]
public Self Event { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("event-attendees")]
public EventAttendees? EventAttendees { get; set; }
}
public partial class EventRoom
{
[JsonPropertyName("_links")]
public EventRoomLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class EventRoomLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("event")]
public Self Event { get; set; }
[JsonPropertyName("room")]
public Self Room { get; set; }
}
public partial class EventTeam
{
[JsonPropertyName("eventId")]
public Guid EventId { get; set; }
[JsonPropertyName("size")]
public long Size { get; set; }
[JsonPropertyName("_links")]
public EventTeamLinks Links { get; set; }
}
public partial class EventTeamLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("event")]
public Self Event { get; set; }
}
public partial class Event
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("description")]
public object Description { get; set; }
[JsonPropertyName("typeId")]
public string TypeId { get; set; }
[JsonPropertyName("formatId")]
public string FormatId { get; set; }
[JsonPropertyName("start")]
public DateTime Start { get; set; }
[JsonPropertyName("end")]
public DateTime End { get; set; }
[JsonPropertyName("startsAtLocal")]
public DateTime StartsAtLocal { get; set; }
[JsonPropertyName("endsAtLocal")]
public DateTime EndsAtLocal { get; set; }
[JsonPropertyName("startsAt")]
public DateTime StartsAt { get; set; }
[JsonPropertyName("endsAt")]
public DateTime EndsAt { get; set; }
[JsonPropertyName("holdingStatus")]
public HoldingStatus HoldingStatus { get; set; }
[JsonPropertyName("repeatedLessonRealization")]
public RepeatedLessonRealization RepeatedLessonRealization { get; set; }
[JsonPropertyName("userRoleIds")]
public string[] UserRoleIds { get; set; }
[JsonPropertyName("lessonTemplateId")]
public Guid LessonTemplateId { get; set; }
[JsonPropertyName("__version")]
public long Version { get; set; }
[JsonPropertyName("_links")]
public Dictionary<string, Self> Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class HoldingStatus
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("audModifiedAt")]
public DateTimeOffset? AudModifiedAt { get; set; }
[JsonPropertyName("audModifiedBy")]
public Guid? AudModifiedBy { get; set; }
[JsonPropertyName("audModifiedBySystem")]
public bool? AudModifiedBySystem { get; set; }
}
public partial class RepeatedLessonRealization
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("lessonTeamName")]
public string LessonTeamName { get; set; }
}
public partial class LessonRealizationTeam
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("cycleRealizationId")]
public Guid CycleRealizationId { get; set; }
[JsonPropertyName("_links")]
public BuildingLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class LessonRealization
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("prototypeId")]
public Guid PrototypeId { get; set; }
[JsonPropertyName("ordinal")]
public long Ordinal { get; set; }
[JsonPropertyName("_links")]
public BuildingLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class Person
{
[JsonPropertyName("lastName")]
public string LastName { get; set; }
[JsonPropertyName("firstName")]
public string FirstName { get; set; }
[JsonPropertyName("middleName")]
public string MiddleName { get; set; }
[JsonPropertyName("fullName")]
public string FullName { get; set; }
[JsonPropertyName("_links")]
public BuildingLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class Room
{
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("building")]
public RoomBuilding Building { get; set; }
[JsonPropertyName("projectorAvailable")]
public bool ProjectorAvailable { get; set; }
[JsonPropertyName("totalCapacity")]
public long TotalCapacity { get; set; }
[JsonPropertyName("workingCapacity")]
public long WorkingCapacity { get; set; }
[JsonPropertyName("deletedAtUtc")]
public object DeletedAtUtc { get; set; }
[JsonPropertyName("_links")]
public RoomLinks Links { get; set; }
[JsonPropertyName("id")]
public Guid Id { get; set; }
}
public partial class RoomBuilding
{
[JsonPropertyName("id")]
public Guid Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("nameShort")]
public string NameShort { get; set; }
[JsonPropertyName("address")]
public string Address { get; set; }
[JsonPropertyName("displayOrder")]
public long DisplayOrder { get; set; }
}
public partial class RoomLinks
{
[JsonPropertyName("self")]
public Self Self { get; set; }
[JsonPropertyName("type")]
public Self Type { get; set; }
[JsonPropertyName("building")]
public Self Building { get; set; }
}
public partial class Page
{
[JsonPropertyName("size")]
public long Size { get; set; }
[JsonPropertyName("totalElements")]
public long TotalElements { get; set; }
[JsonPropertyName("totalPages")]
public long TotalPages { get; set; }
[JsonPropertyName("number")]
public long Number { get; set; }
}
public partial struct EventAttendees
{
public Self Self;
public Self[] SelfArray;
public static implicit operator EventAttendees(Self Self) => new EventAttendees { Self = Self };
public static implicit operator EventAttendees(Self[] SelfArray) => new EventAttendees { SelfArray = SelfArray };
}
public partial class Schedule
{
public static Schedule FromJson(string json) => JsonSerializer.Deserialize<Schedule>(json, SfeduSchedule.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Schedule self) => JsonSerializer.Serialize(self, SfeduSchedule.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerOptions Settings = new(JsonSerializerDefaults.General)
{
Converters =
{
EventAttendeesConverter.Singleton,
new DateOnlyConverter(),
new TimeOnlyConverter(),
IsoDateTimeOffsetConverter.Singleton
},
};
}
internal class EventAttendeesConverter : JsonConverter<EventAttendees>
{
public override bool CanConvert(Type t) => t == typeof(EventAttendees);
public override EventAttendees Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.StartObject:
var objectValue = JsonSerializer.Deserialize<Self>(ref reader, options);
return new EventAttendees { Self = objectValue };
case JsonTokenType.StartArray:
var arrayValue = JsonSerializer.Deserialize<Self[]>(ref reader, options);
return new EventAttendees { SelfArray = arrayValue };
}
throw new Exception("Cannot unmarshal type EventAttendees");
}
public override void Write(Utf8JsonWriter writer, EventAttendees value, JsonSerializerOptions options)
{
if (value.SelfArray != null)
{
JsonSerializer.Serialize(writer, value.SelfArray, options);
return;
}
if (value.Self != null)
{
JsonSerializer.Serialize(writer, value.Self, options);
return;
}
throw new Exception("Cannot marshal type EventAttendees");
}
public static readonly EventAttendeesConverter Singleton = new EventAttendeesConverter();
}
public class DateOnlyConverter : JsonConverter<DateOnly>
{
private readonly string serializationFormat;
public DateOnlyConverter() : this(null) { }
public DateOnlyConverter(string? serializationFormat)
{
this.serializationFormat = serializationFormat ?? "yyyy-MM-dd";
}
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
return DateOnly.Parse(value!);
}
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString(serializationFormat));
}
public class TimeOnlyConverter : JsonConverter<TimeOnly>
{
private readonly string serializationFormat;
public TimeOnlyConverter() : this(null) { }
public TimeOnlyConverter(string? serializationFormat)
{
this.serializationFormat = serializationFormat ?? "HH:mm:ss.fff";
}
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var value = reader.GetString();
return TimeOnly.Parse(value!);
}
public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString(serializationFormat));
}
internal class IsoDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
{
public override bool CanConvert(Type t) => t == typeof(DateTimeOffset);
private const string DefaultDateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK";
private DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind;
private string? _dateTimeFormat;
private CultureInfo? _culture;
public DateTimeStyles DateTimeStyles
{
get => _dateTimeStyles;
set => _dateTimeStyles = value;
}
public string? DateTimeFormat
{
get => _dateTimeFormat ?? string.Empty;
set => _dateTimeFormat = (string.IsNullOrEmpty(value)) ? null : value;
}
public CultureInfo Culture
{
get => _culture ?? CultureInfo.CurrentCulture;
set => _culture = value;
}
public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
{
string text;
if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
|| (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
{
value = value.ToUniversalTime();
}
text = value.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
writer.WriteStringValue(text);
}
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? dateText = reader.GetString();
if (string.IsNullOrEmpty(dateText) == false)
{
if (!string.IsNullOrEmpty(_dateTimeFormat))
{
return DateTimeOffset.ParseExact(dateText, _dateTimeFormat, Culture, _dateTimeStyles);
}
else
{
return DateTimeOffset.Parse(dateText, Culture, _dateTimeStyles);
}
}
else
{
return default(DateTimeOffset);
}
}
public static readonly IsoDateTimeOffsetConverter Singleton = new IsoDateTimeOffsetConverter();
}
}
#pragma warning restore CS8618
#pragma warning restore CS8601
#pragma warning restore CS8603

View File

@@ -7,4 +7,8 @@
<OutputType>Library</OutputType>
</PropertyGroup>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591;CS1573</NoWarn>
</PropertyGroup>
</Project>

View File

@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using SfeduSchedule.Plugin.Abstractions;
using SfeduSchedule.Abstractions;
namespace SfeduSchedule.Plugin.Sample;

View File

@@ -12,9 +12,7 @@
</PropertyGroup>
<ItemGroup>
<!-- Даёт доступ к Microsoft.AspNetCore.* (ControllerBase и т.п.) -->
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
<ProjectReference Include="..\SfeduSchedule.Plugin.Abstractions\SfeduSchedule.Plugin.Abstractions.csproj" />
<ProjectReference Include="..\SfeduSchedule.Abstractions\SfeduSchedule.Abstractions.csproj" />
</ItemGroup>

View File

@@ -2,7 +2,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule", "SfeduSchedule\SfeduSchedule.csproj", "{57B088A7-D7E2-4B5D-82A4-A3070A78A3E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule.Plugin.Abstractions", "SfeduSchedule.Plugin.Abstractions\SfeduSchedule.Plugin.Abstractions.csproj", "{B2E8DAD7-7373-4155-B230-4E53DFC04445}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule.Abstractions", "SfeduSchedule.Abstractions\SfeduSchedule.Abstractions.csproj", "{B2E8DAD7-7373-4155-B230-4E53DFC04445}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SfeduSchedule.Plugin.Sample", "SfeduSchedule.Plugin.Sample\SfeduSchedule.Plugin.Sample.csproj", "{B2B6D730-46AE-40ED-815F-81176FB4E545}"
EndProject

View File

@@ -3,17 +3,16 @@ using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using SfeduSchedule.Plugin.Abstractions;
using SfeduSchedule.Abstractions;
using SfeduSchedule.Services;
namespace SfeduSchedule.Controllers
{
namespace SfeduSchedule.Controllers;
[ApiController]
[Route("api/[controller]")]
[EnableRateLimiting("throttle")]
public class ScheduleController(ModeusService modeusService, ILogger<ScheduleController> logger) : ControllerBase
{
/// <summary>
/// Получить расписание по пользовательскому запросу.
/// </summary>
@@ -31,9 +30,13 @@ namespace SfeduSchedule.Controllers
}
catch (HttpRequestException e)
{
logger.LogError("Ошибка при получении расписания\n\n" + e.Message + "\n\n" + e.StackTrace + "\n\n JSON: " + JsonSerializer.Serialize(request, GlobalVariables.jsonSerializerOptions));
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError), "Proxied Modeus: " + e.Message);
logger.LogError("Ошибка при получении расписания\n\n" + e.Message + "\n\n" + e.StackTrace +
"\n\n JSON: " +
JsonSerializer.Serialize(request, GlobalVariables.jsonSerializerOptions));
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError),
"Proxied Modeus: " + e.Message);
}
return Ok(schedule);
}
@@ -55,9 +58,12 @@ namespace SfeduSchedule.Controllers
}
catch (HttpRequestException e)
{
logger.LogError("Ошибка при поиске аудиторий\n\n" + e.Message + "\n\n" + e.StackTrace + "\n\n JSON: " + JsonSerializer.Serialize(request, GlobalVariables.jsonSerializerOptions));
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError), "Proxied Modeus: " + e.Message);
logger.LogError("Ошибка при поиске аудиторий\n\n" + e.Message + "\n\n" + e.StackTrace + "\n\n JSON: " +
JsonSerializer.Serialize(request, GlobalVariables.jsonSerializerOptions));
return StatusCode((int)(e.StatusCode ?? HttpStatusCode.InternalServerError),
"Proxied Modeus: " + e.Message);
}
return Ok(rooms);
}
@@ -71,6 +77,7 @@ namespace SfeduSchedule.Controllers
/// <response code="401">Неавторизованный</response>
[HttpGet]
[Authorize(AuthenticationSchemes = "ApiKey")]
[DisableRateLimiting]
[Route("getguid")]
public async Task<IActionResult> GetGuid(string fullname)
{
@@ -80,5 +87,43 @@ namespace SfeduSchedule.Controllers
return Ok(guid);
}
/// <summary>
/// Получить расписание в формате ICS по пользовательскому запросу.
/// </summary>
/// <param name="request">Объект запроса, содержащий параметры фильтрации расписания.</param>
/// <returns>Файл ICS с расписанием за -1 неделя + 1 месяц</returns>
/// <response code="200">Возвращает файл ICS с расписанием</response>
/// <response code="404">Расписание не найдено</response>
/// <response code="429">Слишком много запросов</response>
[HttpPost]
[Route("ics")]
public async Task<IActionResult> PostIcs([FromBody] ModeusScheduleRequest request)
{
var ics = await modeusService.GetIcsAsync(request);
if (string.IsNullOrEmpty(ics))
return NotFound();
return new FileContentResult(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar")
{
FileDownloadName = "schedule.ics"
};
}
/// <summary>
/// Получить расписание в формате ICS для указанного пользователя за -1 неделя + 1 месяц.
/// </summary>
/// <param name="attendeePersonId"></param>
/// <returns>Файл ICS с расписанием</returns>
/// <response code="200">Возвращает файл ICS с расписанием</response>
/// <response code="404">Расписание не найдено</response>
/// <response code="429">Слишком много запросов</response>
[HttpGet]
[Route("ics")]
public async Task<IActionResult> GetIcs([FromQuery] Guid attendeePersonId)
{
return await PostIcs(new ModeusScheduleRequest(1000, DateTime.UtcNow.AddDays(-7),
DateTime.UtcNow.AddMonths(1), null, new List<Guid> { attendeePersonId }, null, null, null, null, null,
null, null));
}
}

View File

@@ -16,7 +16,7 @@ namespace SfeduSchedule.Controllers
/// <summary>
/// Получить GUID пользователя через авторизацию Microsoft.
/// </summary>
/// <param name="redirectUri">Необязательный параметр. Если указан, произойдет редирект на указанный URI после получения GUID. (<url>/?guid=XXX)</param>
/// <param name="redirectUri">Необязательный параметр. Если указан, произойдет редирект на указанный URI после получения GUID. ([url]/?guid=XXX)</param>
/// <returns>Строка GUID пользователя или редирект на указанный URI.</returns>
/// <response code="200">Возвращает GUID пользователя</response>
/// <response code="302">Редирект на указанный URI</response>

View File

@@ -15,9 +15,16 @@ public static class MicrosoftLoginHelper
await page.WaitForURLAsync(new Regex("login\\.(microsoftonline|live)\\.com", RegexOptions.IgnoreCase), new PageWaitForURLOptions { Timeout = 60_000 });
var useAnotherAccount = page.Locator("div#otherTile, #otherTileText, div[data-test-id='useAnotherAccount']");
if (await useAnotherAccount.First.IsVisibleAsync(new LocatorIsVisibleOptions { Timeout = 2000 }))
await useAnotherAccount.First.ClickAsync();
var useAnotherAccount = page.Locator("div#otherTile, #otherTileText, div[data-test-id='useAnotherAccount']").First;
try
{
await Assertions.Expect(useAnotherAccount).ToBeVisibleAsync(new() { Timeout = 2000 });
await useAnotherAccount.ClickAsync();
}
catch (PlaywrightException)
{
// Кнопка не появилась — пропускаем
}
var emailInput = page.Locator("input[name='loginfmt'], input#i0116");
await emailInput.WaitForAsync(new LocatorWaitForOptions { State = WaitForSelectorState.Visible, Timeout = 30_000 });
@@ -33,15 +40,20 @@ public static class MicrosoftLoginHelper
await page.WaitForSelectorAsync("button, input[type='submit'], a", new PageWaitForSelectorOptions { Timeout = 8000 });
var kmsiYesNoVisible = await page.Locator("#idSIButton9, #idBtn_Back").First.IsVisibleAsync(new LocatorIsVisibleOptions { Timeout = 3000 });
if (kmsiYesNoVisible)
var locator = page.Locator("#idSIButton9, #idBtn_Back").First;
try
{
await Assertions.Expect(locator).ToBeVisibleAsync(new() { Timeout = 3000 });
var noBtn = page.Locator("#idBtn_Back");
if (await noBtn.IsVisibleAsync())
await noBtn.ClickAsync();
else
await page.Locator("#idSIButton9").ClickAsync();
}
catch (PlaywrightException)
{
// Кнопки не появились — пропускаем этот шаг
}
await page.WaitForURLAsync(url => !Regex.IsMatch(new Uri(url).Host, "login\\.(microsoftonline|live)\\.com", RegexOptions.IgnoreCase), new PageWaitForURLOptions { Timeout = 60_000 });
await page.WaitForLoadStateAsync(LoadState.NetworkIdle);

View File

@@ -1,6 +1,6 @@
using System.Reflection;
using System.Runtime.Loader;
using SfeduSchedule.Plugin.Abstractions;
using SfeduSchedule.Abstractions;
namespace SfeduSchedule;
@@ -50,7 +50,7 @@ public sealed class PluginLoadContext : AssemblyLoadContext
}
// Разрешаем управляемые зависимости плагина из его папки.
// Возвращаем null, чтобы отдать решение в Default ALC для общих сборок (например, SfeduSchedule.Plugin.Abstractions).
// Возвращаем null, чтобы отдать решение в Default ALC для общих сборок (например, SfeduSchedule.Abstractions).
protected override Assembly? Load(AssemblyName assemblyName)
{
var path = _resolver.ResolveAssemblyToPath(assemblyName);

View File

@@ -18,6 +18,12 @@ string? tgChatId = configuration["TG_CHAT_ID"];
string? tgToken = configuration["TG_TOKEN"];
string updateJwtCron = configuration["UPDATE_JWT_CRON"] ?? "0 0 4 ? * *";
// Если не указана TZ, ставим Europe/Moscow
if (string.IsNullOrEmpty(configuration["TZ"]))
{
configuration["TZ"] = "Europe/Moscow";
}
int permitLimit = int.TryParse(configuration["PERMIT_LIMIT"], out var parsedPermitLimit) ? parsedPermitLimit : 40;
int timeLimit = int.TryParse(configuration["TIME_LIMIT"], out var parsedTimeLimit) ? parsedTimeLimit : 10;
@@ -92,9 +98,13 @@ if (string.IsNullOrEmpty(preinstalledJwtToken))
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
var xmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
var mainXmlFile = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
var mainXmlPath = Path.Combine(AppContext.BaseDirectory, mainXmlFile);
options.IncludeXmlComments(mainXmlPath);
var pluginXmlFile = "SfeduSchedule.Abstractions.xml";
var pluginXmlPath = Path.Combine(AppContext.BaseDirectory, pluginXmlFile);
options.IncludeXmlComments(pluginXmlPath);
// Добавляем только схему авторизации по ApiKey
options.AddSecurityDefinition(ApiKeyAuthenticationDefaults.Scheme, new Microsoft.OpenApi.Models.OpenApiSecurityScheme

View File

@@ -1,6 +1,9 @@
using System.Text.Json;
using Ical.Net.CalendarComponents;
using Ical.Net.DataTypes;
using Ical.Net.Serialization;
using Microsoft.Net.Http.Headers;
using SfeduSchedule.Plugin.Abstractions;
using SfeduSchedule.Abstractions;
namespace SfeduSchedule.Services
{
@@ -20,9 +23,9 @@ namespace SfeduSchedule.Services
_httpClient.DefaultRequestHeaders.Add(HeaderNames.Authorization, $"Bearer {token}");
}
public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr, string TZ = "Europe/Moscow")
public async Task<string?> GetScheduleAsync(ModeusScheduleRequest msr)
{
var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/calendar/events/search?tz={TZ}");
var request = new HttpRequestMessage(HttpMethod.Post, $"schedule-calendar-v2/api/calendar/events/search?tz={_configuration["TZ"]!}");
request.Content = new StringContent(JsonSerializer.Serialize(msr, GlobalVariables.jsonSerializerOptions), System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.SendAsync(request);
_logger.LogInformation("GetScheduleAsync: Ответ получен: {StatusCode}", response.StatusCode);
@@ -75,5 +78,58 @@ namespace SfeduSchedule.Services
return personId;
}
public async Task<string?> GetIcsAsync(ModeusScheduleRequest msr)
{
var schedule = await GetScheduleAsync(msr);
if (schedule == null)
{
_logger.LogError("GetIcsAsync: Schedule is null. Request: {@msr}", msr);
return null;
}
Schedule? scheduleJson;
try
{
scheduleJson = Schedule.FromJson(schedule);
}
catch (Exception ex)
{
_logger.LogError(ex, "GetIcsAsync: Deserialization failed. Schedule: {Schedule}", schedule);
return null;
}
if (scheduleJson?.Embedded?.Events is not { Length: > 0 } events)
{
if (scheduleJson == null)
_logger.LogError("GetIcsAsync: scheduleJson is null. Schedule: {Schedule}", schedule);
else if (scheduleJson.Embedded == null)
_logger.LogError("GetIcsAsync: scheduleJson.Embedded is null. scheduleJson: {@scheduleJson}", scheduleJson);
else if (scheduleJson.Embedded.Events == null)
_logger.LogError("GetIcsAsync: scheduleJson.Embedded.Events is null. Embedded: {@Embedded}", scheduleJson.Embedded);
else
_logger.LogWarning("GetIcsAsync: scheduleJson.Embedded.Events is empty. Embedded: {@Embedded}", scheduleJson.Embedded);
return null;
}
var calendar = new Ical.Net.Calendar();
calendar.AddTimeZone(new VTimeZone(_configuration["TZ"]!));
foreach (var e in scheduleJson.Embedded.Events)
{
calendar.Events.Add(new CalendarEvent
{
Summary = e.Name,
Description = e.NameShort,
Start = new CalDateTime(e.StartsAtLocal, _configuration["TZ"]!),
End = new CalDateTime(e.EndsAtLocal, _configuration["TZ"]!),
});
}
var serializer = new CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
_logger.LogInformation("GetIcsAsync: Serialized calendar created. Length: {Length}", serializedCalendar?.Length ?? 0);
return serializedCalendar;
}
}
}

View File

@@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ical.Net" Version="5.1.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.14.0" />
<PackageReference Include="Microsoft.Playwright" Version="1.55.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.15.0" />
@@ -18,7 +19,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SfeduSchedule.Plugin.Abstractions\SfeduSchedule.Plugin.Abstractions.csproj" />
<ProjectReference Include="..\SfeduSchedule.Abstractions\SfeduSchedule.Abstractions.csproj" />
</ItemGroup>
</Project>