1
1
using System ;
2
+ using System . Security . Cryptography ;
2
3
using System . Text ;
3
4
using DL444 . CquSchedule . Backend . Models ;
4
5
using Ical . Net ;
@@ -22,7 +23,7 @@ internal interface ICalendarService
22
23
{
23
24
int VacationCalendarServeDays { get ; }
24
25
25
- string GetCalendar ( Term currentTerm , Schedule schedule , CalenderEventCategories eventCategories = CalenderEventCategories . All , int remindTime = 15 ) ;
26
+ string GetCalendar ( string username , Term currentTerm , Schedule schedule , CalenderEventCategories eventCategories = CalenderEventCategories . All , int remindTime = 15 ) ;
26
27
string GetEmptyCalendar ( ) ;
27
28
}
28
29
@@ -37,7 +38,7 @@ public CalendarService(IConfiguration config, IWellknownDataService wellknown, I
37
38
38
39
public int VacationCalendarServeDays { get ; }
39
40
40
- public string GetCalendar ( Term currentTerm , Schedule schedule , CalenderEventCategories eventCategories , int remindTime )
41
+ public string GetCalendar ( string username , Term currentTerm , Schedule schedule , CalenderEventCategories eventCategories , int remindTime )
41
42
{
42
43
if ( DateTimeOffset . Now > currentTerm . EndDate . AddDays ( VacationCalendarServeDays )
43
44
|| DateTimeOffset . Now < currentTerm . StartDate . AddDays ( - VacationCalendarServeDays ) )
@@ -57,7 +58,7 @@ public string GetCalendar(Term currentTerm, Schedule schedule, CalenderEventCate
57
58
{
58
59
continue ;
59
60
}
60
- calendar . Events . Add ( GetCalendarEvent ( entry , currentTerm , week , remindTime , descriptionBuilder ) ) ;
61
+ calendar . Events . Add ( GetCalendarEvent ( username , entry , currentTerm , week , remindTime , descriptionBuilder ) ) ;
61
62
}
62
63
}
63
64
}
@@ -66,7 +67,7 @@ public string GetCalendar(Term currentTerm, Schedule schedule, CalenderEventCate
66
67
{
67
68
foreach ( var exam in schedule . Exams )
68
69
{
69
- calendar . Events . Add ( GetCalendarEvent ( exam , remindTime ) ) ;
70
+ calendar . Events . Add ( GetCalendarEvent ( username , exam , remindTime ) ) ;
70
71
}
71
72
}
72
73
@@ -75,7 +76,7 @@ public string GetCalendar(Term currentTerm, Schedule schedule, CalenderEventCate
75
76
76
77
public string GetEmptyCalendar ( ) => new CalendarSerializer ( new Calendar ( ) ) . SerializeToString ( ) ;
77
78
78
- private CalendarEvent GetCalendarEvent ( ScheduleEntry entry , Term currentTerm , ScheduleWeek week , int remindTime , StringBuilder descriptionBuilder )
79
+ private CalendarEvent GetCalendarEvent ( string username , ScheduleEntry entry , Term currentTerm , ScheduleWeek week , int remindTime , StringBuilder descriptionBuilder )
79
80
{
80
81
TimeSpan startTime = wellknown . Schedule [ entry . StartSession - 1 ] . StartOffset ;
81
82
int endSession = Math . Min ( entry . EndSession , wellknown . Schedule . Count ) ;
@@ -97,7 +98,7 @@ private CalendarEvent GetCalendarEvent(ScheduleEntry entry, Term currentTerm, Sc
97
98
descriptionBuilder . Append ( locService . GetString ( "CalendarLecturer" , locService . DefaultCulture , entry . Lecturer ) ) ;
98
99
}
99
100
100
- return new CalendarEvent ( )
101
+ var calendarEvent = new CalendarEvent ( )
101
102
{
102
103
Summary = entry . Name ,
103
104
DtStart = GetTime ( currentTerm . StartDate , week . WeekNumber , entry . DayOfWeek , startTime ) ,
@@ -113,12 +114,14 @@ private CalendarEvent GetCalendarEvent(ScheduleEntry entry, Term currentTerm, Sc
113
114
}
114
115
}
115
116
} ;
117
+ calendarEvent . Uid = GetUid ( calendarEvent , username ) ;
118
+ return calendarEvent ;
116
119
}
117
120
118
- private static CalendarEvent GetCalendarEvent ( ExamEntry exam , int remindTime )
121
+ private static CalendarEvent GetCalendarEvent ( string username , ExamEntry exam , int remindTime )
119
122
{
120
123
bool roomSimplified = exam . Room != null && ! exam . Room . Equals ( exam . SimplifiedRoom , StringComparison . Ordinal ) ;
121
- return new CalendarEvent ( )
124
+ var calendarEvent = new CalendarEvent ( )
122
125
{
123
126
Summary = exam . Name ,
124
127
DtStart = new CalDateTime ( exam . StartTime . UtcDateTime ) ,
@@ -134,6 +137,8 @@ private static CalendarEvent GetCalendarEvent(ExamEntry exam, int remindTime)
134
137
}
135
138
}
136
139
} ;
140
+ calendarEvent . Uid = GetUid ( calendarEvent , username ) ;
141
+ return calendarEvent ;
137
142
}
138
143
139
144
private static CalDateTime GetTime ( DateTimeOffset termStartDate , int week , int dayOfWeek , TimeSpan time )
@@ -143,6 +148,17 @@ private static CalDateTime GetTime(DateTimeOffset termStartDate, int week, int d
143
148
return new CalDateTime ( dateTime . UtcDateTime ) ;
144
149
}
145
150
151
+ private static string GetUid ( CalendarEvent calendarEvent , string ancillaryIdentifier )
152
+ {
153
+ // UIDs must be unique across events and temporally persistent at the same time,
154
+ // or clients might briefly show duplicated events during refresh.
155
+ // This property choice ensures that no duplicated UIDs will be generated even if
156
+ // the user has conflicting schedules, while minimizing the likelihood of UID changes
157
+ // in the event of schedule updates (summary and time rarely change).
158
+ var idSource = $ "{ ancillaryIdentifier } , { calendarEvent . Summary } , { calendarEvent . DtStart } , { calendarEvent . DtEnd } ";
159
+ return Convert . ToBase64String ( SHA256 . HashData ( Encoding . UTF8 . GetBytes ( idSource ) ) ) ;
160
+ }
161
+
146
162
private readonly IWellknownDataService wellknown ;
147
163
private readonly ILocalizationService locService ;
148
164
}
0 commit comments