Skip to content

Instantly share code, notes, and snippets.

@axavio
Created January 17, 2012 16:56
Show Gist options
  • Save axavio/1627481 to your computer and use it in GitHub Desktop.
Save axavio/1627481 to your computer and use it in GitHub Desktop.
maxwell-robhogg-nsfw-shield
From 39a86ebc2c91a6cbb4f27b7555be9c208d437d1d Mon Sep 17 00:00:00 2001
From: Dave Yingling <[email protected]>
Date: Tue, 17 Jan 2012 08:53:33 -0800
Subject: [PATCH] add nsfw shield by ms and rh
---
app/helpers/stream_element_helper.rb | 8 ++++++++
app/models/status_message.rb | 5 +++++
app/views/shared/_stream_element.html.haml | 16 +++++++++-------
.../status_messages/_status_message.html.haml | 1 -
config/locales/diaspora/en.yml | 2 ++
public/javascripts/stream.js | 10 ++++++++++
public/stylesheets/sass/application.sass | 11 +++++++++++
spec/models/status_message_spec.rb | 12 ++++++++++++
8 files changed, 57 insertions(+), 8 deletions(-)
diff --git a/app/helpers/stream_element_helper.rb b/app/helpers/stream_element_helper.rb
index f79c5ca..9942585 100644
--- a/app/helpers/stream_element_helper.rb
+++ b/app/helpers/stream_element_helper.rb
@@ -16,4 +16,12 @@ module StreamElementHelper
link_to image_tag('deletelabel.png'), share_visibility_path(:id => "42", :post_id => post.id), :method => :put, :remote => true, :class => "delete remove_post control_icon vis_hide", :title => t('.hide_and_mute')
end
end
+
+ def nsfw_shield(post)
+ if post.respond_to?(:nsfw?) && post.nsfw?
+ content_tag(:div, :class => 'shield') do
+ I18n.translate('shared.stream_element.nsfw', :link => link_to(I18n.translate('shared.stream_element.show'), '#')).html_safe
+ end
+ end
+ end
end
diff --git a/app/models/status_message.rb b/app/models/status_message.rb
index 79c1e0d..52bc63c 100644
--- a/app/models/status_message.rb
+++ b/app/models/status_message.rb
@@ -65,10 +65,15 @@ class StatusMessage < Post
def raw_message
read_attribute(:text)
end
+
def raw_message=(text)
write_attribute(:text, text)
end
+ def nsfw?
+ self.raw_message.match(/#nsfw/i)
+ end
+
def formatted_message(opts={})
return self.raw_message unless self.raw_message
diff --git a/app/views/shared/_stream_element.html.haml b/app/views/shared/_stream_element.html.haml
index d76699d..7870143 100644
--- a/app/views/shared/_stream_element.html.haml
+++ b/app/views/shared/_stream_element.html.haml
@@ -23,7 +23,7 @@
.sm_body
= person_image_link(post.author, :size => :thumb_small)
.content
- %div.post_initial_info
+ .post_initial_info
%span.from
= person_link(post.author, :class => 'hovercardable')
%time.time.timeago{:datetime => post.created_at, :integer => time_for_sort(post).to_i}
@@ -37,12 +37,14 @@
%span.num_reshares
= t("reshares.reshare.reshare", :count => post.reshares.size)
- - if post.activity_streams?
- = link_to image_tag(post.image_url, 'data-small-photo' => post.image_url, 'data-full-photo' => post.image_url, :class => 'stream-photo'), post.object_url, :class => "stream-photo-link"
- - elsif reshare?(post)
- = render 'reshares/reshare', :reshare => post, :post => post.root
- - else
- = render 'status_messages/status_message', :post => post, :photos => post.photos
+ .post-content
+ = nsfw_shield(post)
+ - if post.activity_streams?
+ = link_to image_tag(post.image_url, 'data-small-photo' => post.image_url, 'data-full-photo' => post.image_url, :class => 'stream-photo'), post.object_url, :class => "stream-photo-link"
+ - elsif reshare?(post)
+ = render 'reshares/reshare', :reshare => post, :post => post.root
+ - else
+ = render 'status_messages/status_message', :post => post, :photos => post.photos
.info
%span.via
diff --git a/app/views/status_messages/_status_message.html.haml b/app/views/status_messages/_status_message.html.haml
index b5c5cd7..3fc82ec 100644
--- a/app/views/status_messages/_status_message.html.haml
+++ b/app/views/status_messages/_status_message.html.haml
@@ -2,7 +2,6 @@
-# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file.
-
- if photos.size > 0
.photo_attachments
.big_stream_photo
diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml
index 14555f1..9fb1c51 100644
--- a/config/locales/diaspora/en.yml
+++ b/config/locales/diaspora/en.yml
@@ -896,6 +896,8 @@ en:
unlike: "Unlike"
dislike: "Dislike"
shared_with: "Shared with: %{aspect_names}"
+ nsfw: "This post has been flagged as NSFW by its author. %{link}"
+ show: "show"
footer:
logged_in_as: "logged in as %{name}"
your_aspects: "your aspects"
diff --git a/public/javascripts/stream.js b/public/javascripts/stream.js
index 47bcd93..803a985 100644
--- a/public/javascripts/stream.js
+++ b/public/javascripts/stream.js
@@ -5,6 +5,7 @@
var Stream = {
selector: "#main_stream",
+ nsfw_links: ".shield a",
initialize: function() {
Diaspora.page.directionDetector.updateBinds();
@@ -13,6 +14,8 @@ var Stream = {
},
initializeLives: function(){
+ Stream.setUpNsfwLinks();
+
// reshare button action
$(".reshare_button", this.selector).live("click", function(evt) {
evt.preventDefault();
@@ -26,6 +29,13 @@ var Stream = {
});
},
+ setUpNsfwLinks:function(){
+ $(this.nsfw_links).click(function(e){
+ e.preventDefault();
+ $(this).parent().fadeOut();
+ });
+ },
+
setUpAudioLinks: function() {
$(".stream a[target='_blank']").each(function(r){
var link = $(this);
diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass
index 2456f5e..3c83d26 100644
--- a/public/stylesheets/sass/application.sass
+++ b/public/stylesheets/sass/application.sass
@@ -3496,6 +3496,17 @@ a.toggle_selector
&:hover
@include opacity(1)
+.post-content
+ :position relative
+
+.shield
+ :z-index 3
+ :background-color #eee
+ :position absolute
+ :width 100%
+ :height 100%
+ :padding 5px
+
#gs-shim
:position absolute
:top 380px
diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb
index 9c34502..cb06c0e 100644
--- a/spec/models/status_message_spec.rb
+++ b/spec/models/status_message_spec.rb
@@ -217,6 +217,18 @@ STR
end
end
+ describe "#nsfw?" do
+ it 'returns MatchObject (true) if the post contains #nsfw (however capitalised)' do
+ status = Factory(:status_message, :text => "This message is #nSFw")
+ status.nsfw?.should be_true
+ end
+
+ it 'returns nil (false) if the post does not contain #nsfw' do
+ status = Factory(:status_message, :text => "This message is #sFW")
+ status.nsfw?.should be_false
+ end
+ end
+
describe "#notify_person" do
it 'notifies the person mentioned' do
Notification.should_receive(:notify).with(alice, anything, anything)
--
1.7.5.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment