MS DC show last post and show page works unauthenticated

phasing out app.user() for app.currentUser
This commit is contained in:
Dennis Collinson 2012-02-24 19:09:00 -08:00
parent 5154500239
commit b28508ecbc
22 changed files with 97 additions and 41 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -30,12 +30,14 @@
<![endif]-->
= 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

View file

@ -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?

View file

@ -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"

View file

@ -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

View file

@ -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"

View file

@ -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...

View file

@ -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;
}
});

View file

@ -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() {

View file

@ -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())
}
});

View file

@ -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;
}
})

View file

@ -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 })
}

View file

@ -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() {

View file

@ -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");

View file

@ -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();
});
});
});

View file

@ -1,6 +1,7 @@
describe("app.views.CommentStream", function(){
beforeEach(function(){
this.view = new app.views.CommentStream({model : factory.post()})
loginAs({})
})
describe("postRenderTemplate", function(){

View file

@ -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();
})

View file

@ -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() {