Skip to content

Commit

Permalink
open existing chats if they exist, fetch all channels to check that
Browse files Browse the repository at this point in the history
  • Loading branch information
mstorus committed Apr 19, 2013
1 parent 6bb07ac commit 8c4b32a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 72 deletions.
73 changes: 38 additions & 35 deletions static/js/channel-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
.factory('channelState', ['$http', '$rootScope', '$q', '$routeParams', 'Socket', 'User', 'Channel', 'Message', 'utils', function ($http, $rootScope, $q, $routeParams, Socket, User, Channel, Message, utils) {
var channel_cache = {};
var channels_queried = false;
var channel_min_id;

var query_messages = function (channel) {
// TODO: enable this to backfill
Expand Down Expand Up @@ -117,47 +116,51 @@
}
};

var query_channels = function (num_to_fetch, fetch_older) {
num_to_fetch = num_to_fetch || 0;
if (channels_queried && !fetch_older) {
var defer = $q.defer();
defer.resolve($rootScope.channel_list);
return defer.promise;
}

var query_channels = function () {
var url = '/adn-proxy/stream/0/channels';
var params = {
count: num_to_fetch,
count: 20,
include_recent_message: 1,
channel_types: 'net.app.core.pm'
};

if (channel_min_id) {
params.before_id = channel_min_id;
}

var url = '/adn-proxy/stream/0/channels';
return $http({
method: 'GET',
url: url,
params: params
}).then(function (response) {
channel_min_id = response.data.meta.min_id;
var fetched_channels = [];

angular.forEach(response.data.data, function (value) {
var channel = new Channel(value, true);
channel_cache[channel.id] = channel;
fetched_channels.push(channel);
var ajax_call = function (before_id) {
if (before_id) {
params.before_id = before_id;
} else {
delete params.before_id;
}
return $http({
method: 'GET',
url: url,
params: params
}).then(function (response) {
var more_channels = response.data.meta.more;
var min_id = response.data.meta.min_id;
var fetched_channels = [];
angular.forEach(response.data.data, function (value) {
var channel = new Channel(value, true);
channel_cache[channel.id] = channel;
fetched_channels.push(channel);
});
User.fetch_pending();
channels_queried = true;
if (more_channels) {
ajax_call(min_id);
}
$rootScope.channel_list = _.values(channel_cache);
return fetched_channels;
});
};

User.fetch_pending();

channels_queried = true;

$rootScope.channel_list = _.values(channel_cache);

return fetched_channels;
});
if (!channels_queried) {
// query all channels so we know which channels the user is in and with whom
return ajax_call();
} else {
var deferred = $q.defer();
deferred.resolve();
return deferred.promise;
}
};

var fetch_muted_channels = function () {
Expand Down
21 changes: 13 additions & 8 deletions static/js/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@

return Channel;
}]).controller('ChannelListCtrl', ['$scope', '$rootScope', '$location', 'Channel', 'Message', 'User', 'channelState', 'utils', function ($scope, $rootScope, $location, Channel, Message, User, channelState, utils) {
$scope.has_more_channels = true;
$scope.channel_fetch_size = 10;
$scope.num_channels_to_show = 10;

var watch_notifications = function () {
$scope.$watch('channel_list', function (newVal, oldVal) {
Expand All @@ -101,7 +101,8 @@
if ($rootScope.selectedNav === 'muted') {
channelState.fetch_muted_channels();
} else {
channelState.query_channels($scope.channel_fetch_size, false).then(function () {
var deferred = channelState.query_channels($scope.channel_fetch_size, false);
deferred.then(function () {
watch_notifications();
});
}
Expand All @@ -110,20 +111,24 @@
$scope.message = new Message();

$scope.loadOlderChannels = function () {
channelState.query_channels($scope.channel_fetch_size, true).then(function (channels) {
if (channels.length < $scope.channel_fetch_size) {
$scope.has_more_channels = false;
}
});
$scope.num_channels_to_show += $scope.channel_fetch_size;
};

$scope.has_more_channels = function () {
return $scope.num_channels_to_show < $rootScope.channel_list.length;
};

$scope.getRoster = function () {
var recent_user_ids = [];
angular.forEach($rootScope.channel_list, function (channel) {
recent_user_ids.push.apply(recent_user_ids, _.map(channel.writers.user_ids, function (elem) {
var user_ids = _.pluck(channel.users, 'id');
recent_user_ids.push.apply(recent_user_ids, _.map(user_ids, function (elem) {
return parseInt(elem, 10);
}));
});
recent_user_ids = _.reject(recent_user_ids, function (user_id) {
return user_id === parseInt($rootScope.user_id);
});
return User.bulk_get(recent_user_ids);
};
}]).controller('ChannelDetailCtrl', ['$scope', '$element', '$timeout', 'channelState', '$routeParams', '$location', function ($scope, $element, $timeout, channelState, $routeParams, $location) {
Expand Down
75 changes: 53 additions & 22 deletions static/js/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,34 +396,65 @@
}
};
}]).directive('resizeHeight', ['$timeout', function ($timeout) {
return function (scope, element) {
var t;
var w = $(window);
var layout_dimensions = {};
var check_dimensions = function () {
var dimensions = {
window_height: w.height(),
window_width: w.width(),
container_offset: element.offset()
};
return {
restrict: 'A',
scope: {
// these scope attributes can only be numbers?? apparently
bottomMargin: "&bottomMargin",
maxHeight: "&maxHeight",
},
link: function (scope, element) {
var t;
var w = $(window);
var layout_dimensions = {};
var check_dimensions = function () {
var dimensions = {
window_height: w.height(),
window_width: w.width(),
container_offset: element.offset()
};

if (!_.isEqual(layout_dimensions, dimensions)) {
layout_dimensions = dimensions;
if (!_.isEqual(layout_dimensions, dimensions)) {
layout_dimensions = dimensions;

// fix dimensions
var min_height = 200;
var new_height = Math.max(min_height, dimensions.window_height - dimensions.container_offset.top - 130);
element.height(new_height);
}
t = $timeout(check_dimensions, 200, false);
};
check_dimensions();
// fix dimensions
var min_height = 200;
var bottomMargin = scope.bottomMargin && scope.bottomMargin() || 0;
var maxHeight = scope.maxHeight && scope.maxHeight() || 9999;
var new_height = Math.max(min_height, dimensions.window_height - dimensions.container_offset.top - bottomMargin);
new_height = Math.min(maxHeight, new_height);
element.height(new_height);
}
t = $timeout(check_dimensions, 200, false);
};
check_dimensions();
}
};
}]).directive('roster', [function () {
}]).directive('roster', ['$location', '$rootScope', function ($location, $rootScope) {
return {
restrict: 'E',
templateUrl: 'roster.html',
replace: true
replace: true,
link: function (scope, element) {
scope.getChatUrl = function (user_id) {
if ($rootScope.user_id === user_id) {
return;
}
// try to find an existing channel with this user
var to_match = [$rootScope.user_id, user_id].sort().join(',');
var existing_channel = _.find($rootScope.channel_list, function (channel) {
var user_ids = _.pluck(channel.users, 'id');
return user_ids.sort().join(',') === to_match;
});

// if found, then change the location to that channel
if (existing_channel) {
return $location.path('/channel/' + existing_channel.id);
} else {
return $location.path('/new-message').search('to', user_id);
}
}
}
};
}]).factory('Message', ['User', '$http', function (User, $http) {
var Message = function (data) {
Expand Down
4 changes: 2 additions & 2 deletions static/scss/_ohe.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ html.omega {
}

.message-list {
overflow-y: scroll;
overflow-y: auto;
html.no-overflowscrolling.touch {
overflow-y: hidden;
}
Expand Down Expand Up @@ -329,7 +329,7 @@ html.omega {
}

.roster {
overflow-y: scroll;
overflow-y: auto;
html.brekapoint-phone & {
overflow-y: visible;
}
Expand Down
4 changes: 2 additions & 2 deletions static/templates/channel-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="row-fluid">
<div class='ul-container yui3-u-1'>
<ul class='unstyled'>
<li ng-repeat="channel in (selectedNav == 'inbox' && channel_list || muted_channel_list) | orderBy:['-has_unread', '-recent_message.created_at']" ng-class="{'last':$last}">
<li ng-repeat="channel in (selectedNav == 'inbox' && channel_list.slice(0, num_channels_to_show) || muted_channel_list) | orderBy:['-has_unread', '-recent_message.created_at']" ng-class="{'last':$last}">
<a href="{{ channel.detail_url() }}">
<div class="row {{channel.has_unread && 'unread'}} channel">
<div class='pull-left img-block'>
Expand All @@ -39,7 +39,7 @@
</div>
</a>
</li>
<li class='show-older-channels ta-center' ng-show="selectedNav == 'inbox' && has_more_channels && channel_list.length >= channel_fetch_size"><a href='#' ng-click="loadOlderChannels()">Show Older <i class='icon-chevron-down'></i></a></li>
<li class='show-older-channels ta-center' ng-show="selectedNav == 'inbox' && has_more_channels() && channel_list.length >= channel_fetch_size"><a href='#' ng-click="loadOlderChannels()">Show Older <i class='icon-chevron-down'></i></a></li>
<div class='no-channels' ng-show="!channel_list || channel_list.length == 0">No messages yet</div>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion static/templates/message-list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="message-list-container" resize-height>
<div class="message-list-container" resize-height bottom-margin="130">
<div class="message-list">
<ul class='unstyled'>
<li ng-show='has_older_messages && channel.messages.length >= message_fetch_size' class='see-more message-wrapper'><a href='#' ng-click='loadOlderMessages()'>&#9650; Load Older Messages</a></li>
Expand Down
4 changes: 2 additions & 2 deletions static/templates/roster.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class='roster' resize-height>
<div class='roster' resize-height bottom-margin="130" max-height="620">
<div class='roster-member' ng-repeat="user in getRoster() | orderBy:['username']">
<img width=25 height=25 alt='roster icon for {{ user.username }}' class='hidden-phone' ng-src="{{ user.avatar_image.url }}?w=25">
<span class='username'><a href='/new-message?to={{user.id}}'><strong>@{{user.username}}</strong></a> <a class='profile-link yui3-u-none m-yui3-u t-yui3-u' alt='go to profile' href='{{alpha_url_base}}/{{user.username}}'><i class='icon-user'></i></a></span>
<span class='username'><a href='#' ng-click='getChatUrl(user.id)'><strong>@{{user.username}}</strong></a> <a class='profile-link yui3-u-none m-yui3-u t-yui3-u' alt='go to profile' href='{{alpha_url_base}}/{{user.username}}'><i class='icon-user'></i></a></span>
</div>
</div>

0 comments on commit 8c4b32a

Please sign in to comment.