Skip to content

Commit b8c50ef

Browse files
authored
Merge pull request #35 from YassinLokhat/30-core-improvments
Core improvement from GUI project
2 parents cb4fcd9 + 54eb3d5 commit b8c50ef

32 files changed

Lines changed: 751 additions & 775 deletions

Core/Models/Account.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,17 @@ internal bool PasswordExpired
141141
DateTime lastPassword = Passwords.Keys.Max();
142142
int delay = ((DateTime.Now.Year - lastPassword.Year) * 12) + DateTime.Now.Month - lastPassword.Month;
143143

144-
return delay >= PasswordUpdateReminderDelay;
144+
return delay > PasswordUpdateReminderDelay;
145145
}
146146
}
147147

148-
internal bool PasswordLeaked => Options.ContainsFlag(AccountOption.WarnIfPasswordLeaked) && Database.PasswordFactory.PasswordLeaked(Password);
148+
internal bool PasswordLeaked { get; set; } = false;
149149

150-
public void Apply(Change change)
150+
internal void Apply(Change change)
151151
{
152152
switch (change.ActionType)
153153
{
154-
case LogEventType.ItemUpdated:
154+
case ActivityEventType.ItemUpdated:
155155
switch (change.FieldName)
156156
{
157157
case nameof(Label):
@@ -178,7 +178,7 @@ public void Apply(Change change)
178178
}
179179
break;
180180
default:
181-
throw new InvalidEnumArgumentException(nameof(change.ActionType), (int)change.ActionType, typeof(LogEventType));
181+
throw new InvalidEnumArgumentException(nameof(change.ActionType), (int)change.ActionType, typeof(ActivityEventType));
182182
}
183183
}
184184

@@ -193,5 +193,7 @@ public override string ToString()
193193

194194
return account + $"({string.Join(", ", Identifiers)})";
195195
}
196+
197+
public bool HasChanged() => Database.HasChanged(ItemId);
196198
}
197199
}

