Skip to content

Commit

Permalink
Merge pull request #425 from mradionov/hide-reposts-in-stream
Browse files Browse the repository at this point in the history
fix #364: Hide reposts in stream
  • Loading branch information
weblancaster committed Oct 26, 2015
2 parents 10abb6d + 322c20c commit d2402cb
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions app/public/js/stream/streamCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

app.controller('StreamCtrl', function ($scope, SCapiService, $rootScope) {
var endpoint = 'me/activities'
, params = 'limit=33';
, params = 'limit=33'
, tracksIds = [];

$scope.title = 'Stream';
$scope.data = '';
Expand All @@ -11,7 +12,8 @@ app.controller('StreamCtrl', function ($scope, SCapiService, $rootScope) {

SCapiService.get(endpoint, params)
.then(function(data) {
$scope.data = data.collection;
var tracks = filterTracks(data.collection);
$scope.data = tracks;
}, function(error) {
console.log('error', error);
}).finally(function() {
Expand All @@ -35,10 +37,9 @@ app.controller('StreamCtrl', function ($scope, SCapiService, $rootScope) {

SCapiService.getNextPage()
.then(function(data) {
markLikedTracks(data);
for ( var i = 0; i < data.collection.length; i++ ) {
$scope.data.push( data.collection[i] )
}
var tracks = filterTracks(data.collection);
markLikedTracks(tracks);
$scope.data = $scope.data.concat(tracks);
}, function(error) {
console.log('error', error);
}).finally(function(){
Expand All @@ -47,6 +48,18 @@ app.controller('StreamCtrl', function ($scope, SCapiService, $rootScope) {
});
};

function filterTracks(tracks) {
// Filter reposts: display only first appearance of track in stream
var result = tracks.filter(function (track) {
var exists = tracksIds.indexOf(track.origin.id) > -1;
if (!exists) {
tracksIds.push(track.origin.id);
}
return !exists;
});
return result;
}

function markLikedTracks (tracks) {
var tracksData = tracks.collection || tracks;
for (var i = 0; i < tracksData.length; ++i) {
Expand Down

0 comments on commit d2402cb

Please sign in to comment.