diff --git a/Changelog.md b/Changelog.md index a4a9d2e2d..88c4a9603 100644 --- a/Changelog.md +++ b/Changelog.md @@ -34,6 +34,7 @@ * Add rake task to send a mail to all users [#5111](https://github.com/diaspora/diaspora/pull/5111) * Expose which services are configured in /statistics.json [#5121](https://github.com/diaspora/diaspora/pull/5121) * In filtered notification views, replace "Mark all as read" with "Mark shown as read" [#5122](https://github.com/diaspora/diaspora/pull/5122) +* When ignoring a user remove his posts from the stream instantly [#5127](https://github.com/diaspora/diaspora/pull/5127) # 0.4.0.1 diff --git a/app/assets/javascripts/app/app.js b/app/assets/javascripts/app/app.js index 0d3af147f..efb3232a8 100644 --- a/app/assets/javascripts/app/app.js +++ b/app/assets/javascripts/app/app.js @@ -20,6 +20,16 @@ var app = { pages: {}, forms: {}, + // global event broker - use event names in the form of "object:action:data" + // [object]: the class of the acting object + // [action]: infinitive verb naming the performed action + // [data]: (optional) unique name or ID of the specific instance + // e.g. "person:ignore:123" + // if your event has to pass more than one datum (singular) - or in case you + // need structured data - specify them as arguments to the `#trigger` call + // e.g. `app.events.trigger('example:event', {more: 'data'})` + events: _.extend({}, Backbone.Events), + user: function(userAttrs) { if(userAttrs) { return this._user = new app.models.User(userAttrs) } return this._user || false diff --git a/app/assets/javascripts/app/models/post.js b/app/assets/javascripts/app/models/post.js index 51623c5ae..74a3cec90 100644 --- a/app/assets/javascripts/app/models/post.js +++ b/app/assets/javascripts/app/models/post.js @@ -27,6 +27,14 @@ app.models.Post = Backbone.Model.extend(_.extend({}, app.models.formatDateMixin, return this.get("author") }, + blockAuthor: function() { + var personId = this.get("author").id; + var block = new app.models.Block(); + + return block.save({block : {person_id : personId}}) + .done(function(){ app.events.trigger('person:block:'+personId); }); + }, + toggleFavorite : function(options){ this.set({favorite : !this.get("favorite")}) diff --git a/app/assets/javascripts/app/views/stream_post_views.js b/app/assets/javascripts/app/views/stream_post_views.js index ef557362d..6993bd79e 100644 --- a/app/assets/javascripts/app/views/stream_post_views.js +++ b/app/assets/javascripts/app/views/stream_post_views.js @@ -27,6 +27,9 @@ app.views.StreamPost = app.views.Post.extend({ tooltipSelector : ".timeago, .post_scope, .block_user, .delete", initialize : function(){ + var personId = this.model.get('author').id; + app.events.on('person:block:'+personId, this.remove, this); + this.model.on('remove', this.remove, this); //subviews this.commentStreamView = new app.views.CommentStream({model : this.model}); @@ -72,20 +75,13 @@ app.views.StreamPost = app.views.Post.extend({ if(evt) { evt.preventDefault(); } if(!confirm(Diaspora.I18n.t('ignore_user'))) { return } - var personId = this.model.get("author").id; - var block = new app.models.Block(); - - block.save({block : {person_id : personId}}, { - success : function(){ - if(!app.stream) { return } - - _.each(app.stream.posts.models, function(model){ - if(model.get("author").id == personId) { - app.stream.posts.remove(model); - } - }) - } - }) + this.model.blockAuthor() + .fail(function() { + Diaspora.page.flashMessages.render({ + success: false, + notice: Diaspora.I18n.t('ignore_failed') + }); + }); }, remove : function() { diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index a413d01d6..f99a08d90 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -16,6 +16,7 @@ en: created: "The report was successfully created" exists: "The report already exists" ignore_user: "Ignore this user?" + ignore_failed: "Unable to ignore this user" and: "and" comma: "," edit: "Edit" diff --git a/spec/javascripts/app/views/stream_post_spec.js b/spec/javascripts/app/views/stream_post_spec.js index d66bf1fa8..7f7de9b5e 100644 --- a/spec/javascripts/app/views/stream_post_spec.js +++ b/spec/javascripts/app/views/stream_post_spec.js @@ -1,8 +1,32 @@ describe("app.views.StreamPost", function(){ beforeEach(function(){ this.PostViewClass = app.views.StreamPost + + var posts = $.parseJSON(spec.readFixture("stream_json")); + this.collection = new app.collections.Posts(posts); + this.statusMessage = this.collection.models[0]; + this.reshare = this.collection.models[1]; }) + describe("events", function(){ + var _PostViewClass = undefined; + var author_id = undefined; + + beforeEach(function(){ + _PostViewClass = this.PostViewClass; + authorId = this.statusMessage.get('author').id; + }); + + describe("remove posts for blocked person", function(){ + it("setup remove:author:posts:#{id} to #remove", function(){ + spyOn(_PostViewClass.prototype, 'remove'); + view = new _PostViewClass({model : this.statusMessage}); + app.events.trigger('person:block:'+authorId); + expect(_PostViewClass.prototype.remove).toHaveBeenCalled(); + }); + }); + }); + describe("#render", function(){ var o_embed_cache = { "data" : { @@ -32,12 +56,6 @@ describe("app.views.StreamPost", function(){ other : "<%= count %> Likes" } }}) - - var posts = $.parseJSON(spec.readFixture("stream_json")); - - this.collection = new app.collections.Posts(posts); - this.statusMessage = this.collection.models[0]; - this.reshare = this.collection.models[1]; }) context("reshare", function(){