Core/Models/Activity.cs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using Upsilon.Apps.Passkey.Core.Utils;
2+
using Upsilon.Apps.Passkey.Interfaces.Enums;
3+
using Upsilon.Apps.Passkey.Interfaces.Models;
4+
5+
namespace Upsilon.Apps.Passkey.Core.Models
6+
{
7+
internal class Activity : IActivity
8+
{
9+
#region IActivity interface
10+
11+
public DateTime DateTime => new(DateTimeTicks);
12+
13+
public string ItemId { get; } = string.Empty;
14+
15+
public ActivityEventType EventType { get; set; } = ActivityEventType.None;
16+
17+
public bool NeedsReview { get; set; } = true;
18+
19+
public string Message => _buildMessage();
20+
21+
#endregion
22+
23+
public long DateTimeTicks { get; set; }
24+
public string[] Data { get; set; } = [];
25+
26+
public Activity(long dateTimeTicks, string itemId, ActivityEventType eventType, string[] data, bool needsReview)
27+
{
28+
DateTimeTicks = dateTimeTicks;
29+
ItemId = itemId;
30+
EventType = eventType;
31+
Data = data;
32+
NeedsReview = needsReview;
33+
}
34+
35+
public Activity(string activity)
36+
{
37+
string[] info = activity.Split('|');
38+
39+
if (info.Length > 0)
40+
{
41+
DateTimeTicks = Convert.ToInt64(info[0], 16);
42+
}
43+
44+
if (info.Length > 1)
45+
{
46+
ItemId = info[1];
47+
}
48+
49+
if (info.Length > 2
50+
&& byte.TryParse(info[2], out byte eventType))
51+
{
52+
EventType = (ActivityEventType)eventType;
53+
}
54+
55+
if (info.Length > 3)
56+
{
57+
NeedsReview = !string.IsNullOrEmpty(info[3]);
58+
}
59+
60+
if (info.Length > 4)
61+
{
62+
activity = string.Join("|", info[4..])
63+
.Replace("|", "/|")
64+
.Replace("\\/|", "\\|");
65+
info = activity.Split("/|");
66+
Data = [.. info.Select(x => x.Replace("\\|", "|"))];
67+
}
68+
}
69+
70+
public override string ToString()
71+
{
72+
string activity = $"{DateTimeTicks:X}|{ItemId}|{(int)EventType}|{(NeedsReview ? "1" : "")}";
73+
74+
string[] data = [.. Data.Select(x => x.Replace("|", "\\|"))];
75+
if (data.Length != 0)
76+
{
77+
activity += $"|{string.Join("|", data)}";
78+
}
79+
80+
return activity;
81+
}
82+
83+
private string _buildMessage()
84+
{
85+
string message = EventType switch
86+
{
87+
ActivityEventType.MergeAndSaveThenRemoveAutoSaveFile => $"User {Data[0]}'s autosave merged and saved",
88+
ActivityEventType.MergeWithoutSavingAndKeepAutoSaveFile => $"User {Data[0]}'s autosave merged without saving",
89+
ActivityEventType.DontMergeAndRemoveAutoSaveFile => $"User {Data[0]}'s autosave not merged and removed",
90+
ActivityEventType.DontMergeAndKeepAutoSaveFile => $"User {Data[0]}'s autosave not merged and keeped",
91+
ActivityEventType.DatabaseCreated => $"User {Data[0]}'s database created",
92+
ActivityEventType.DatabaseOpened => $"User {Data[0]}'s database opened",
93+
ActivityEventType.DatabaseSaved => $"User {Data[0]}'s database saved",
94+
ActivityEventType.DatabaseClosed => $"User {Data[0]}'s database closed",
95+
ActivityEventType.LoginSessionTimeoutReached => $"User {Data[0]}'s login session timeout reached",
96+
ActivityEventType.LoginFailed => $"User {Data[0]} login failed at level {Data[1]}",
97+
ActivityEventType.UserLoggedIn => $"User {Data[0]} logged in",
98+
ActivityEventType.UserLoggedOut => $"User {Data[0]} logged out {(!string.IsNullOrEmpty(Data[1]) ? "without saving" : "")}",
99+
ActivityEventType.ImportingDataStarted => $"Importing data from file : '{Data[0]}'",
100+
ActivityEventType.ImportingDataSucceded => $"Import completed successfully",
101+
ActivityEventType.ImportingDataFailed => $"Import failed because {Data[0]}",
102+
ActivityEventType.ExportingDataStarted => $"Exporting data to file : '{Data[0]}'",
103+
ActivityEventType.ExportingDataSucceded => $"Export completed successfully",
104+
ActivityEventType.ExportingDataFailed => $"Export failed because {Data[0]}",
105+
ActivityEventType.ItemUpdated => $"{(Data.Length > 3 ? $"{Data[3]}'s " : "")}{Data[0]}'s {Data[1].ToSentenceCase().ToLower()} has been {(string.IsNullOrWhiteSpace(Data[2]) ? $"updated" : $"set to {Data[2]}")}",
106+
ActivityEventType.ItemAdded => $"{Data[2]} has been added to {Data[0]}",
107+
ActivityEventType.ItemDeleted => $"{Data[2]} has been removed from {Data[0]}",
108+
_ => ToString(),
109+
};
110+
111+
return message.Trim();
112+
}
113+
}
114+
}

Core/Models/AutoSave.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal T UpdateValue<T>(string itemId,
2828
newValue.SerializeWith(Database.SerializationCenter),
2929
readableValue,
3030
needsReview,
31-
LogEventType.ItemUpdated);
31+
ActivityEventType.ItemUpdated);
3232
}
3333

