mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Refactor User and Role into data classes (#531)
Use the JSON data class pattern for User, Role, and PlayerStats and move them into lib/src/data. This means that User is purely data now and can't reference role objects directly. To make it possible easily check permissions attached to roles and store data only visible on the controller on user objects a ControllerUser type is introduced which is linked with the UserManager and can therefore reference role objects directly. Resolves #525.
This commit is contained in:
1 parent
f802e6ba67
commit
e453a80155
37 files changed
+629
-613
No files matched your search
@@ -26,7 +26,7 @@ function calculateLastSeen(user: RawUserState, instanceId?: number) {
|
||||
}
|
||||
|
||||
export function formatLastSeen(user: RawUserState, instanceId?: number) {
|
||||
if (user.instances && user.instances.some(id => instanceId === undefined || id === instanceId)) {
|
||||
if (user.instances && [...user.instances].some(id => instanceId === undefined || id === instanceId)) {
|
||||
return <Tag color="green">Online</Tag>;
|
||||
}
|
||||
let lastSeen = calculateLastSeen(user, instanceId);
|
||||
@@ -38,7 +38,7 @@ export function formatLastSeen(user: RawUserState, instanceId?: number) {
|
||||
|
||||
export function sortLastSeen(userA:RawUserState, userB:RawUserState, instanceIdA?: number, instanceIdB?: number) {
|
||||
function epoch(user:RawUserState, instanceId?: number) {
|
||||
return user.instances && user.instances.some(id => instanceId === undefined || id === instanceId);
|
||||
return user.instances && [...user.instances].some(id => instanceId === undefined || id === instanceId);
|
||||
}
|
||||
|
||||
let epochA = epoch(userA, instanceIdA);
|
||||
@@ -52,7 +52,7 @@ export function sortLastSeen(userA:RawUserState, userB:RawUserState, instanceIdA
|
||||
return lastSeenA - lastSeenB;
|
||||
}
|
||||
|
||||
export type RawUserState = Partial<lib.RawUser> & {
|
||||
export type RawUserState = Partial<lib.User> & {
|
||||
loading?: boolean;
|
||||
present?: boolean;
|
||||
missing?: boolean;
|
||||
@@ -78,7 +78,7 @@ export function useUser(name: string): [RawUserState, () => void] {
|
||||
}
|
||||
updateUser();
|
||||
|
||||
function updateHandler(newUser: lib.RawUser) {
|
||||
function updateHandler(newUser: lib.User) {
|
||||
setUser({ ...newUser, present: true });
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ export function useUser(name: string): [RawUserState, () => void] {
|
||||
|
||||
export function useUserList() {
|
||||
let control = useContext(ControlContext);
|
||||
let [userList, setUserList] = useState<lib.RawUser[]>([]);
|
||||
let [userList, setUserList] = useState<lib.User[]>([]);
|
||||
|
||||
function updateUserList() {
|
||||
control.send(new lib.UserListRequest()).then(users => {
|
||||
@@ -106,7 +106,7 @@ export function useUserList() {
|
||||
useEffect(() => {
|
||||
updateUserList();
|
||||
|
||||
function updateHandler(newUser: lib.RawUser) {
|
||||
function updateHandler(newUser: lib.User) {
|
||||
setUserList(oldList => {
|
||||
let newList = oldList.concat();
|
||||
let index = newList.findIndex(u => u.name === newUser.name);
|
||||
|
||||
Reference in new issue
Block a user