2022-01-10 14:01:24 -08:00
|
|
|
import {
|
|
|
|
Map as ImmutableMap,
|
|
|
|
List as ImmutableList,
|
|
|
|
Set as ImmutableSet,
|
|
|
|
OrderedSet as ImmutableOrderedSet,
|
|
|
|
fromJS,
|
|
|
|
is,
|
|
|
|
} from 'immutable';
|
2020-12-29 13:55:04 -08:00
|
|
|
import {
|
2020-12-29 20:17:03 -08:00
|
|
|
ADMIN_CONFIG_FETCH_SUCCESS,
|
2021-07-28 15:06:21 -07:00
|
|
|
ADMIN_CONFIG_UPDATE_SUCCESS,
|
2020-12-29 13:55:04 -08:00
|
|
|
ADMIN_REPORTS_FETCH_SUCCESS,
|
2020-12-31 16:24:52 -08:00
|
|
|
ADMIN_REPORTS_PATCH_REQUEST,
|
|
|
|
ADMIN_REPORTS_PATCH_SUCCESS,
|
2020-12-29 13:55:04 -08:00
|
|
|
ADMIN_USERS_FETCH_SUCCESS,
|
2020-12-29 16:38:58 -08:00
|
|
|
ADMIN_USERS_DELETE_REQUEST,
|
2020-12-29 16:22:31 -08:00
|
|
|
ADMIN_USERS_DELETE_SUCCESS,
|
2020-12-29 16:38:58 -08:00
|
|
|
ADMIN_USERS_APPROVE_REQUEST,
|
2020-12-29 16:22:31 -08:00
|
|
|
ADMIN_USERS_APPROVE_SUCCESS,
|
2020-12-29 13:55:04 -08:00
|
|
|
} from '../actions/admin';
|
2020-08-24 15:18:53 -07:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
2020-12-31 12:29:31 -08:00
|
|
|
reports: ImmutableMap(),
|
|
|
|
openReports: ImmutableOrderedSet(),
|
2020-12-29 13:55:04 -08:00
|
|
|
users: ImmutableMap(),
|
2021-07-14 10:27:51 -07:00
|
|
|
latestUsers: ImmutableOrderedSet(),
|
2020-12-29 13:55:04 -08:00
|
|
|
awaitingApproval: ImmutableOrderedSet(),
|
2020-12-29 20:17:03 -08:00
|
|
|
configs: ImmutableList(),
|
|
|
|
needsReboot: false,
|
2020-08-24 15:18:53 -07:00
|
|
|
});
|
|
|
|
|
2021-07-14 10:27:51 -07:00
|
|
|
const FILTER_UNAPPROVED = ['local', 'need_approval'];
|
|
|
|
const FILTER_LATEST = ['local', 'active'];
|
|
|
|
|
|
|
|
const filtersMatch = (f1, f2) => is(ImmutableSet(f1), ImmutableSet(f2));
|
|
|
|
const toIds = items => items.map(item => item.id);
|
|
|
|
|
|
|
|
const mergeSet = (state, key, users) => {
|
|
|
|
const newIds = toIds(users);
|
|
|
|
return state.update(key, ImmutableOrderedSet(), ids => ids.union(newIds));
|
|
|
|
};
|
|
|
|
|
|
|
|
const replaceSet = (state, key, users) => {
|
|
|
|
const newIds = toIds(users);
|
|
|
|
return state.set(key, ImmutableOrderedSet(newIds));
|
|
|
|
};
|
|
|
|
|
|
|
|
const maybeImportUnapproved = (state, users, filters) => {
|
|
|
|
if (filtersMatch(FILTER_UNAPPROVED, filters)) {
|
|
|
|
return mergeSet(state, 'awaitingApproval', users);
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const maybeImportLatest = (state, users, filters, page) => {
|
|
|
|
if (page === 1 && filtersMatch(FILTER_LATEST, filters)) {
|
|
|
|
return replaceSet(state, 'latestUsers', users);
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const importUser = (state, user) => (
|
|
|
|
state.setIn(['users', user.id], ImmutableMap({
|
|
|
|
email: user.email,
|
|
|
|
registration_reason: user.registration_reason,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
|
|
|
|
function importUsers(state, users, filters, page) {
|
2020-12-29 13:55:04 -08:00
|
|
|
return state.withMutations(state => {
|
2021-07-14 10:27:51 -07:00
|
|
|
maybeImportUnapproved(state, users, filters);
|
|
|
|
maybeImportLatest(state, users, filters, page);
|
|
|
|
|
2020-12-29 13:55:04 -08:00
|
|
|
users.forEach(user => {
|
2021-07-14 10:27:51 -07:00
|
|
|
importUser(state, user);
|
2020-12-29 16:22:31 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-13 16:11:11 -07:00
|
|
|
function deleteUsers(state, accountIds) {
|
2020-12-29 16:22:31 -08:00
|
|
|
return state.withMutations(state => {
|
2021-07-13 16:11:11 -07:00
|
|
|
accountIds.forEach(id => {
|
|
|
|
state.update('awaitingApproval', orderedSet => orderedSet.delete(id));
|
|
|
|
state.deleteIn(['users', id]);
|
2020-12-29 16:22:31 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function approveUsers(state, users) {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
users.forEach(user => {
|
|
|
|
state.update('awaitingApproval', orderedSet => orderedSet.delete(user.nickname));
|
|
|
|
state.setIn(['users', user.nickname], fromJS(user));
|
2020-12-29 13:55:04 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-31 12:29:31 -08:00
|
|
|
function importReports(state, reports) {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
reports.forEach(report => {
|
2020-12-31 18:11:08 -08:00
|
|
|
report.statuses = report.statuses.map(status => status.id);
|
2020-12-31 12:29:31 -08:00
|
|
|
if (report.state === 'open') {
|
|
|
|
state.update('openReports', orderedSet => orderedSet.add(report.id));
|
|
|
|
}
|
|
|
|
state.setIn(['reports', report.id], fromJS(report));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-31 16:24:52 -08:00
|
|
|
function handleReportDiffs(state, reports) {
|
|
|
|
// Note: the reports here aren't full report objects
|
|
|
|
// hence the need for a new function.
|
|
|
|
return state.withMutations(state => {
|
|
|
|
reports.forEach(report => {
|
|
|
|
switch(report.state) {
|
|
|
|
case 'open':
|
|
|
|
state.update('openReports', orderedSet => orderedSet.add(report.id));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state.update('openReports', orderedSet => orderedSet.delete(report.id));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:18:53 -07:00
|
|
|
export default function admin(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2020-12-29 20:17:03 -08:00
|
|
|
case ADMIN_CONFIG_FETCH_SUCCESS:
|
2021-07-28 15:06:21 -07:00
|
|
|
case ADMIN_CONFIG_UPDATE_SUCCESS:
|
2020-12-29 20:17:03 -08:00
|
|
|
return state.set('configs', fromJS(action.configs));
|
2020-08-24 15:18:53 -07:00
|
|
|
case ADMIN_REPORTS_FETCH_SUCCESS:
|
2020-12-31 12:29:31 -08:00
|
|
|
return importReports(state, action.reports);
|
2020-12-31 16:24:52 -08:00
|
|
|
case ADMIN_REPORTS_PATCH_REQUEST:
|
|
|
|
case ADMIN_REPORTS_PATCH_SUCCESS:
|
|
|
|
return handleReportDiffs(state, action.reports);
|
2020-12-29 13:55:04 -08:00
|
|
|
case ADMIN_USERS_FETCH_SUCCESS:
|
2021-07-14 10:27:51 -07:00
|
|
|
return importUsers(state, action.users, action.filters, action.page);
|
2020-12-29 16:38:58 -08:00
|
|
|
case ADMIN_USERS_DELETE_REQUEST:
|
2020-12-29 16:22:31 -08:00
|
|
|
case ADMIN_USERS_DELETE_SUCCESS:
|
2021-07-13 16:11:11 -07:00
|
|
|
return deleteUsers(state, action.accountIds);
|
2020-12-29 16:38:58 -08:00
|
|
|
case ADMIN_USERS_APPROVE_REQUEST:
|
2021-07-13 16:11:11 -07:00
|
|
|
return state.update('awaitingApproval', set => set.subtract(action.accountIds));
|
2020-12-29 16:22:31 -08:00
|
|
|
case ADMIN_USERS_APPROVE_SUCCESS:
|
|
|
|
return approveUsers(state, action.users);
|
2020-08-24 15:18:53 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|