bigbuffet-rw/app/soapbox/reducers/accounts.ts

293 lines
8.9 KiB
TypeScript
Raw Normal View History

2021-03-15 19:50:16 -07:00
import {
Map as ImmutableMap,
List as ImmutableList,
OrderedSet as ImmutableOrderedSet,
2021-03-15 19:50:16 -07:00
fromJS,
} from 'immutable';
2022-03-19 12:22:52 -07:00
import { AnyAction } from 'redux';
2021-03-15 19:50:16 -07:00
import {
ADMIN_USERS_FETCH_SUCCESS,
2021-03-15 19:50:16 -07:00
ADMIN_USERS_TAG_REQUEST,
2021-07-13 17:38:58 -07:00
ADMIN_USERS_TAG_SUCCESS,
2021-03-15 19:50:16 -07:00
ADMIN_USERS_TAG_FAIL,
ADMIN_USERS_UNTAG_REQUEST,
2021-07-13 17:38:58 -07:00
ADMIN_USERS_UNTAG_SUCCESS,
2021-03-15 19:50:16 -07:00
ADMIN_USERS_UNTAG_FAIL,
ADMIN_ADD_PERMISSION_GROUP_REQUEST,
2021-07-13 17:38:58 -07:00
ADMIN_ADD_PERMISSION_GROUP_SUCCESS,
ADMIN_ADD_PERMISSION_GROUP_FAIL,
ADMIN_REMOVE_PERMISSION_GROUP_REQUEST,
2021-07-13 17:38:58 -07:00
ADMIN_REMOVE_PERMISSION_GROUP_SUCCESS,
ADMIN_REMOVE_PERMISSION_GROUP_FAIL,
ADMIN_USERS_DELETE_REQUEST,
ADMIN_USERS_DELETE_FAIL,
ADMIN_USERS_DEACTIVATE_REQUEST,
ADMIN_USERS_DEACTIVATE_FAIL,
ADMIN_USERS_SUGGEST_REQUEST,
ADMIN_USERS_SUGGEST_FAIL,
ADMIN_USERS_UNSUGGEST_REQUEST,
ADMIN_USERS_UNSUGGEST_FAIL,
} from 'soapbox/actions/admin';
import { CHATS_FETCH_SUCCESS, CHATS_EXPAND_SUCCESS, CHAT_FETCH_SUCCESS } from 'soapbox/actions/chats';
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
2022-02-27 12:42:42 -08:00
import { normalizeAccount } from 'soapbox/normalizers/account';
import { normalizeId } from 'soapbox/utils/normalizers';
2022-01-10 14:01:24 -08:00
import {
ACCOUNT_IMPORT,
ACCOUNTS_IMPORT,
ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP,
} from '../actions/importer';
2020-03-27 13:59:38 -07:00
2022-03-19 12:22:52 -07:00
type AccountRecord = ReturnType<typeof normalizeAccount>;
type AccountMap = ImmutableMap<string, any>;
type APIEntity = Record<string, any>;
type APIEntities = Array<APIEntity>;
2020-03-27 13:59:38 -07:00
2022-03-31 15:00:31 -07:00
export interface ReducerAccount extends AccountRecord {
moved: string | null,
}
2022-04-04 12:24:42 -07:00
type State = ImmutableMap<any, ReducerAccount>;
2022-03-19 12:22:52 -07:00
const initialState: State = ImmutableMap();
2022-03-31 15:00:31 -07:00
const minifyAccount = (account: AccountRecord): ReducerAccount => {
2022-03-11 18:48:00 -08:00
return account.mergeWith((o, n) => n || o, {
moved: normalizeId(account.getIn(['moved', 'id'])),
2022-03-31 15:00:31 -07:00
}) as ReducerAccount;
2022-03-11 18:48:00 -08:00
};
2022-03-19 12:22:52 -07:00
const fixAccount = (state: State, account: APIEntity) => {
2022-03-16 19:52:20 -07:00
const normalized = minifyAccount(normalizeAccount(account));
return state.set(account.id, normalized);
2020-03-27 13:59:38 -07:00
};
2022-03-19 12:22:52 -07:00
const normalizeAccounts = (state: State, accounts: ImmutableList<AccountMap>) => {
2020-03-27 13:59:38 -07:00
accounts.forEach(account => {
2022-02-27 12:42:42 -08:00
state = fixAccount(state, account);
2020-03-27 13:59:38 -07:00
});
return state;
};
2022-03-19 12:22:52 -07:00
const importAccountFromChat = (
state: State,
chat: APIEntity,
): State => fixAccount(state, chat.account);
2022-03-19 12:22:52 -07:00
const importAccountsFromChats = (state: State, chats: APIEntities): State =>
state.withMutations(mutable =>
chats.forEach(chat => importAccountFromChat(mutable, chat)));
2022-03-19 12:22:52 -07:00
const addTags = (
state: State,
accountIds: Array<string>,
tags: Array<string>,
): State => {
2021-03-15 19:50:16 -07:00
return state.withMutations(state => {
accountIds.forEach(id => {
state.updateIn([id, 'pleroma', 'tags'], ImmutableList(), v =>
ImmutableOrderedSet(fromJS(v)).union(tags).toList(),
2021-03-15 19:50:16 -07:00
);
2022-04-24 15:27:08 -07:00
tags.forEach(tag => {
switch (tag) {
2022-05-11 14:06:35 -07:00
case 'verified':
state.setIn([id, 'verified'], true);
break;
case 'donor':
state.setIn([id, 'donor'], true);
break;
2022-04-24 15:27:08 -07:00
}
});
2021-03-15 19:50:16 -07:00
});
});
};
2022-03-19 12:22:52 -07:00
const removeTags = (
state: State,
accountIds: Array<string>,
tags: Array<string>,
): State => {
2021-03-15 19:50:16 -07:00
return state.withMutations(state => {
accountIds.forEach(id => {
state.updateIn([id, 'pleroma', 'tags'], ImmutableList(), v =>
ImmutableOrderedSet(fromJS(v)).subtract(tags).toList(),
2021-03-15 19:50:16 -07:00
);
2022-04-24 15:27:08 -07:00
tags.forEach(tag => {
switch (tag) {
2022-05-11 14:06:35 -07:00
case 'verified':
state.setIn([id, 'verified'], false);
break;
case 'donor':
state.setIn([id, 'donor'], false);
break;
2022-04-24 15:27:08 -07:00
}
});
2021-03-15 19:50:16 -07:00
});
});
};
2022-03-19 12:22:52 -07:00
const setActive = (state: State, accountIds: Array<string>, active: boolean): State => {
2021-06-30 01:02:52 -07:00
return state.withMutations(state => {
accountIds.forEach(id => {
state.setIn([id, 'pleroma', 'is_active'], active);
2021-06-30 01:02:52 -07:00
});
});
};
2022-03-19 12:22:52 -07:00
const permissionGroupFields: Record<string, string> = {
admin: 'is_admin',
moderator: 'is_moderator',
};
2022-03-19 12:22:52 -07:00
const addPermission = (
state: State,
accountIds: Array<string>,
permissionGroup: string,
): State => {
const field = permissionGroupFields[permissionGroup];
if (!field) return state;
return state.withMutations(state => {
accountIds.forEach(id => {
state.setIn([id, 'pleroma', field], true);
});
});
};
2022-03-19 12:22:52 -07:00
const removePermission = (
state: State,
accountIds: Array<string>,
permissionGroup: string,
): State => {
const field = permissionGroupFields[permissionGroup];
if (!field) return state;
return state.withMutations(state => {
accountIds.forEach(id => {
state.setIn([id, 'pleroma', field], false);
});
});
};
2022-03-19 12:22:52 -07:00
const buildAccount = (adminUser: ImmutableMap<string, any>): AccountRecord => normalizeAccount({
id: adminUser.get('id'),
username: adminUser.get('nickname').split('@')[0],
acct: adminUser.get('nickname'),
display_name: adminUser.get('display_name'),
display_name_html: adminUser.get('display_name'),
url: adminUser.get('url'),
avatar: adminUser.get('avatar'),
avatar_static: adminUser.get('avatar'),
created_at: adminUser.get('created_at'),
pleroma: {
is_active: adminUser.get('is_active'),
is_confirmed: adminUser.get('is_confirmed'),
is_admin: adminUser.getIn(['roles', 'admin']),
is_moderator: adminUser.getIn(['roles', 'moderator']),
2021-07-13 16:41:38 -07:00
tags: adminUser.get('tags'),
},
source: {
pleroma: {
actor_type: adminUser.get('actor_type'),
},
},
2021-07-13 16:35:37 -07:00
should_refetch: true,
2022-03-16 19:52:20 -07:00
});
2022-03-19 12:22:52 -07:00
const mergeAdminUser = (
account: AccountRecord,
adminUser: ImmutableMap<string, any>,
) => {
return account.withMutations(account => {
account.set('display_name', adminUser.get('display_name'));
account.set('avatar', adminUser.get('avatar'));
account.set('avatar_static', adminUser.get('avatar'));
account.setIn(['pleroma', 'is_active'], adminUser.get('is_active'));
account.setIn(['pleroma', 'is_admin'], adminUser.getIn(['roles', 'admin']));
account.setIn(['pleroma', 'is_moderator'], adminUser.getIn(['roles', 'moderator']));
account.setIn(['pleroma', 'is_confirmed'], adminUser.get('is_confirmed'));
2021-07-13 16:41:38 -07:00
account.setIn(['pleroma', 'tags'], adminUser.get('tags'));
});
};
2022-03-19 12:22:52 -07:00
const importAdminUser = (state: State, adminUser: ImmutableMap<string, any>): State => {
const id = adminUser.get('id');
const account = state.get(id);
if (!account) {
2022-03-31 15:00:31 -07:00
return state.set(id, minifyAccount(buildAccount(adminUser)));
} else {
2022-03-31 15:00:31 -07:00
return state.set(id, minifyAccount(mergeAdminUser(account, adminUser)));
}
};
2022-03-19 12:22:52 -07:00
const importAdminUsers = (state: State, adminUsers: Array<Record<string, any>>): State => {
return state.withMutations((state: State) => {
adminUsers.forEach(adminUser => {
importAdminUser(state, ImmutableMap(fromJS(adminUser)));
});
});
};
2022-03-19 12:22:52 -07:00
const setSuggested = (state: State, accountIds: Array<string>, isSuggested: boolean): State => {
return state.withMutations(state => {
accountIds.forEach(id => {
state.setIn([id, 'pleroma', 'is_suggested'], isSuggested);
});
});
};
2022-03-19 12:22:52 -07:00
export default function accounts(state: State = initialState, action: AnyAction): State {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case ACCOUNT_IMPORT:
return fixAccount(state, action.account);
case ACCOUNTS_IMPORT:
return normalizeAccounts(state, action.accounts);
case ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP:
return fixAccount(state, { id: -1, username: action.username });
case CHATS_FETCH_SUCCESS:
case CHATS_EXPAND_SUCCESS:
return importAccountsFromChats(state, action.chats);
case CHAT_FETCH_SUCCESS:
case STREAMING_CHAT_UPDATE:
return importAccountsFromChats(state, [action.chat]);
case ADMIN_USERS_TAG_REQUEST:
case ADMIN_USERS_TAG_SUCCESS:
case ADMIN_USERS_UNTAG_FAIL:
return addTags(state, action.accountIds, action.tags);
case ADMIN_USERS_UNTAG_REQUEST:
case ADMIN_USERS_UNTAG_SUCCESS:
case ADMIN_USERS_TAG_FAIL:
return removeTags(state, action.accountIds, action.tags);
case ADMIN_ADD_PERMISSION_GROUP_REQUEST:
case ADMIN_ADD_PERMISSION_GROUP_SUCCESS:
case ADMIN_REMOVE_PERMISSION_GROUP_FAIL:
return addPermission(state, action.accountIds, action.permissionGroup);
case ADMIN_REMOVE_PERMISSION_GROUP_REQUEST:
case ADMIN_REMOVE_PERMISSION_GROUP_SUCCESS:
case ADMIN_ADD_PERMISSION_GROUP_FAIL:
return removePermission(state, action.accountIds, action.permissionGroup);
case ADMIN_USERS_DELETE_REQUEST:
case ADMIN_USERS_DEACTIVATE_REQUEST:
return setActive(state, action.accountIds, false);
case ADMIN_USERS_DELETE_FAIL:
case ADMIN_USERS_DEACTIVATE_FAIL:
return setActive(state, action.accountIds, true);
case ADMIN_USERS_FETCH_SUCCESS:
return importAdminUsers(state, action.users);
case ADMIN_USERS_SUGGEST_REQUEST:
case ADMIN_USERS_UNSUGGEST_FAIL:
return setSuggested(state, action.accountIds, true);
case ADMIN_USERS_UNSUGGEST_REQUEST:
case ADMIN_USERS_SUGGEST_FAIL:
return setSuggested(state, action.accountIds, false);
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}