diff options
author | Joram Wilander <jwawilander@gmail.com> | 2016-10-19 14:49:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-19 14:49:25 -0400 |
commit | 365b8b465e8a53ebb2da2bf3aef659ac81a2bc6a (patch) | |
tree | 643b2dd52b478c2c0b049ac28798d870b9dfd397 /webapp/stores/user_store.jsx | |
parent | 0512bd26ee85473aa47206d5f207a9a506019138 (diff) | |
download | chat-365b8b465e8a53ebb2da2bf3aef659ac81a2bc6a.tar.gz chat-365b8b465e8a53ebb2da2bf3aef659ac81a2bc6a.tar.bz2 chat-365b8b465e8a53ebb2da2bf3aef659ac81a2bc6a.zip |
Merging performance branch into master (#4268)
* improve performance on sendNotifications
* Fix SQL queries
* Remove get direct profiles, not needed anymore
* Add raw data to error details if AppError fails to decode
* men
* Fix decode (#4052)
* Fixing json decode
* Adding unit test
* Initial work for client scaling (#4051)
* Begin adding paging to profiles API
* Added more paging functionality
* Finish hooking up admin console user lists
* Add API for searching users and add searching to all user lists
* Add lazy loading of profiles
* Revert config.json
* Fix unit tests and some style issues
* Add GetProfilesFromList to Go driver and fix web unit test
* Update etag for GetProfiles
* Updating ui for filters and pagination (#4044)
* Updating UI for pagination
* Adjusting margins for filter row
* Adjusting margin for specific modals
* Adding relative padding to system console
* Adjusting responsive view
* Update client user tests
* Minor fixes for direct messages modal (#4056)
* Remove some unneeded initial load calls (#4057)
* UX updates to user lists, added smart counts and bug fixes (#4059)
* Improved getExplicitMentions and unit tests (#4064)
* Refactor getting posts to lazy load profiles correctly (#4062)
* Comment out SetActiveChannel test (#4066)
* Profiler cpu, block, and memory profiler. (#4081)
* Fix TestSetActiveChannel unit test (#4071)
* Fixing build failure caused by dependancies updating (#4076)
* Adding profiler
* Fix admin_team_member_dropdown eslint errors
* Bumping session cache size (#4077)
* Bumping session cache size
* Bumping status cache
* Refactor how the client handles channel members to be large team friendly (#4106)
* Refactor how the client handles channel members to be large team friendly
* Change Id to ChannelId in ChannelStats model
* Updated getChannelMember and getProfilesByIds routes to match proposal
* Performance improvements (#4100)
* Performance improvements
* Fixing re-connect issue
* Fixing error message
* Some other minor perf tweaks
* Some other minor perf tweaks
* Fixing config file
* Fixing buffer size
* Fixing web socket send message
* adding some error logging
* fix getMe to be user required
* Fix websocket event for new user
* Fixing shutting down
* Reverting web socket changes
* Fixing logging lvl
* Adding caching to GetMember
* Adding some logging
* Fixing caching
* Fixing caching invalidate
* Fixing direct message caching
* Fixing caching
* Fixing caching
* Remove GetDirectProfiles from initial load
* Adding logging and fixing websocket client
* Adding back caching from bad merge.
* Explicitly close go driver requests (#4162)
* Refactored how the client handles team members to be more large team friendly (#4159)
* Refactor getProfilesForDirectMessageList API into getAllProfiles API
* Refactored how the client handles team members to be more large team friendly
* Fix js error when receiving a notification
* Fix JS error caused by current user being overwritten with sanitized version (#4165)
* Adding error message to status failure (#4167)
* Fix a few bugs caused by client scaling refactoring (#4170)
* When there is no read replica, don't open a second set of connections to the master database (#4173)
* Adding connection tacking to stats (#4174)
* Reduce DB writes for statuses and other status related changes (#4175)
* Fix bug preventing opening of DM channels from more modal (#4181)
* Fixing socket timing error (#4183)
* Fixing ping/pong handler
* Fixing socket timing error
* Commenting out status broadcasting
* Removing user status changes
* Removing user status changes
* Removing user status changes
* Removing user status changes
* Adding DoPreComputeJson()
* Performance improvements (#4194)
* * Fix System Console Analytics queries
* Add db.SetConnMaxLifetime to 15 minutes
* Add "net/http/pprof" for profiling
* Add FreeOSMemory() to manually release memory on reload config
* Add flag to enable http profiler
* Fix memory leak (#4197)
* Fix memory leak
* removed unneeded nil assignment
* Fixing go routine leak (#4208)
* Merge fixes
* Merge fix
* Refactored statuses to be queried by the client rather than broadcast by the server (#4212)
* Refactored server code to reduce status broadcasts and to allow getting statuses by IDs
* Refactor client code to periodically fetch statuses
* Add store unit test for getting statuses by ids
* Fix status unit test
* Add getStatusesByIds REST API and move the client over to use that instead of the WebSocket
* Adding multiple threads to websocket hub (#4230)
* Adding multiple threads to websocket hub
* Fixing unit tests
* Fixing so websocket connections from the same user end up in the sameā¦ (#4240)
* Fixing so websocket connections from the same user end up in the same list
* Removing old comment
* Refactor user autocomplete to query the server (#4239)
* Add API for autocompleting users
* Converted at mention autocomplete to query server
* Converted user search autocomplete to query server
* Switch autocomplete API naming to use term instead of username
* Split autocomplete API into two, one for channels and for teams
* Fix copy/paste error
* Some final client scaling fixes (#4246)
* Add lazy loading of profiles to integration pages
* Add lazy loading of profiles to emoji page
* Fix JS error when receiving post in select team menu and also clean up channel store
Diffstat (limited to 'webapp/stores/user_store.jsx')
-rw-r--r-- | webapp/stores/user_store.jsx | 430 |
1 files changed, 362 insertions, 68 deletions
diff --git a/webapp/stores/user_store.jsx b/webapp/stores/user_store.jsx index 859f385c0..d93848670 100644 --- a/webapp/stores/user_store.jsx +++ b/webapp/stores/user_store.jsx @@ -6,12 +6,16 @@ import EventEmitter from 'events'; import * as GlobalActions from 'actions/global_actions.jsx'; import LocalizationStore from './localization_store.jsx'; +import ChannelStore from 'stores/channel_store.jsx'; +import TeamStore from 'stores/team_store.jsx'; import Constants from 'utils/constants.jsx'; const ActionTypes = Constants.ActionTypes; const UserStatuses = Constants.UserStatuses; -const CHANGE_EVENT_DM_LIST = 'change_dm_list'; +const CHANGE_EVENT_NOT_IN_CHANNEL = 'change_not_in_channel'; +const CHANGE_EVENT_IN_CHANNEL = 'change_in_channel'; +const CHANGE_EVENT_IN_TEAM = 'change_in_team'; const CHANGE_EVENT = 'change'; const CHANGE_EVENT_SESSIONS = 'change_sessions'; const CHANGE_EVENT_AUDITS = 'change_audits'; @@ -26,9 +30,26 @@ class UserStoreClass extends EventEmitter { } clear() { - this.profiles_for_dm_list = {}; + // All the profiles, regardless of where they came from this.profiles = {}; - this.direct_profiles = {}; + this.paging_offset = 0; + this.paging_count = 0; + + // Lists of sorted IDs for users in a team + this.profiles_in_team = {}; + this.in_team_offset = 0; + this.in_team_count = 0; + + // Lists of sorted IDs for users in a channel + this.profiles_in_channel = {}; + this.in_channel_offset = {}; + this.in_channel_count = {}; + + // Lists of sorted IDs for users not in a channel + this.profiles_not_in_channel = {}; + this.not_in_channel_offset = {}; + this.not_in_channel_count = {}; + this.statuses = {}; this.sessions = {}; this.audits = {}; @@ -48,16 +69,40 @@ class UserStoreClass extends EventEmitter { this.removeListener(CHANGE_EVENT, callback); } - emitDmListChange() { - this.emit(CHANGE_EVENT_DM_LIST); + emitInTeamChange() { + this.emit(CHANGE_EVENT_IN_TEAM); + } + + addInTeamChangeListener(callback) { + this.on(CHANGE_EVENT_IN_TEAM, callback); } - addDmListChangeListener(callback) { - this.on(CHANGE_EVENT_DM_LIST, callback); + removeInTeamChangeListener(callback) { + this.removeListener(CHANGE_EVENT_IN_TEAM, callback); } - removeDmListChangeListener(callback) { - this.removeListener(CHANGE_EVENT_DM_LIST, callback); + emitInChannelChange() { + this.emit(CHANGE_EVENT_IN_CHANNEL); + } + + addInChannelChangeListener(callback) { + this.on(CHANGE_EVENT_IN_CHANNEL, callback); + } + + removeInChannelChangeListener(callback) { + this.removeListener(CHANGE_EVENT_IN_CHANNEL, callback); + } + + emitNotInChannelChange() { + this.emit(CHANGE_EVENT_NOT_IN_CHANNEL); + } + + addNotInChannelChangeListener(callback) { + this.on(CHANGE_EVENT_NOT_IN_CHANNEL, callback); + } + + removeNotInChannelChangeListener(callback) { + this.removeListener(CHANGE_EVENT_NOT_IN_CHANNEL, callback); } emitSessionsChange() { @@ -96,6 +141,8 @@ class UserStoreClass extends EventEmitter { this.removeListener(CHANGE_EVENT_STATUSES, callback); } + // General + getCurrentUser() { return this.getProfiles()[this.currentUserId]; } @@ -119,29 +166,30 @@ class UserStoreClass extends EventEmitter { return null; } - hasProfile(userId) { - return this.getProfile(userId) != null; - } + // System-Wide Profiles - hasTeamProfile(userId) { - return this.getProfiles()[userId]; + saveProfiles(profiles) { + const currentId = this.getCurrentId(); + if (profiles[currentId]) { + Reflect.deleteProperty(profiles, currentId); + } + this.profiles = Object.assign({}, this.profiles, profiles); } - hasDirectProfile(userId) { - return this.getDirectProfiles()[userId]; + getProfiles() { + return this.profiles; } getProfile(userId) { - if (userId === this.getCurrentId()) { - return this.getCurrentUser(); + if (this.profiles[userId]) { + return Object.assign({}, this.profiles[userId]); } - const user = this.getProfiles()[userId]; - if (user) { - return user; - } + return null; + } - return this.getDirectProfiles()[userId]; + hasProfile(userId) { + return this.getProfile(userId) != null; } getProfileByUsername(username) { @@ -162,22 +210,6 @@ class UserStoreClass extends EventEmitter { return profileUsernameMap; } - getDirectProfiles() { - return this.direct_profiles; - } - - saveDirectProfile(profile) { - this.direct_profiles[profile.id] = profile; - } - - saveDirectProfiles(profiles) { - this.direct_profiles = profiles; - } - - getProfiles() { - return this.profiles; - } - getActiveOnlyProfiles(skipCurrent) { const active = {}; const profiles = this.getProfiles(); @@ -195,14 +227,54 @@ class UserStoreClass extends EventEmitter { getActiveOnlyProfileList() { const profileMap = this.getActiveOnlyProfiles(); const profiles = []; - const currentId = this.getCurrentId(); for (const id in profileMap) { - if (profileMap.hasOwnProperty(id) && id !== currentId) { + if (profileMap.hasOwnProperty(id)) { profiles.push(profileMap[id]); } } + profiles.sort((a, b) => { + if (a.username < b.username) { + return -1; + } + if (a.username > b.username) { + return 1; + } + return 0; + }); + + return profiles; + } + + getProfileList(skipCurrent) { + const profiles = []; + const currentId = this.getCurrentId(); + + for (const id in this.profiles) { + if (this.profiles.hasOwnProperty(id)) { + var profile = this.profiles[id]; + + if (skipCurrent && id === currentId) { + continue; + } + + if (profile.delete_at === 0) { + profiles.push(profile); + } + } + } + + profiles.sort((a, b) => { + if (a.username < b.username) { + return -1; + } + if (a.username > b.username) { + return 1; + } + return 0; + }); + return profiles; } @@ -210,44 +282,194 @@ class UserStoreClass extends EventEmitter { this.profiles[profile.id] = profile; } - saveProfiles(profiles) { + // Team-Wide Profiles + + saveProfilesInTeam(teamId, profiles) { + const oldProfileList = this.profiles_in_team[teamId] || []; + const oldProfileMap = {}; + for (let i = 0; i < oldProfileList.length; i++) { + oldProfileMap[oldProfileList[i]] = this.getProfile(oldProfileList[i]); + } + + const newProfileMap = Object.assign({}, oldProfileMap, profiles); + const newProfileList = Object.keys(newProfileMap); + + newProfileList.sort((a, b) => { + const aProfile = newProfileMap[a]; + const bProfile = newProfileMap[b]; + + if (aProfile.username < bProfile.username) { + return -1; + } + if (aProfile.username > bProfile.username) { + return 1; + } + return 0; + }); + + this.profiles_in_team[teamId] = newProfileList; + this.saveProfiles(profiles); + } + + getProfileListInTeam(teamId = TeamStore.getCurrentId(), skipCurrent) { + const userIds = this.profiles_in_team[teamId] || []; + const profiles = []; const currentId = this.getCurrentId(); - const currentUser = this.profiles[currentId]; - if (currentUser) { - if (currentId in this.profiles) { - Reflect.deleteProperty(this.profiles, currentId); + + for (let i = 0; i < userIds.length; i++) { + const profile = this.getProfile(userIds[i]); + + if (skipCurrent && profile.id === currentId) { + continue; } - this.profiles = profiles; - this.profiles[currentId] = currentUser; - } else { - this.profiles = profiles; + if (profile) { + profiles.push(profile); + } } + + return profiles; } - getProfilesForDmList() { - const currentId = this.getCurrentId(); - const profiles = []; + // Channel-Wide Profiles - for (const id in this.profiles_for_dm_list) { - if (this.profiles_for_dm_list.hasOwnProperty(id) && id !== currentId) { - var profile = this.profiles_for_dm_list[id]; + saveProfilesInChannel(channelId = ChannelStore.getCurrentId(), profiles) { + const oldProfileList = this.profiles_in_channel[channelId] || []; + const oldProfileMap = {}; + for (let i = 0; i < oldProfileList.length; i++) { + oldProfileMap[oldProfileList[i]] = this.getProfile(oldProfileList[i]); + } - if (profile.delete_at === 0) { - profiles.push(profile); - } + const newProfileMap = Object.assign({}, oldProfileMap, profiles); + const newProfileList = Object.keys(newProfileMap); + + newProfileList.sort((a, b) => { + const aProfile = newProfileMap[a]; + const bProfile = newProfileMap[b]; + + if (aProfile.username < bProfile.username) { + return -1; } + if (aProfile.username > bProfile.username) { + return 1; + } + return 0; + }); + + this.profiles_in_channel[channelId] = newProfileList; + this.saveProfiles(profiles); + } + + saveProfileInChannel(channelId = ChannelStore.getCurrentId(), profile) { + const profileMap = {}; + profileMap[profile.id] = profile; + this.saveProfilesInChannel(channelId, profileMap); + } + + saveUserIdInChannel(channelId = ChannelStore.getCurrentId(), userId) { + const profile = this.getProfile(userId); + + // Must have profile or we can't sort the list + if (!profile) { + return false; + } + + this.saveProfileInChannel(channelId, profile); + + return true; + } + + removeProfileInChannel(channelId, userId) { + const userIds = this.profiles_in_channel[channelId]; + if (!userIds) { + return; } - profiles.sort((a, b) => a.username.localeCompare(b.username)); + const index = userIds.indexOf(userId); + if (index === -1) { + return; + } + + userIds.splice(index, 1); + } + + getProfileListInChannel(channelId = ChannelStore.getCurrentId()) { + const userIds = this.profiles_in_channel[channelId] || []; + const profiles = []; + + for (let i = 0; i < userIds.length; i++) { + const profile = this.getProfile(userIds[i]); + if (profile) { + profiles.push(profile); + } + } return profiles; } - saveProfilesForDmList(profiles) { - this.profiles_for_dm_list = profiles; + saveProfilesNotInChannel(channelId = ChannelStore.getCurrentId(), profiles) { + const oldProfileList = this.profiles_not_in_channel[channelId] || []; + const oldProfileMap = {}; + for (let i = 0; i < oldProfileList.length; i++) { + oldProfileMap[oldProfileList[i]] = this.getProfile(oldProfileList[i]); + } + + const newProfileMap = Object.assign({}, oldProfileMap, profiles); + const newProfileList = Object.keys(newProfileMap); + + newProfileList.sort((a, b) => { + const aProfile = newProfileMap[a]; + const bProfile = newProfileMap[b]; + + if (aProfile.username < bProfile.username) { + return -1; + } + if (aProfile.username > bProfile.username) { + return 1; + } + return 0; + }); + + this.profiles_not_in_channel[channelId] = newProfileList; + this.saveProfiles(profiles); + } + + saveProfileNotInChannel(channelId = ChannelStore.getCurrentId(), profile) { + const profileMap = {}; + profileMap[profile.id] = profile; + this.saveProfilesNotInChannel(channelId, profileMap); + } + + removeProfileNotInChannel(channelId, userId) { + const userIds = this.profiles_not_in_channel[channelId]; + if (!userIds) { + return; + } + + const index = userIds.indexOf(userId); + if (index === -1) { + return; + } + + userIds.splice(index, 1); + } + + getProfileListNotInChannel(channelId = ChannelStore.getCurrentId()) { + const userIds = this.profiles_not_in_channel[channelId] || []; + const profiles = []; + + for (let i = 0; i < userIds.length; i++) { + const profile = this.getProfile(userIds[i]); + if (profile) { + profiles.push(profile); + } + } + + return profiles; } + // Other + setSessions(sessions) { this.sessions = sessions; } @@ -331,6 +553,58 @@ class UserStoreClass extends EventEmitter { return false; } + + setPage(offset, count) { + this.paging_offset = offset + count; + this.paging_count = this.paging_count + count; + } + + getPagingOffset() { + return this.paging_offset; + } + + getPagingCount() { + return this.paging_count; + } + + setInTeamPage(offset, count) { + this.in_team_offset = offset + count; + this.in_team_count = this.in_team_count + count; + } + + getInTeamPagingOffset() { + return this.in_team_offset; + } + + getInTeamPagingCount() { + return this.in_team_count; + } + + setInChannelPage(channelId, offset, count) { + this.in_channel_offset[channelId] = offset + count; + this.in_channel_count[channelId] = this.dm_paging_count + count; + } + + getInChannelPagingOffset(channelId) { + return this.in_channel_offset[channelId] | 0; + } + + getInChannelPagingCount(channelId) { + return this.in_channel_count[channelId] | 0; + } + + setNotInChannelPage(channelId, offset, count) { + this.not_in_channel_offset[channelId] = offset + count; + this.not_in_channel_count[channelId] = this.dm_paging_count + count; + } + + getNotInChannelPagingOffset(channelId) { + return this.not_in_channel_offset[channelId] | 0; + } + + getNotInChannelPagingCount(channelId) { + return this.not_in_channel_count[channelId] | 0; + } } var UserStore = new UserStoreClass(); @@ -340,16 +614,36 @@ UserStore.dispatchToken = AppDispatcher.register((payload) => { var action = payload.action; switch (action.type) { - case ActionTypes.RECEIVED_PROFILES_FOR_DM_LIST: - UserStore.saveProfilesForDmList(action.profiles); - UserStore.emitDmListChange(); - break; case ActionTypes.RECEIVED_PROFILES: UserStore.saveProfiles(action.profiles); + if (action.offset != null && action.count != null) { + UserStore.setPage(action.offset, action.count); + } UserStore.emitChange(); break; - case ActionTypes.RECEIVED_DIRECT_PROFILES: - UserStore.saveDirectProfiles(action.profiles); + case ActionTypes.RECEIVED_PROFILES_IN_TEAM: + UserStore.saveProfilesInTeam(action.team_id, action.profiles); + if (action.offset != null && action.count != null) { + UserStore.setInTeamPage(action.offset, action.count); + } + UserStore.emitInTeamChange(); + break; + case ActionTypes.RECEIVED_PROFILES_IN_CHANNEL: + UserStore.saveProfilesInChannel(action.channel_id, action.profiles); + if (action.offset != null && action.count != null) { + UserStore.setInChannelPage(action.offset, action.count); + } + UserStore.emitInChannelChange(); + break; + case ActionTypes.RECEIVED_PROFILES_NOT_IN_CHANNEL: + UserStore.saveProfilesNotInChannel(action.channel_id, action.profiles); + if (action.offset != null && action.count != null) { + UserStore.setNotInChannelPage(action.offset, action.count); + } + UserStore.emitNotInChannelChange(); + break; + case ActionTypes.RECEIVED_PROFILE: + UserStore.saveProfile(action.profile); UserStore.emitChange(); break; case ActionTypes.RECEIVED_ME: |