UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

506 lines (505 loc) 28.1 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.getLastActiveTimestampUnits = exports.displayLastActiveLabel = exports.isFirstAdmin = exports.getProfilesNotInCurrentGroup = exports.getProfilesInGroupWithoutSorting = exports.getProfilesInGroup = exports.getUsersInVisibleDMs = exports.shouldShowTermsOfService = exports.getProfilesNotInCurrentTeam = exports.getProfilesNotInTeam = exports.getProfilesInTeam = exports.getProfilesInCurrentTeam = exports.getProfilesNotInCurrentChannel = exports.getActiveProfilesInCurrentChannelWithoutSorting = exports.getActiveProfilesInCurrentChannel = exports.getProfilesInCurrentChannel = exports.getProfiles = exports.getProfileSetNotInCurrentTeam = exports.getProfileSetInCurrentTeam = exports.getProfileSetNotInCurrentChannel = exports.getProfileSetInCurrentChannel = exports.getHighlightWithoutNotificationKeys = exports.getCurrentUserMentionKeys = exports.getCurrentUserRoles = exports.currentUserHasAnAdminRole = exports.isCurrentUserGuestUser = exports.isCurrentUserSystemAdmin = exports.getUsersByEmail = exports.getUsersByUsername = exports.getUsers = exports.getCurrentUserId = exports.getCurrentUser = void 0; exports.getUserIdsInChannels = getUserIdsInChannels; exports.getUserIdsNotInChannels = getUserIdsNotInChannels; exports.getUserIdsInTeams = getUserIdsInTeams; exports.getUserIdsNotInTeams = getUserIdsNotInTeams; exports.getUserIdsInGroups = getUserIdsInGroups; exports.getUserIdsNotInGroups = getUserIdsNotInGroups; exports.getUserStatuses = getUserStatuses; exports.getUserSessions = getUserSessions; exports.getUserAudits = getUserAudits; exports.getUser = getUser; exports.makeGetUsersByIds = makeGetUsersByIds; exports.getUserByUsername = getUserByUsername; exports.getUserByEmail = getUserByEmail; exports.filterProfiles = filterProfiles; exports.getIsManualStatusForUserId = getIsManualStatusForUserId; exports.getStatusForUserId = getStatusForUserId; exports.getDndEndTimeForUserId = getDndEndTimeForUserId; exports.getTotalUsersStats = getTotalUsersStats; exports.getFilteredUsersStats = getFilteredUsersStats; exports.makeSearchProfilesStartingWithTerm = makeSearchProfilesStartingWithTerm; exports.makeSearchProfilesMatchingWithTerm = makeSearchProfilesMatchingWithTerm; exports.makeSearchProfilesInChannel = makeSearchProfilesInChannel; exports.searchProfilesInCurrentChannel = searchProfilesInCurrentChannel; exports.searchActiveProfilesInCurrentChannel = searchActiveProfilesInCurrentChannel; exports.searchProfilesNotInCurrentChannel = searchProfilesNotInCurrentChannel; exports.searchProfilesInCurrentTeam = searchProfilesInCurrentTeam; exports.searchProfilesInTeam = searchProfilesInTeam; exports.searchProfilesNotInCurrentTeam = searchProfilesNotInCurrentTeam; exports.makeGetProfilesForReactions = makeGetProfilesForReactions; exports.makeGetProfilesInChannel = makeGetProfilesInChannel; exports.makeGetProfilesNotInChannel = makeGetProfilesNotInChannel; exports.makeGetProfilesByIdsAndUsernames = makeGetProfilesByIdsAndUsernames; exports.makeGetDisplayName = makeGetDisplayName; exports.makeDisplayNameGetter = makeDisplayNameGetter; exports.searchProfilesInGroup = searchProfilesInGroup; exports.searchProfilesInGroupWithoutSorting = searchProfilesInGroupWithoutSorting; exports.getUserLastActivities = getUserLastActivities; exports.getLastActivityForUserId = getLastActivityForUserId; exports.checkIsFirstAdmin = checkIsFirstAdmin; const constants_1 = require("mattermost-redux/constants"); const create_selector_1 = require("mattermost-redux/selectors/create_selector"); const common_1 = require("mattermost-redux/selectors/entities/common"); const general_1 = require("mattermost-redux/selectors/entities/general"); const preferences_1 = require("mattermost-redux/selectors/entities/preferences"); const post_utils_1 = require("mattermost-redux/utils/post_utils"); const user_utils_1 = require("mattermost-redux/utils/user_utils"); // Re-define these types to ensure that these are typed correctly when mattermost-redux is published exports.getCurrentUser = common_1.getCurrentUser; exports.getCurrentUserId = common_1.getCurrentUserId; exports.getUsers = common_1.getUsers; function getUserIdsInChannels(state) { return state.entities.users.profilesInChannel; } function getUserIdsNotInChannels(state) { return state.entities.users.profilesNotInChannel; } function getUserIdsInTeams(state) { return state.entities.users.profilesInTeam; } function getUserIdsNotInTeams(state) { return state.entities.users.profilesNotInTeam; } function getUserIdsInGroups(state) { return state.entities.users.profilesInGroup; } function getUserIdsNotInGroups(state) { return state.entities.users.profilesNotInGroup; } function getUserStatuses(state) { return state.entities.users.statuses; } function getUserSessions(state) { return state.entities.users.mySessions; } function getUserAudits(state) { return state.entities.users.myAudits; } function getUser(state, id) { return state.entities.users.profiles[id]; } function makeGetUsersByIds() { return (0, create_selector_1.createSelector)('getUsersByIds', exports.getUsers, (state, ids) => ids, (users, ids) => { return ids.map((userId) => users[userId]); }); } exports.getUsersByUsername = (0, create_selector_1.createSelector)('getUsersByUsername', exports.getUsers, (users) => { const usersByUsername = {}; for (const id in users) { if (Object.hasOwn(users, id)) { const user = users[id]; usersByUsername[user.username] = user; } } return usersByUsername; }); function getUserByUsername(state, username) { return (0, exports.getUsersByUsername)(state)[username]; } exports.getUsersByEmail = (0, create_selector_1.createSelector)('getUsersByEmail', exports.getUsers, (users) => { const usersByEmail = {}; for (const user of Object.keys(users).map((key) => users[key])) { usersByEmail[user.email] = user; } return usersByEmail; }); function getUserByEmail(state, email) { return (0, exports.getUsersByEmail)(state)[email]; } exports.isCurrentUserSystemAdmin = (0, create_selector_1.createSelector)('isCurrentUserSystemAdmin', exports.getCurrentUser, (user) => { const roles = user?.roles || ''; return (0, user_utils_1.isSystemAdmin)(roles); }); exports.isCurrentUserGuestUser = (0, create_selector_1.createSelector)('isCurrentUserGuestUser', exports.getCurrentUser, (user) => { const roles = user?.roles || ''; return (0, user_utils_1.isGuest)(roles); }); exports.currentUserHasAnAdminRole = (0, create_selector_1.createSelector)('currentUserHasAnAdminRole', exports.getCurrentUser, (user) => { const roles = user.roles || ''; return (0, user_utils_1.includesAnAdminRole)(roles); }); exports.getCurrentUserRoles = (0, create_selector_1.createSelector)('getCurrentUserRoles', common_1.getMyCurrentChannelMembership, (state) => state.entities.teams.myMembers[state.entities.teams.currentTeamId], exports.getCurrentUser, (currentChannelMembership, currentTeamMembership, currentUser) => { let roles = ''; if (currentTeamMembership) { roles += `${currentTeamMembership.roles} `; } if (currentChannelMembership) { roles += `${currentChannelMembership.roles} `; } if (currentUser) { roles += currentUser.roles; } return roles.trim(); }); exports.getCurrentUserMentionKeys = (0, create_selector_1.createSelector)('getCurrentUserMentionKeys', exports.getCurrentUser, (user) => { let keys = []; if (!user || !user.notify_props) { return keys; } if (user.notify_props.mention_keys) { keys = keys.concat(user.notify_props.mention_keys.split(',').map((key) => { return { key }; })); } if (user.notify_props.first_name === 'true' && user.first_name) { keys.push({ key: user.first_name, caseSensitive: true }); } if (user.notify_props.channel === 'true') { keys.push({ key: '@channel' }); keys.push({ key: '@all' }); keys.push({ key: '@here' }); } const usernameKey = '@' + user.username; if (keys.findIndex((key) => key.key === usernameKey) === -1) { keys.push({ key: usernameKey }); } return keys; }); exports.getHighlightWithoutNotificationKeys = (0, create_selector_1.createSelector)('getHighlightWithoutNotificationKeys', exports.getCurrentUser, (user) => { const highlightKeys = []; if (user?.notify_props?.highlight_keys?.length > 0) { user.notify_props.highlight_keys.split(',').forEach((key) => { highlightKeys.push({ key }); }); } return highlightKeys; }); exports.getProfileSetInCurrentChannel = (0, create_selector_1.createSelector)('getProfileSetInCurrentChannel', common_1.getCurrentChannelId, getUserIdsInChannels, (currentChannel, channelProfiles) => { return channelProfiles[currentChannel]; }); exports.getProfileSetNotInCurrentChannel = (0, create_selector_1.createSelector)('getProfileSetNotInCurrentChannel', common_1.getCurrentChannelId, getUserIdsNotInChannels, (currentChannel, channelProfiles) => { return channelProfiles[currentChannel]; }); exports.getProfileSetInCurrentTeam = (0, create_selector_1.createSelector)('getProfileSetInCurrentTeam', (state) => state.entities.teams.currentTeamId, getUserIdsInTeams, (currentTeam, teamProfiles) => { return teamProfiles[currentTeam]; }); exports.getProfileSetNotInCurrentTeam = (0, create_selector_1.createSelector)('getProfileSetNotInCurrentTeam', (state) => state.entities.teams.currentTeamId, getUserIdsNotInTeams, (currentTeam, teamProfiles) => { return teamProfiles[currentTeam]; }); const PROFILE_SET_ALL = 'all'; function sortAndInjectProfiles(profiles, profileSet) { const currentProfiles = injectProfiles(profiles, profileSet); return currentProfiles.sort(user_utils_1.sortByUsername); } function injectProfiles(profiles, profileSet) { let currentProfiles = []; if (typeof profileSet === 'undefined') { return currentProfiles; } else if (profileSet === PROFILE_SET_ALL) { currentProfiles = Object.keys(profiles).map((key) => profiles[key]); } else { currentProfiles = Array.from(profileSet).map((p) => profiles[p]); } return currentProfiles.filter((profile) => Boolean(profile)); } exports.getProfiles = (0, create_selector_1.createSelector)('getProfiles', exports.getUsers, (state, filters) => filters, (profiles, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters), PROFILE_SET_ALL); }); function filterProfiles(profiles, filters, memberships) { if (!filters) { return profiles; } let users = Object.keys(profiles).map((key) => profiles[key]); const filterRole = (filters.role && filters.role !== '') ? [filters.role] : []; const filterRoles = [...filterRole, ...(filters.roles || []), ...(filters.team_roles || []), ...(filters.channel_roles || [])]; const excludeRoles = filters.exclude_roles || []; if (filterRoles.length > 0 || excludeRoles.length > 0) { users = users.filter((user) => { return user.roles.length > 0 && (0, user_utils_1.applyRolesFilters)(user, filterRoles, excludeRoles, memberships?.[user.id]); }); } if (filters.exclude_bots) { users = users.filter((user) => !user.is_bot); } if (filters.inactive) { users = users.filter((user) => user.delete_at !== 0); } else if (filters.active) { users = users.filter((user) => user.delete_at === 0); } if (filters.exclude_remote) { users = users.filter((user) => !user.remote_id); } return users.reduce((acc, user) => { acc[user.id] = user; return acc; }, {}); } function getIsManualStatusForUserId(state, userId) { return state.entities.users.isManualStatus[userId]; } exports.getProfilesInCurrentChannel = (0, create_selector_1.createSelector)('getProfilesInCurrentChannel', exports.getUsers, exports.getProfileSetInCurrentChannel, (profiles, currentChannelProfileSet) => { return sortAndInjectProfiles(profiles, currentChannelProfileSet); }); exports.getActiveProfilesInCurrentChannel = (0, create_selector_1.createSelector)('getProfilesInCurrentChannel', exports.getUsers, exports.getProfileSetInCurrentChannel, (profiles, currentChannelProfileSet) => { return sortAndInjectProfiles(profiles, currentChannelProfileSet).filter((user) => user.delete_at === 0); }); exports.getActiveProfilesInCurrentChannelWithoutSorting = (0, create_selector_1.createSelector)('getProfilesInCurrentChannel', exports.getUsers, exports.getProfileSetInCurrentChannel, (profiles, currentChannelProfileSet) => { return injectProfiles(profiles, currentChannelProfileSet).filter((user) => user.delete_at === 0); }); exports.getProfilesNotInCurrentChannel = (0, create_selector_1.createSelector)('getProfilesNotInCurrentChannel', exports.getUsers, exports.getProfileSetNotInCurrentChannel, (state, filters) => filters, (profiles, notInCurrentChannelProfileSet, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters), notInCurrentChannelProfileSet); }); exports.getProfilesInCurrentTeam = (0, create_selector_1.createSelector)('getProfilesInCurrentTeam', exports.getUsers, exports.getProfileSetInCurrentTeam, (state, filters) => filters, (profiles, currentTeamProfileSet, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters), currentTeamProfileSet); }); exports.getProfilesInTeam = (0, create_selector_1.createSelector)('getProfilesInTeam', exports.getUsers, getUserIdsInTeams, common_1.getMembersInTeam, (state, teamId) => teamId, (state, teamId, filters) => filters, (profiles, usersInTeams, memberships, teamId, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters, memberships), usersInTeams[teamId] || new Set()); }); exports.getProfilesNotInTeam = (0, create_selector_1.createSelector)('getProfilesNotInTeam', exports.getUsers, getUserIdsNotInTeams, (state, teamId) => teamId, (state, teamId, filters) => filters, (profiles, usersNotInTeams, teamId, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters), usersNotInTeams[teamId] || new Set()); }); exports.getProfilesNotInCurrentTeam = (0, create_selector_1.createSelector)('getProfilesNotInCurrentTeam', exports.getUsers, exports.getProfileSetNotInCurrentTeam, (profiles, notInCurrentTeamProfileSet) => { return sortAndInjectProfiles(profiles, notInCurrentTeamProfileSet); }); function getStatusForUserId(state, userId) { return getUserStatuses(state)[userId]; } function getDndEndTimeForUserId(state, userId) { return state.entities.users.dndEndTimes[userId]; } function getTotalUsersStats(state) { return state.entities.users.stats; } function getFilteredUsersStats(state) { return state.entities.users.filteredStats; } function filterFromProfiles(currentUserId, profiles, skipCurrent = false, filters) { const filteredProfilesMap = filterProfiles((0, user_utils_1.profileListToMap)(profiles), filters); const filteredProfiles = Object.keys(filteredProfilesMap).map((key) => filteredProfilesMap[key]); if (skipCurrent) { removeCurrentUserFromList(filteredProfiles, currentUserId); } return filteredProfiles; } function makeSearchProfilesStartingWithTerm() { return (0, create_selector_1.createSelector)('makeSearchProfilesStartingWithTerm', exports.getUsers, exports.getCurrentUserId, (state, term) => term, (state, term, skipCurrent) => skipCurrent || false, (stateGlobalState, term, skipCurrent, filters) => filters, (users, currentUserId, term, skipCurrent, filters) => { const profiles = (0, user_utils_1.filterProfilesMatchingWithTerm)(Object.values(users), term); return filterFromProfiles(currentUserId, profiles, skipCurrent, filters); }); } function makeSearchProfilesMatchingWithTerm() { return (0, create_selector_1.createSelector)('makeSearchProfilesMatchingWithTerm', exports.getUsers, exports.getCurrentUserId, (state, term) => term, (state, term, skipCurrent) => skipCurrent || false, (stateGlobalState, term, skipCurrent, filters) => filters, (users, currentUserId, term, skipCurrent, filters) => { const profiles = (0, user_utils_1.filterProfilesMatchingWithTerm)(Object.values(users), term); return filterFromProfiles(currentUserId, profiles, skipCurrent, filters); }); } function makeSearchProfilesInChannel() { const doGetProfilesInChannel = makeGetProfilesInChannel(); return (state, channelId, term, skipCurrent = false, filters) => { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)(doGetProfilesInChannel(state, channelId, filters), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; }; } function searchProfilesInCurrentChannel(state, term, skipCurrent = false) { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)((0, exports.getProfilesInCurrentChannel)(state), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function searchActiveProfilesInCurrentChannel(state, term, skipCurrent = false) { return searchProfilesInCurrentChannel(state, term, skipCurrent).filter((user) => user.delete_at === 0); } function searchProfilesNotInCurrentChannel(state, term, skipCurrent = false) { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)((0, exports.getProfilesNotInCurrentChannel)(state), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function searchProfilesInCurrentTeam(state, term, skipCurrent = false, filters) { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)((0, exports.getProfilesInCurrentTeam)(state, filters), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function searchProfilesInTeam(state, teamId, term, skipCurrent = false, filters) { const profiles = (0, user_utils_1.filterProfilesMatchingWithTerm)((0, exports.getProfilesInTeam)(state, teamId, filters), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function searchProfilesNotInCurrentTeam(state, term, skipCurrent = false) { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)((0, exports.getProfilesNotInCurrentTeam)(state), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function removeCurrentUserFromList(profiles, currentUserId) { const index = profiles.findIndex((p) => p.id === currentUserId); if (index >= 0) { profiles.splice(index, 1); } } exports.shouldShowTermsOfService = (0, create_selector_1.createSelector)('shouldShowTermsOfService', general_1.getConfig, exports.getCurrentUser, general_1.getLicense, (config, user, license) => { // Defaults to false if the user is not logged in or the setting doesn't exist const acceptedTermsId = user ? user.terms_of_service_id : ''; const acceptedAt = user ? user.terms_of_service_create_at : 0; const featureEnabled = license.IsLicensed === 'true' && config.EnableCustomTermsOfService === 'true'; const reacceptanceTime = parseInt(config.CustomTermsOfServiceReAcceptancePeriod, 10) * 1000 * 60 * 60 * 24; const timeElapsed = new Date().getTime() - acceptedAt; return Boolean(user && featureEnabled && (config.CustomTermsOfServiceId !== acceptedTermsId || timeElapsed > reacceptanceTime)); }); exports.getUsersInVisibleDMs = (0, create_selector_1.createSelector)('getUsersInVisibleDMs', exports.getUsers, preferences_1.getDirectShowPreferences, (users, preferences) => { const dmUsers = []; preferences.forEach((pref) => { if (pref.value === 'true' && users[pref.name]) { dmUsers.push(users[pref.name]); } }); return dmUsers; }); function makeGetProfilesForReactions() { return (0, create_selector_1.createSelector)('makeGetProfilesForReactions', exports.getUsers, (state, reactions) => reactions, (users, reactions) => { const profiles = []; reactions.forEach((r) => { if (users[r.user_id]) { profiles.push(users[r.user_id]); } }); return profiles; }); } /** * Returns a selector that returns all profiles in a given channel with the given filters applied. * * Note that filters, if provided, must be either a constant or memoized to prevent constant recomputation of the selector. */ function makeGetProfilesInChannel() { return (0, create_selector_1.createSelector)('makeGetProfilesInChannel', exports.getUsers, getUserIdsInChannels, common_1.getMembersInChannel, (state, channelId) => channelId, (state, channelId, filters) => filters, (users, userIds, membersInChannel, channelId, filters = {}) => { const userIdsInChannel = userIds[channelId]; if (!userIdsInChannel) { return []; } return sortAndInjectProfiles(filterProfiles(users, filters, membersInChannel), userIdsInChannel); }); } /** * Returns a selector that returns all profiles not in a given channel. */ function makeGetProfilesNotInChannel() { return (0, create_selector_1.createSelector)('makeGetProfilesNotInChannel', exports.getUsers, getUserIdsNotInChannels, (state, channelId) => channelId, (users, userIds, channelId) => { const userIdsInChannel = userIds[channelId]; if (!userIdsInChannel) { return []; } return sortAndInjectProfiles(users, userIdsInChannel); }); } function makeGetProfilesByIdsAndUsernames() { return (0, create_selector_1.createSelector)('makeGetProfilesByIdsAndUsernames', exports.getUsers, exports.getUsersByUsername, (state, props) => props.allUserIds, (state, props) => props.allUsernames, (allProfilesById, allProfilesByUsername, allUserIds = [], allUsernames = []) => { const userProfiles = []; if (allUserIds && allUserIds.length > 0) { const profilesById = allUserIds. filter((userId) => (0, post_utils_1.secureGetFromRecord)(allProfilesById, userId)). map((userId) => allProfilesById[userId]); if (profilesById && profilesById.length > 0) { userProfiles.push(...profilesById); } } if (allUsernames && allUsernames.length > 0) { const profilesByUsername = allUsernames. filter((username) => (0, post_utils_1.secureGetFromRecord)(allProfilesByUsername, username)). map((username) => allProfilesByUsername[username]); if (profilesByUsername && profilesByUsername.length > 0) { userProfiles.push(...profilesByUsername); } } return userProfiles; }); } function makeGetDisplayName() { return (0, create_selector_1.createSelector)('makeGetDisplayName', (state, userId) => getUser(state, userId), preferences_1.getTeammateNameDisplaySetting, (state, userId, useFallbackUsername = true) => useFallbackUsername, (user, teammateNameDisplaySetting, useFallbackUsername) => { return (0, user_utils_1.displayUsername)(user, teammateNameDisplaySetting, useFallbackUsername); }); } function makeDisplayNameGetter() { return (0, create_selector_1.createSelector)('makeDisplayNameGetter', preferences_1.getTeammateNameDisplaySetting, (_, useFallbackUsername = true) => useFallbackUsername, (teammateNameDisplaySetting, useFallbackUsername) => { return (user) => (0, user_utils_1.displayUsername)(user, teammateNameDisplaySetting, useFallbackUsername); }); } exports.getProfilesInGroup = (0, create_selector_1.createSelector)('getProfilesInGroup', exports.getUsers, getUserIdsInGroups, (state, groupId) => groupId, (state, groupId, filters) => filters, (profiles, usersInGroups, groupId, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters), usersInGroups[groupId] || new Set()); }); exports.getProfilesInGroupWithoutSorting = (0, create_selector_1.createSelector)('getProfilesInGroup', exports.getUsers, getUserIdsInGroups, (state, groupId) => groupId, (state, groupId, filters) => filters, (profiles, usersInGroups, groupId, filters) => { return injectProfiles(filterProfiles(profiles, filters), usersInGroups[groupId] || new Set()); }); exports.getProfilesNotInCurrentGroup = (0, create_selector_1.createSelector)('getProfilesNotInGroup', exports.getUsers, getUserIdsNotInGroups, (state, groupId) => groupId, (state, groupId, filters) => filters, (profiles, usersNotInGroups, groupId, filters) => { return sortAndInjectProfiles(filterProfiles(profiles, filters), usersNotInGroups[groupId] || new Set()); }); function searchProfilesInGroup(state, groupId, term, skipCurrent = false, filters) { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)((0, exports.getProfilesInGroup)(state, groupId, filters), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function searchProfilesInGroupWithoutSorting(state, groupId, term, skipCurrent = false, filters) { const profiles = (0, user_utils_1.filterProfilesStartingWithTerm)((0, exports.getProfilesInGroupWithoutSorting)(state, groupId, filters), term); if (skipCurrent) { removeCurrentUserFromList(profiles, (0, exports.getCurrentUserId)(state)); } return profiles; } function getUserLastActivities(state) { return state.entities.users.lastActivity; } function getLastActivityForUserId(state, userId) { return getUserLastActivities(state)[userId]; } function checkIsFirstAdmin(currentUser, users) { if (!currentUser) { return false; } if (!currentUser.roles.includes('system_admin')) { return false; } for (const user of Object.values(users)) { if (user.roles.includes('system_admin') && user.create_at < currentUser.create_at) { // If the user in the list is an admin with create_at less than our user, than that user is older than the current one, so it can't be the first admin. return false; } } return true; } exports.isFirstAdmin = (0, create_selector_1.createSelector)('isFirstAdmin', (state) => (0, exports.getCurrentUser)(state), (state) => (0, exports.getUsers)(state), checkIsFirstAdmin); exports.displayLastActiveLabel = (0, create_selector_1.createSelector)('displayLastActiveLabel', (state, userId) => getStatusForUserId(state, userId), (state, userId) => getLastActivityForUserId(state, userId), (state, userId) => getUser(state, userId), general_1.getConfig, (userStatus, timestamp, user, config) => { const currentTime = new Date(); const oneMin = 60 * 1000; if ((!userStatus || userStatus === constants_1.General.ONLINE) || (timestamp && (currentTime.valueOf() - new Date(timestamp).valueOf()) <= oneMin) || user?.props?.show_last_active === 'false' || user?.is_bot || timestamp === 0 || config.EnableLastActiveTime !== 'true') { return false; } return true; }); exports.getLastActiveTimestampUnits = (0, create_selector_1.createSelector)('getLastActiveTimestampUnits', (state, userId) => getLastActivityForUserId(state, userId), (timestamp) => { const timestampUnits = [ 'now', 'minute', 'hour', ]; const currentTime = new Date(); const twoDaysAgo = 48 * 60 * 60 * 1000; if ((currentTime.valueOf() - new Date(timestamp).valueOf()) < twoDaysAgo) { timestampUnits.push('day'); } return timestampUnits; });