3434
return newValue;
@@ -39,7 +39,7 @@ internal T AddValue<T>(string itemId,
3939
bool needsReview,
4040
T value) where T : notnull
4141
{
42-
_addChange(itemId, string.Empty, value.SerializeWith(Database.SerializationCenter), readableValue, needsReview, LogEventType.ItemAdded);
42+
_addChange(itemId, string.Empty, value.SerializeWith(Database.SerializationCenter), readableValue, needsReview, ActivityEventType.ItemAdded);
4343

4444
return value;
4545
}
@@ -49,7 +49,7 @@ internal T DeleteValue<T>(string itemId,
4949
bool needsReview,
5050
T value) where T : notnull
5151
{
52-
_addChange(itemId, string.Empty, value.SerializeWith(Database.SerializationCenter), readableValue, needsReview, LogEventType.ItemDeleted);
52+
_addChange(itemId, string.Empty, value.SerializeWith(Database.SerializationCenter), readableValue, needsReview, ActivityEventType.ItemDeleted);
5353

5454
return value;
5555
}
@@ -59,7 +59,7 @@ private void _addChange(string itemId,
5959
string newValue,
6060
string readableValue,
6161
bool needsReview,
62-
LogEventType action)
62+
ActivityEventType action)
6363
{
6464
_addChange(itemId,
6565
fieldName,
@@ -76,7 +76,7 @@ private void _addChange(string itemId,
7676
string newValue,
7777
string readableValue,
7878
bool needsReview,
79-
LogEventType action)
79+
ActivityEventType action)
8080
{
8181
string changeKey = $"{itemId}\t{fieldName}";
8282
if (!Changes.ContainsKey(changeKey))
@@ -97,12 +97,14 @@ private void _addChange(string itemId,
9797
_mergeChanges(changeKey, currentChange);
9898

9999
Database.FileLocker.Save(this, Database.AutoSaveFileEntry, Database.Passkeys);
100+
string itemName = string.Empty;
101+
string parentName = string.Empty;
100102

101103
if (itemId == Database.User?.ItemId)
102104
{
103105
if (Database.User is not null)
104106
{
105-
itemId = Database.User.ToString();
107+
itemName = Database.User.ToString();
106108
}
107109
}
108110
else if (itemId.StartsWith('S'))
@@ -111,7 +113,7 @@ private void _addChange(string itemId,
111113

112114
if (s is not null)
113115
{
114-
itemId = s.ToString();
116+
itemName = s.ToString();
115117
}
116118
}
117119
else if (itemId.StartsWith('A'))
@@ -120,20 +122,33 @@ private void _addChange(string itemId,
120122

121123
if (a is not null)
122124
{
123-
itemId = a.ToString();
125+
itemName = a.ToString();
126+
127+
if (action == ActivityEventType.ItemUpdated)
128+
{
129+
parentName = a.Service.ToString();
130+
}
124131
}
125132
}
126133

127-
Database.Logs.AddLog(data: [itemId, fieldName, readableValue],
134+
string[] data = [itemName, fieldName, readableValue];
135+
136+
if (!string.IsNullOrEmpty(parentName))
137+
{
138+
data = [.. data, parentName];
139+
}
140+
141+
Database.ActivityCenter.AddActivity(itemId: itemId,
128142
eventType: action,
143+
data,
129144
needsReview);
130145
}
131146

132147
private void _mergeChanges(string changeKey, Change currentChange)
133148
{
134-
Change? lastUpdate = Changes[changeKey].LastOrDefault(x => x.ActionType == LogEventType.ItemUpdated);
149+
Change? lastUpdate = Changes[changeKey].LastOrDefault(x => x.ActionType == ActivityEventType.ItemUpdated);
135150

136-
if (currentChange.ActionType != LogEventType.ItemUpdated
151+
if (currentChange.ActionType != ActivityEventType.ItemUpdated
137152
|| lastUpdate is null)
138153
{
139154
Changes[changeKey].Add(currentChange);

Core/Models/Change.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Upsilon.Apps.Passkey.Core.Models
55
internal sealed class Change
66
{
77
public long Index { get; set; } = long.MaxValue;
8-
public LogEventType ActionType { get; set; } = LogEventType.None;
8+
public ActivityEventType ActionType { get; set; } = ActivityEventType.None;
99
public string ItemId { get; set; } = string.Empty;
1010
public string FieldName { get; set; } = string.Empty;
1111
public string? OldValue { get; set; } = null;

0 commit comments

Comments
 (0)