diff options
Diffstat (limited to 'web/react/utils')
-rw-r--r-- | web/react/utils/async_client.jsx | 266 | ||||
-rw-r--r-- | web/react/utils/client.jsx | 52 | ||||
-rw-r--r-- | web/react/utils/constants.jsx | 5 | ||||
-rw-r--r-- | web/react/utils/utils.jsx | 23 |
4 files changed, 253 insertions, 93 deletions
diff --git a/web/react/utils/async_client.jsx b/web/react/utils/async_client.jsx index f35b0f6cc..0b87bbd7b 100644 --- a/web/react/utils/async_client.jsx +++ b/web/react/utils/async_client.jsx @@ -4,6 +4,7 @@ var client = require('./client.jsx'); var AppDispatcher = require('../dispatcher/app_dispatcher.jsx'); var ChannelStore = require('../stores/channel_store.jsx'); +var ConfigStore = require('../stores/config_store.jsx'); var PostStore = require('../stores/post_store.jsx'); var UserStore = require('../stores/user_store.jsx'); var utils = require('./utils.jsx'); @@ -14,100 +15,171 @@ var ActionTypes = Constants.ActionTypes; // Used to track in progress async calls var callTracker = {}; -var dispatchError = function(err, method) { +function dispatchError(err, method) { AppDispatcher.handleServerAction({ type: ActionTypes.RECIEVED_ERROR, err: err, method: method }); -}; +} +module.exports.dispatchError = dispatchError; -var isCallInProgress = function(callName) { - if (!(callName in callTracker)) return false; +function isCallInProgress(callName) { + if (!(callName in callTracker)) { + return false; + } - if (callTracker[callName] === 0) return false; + if (callTracker[callName] === 0) { + return false; + } if (utils.getTimestamp() - callTracker[callName] > 5000) { - console.log("AsyncClient call " + callName + " expired after more than 5 seconds"); + console.log('AsyncClient call ' + callName + ' expired after more than 5 seconds'); return false; } return true; -}; +} -module.exports.dispatchError = dispatchError; +function getChannels(force, updateLastViewed, checkVersion) { + var channels = ChannelStore.getAll(); -module.exports.getChannels = function(force, updateLastViewed, checkVersion) { - if (isCallInProgress("getChannels")) return; + if (channels.length === 0 || force) { + if (isCallInProgress('getChannels')) { + return; + } + + callTracker.getChannels = utils.getTimestamp(); - if (ChannelStore.getAll().length == 0 || force) { - callTracker["getChannels"] = utils.getTimestamp(); client.getChannels( function(data, textStatus, xhr) { - callTracker["getChannels"] = 0; - - if (updateLastViewed && ChannelStore.getCurrentId() != null) { - module.exports.updateLastViewedAt(); - } + callTracker.getChannels = 0; if (checkVersion) { - var serverVersion = xhr.getResponseHeader("X-Version-ID"); + var serverVersion = xhr.getResponseHeader('X-Version-ID'); if (!UserStore.getLastVersion()) { UserStore.setLastVersion(serverVersion); } - if (serverVersion != UserStore.getLastVersion()) { + if (serverVersion !== UserStore.getLastVersion()) { UserStore.setLastVersion(serverVersion); window.location.href = window.location.href; - console.log("Detected version update refreshing the page"); + console.log('Detected version update refreshing the page'); } } - if (xhr.status === 304 || !data) return; + if (xhr.status === 304 || !data) { + return; + } AppDispatcher.handleServerAction({ type: ActionTypes.RECIEVED_CHANNELS, channels: data.channels, members: data.members }); + }, + function(err) { + callTracker.getChannels = 0; + dispatchError(err, 'getChannels'); + } + ); + } else { + if (isCallInProgress('getChannelCounts')) { + return; + } + + callTracker.getChannelCounts = utils.getTimestamp(); + + client.getChannelCounts( + function(data, textStatus, xhr) { + callTracker.getChannelCounts = 0; + if (xhr.status === 304 || !data) { + return; + } + + var countMap = data.counts; + var updateAtMap = data.update_times; + + for (var id in countMap) { + var c = ChannelStore.get(id); + var count = countMap[id]; + var updateAt = updateAtMap[id]; + if (!c || c.total_msg_count !== count || updateAt > c.update_at) { + getChannel(id); + } + } }, function(err) { - callTracker["getChannels"] = 0; - dispatchError(err, "getChannels"); + callTracker.getChannelCounts = 0; + dispatchError(err, 'getChannelCounts'); } ); } + + if (updateLastViewed && ChannelStore.getCurrentId() != null) { + module.exports.updateLastViewedAt(); + } } +module.exports.getChannels = getChannels; + +function getChannel(id) { + if (isCallInProgress('getChannel' + id)) { + return; + } + + callTracker['getChannel' + id] = utils.getTimestamp(); + + client.getChannel(id, + function(data, textStatus, xhr) { + callTracker['getChannel' + id] = 0; + + if (xhr.status === 304 || !data) { + return; + } + + AppDispatcher.handleServerAction({ + type: ActionTypes.RECIEVED_CHANNEL, + channel: data.channel, + member: data.member + }); + }, + function(err) { + callTracker['getChannel' + id] = 0; + dispatchError(err, 'getChannel'); + } + ); +} +module.exports.getChannel = getChannel; module.exports.updateLastViewedAt = function() { - if (isCallInProgress("updateLastViewed")) return; + if (isCallInProgress('updateLastViewed')) return; if (ChannelStore.getCurrentId() == null) return; - callTracker["updateLastViewed"] = utils.getTimestamp(); + callTracker['updateLastViewed'] = utils.getTimestamp(); client.updateLastViewedAt( ChannelStore.getCurrentId(), function(data) { - callTracker["updateLastViewed"] = 0; + callTracker['updateLastViewed'] = 0; }, function(err) { - callTracker["updateLastViewed"] = 0; - dispatchError(err, "updateLastViewedAt"); + callTracker['updateLastViewed'] = 0; + dispatchError(err, 'updateLastViewedAt'); } ); } module.exports.getMoreChannels = function(force) { - if (isCallInProgress("getMoreChannels")) return; + if (isCallInProgress('getMoreChannels')) return; if (ChannelStore.getMoreAll().loading || force) { - callTracker["getMoreChannels"] = utils.getTimestamp(); + callTracker['getMoreChannels'] = utils.getTimestamp(); client.getMoreChannels( function(data, textStatus, xhr) { - callTracker["getMoreChannels"] = 0; + callTracker['getMoreChannels'] = 0; if (xhr.status === 304 || !data) return; @@ -118,8 +190,8 @@ module.exports.getMoreChannels = function(force) { }); }, function(err) { - callTracker["getMoreChannels"] = 0; - dispatchError(err, "getMoreChannels"); + callTracker['getMoreChannels'] = 0; + dispatchError(err, 'getMoreChannels'); } ); } @@ -129,15 +201,15 @@ module.exports.getChannelExtraInfo = function(force) { var channelId = ChannelStore.getCurrentId(); if (channelId != null) { - if (isCallInProgress("getChannelExtraInfo_"+channelId)) return; + if (isCallInProgress('getChannelExtraInfo_'+channelId)) return; var minMembers = ChannelStore.getCurrent() && ChannelStore.getCurrent().type === 'D' ? 1 : 0; if (ChannelStore.getCurrentExtraInfo().members.length <= minMembers || force) { - callTracker["getChannelExtraInfo_"+channelId] = utils.getTimestamp(); + callTracker['getChannelExtraInfo_'+channelId] = utils.getTimestamp(); client.getChannelExtraInfo( channelId, function(data, textStatus, xhr) { - callTracker["getChannelExtraInfo_"+channelId] = 0; + callTracker['getChannelExtraInfo_'+channelId] = 0; if (xhr.status === 304 || !data) return; @@ -147,8 +219,8 @@ module.exports.getChannelExtraInfo = function(force) { }); }, function(err) { - callTracker["getChannelExtraInfo_"+channelId] = 0; - dispatchError(err, "getChannelExtraInfo"); + callTracker['getChannelExtraInfo_'+channelId] = 0; + dispatchError(err, 'getChannelExtraInfo'); } ); } @@ -156,12 +228,12 @@ module.exports.getChannelExtraInfo = function(force) { } module.exports.getProfiles = function() { - if (isCallInProgress("getProfiles")) return; + if (isCallInProgress('getProfiles')) return; - callTracker["getProfiles"] = utils.getTimestamp(); + callTracker['getProfiles'] = utils.getTimestamp(); client.getProfiles( function(data, textStatus, xhr) { - callTracker["getProfiles"] = 0; + callTracker['getProfiles'] = 0; if (xhr.status === 304 || !data) return; @@ -171,20 +243,20 @@ module.exports.getProfiles = function() { }); }, function(err) { - callTracker["getProfiles"] = 0; - dispatchError(err, "getProfiles"); + callTracker['getProfiles'] = 0; + dispatchError(err, 'getProfiles'); } ); } module.exports.getSessions = function() { - if (isCallInProgress("getSessions")) return; + if (isCallInProgress('getSessions')) return; - callTracker["getSessions"] = utils.getTimestamp(); + callTracker['getSessions'] = utils.getTimestamp(); client.getSessions( UserStore.getCurrentId(), function(data, textStatus, xhr) { - callTracker["getSessions"] = 0; + callTracker['getSessions'] = 0; if (xhr.status === 304 || !data) return; @@ -194,20 +266,20 @@ module.exports.getSessions = function() { }); }, function(err) { - callTracker["getSessions"] = 0; - dispatchError(err, "getSessions"); + callTracker['getSessions'] = 0; + dispatchError(err, 'getSessions'); } ); } module.exports.getAudits = function() { - if (isCallInProgress("getAudits")) return; + if (isCallInProgress('getAudits')) return; - callTracker["getAudits"] = utils.getTimestamp(); + callTracker['getAudits'] = utils.getTimestamp(); client.getAudits( UserStore.getCurrentId(), function(data, textStatus, xhr) { - callTracker["getAudits"] = 0; + callTracker['getAudits'] = 0; if (xhr.status === 304 || !data) return; @@ -217,22 +289,22 @@ module.exports.getAudits = function() { }); }, function(err) { - callTracker["getAudits"] = 0; - dispatchError(err, "getAudits"); + callTracker['getAudits'] = 0; + dispatchError(err, 'getAudits'); } ); } module.exports.findTeams = function(email) { - if (isCallInProgress("findTeams_"+email)) return; + if (isCallInProgress('findTeams_'+email)) return; var user = UserStore.getCurrentUser(); if (user) { - callTracker["findTeams_"+email] = utils.getTimestamp(); + callTracker['findTeams_'+email] = utils.getTimestamp(); client.findTeams( user.email, function(data, textStatus, xhr) { - callTracker["findTeams_"+email] = 0; + callTracker['findTeams_'+email] = 0; if (xhr.status === 304 || !data) return; @@ -242,21 +314,21 @@ module.exports.findTeams = function(email) { }); }, function(err) { - callTracker["findTeams_"+email] = 0; - dispatchError(err, "findTeams"); + callTracker['findTeams_'+email] = 0; + dispatchError(err, 'findTeams'); } ); } } module.exports.search = function(terms) { - if (isCallInProgress("search_"+String(terms))) return; + if (isCallInProgress('search_'+String(terms))) return; - callTracker["search_"+String(terms)] = utils.getTimestamp(); + callTracker['search_'+String(terms)] = utils.getTimestamp(); client.search( terms, function(data, textStatus, xhr) { - callTracker["search_"+String(terms)] = 0; + callTracker['search_'+String(terms)] = 0; if (xhr.status === 304 || !data) return; @@ -266,8 +338,8 @@ module.exports.search = function(terms) { }); }, function(err) { - callTracker["search_"+String(terms)] = 0; - dispatchError(err, "search"); + callTracker['search_'+String(terms)] = 0; + dispatchError(err, 'search'); } ); } @@ -276,7 +348,7 @@ module.exports.getPosts = function(force, id, maxPosts) { if (PostStore.getCurrentPosts() == null || force) { var channelId = id ? id : ChannelStore.getCurrentId(); - if (isCallInProgress("getPosts_"+channelId)) return; + if (isCallInProgress('getPosts_'+channelId)) return; var post_list = PostStore.getCurrentPosts(); @@ -291,7 +363,7 @@ module.exports.getPosts = function(force, id, maxPosts) { } if (channelId != null) { - callTracker["getPosts_"+channelId] = utils.getTimestamp(); + callTracker['getPosts_'+channelId] = utils.getTimestamp(); client.getPosts( channelId, 0, @@ -308,23 +380,25 @@ module.exports.getPosts = function(force, id, maxPosts) { module.exports.getProfiles(); }, function(err) { - dispatchError(err, "getPosts"); + dispatchError(err, 'getPosts'); }, function() { - callTracker["getPosts_"+channelId] = 0; + callTracker['getPosts_'+channelId] = 0; } ); } } } -module.exports.getMe = function() { - if (isCallInProgress("getMe")) return; +function getMe() { + if (isCallInProgress('getMe')) { + return; + } - callTracker["getMe"] = utils.getTimestamp(); + callTracker.getMe = utils.getTimestamp(); client.getMe( function(data, textStatus, xhr) { - callTracker["getMe"] = 0; + callTracker.getMe = 0; if (xhr.status === 304 || !data) return; @@ -334,19 +408,20 @@ module.exports.getMe = function() { }); }, function(err) { - callTracker["getMe"] = 0; - dispatchError(err, "getMe"); + callTracker.getMe = 0; + dispatchError(err, 'getMe'); } ); } +module.exports.getMe = getMe; module.exports.getStatuses = function() { - if (isCallInProgress("getStatuses")) return; + if (isCallInProgress('getStatuses')) return; - callTracker["getStatuses"] = utils.getTimestamp(); + callTracker['getStatuses'] = utils.getTimestamp(); client.getStatuses( function(data, textStatus, xhr) { - callTracker["getStatuses"] = 0; + callTracker['getStatuses'] = 0; if (xhr.status === 304 || !data) return; @@ -356,19 +431,19 @@ module.exports.getStatuses = function() { }); }, function(err) { - callTracker["getStatuses"] = 0; - dispatchError(err, "getStatuses"); + callTracker['getStatuses'] = 0; + dispatchError(err, 'getStatuses'); } ); } module.exports.getMyTeam = function() { - if (isCallInProgress("getMyTeam")) return; + if (isCallInProgress('getMyTeam')) return; - callTracker["getMyTeam"] = utils.getTimestamp(); + callTracker['getMyTeam'] = utils.getTimestamp(); client.getMyTeam( function(data, textStatus, xhr) { - callTracker["getMyTeam"] = 0; + callTracker['getMyTeam'] = 0; if (xhr.status === 304 || !data) return; @@ -378,8 +453,33 @@ module.exports.getMyTeam = function() { }); }, function(err) { - callTracker["getMyTeam"] = 0; - dispatchError(err, "getMyTeam"); + callTracker['getMyTeam'] = 0; + dispatchError(err, 'getMyTeam'); + } + ); +} + +function getConfig() { + if (isCallInProgress('getConfig')) { + return; + } + + callTracker['getConfig'] = utils.getTimestamp(); + client.getConfig( + function(data, textStatus, xhr) { + callTracker['getConfig'] = 0; + + if (data && xhr.status !== 304) { + AppDispatcher.handleServerAction({ + type: ActionTypes.RECIEVED_CONFIG, + settings: data + }); + } + }, + function(err) { + callTracker['getConfig'] = 0; + dispatchError(err, 'getConfig'); } ); } +module.exports.getConfig = getConfig; diff --git a/web/react/utils/client.jsx b/web/react/utils/client.jsx index 6a1f7c820..5aab80d01 100644 --- a/web/react/utils/client.jsx +++ b/web/react/utils/client.jsx @@ -540,18 +540,34 @@ module.exports.updateLastViewedAt = function(channelId, success, error) { }); }; -module.exports.getChannels = function(success, error) { +function getChannels(success, error) { $.ajax({ - url: "/api/v1/channels/", + url: '/api/v1/channels/', dataType: 'json', type: 'GET', success: success, ifModified: true, error: function(xhr, status, err) { - e = handleError("getChannels", xhr, status, err); + var e = handleError('getChannels', xhr, status, err); error(e); } }); +} +module.exports.getChannels = getChannels; + +module.exports.getChannel = function(id, success, error) { + $.ajax({ + url: "/api/v1/channels/" + id + "/", + dataType: 'json', + type: 'GET', + success: success, + error: function(xhr, status, err) { + e = handleError("getChannel", xhr, status, err); + error(e); + } + }); + + module.exports.track('api', 'api_channel_get'); }; module.exports.getMoreChannels = function(success, error) { @@ -568,6 +584,21 @@ module.exports.getMoreChannels = function(success, error) { }); }; +function getChannelCounts(success, error) { + $.ajax({ + url: '/api/v1/channels/counts', + dataType: 'json', + type: 'GET', + success: success, + ifModified: true, + error: function(xhr, status, err) { + var e = handleError('getChannelCounts', xhr, status, err); + error(e); + } + }); +} +module.exports.getChannelCounts = getChannelCounts; + module.exports.getChannelExtraInfo = function(id, success, error) { $.ajax({ url: "/api/v1/channels/" + id + "/extra_info", @@ -849,3 +880,18 @@ module.exports.updateValetFeature = function(data, success, error) { module.exports.track('api', 'api_teams_update_valet_feature'); }; + +function getConfig(success, error) { + $.ajax({ + url: '/api/v1/config/get_all', + dataType: 'json', + type: 'GET', + ifModified: true, + success: success, + error: function(xhr, status, err) { + var e = handleError('getConfig', xhr, status, err); + error(e); + } + }); +}; +module.exports.getConfig = getConfig; diff --git a/web/react/utils/constants.jsx b/web/react/utils/constants.jsx index bed0ec556..508de9185 100644 --- a/web/react/utils/constants.jsx +++ b/web/react/utils/constants.jsx @@ -10,6 +10,7 @@ module.exports = { CLICK_CHANNEL: null, CREATE_CHANNEL: null, RECIEVED_CHANNELS: null, + RECIEVED_CHANNEL: null, RECIEVED_MORE_CHANNELS: null, RECIEVED_CHANNEL_EXTRA_INFO: null, @@ -30,6 +31,8 @@ module.exports = { CLICK_TEAM: null, RECIEVED_TEAM: null, + + RECIEVED_CONFIG: null }), PayloadSources: keyMirror({ @@ -38,7 +41,7 @@ module.exports = { }), SPECIAL_MENTIONS: ['all', 'channel'], CHARACTER_LIMIT: 4000, - IMAGE_TYPES: ['jpg', 'gif', 'bmp', 'png'], + IMAGE_TYPES: ['jpg', 'gif', 'bmp', 'png', 'jpeg'], AUDIO_TYPES: ['mp3', 'wav', 'wma', 'm4a', 'flac', 'aac'], VIDEO_TYPES: ['mp4', 'avi', 'webm', 'mkv', 'wmv', 'mpg', 'mov', 'flv'], SPREADSHEET_TYPES: ['ppt', 'pptx', 'csv'], diff --git a/web/react/utils/utils.jsx b/web/react/utils/utils.jsx index 2214b6239..7591c138f 100644 --- a/web/react/utils/utils.jsx +++ b/web/react/utils/utils.jsx @@ -732,20 +732,19 @@ module.exports.isValidUsername = function (name) { return error; } -module.exports.switchChannel = function(channel, teammate_name) { +function switchChannel(channel, teammateName) { AppDispatcher.handleViewAction({ type: ActionTypes.CLICK_CHANNEL, name: channel.name, id: channel.id }); - var teamURL = window.location.href.split('/channels')[0]; - history.replaceState('data', '', teamURL + '/channels/' + channel.name); + updateAddressBar(channel.name); - if (channel.type === 'D' && teammate_name) { - document.title = teammate_name + " " + document.title.substring(document.title.lastIndexOf("-")); + if (channel.type === 'D' && teammateName) { + updateTabTitle(teammateName); } else { - document.title = channel.display_name + " " + document.title.substring(document.title.lastIndexOf("-")); + updateTabTitle(channel.display_name); } AsyncClient.getChannels(true, true, true); @@ -759,6 +758,18 @@ module.exports.switchChannel = function(channel, teammate_name) { return false; } +module.exports.switchChannel = switchChannel; + +function updateTabTitle(name) { + document.title = name + ' ' + document.title.substring(document.title.lastIndexOf('-')); +} +module.exports.updateTabTitle = updateTabTitle; + +function updateAddressBar(channelName) { + var teamURL = window.location.href.split('/channels')[0]; + history.replaceState('data', '', teamURL + '/channels/' + channelName); +} +module.exports.updateAddressBar = updateAddressBar; module.exports.isMobile = function() { return screen.width <= 768; |