diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 3787749bf..8b9c6348e 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -5,7 +5,7 @@ require File.join(Rails.root, "lib", 'stream', "person") class PeopleController < ApplicationController - before_filter :authenticate_user!, :except => [:show] + before_filter :authenticate_user!, :except => [:show, :last_post] before_filter :redirect_if_tag_search, :only => [:index] respond_to :html, :except => [:tag_index] @@ -108,6 +108,12 @@ class PeopleController < ApplicationController end end + def last_post + @person = Person.find_from_guid_or_username(params) + last_post = Post.visible_from_author(@person, current_user).last + redirect_to post_path(last_post) + end + def retrieve_remote if params[:diaspora_handle] Webfinger.in_background(params[:diaspora_handle], :single_aspect_form => true) diff --git a/app/helpers/layout_helper.rb b/app/helpers/layout_helper.rb index 27c5a839c..c1cbc2144 100644 --- a/app/helpers/layout_helper.rb +++ b/app/helpers/layout_helper.rb @@ -35,12 +35,11 @@ module LayoutHelper end def set_current_user_in_javascript - return unless current_user - current_user_presenter = UserPresenter.new(current_user) - + return unless user_signed_in? + user = UserPresenter.new(current_user).to_json content_tag(:script) do <<-JS.html_safe - app.user(#{current_user_presenter.to_json}); + window.current_user_attributes = #{user} JS end end @@ -92,4 +91,5 @@ module LayoutHelper end end.join(' ').html_safe end + end diff --git a/app/models/post.rb b/app/models/post.rb index cb30eb140..fec8ad412 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -76,6 +76,13 @@ class Post < ActiveRecord::Base where("posts.created_at < ?", post.created_at).first end + def self.visible_from_author(author, current_user=nil) + if current_user.present? + current_user.posts_from(author) + else + author.posts.all_public + end + end def post_type self.class.name end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index e7105caf6..3d51ed17e 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -75,6 +75,13 @@ class PostPresenter end end + + def template_name + @template_name ||= TemplatePicker.new(post).template_name + end + + protected + def next_post post_base.next(post) end @@ -83,12 +90,6 @@ class PostPresenter post_base.previous(post) end - def template_name - @template_name ||= TemplatePicker.new(post).template_name - end - - protected - def as_api(collection) collection.includes(:author => :profile).all.map do |element| element.as_api_response(:backbone) @@ -96,11 +97,7 @@ class PostPresenter end def post_base - if current_user - current_user.posts_from(self.post.author) - else - self.post.author.posts.all_public - end + Post.visible_from_author(self.post.author, current_user) end def person diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 00fd4dc8d..fc853ce40 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -51,7 +51,7 @@ = render 'layouts/header' .container - - if @aspsect == :getting_started || @page == :logged_out + - if @aspect == :getting_started || @page == :logged_out = yield - else .span-24.last diff --git a/app/views/layouts/post.html.haml b/app/views/layouts/post.html.haml index 181ffc532..ba749a692 100644 --- a/app/views/layouts/post.html.haml +++ b/app/views/layouts/post.html.haml @@ -30,12 +30,14 @@ = jquery_include_tag + + = set_current_user_in_javascript + - unless @landing_page = include_javascripts :main, :templates = load_javascript_locales = set_asset_host - = set_current_user_in_javascript = translation_missing_warnings = current_user_atom_tag diff --git a/app/views/people/_sub_header.html.haml b/app/views/people/_sub_header.html.haml index e11abc0b2..8aa6900dc 100644 --- a/app/views/people/_sub_header.html.haml +++ b/app/views/people/_sub_header.html.haml @@ -15,6 +15,7 @@ = person.name %span.diaspora_handle = person.diaspora_handle + = link_to t('people.last_post'), last_post_person_path(person.to_param) .description - if !person.profile.tag_string.blank? && user_signed_in? diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index c7a691054..d8d385fe5 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -575,6 +575,7 @@ en: add_contact_small: add_contact_from_tag: "add contact from tag" + last_post: "Last Post" photos: show: delete_photo: "Delete Photo" diff --git a/config/routes.rb b/config/routes.rb index 4b32af38c..cdeff6936 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -127,8 +127,13 @@ Diaspora::Application.routes.draw do resources :people, :except => [:edit, :update] do resources :status_messages resources :photos - get :contacts + get :contacts get "aspect_membership_button" => :aspect_membership_dropdown, :as => "aspect_membership_button" + + member do + get :last_post + end + collection do post 'by_handle' => :retrieve_remote, :as => 'person_by_handle' get :tag_index @@ -136,6 +141,8 @@ Diaspora::Application.routes.draw do end get '/u/:username' => 'people#show', :as => 'user_profile' get '/u/:username/profile_photo' => 'users#user_photo' + + # Federation controller :publics do diff --git a/features/logged_out_browsing.feature b/features/logged_out_browsing.feature index 114669ecc..c0f26a08d 100644 --- a/features/logged_out_browsing.feature +++ b/features/logged_out_browsing.feature @@ -6,12 +6,18 @@ Feature: Browsing Diaspora as a logged out user Background: Given a user named "Bob Jones" with email "bob@bob.bob" - Given "bob@bob.bob" has a public post with text "public stuff" + And "bob@bob.bob" has a public post with text "public stuff" + And I log out Scenario: Visiting a profile page When I am on "bob@bob.bob"'s page - Then I should see "public stuff" + Then I should see "public stuff" within "body" + + Scenario: Clicking Last Post + When I am on "bob@bob.bob"'s page + And I follow "Last Post" + Then I should see "public stuff" within "body" Scenario: Visiting a post show page When I view "bob@bob.bob"'s first post - Then I should see "public stuff" + Then I should see "public stuff" within "body" diff --git a/public/javascripts/app/app.js b/public/javascripts/app/app.js index e92942473..3f019e0f1 100644 --- a/public/javascripts/app/app.js +++ b/public/javascripts/app/app.js @@ -18,12 +18,16 @@ var app = { initialize: function() { app.router = new app.Router(); - if(this._user){ + app.currentUser = app.user(window.current_user_attributes) || new app.models.User() + + + if(app.currentUser.authenticated()){ app.header = new app.views.Header; $("header").prepend(app.header.el); app.header.render(); } + Backbone.history.start({pushState: true}); // there's probably a better way to do this... diff --git a/public/javascripts/app/models/user.js b/public/javascripts/app/models/user.js index 247346b08..dd2ddd434 100644 --- a/public/javascripts/app/models/user.js +++ b/public/javascripts/app/models/user.js @@ -1,6 +1,11 @@ app.models.User = Backbone.Model.extend({ toggleNsfwState : function() { + if(!app.currentUser.authenticated()){ return false } this.set({showNsfw : !this.get("showNsfw")}); this.trigger("nsfwChanged"); + }, + + authenticated : function() { + return !!this.id; } }); diff --git a/public/javascripts/app/views.js b/public/javascripts/app/views.js index 194a2ef25..9282041a3 100644 --- a/public/javascripts/app/views.js +++ b/public/javascripts/app/views.js @@ -19,7 +19,7 @@ app.views.Base = Backbone.View.extend({ defaultPresenter : function(){ var modelJson = this.model ? this.model.toJSON() : {} - return _.extend(modelJson, {current_user: app.user().attributes}); + return _.extend(modelJson, {current_user: app.currentUser.attributes}); }, render : function() { diff --git a/public/javascripts/app/views/comment_view.js b/public/javascripts/app/views/comment_view.js index 3560df1b5..5c04227cb 100644 --- a/public/javascripts/app/views/comment_view.js +++ b/public/javascripts/app/views/comment_view.js @@ -16,15 +16,14 @@ app.views.Comment = app.views.Content.extend({ }, ownComment : function() { - return this.model.get("author").diaspora_id == app.user().get("diaspora_id") + return app.currentUser.authenticated() && this.model.get("author").diaspora_id == app.currentUser.get("diaspora_id") }, postOwner : function() { - return this.model.get("parent").author.diaspora_id == app.user().get("diaspora_id") + return app.currentUser.authenticated() && this.model.get("parent").author.diaspora_id == app.currentUser.get("diaspora_id") }, canRemove : function() { - if(!app.user()){ return false } - return this.ownComment() || this.postOwner() + return app.currentUser.authenticated() && (this.ownComment() || this.postOwner()) } }); diff --git a/public/javascripts/app/views/feedback_view.js b/public/javascripts/app/views/feedback_view.js index 2fb321df1..5d0449d71 100644 --- a/public/javascripts/app/views/feedback_view.js +++ b/public/javascripts/app/views/feedback_view.js @@ -50,9 +50,9 @@ app.views.Feedback = app.views.Base.extend({ var rootExists = (isReshare ? this.model.get("root") : true) var publicPost = this.model.get("public"); - var userIsNotAuthor = this.model.get("author").diaspora_id != app.user().get("diaspora_id"); - var userIsNotRootAuthor = rootExists && (isReshare ? this.model.get("root").author.diaspora_id != app.user().get("diaspora_id") : true) + var userIsNotAuthor = this.model.get("author").diaspora_id != app.currentUser.get("diaspora_id"); + var userIsNotRootAuthor = rootExists && (isReshare ? this.model.get("root").author.diaspora_id != app.currentUser.get("diaspora_id") : true) - return publicPost && userIsNotAuthor && userIsNotRootAuthor; + return publicPost && app.currentUser.authenticated() && userIsNotAuthor && userIsNotRootAuthor; } }) diff --git a/public/javascripts/app/views/post-viewer/interactions.js b/public/javascripts/app/views/post-viewer/interactions.js index 5feb1bdc5..a3df54603 100644 --- a/public/javascripts/app/views/post-viewer/interactions.js +++ b/public/javascripts/app/views/post-viewer/interactions.js @@ -13,15 +13,15 @@ app.views.PostViewerInteractions = app.views.Base.extend({ initialize : function() { this.initViews(); - this.feedbackView.bind("invokePane", this.invokePane, this) - this.feedbackView.bind("hidePane", this.hidePane, this) + this.feedbackView && this.feedbackView.bind("invokePane", this.invokePane, this) + this.feedbackView && this.feedbackView.bind("hidePane", this.hidePane, this) }, initViews : function() { this.reactionsView = new app.views.PostViewerReactions({ model : this.model }) /* subviews that require user */ - if(window.app.user()) { + if(app.currentUser.authenticated()) { this.feedbackView = new app.views.PostViewerFeedback({ model : this.model }) this.newCommentView = new app.views.PostViewerNewComment({ model : this.model }) } diff --git a/public/javascripts/app/views/post_view.js b/public/javascripts/app/views/post_view.js index 67013a42f..5900bf75c 100644 --- a/public/javascripts/app/views/post_view.js +++ b/public/javascripts/app/views/post_view.js @@ -41,7 +41,7 @@ app.views.Post = app.views.StreamObject.extend({ }, feedbackView : function(){ - if(!window.app.user()) { return null } + if(!app.currentUser.authenticated()) { return null } return new app.views.Feedback({model : this.model}); }, @@ -60,7 +60,7 @@ app.views.Post = app.views.StreamObject.extend({ }, showPost : function() { - return (app.user() && app.user().get("showNsfw")) || !this.model.get("nsfw") + return (app.currentUser.get("showNsfw")) || !this.model.get("nsfw") }, removeNsfwShield: function(evt){ @@ -71,7 +71,7 @@ app.views.Post = app.views.StreamObject.extend({ toggleNsfwState: function(evt){ if(evt){ evt.preventDefault(); } - app.user().toggleNsfwState(); + app.currentUser.toggleNsfwState(); }, blockUser: function(evt){ @@ -118,7 +118,7 @@ app.views.Post = app.views.StreamObject.extend({ }, authorIsNotCurrentUser : function() { - return this.model.get("author").id != (!!app.user() && app.user().id) + return this.model.get("author").id != app.user().id }, isOnShowPage : function() { diff --git a/spec/javascripts/app/app_spec.js b/spec/javascripts/app/app_spec.js index eaf1a90a7..96884b650 100644 --- a/spec/javascripts/app/app_spec.js +++ b/spec/javascripts/app/app_spec.js @@ -3,6 +3,8 @@ describe("app", function() { it("sets the user if given one and returns the current user", function() { expect(app.user()).toBeFalsy() + it("sets the user if given one and returns the current user", function() { + expect(app.user().authenticated()).toBeFalsy() app.user({name: "alice"}); expect(app.user().get("name")).toEqual("alice"); diff --git a/spec/javascripts/app/models/user_spec.js b/spec/javascripts/app/models/user_spec.js new file mode 100644 index 000000000..9565df5cc --- /dev/null +++ b/spec/javascripts/app/models/user_spec.js @@ -0,0 +1,18 @@ +describe("app.models.User", function(){ + beforeEach(function(){ + this.user = new app.models.User({}) + }); + + describe("authenticated", function(){ + it("should be true if ID is nil", function(){ + expect(this.user.authenticated()).toBeFalsy(); + }); + + it('should be true if ID is set', function(){ + this.user.set({id : 1}) + expect(this.user.authenticated()).toBeTruthy(); + }); + + }); +}); + diff --git a/spec/javascripts/app/views/comment_stream_view_spec.js b/spec/javascripts/app/views/comment_stream_view_spec.js index 94d32bd31..6467e210f 100644 --- a/spec/javascripts/app/views/comment_stream_view_spec.js +++ b/spec/javascripts/app/views/comment_stream_view_spec.js @@ -1,6 +1,7 @@ describe("app.views.CommentStream", function(){ beforeEach(function(){ this.view = new app.views.CommentStream({model : factory.post()}) + loginAs({}) }) describe("postRenderTemplate", function(){ diff --git a/spec/javascripts/app/views/feedback_view_spec.js b/spec/javascripts/app/views/feedback_view_spec.js index 2620919b4..6a4815157 100644 --- a/spec/javascripts/app/views/feedback_view_spec.js +++ b/spec/javascripts/app/views/feedback_view_spec.js @@ -108,7 +108,7 @@ describe("app.views.Feedback", function(){ context("when the current user owns the post", function(){ beforeEach(function(){ - this.post.attributes.author = window.current_user; + this.post.attributes.author = app.currentUser; this.view.render(); }) diff --git a/spec/javascripts/helpers/SpecHelper.js b/spec/javascripts/helpers/SpecHelper.js index 7e1e36087..fa6768acf 100644 --- a/spec/javascripts/helpers/SpecHelper.js +++ b/spec/javascripts/helpers/SpecHelper.js @@ -52,12 +52,12 @@ window.stubView = function stubView(text){ } window.loginAs = function loginAs(attrs){ - return window.current_user = app.user(factory.userAttrs(attrs)) + return app.currentUser = app.user(factory.userAttrs(attrs)) } window.logout = function logout(){ this.app._user = undefined - return window.current_user = app.user() + return app.currentUser = new app.models.User() } spec.clearLiveEventBindings = function() {