Skip to content

Commit

Permalink
Adds in reply to context to Twitter posted messages
Browse files Browse the repository at this point in the history
I know this really lacks test, and I don't really know how to fake the calls to the Twitter API. Any help appreviated.
  • Loading branch information
Frédéric de Villamil committed Aug 16, 2013
1 parent 236e617 commit 31aa708
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/statuses_controller.rb
Expand Up @@ -50,7 +50,7 @@ def new_or_edit

if @status.save
flash[:notice] = _("Status was successfully %s.", message)
if params[:status][:push_to_twitter] and @status.twitter_id.nil? or @status.twitter_id.empty?
if params[:status][:push_to_twitter] and params[:status][:push_to_twitter] != "0" and (@status.twitter_id.nil? or @status.twitter_id.empty?)
unless @status.send_to_twitter(current_user)
flash[:error] = _("Oooops something wrong happened")
flash[:notice] = nil
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/statuses_controller.rb
@@ -1,4 +1,6 @@
class StatusesController < ContentController
require 'json'

layout :theme_layout
cache_sweeper :blog_sweeper
caches_page :index, :show, :if => Proc.new {|c|
Expand All @@ -18,6 +20,11 @@ def show
@page_title = this_blog.status_title_template.to_title(@status, this_blog, params)
@description = this_blog.status_desc_template.to_title(@status, this_blog, params)
@canonical_url = @status.permalink_url

if @status.in_reply_to_message and !@status.in_reply_to_message.empty?
@reply = JSON.parse(@status.in_reply_to_message)
end

else
render "errors/404", :status => 404
end
Expand Down
13 changes: 13 additions & 0 deletions app/helpers/statuses_helper.rb
@@ -0,0 +1,13 @@
module StatusesHelper
def get_reply_context_url(reply)
begin
return reply['user']['entities']['url']['urls'][0]['expanded_url']
rescue
return "https://twitter.com/#{reply['user']['name']}"
end
end

def get_reply_context_twitter_link(reply)
link_to(display_date_and_time(reply['created_at'].to_time), "https://twitter.com/#{reply['user']['screen_name']}/status/#{reply['id_str']}")
end
end
17 changes: 14 additions & 3 deletions app/models/status.rb
Expand Up @@ -7,7 +7,10 @@ class Status < Content

serialize :settings, Hash

setting :twitter_id, :string, ''
setting :twitter_id, :string, ""
setting :in_reply_to_status_id, :string, ""
setting :in_reply_to_protected, :boolean, false
setting :in_reply_to_message, :string, ""

belongs_to :user
validates_presence_of :body
Expand Down Expand Up @@ -65,8 +68,16 @@ def send_to_twitter(user)
end

begin
tweet = twitter.update(message)
self.twitter_id = tweet.attrs[:id_str]
options = {}

if self.in_reply_to_status_id and self.in_reply_to_status_id != ""
options = {:in_reply_to_status_id => self.in_reply_to_status_id}
self.in_reply_to_message = twitter.status(self.in_reply_to_status_id).to_json
end

tweet = twitter.update(message, options)
self.twitter_id = tweet.attrs[:id_str]

self.save

user.update_twitter_profile_image(tweet.attrs[:user][:profile_image_url])
Expand Down
12 changes: 7 additions & 5 deletions app/views/admin/statuses/new.html.erb
Expand Up @@ -7,13 +7,15 @@
<script type="text/javascript">//edToolbar('status_body');</script>
</div>
<%= text_area('status', 'body', {:class => 'input-block-level', :rows => '7', :placeholder => _("Compose new status")}) %>
<label for="status_push_to_twitter" class='checkbox'>
<%= check_box 'status', 'push_to_twitter' %>
<%= _("POSSE to Twitter")%>
</label>


<fieldset class="hidden-phone">
<legend><%= _("Publish settings") %></legend>
<label for="status_push_to_twitter" class='checkbox'>
<%= check_box 'status', 'push_to_twitter' %>
<%= _("POSSE to Twitter")%>
</label>
<label for="in_reply_to"><%= _("In reply to")%></label>
<%= text_field 'status', 'in_reply_to_status_id', :class => 'input-block-level', :placeholder => _("tweet id like 123456") %>
<label for="status_permalink"><%= _("Permanent link")%></label>
<%= text_field 'status', 'permalink', :class => 'input-block-level' %>
<label for='status_published' class='checkbox'>
Expand Down
10 changes: 10 additions & 0 deletions app/views/statuses/_status_reply_context.html.erb
@@ -0,0 +1,10 @@
<article class=''>
<%= image_tag(@reply['user']['profile_image_url'] , class: "alignleft", alt: @reply['user']['name']) %>
<strong><%= link_to(@reply['user']['name'], get_reply_context_url(@reply)) %></strong>
<p><%= PublifyApp::Textfilter::Twitterfilter.filtertext(nil,nil,@reply['text'],nil).nofollowify %></p>
<p>
<small>
<%= get_reply_context_twitter_link(@reply) %>
</small>
</p>
</article>
17 changes: 11 additions & 6 deletions app/views/statuses/show.html.erb
@@ -1,13 +1,18 @@
<div class="hfeed">
<div class='h-entry hentry h-as-note'>
<article>
<div class='h-entry hentry h-as-note well well-small'>
<% if @reply %>
<%= render :partial => "status_reply_context", :object => @reply %>
<% end %>
<article class='status'>
<%= author_picture @status %>
<div class='p-name entry-title e-content entry-content article'><%= @status.html(:body) %></div>
<footer>
<%= link_to_permalink(@status, display_date_and_time(@status.published_at)) %> |
<%= link_to @status.redirects.first.to_url, @status.redirects.first.to_url %> |
<%= author_link @status %>
<%= view_on_twitter @status %>
<small>
<%= link_to_permalink(@status, display_date_and_time(@status.published_at)) %> |
<%= link_to @status.redirects.first.to_url, @status.redirects.first.to_url %> |
<%= author_link @status %>
<%= view_on_twitter @status %>
</small>
</footer>
</article>
</div>
Expand Down
11 changes: 8 additions & 3 deletions public/stylesheets/user-styles.css
Expand Up @@ -18,7 +18,12 @@ img.right {
}

.gravatar {
float: left;
margin-right: 3px;
margin-bottom: 3px;
float: left;
margin-right: 3px;
margin-bottom: 3px;
}

.status {
background: #fff;
margin: -8px; padding: 8px;
}
22 changes: 22 additions & 0 deletions spec/models/configuration_spec.rb
Expand Up @@ -382,3 +382,25 @@
@page.password.should == ''
end
end

describe "Given a new status" do
before(:each) do
@status = Status.new
end

it "should not have a twitter id set" do
@status.twitter_id.should == ""
end

it "should not reply to another one" do
@status.in_reply_to_status_id.should == ""
end

it "should not have a reply context message" do
@status.in_reply_to_message.should == ""
end

it "should not have a reply context protected" do
@status.in_reply_to_protected.should == false
end
end

0 comments on commit 31aa708

Please sign in to comment.