-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathSession.cs
102 lines (81 loc) · 2.3 KB
/
Session.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RealtimeCodeEditor.Models.Entities
{
public class Session : IDisposable
{
public string SessionCode { get; private set; }
public SessionTypeEnum Type { get; set; }
public string Creator { get; private set; }
public bool IsLocked { get; private set; }
public string SavedState { get; set; }
public DateTime LastActiveDateTime { get; set; }
private ISet<string> _users;
public Session(string sessionCode, string creator)
{
SessionCode = sessionCode;
Type = SessionTypeEnum.Active;
Creator = creator;
IsLocked = false;
_users = new HashSet<string>();
}
public void AddUser(string user)
{
if (Type == SessionTypeEnum.Expired)
{
throw new Exception("Attempts to update an expired session.");
}
_users.Add(user);
}
public void RemoveUser(string user)
{
if (Type == SessionTypeEnum.Expired)
{
throw new Exception("Attempts to update an expired session.");
}
_users.Remove(user);
}
public string[] GetUsers(string except = "")
{
if (except == "")
{
return _users.ToArray();
}
else
{
return _users.Where(s => s != except).ToArray();
}
}
public bool HasUser(string user)
{
return _users.Contains<string>(user);
}
public void TouchSession()
{
if (Type == SessionTypeEnum.Expired)
{
throw new Exception("Attempts to touch an expired session.");
}
LastActiveDateTime = DateTime.UtcNow;
}
public void Expire()
{
Type = SessionTypeEnum.Expired;
}
public void Lock()
{
IsLocked = true;
}
public void Unlock()
{
IsLocked = false;
}
public void Dispose()
{
_users.Clear();
}
}
}