Skip to content

Commit

Permalink
Auto-linkifying comments and replies, and stripping html from comment…
Browse files Browse the repository at this point in the history
…s, replies, and story titles, tags, and authors.
  • Loading branch information
samuelclay committed Jul 21, 2012
1 parent 1872b0a commit 18428a3
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 12 deletions.
3 changes: 2 additions & 1 deletion apps/reader/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from utils.story_functions import format_story_link_date__long
from utils.story_functions import bunch
from utils.story_functions import story_score
from utils.story_functions import strip_tags
from utils import log as logging
from utils.view_functions import get_argument_or_404, render_to, is_true
from utils.ratelimit import ratelimit
Expand Down Expand Up @@ -478,7 +479,7 @@ def load_single_feed(request, feed_id):
story['shared'] = True
shared_date = localtime_for_timezone(shared_stories[story['id']]['shared_date'], user.profile.timezone)
story['shared_date'] = format_story_link_date__long(shared_date, now)
story['shared_comments'] = shared_stories[story['id']]['comments']
story['shared_comments'] = strip_tags(shared_stories[story['id']]['comments'])
else:
story['read_status'] = 1
story['intelligence'] = {
Expand Down
8 changes: 3 additions & 5 deletions apps/rss_feeds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from utils.feed_functions import timelimit, TimeoutError
from utils.feed_functions import relative_timesince
from utils.feed_functions import seconds_timesince
from utils.story_functions import pre_process_story
from utils.story_functions import strip_tags
from utils.diff import HTMLDiff

ENTRY_NEW, ENTRY_UPDATED, ENTRY_SAME, ENTRY_ERR = range(4)
Expand Down Expand Up @@ -724,8 +724,6 @@ def add_update_stories(self, stories, existing_stories, verbose=False):
}

for story in stories:
story = pre_process_story(story)

if not story.get('title'):
continue

Expand Down Expand Up @@ -979,8 +977,8 @@ def get_tags(self, entry):
if not tagname or tagname == ' ':
continue
fcat.append(tagname)
fcat = [t[:250] for t in fcat]
return fcat[:12]
fcat = [strip_tags(t)[:250] for t in fcat[:12]]
return fcat

def get_permalink(self, entry):
link = entry.get('link')
Expand Down
18 changes: 17 additions & 1 deletion apps/social/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from vendor import tweepy
from utils import log as logging
from utils.feed_functions import relative_timesince
from utils.story_functions import truncate_chars
from utils.story_functions import truncate_chars, strip_tags, linkify
from utils import json_functions as json

RECOMMENDATIONS_LIMIT = 5
Expand Down Expand Up @@ -475,6 +475,10 @@ def common_follows(self, user_id, direction='followers'):
def send_email_for_new_follower(self, follower_user_id):
user = User.objects.get(pk=self.user_id)
if not user.email or not user.profile.send_emails or self.user_id == follower_user_id:
if not user.email:
logging.user(user, "~BB~FMNo email to send to, skipping.")
elif not user.profile.send_emails:
logging.user(user, "~BB~FMDisabled emails, skipping.")
return

emails_sent = MSentEmail.objects.filter(receiver_user_id=user.pk,
Expand Down Expand Up @@ -988,6 +992,10 @@ def save(self, *args, **kwargs):
self.story_original_content_z = zlib.compress(self.story_original_content)
self.story_original_content = None

self.comments = linkify(strip_tags(self.comments))
for reply in self.replies:
reply.comments = linkify(strip_tags(reply.comments))

r = redis.Redis(connection_pool=settings.REDIS_POOL)
share_key = "S:%s:%s" % (self.story_feed_id, self.guid_hash)
r.sadd(share_key, self.user_id)
Expand Down Expand Up @@ -1363,6 +1371,10 @@ def send_emails_for_new_reply(self, reply_user_id):
user = User.objects.get(pk=user_id)

if not user.email or not user.profile.send_emails:
if not user.email:
logging.user(user, "~BB~FMNo email to send to, skipping.")
elif not user.profile.send_emails:
logging.user(user, "~BB~FMDisabled emails, skipping.")
continue

mute_url = "http://%s%s" % (
Expand Down Expand Up @@ -1403,6 +1415,10 @@ def send_email_for_reshare(self):
story_guid=self.story_guid)

if not original_user.email or not original_user.profile.send_emails:
if not original_user.email:
logging.user(original_user, "~BB~FMNo email to send to, skipping.")
elif not original_user.profile.send_emails:
logging.user(original_user, "~BB~FMDisabled emails, skipping.")
return

story_feed = Feed.objects.get(pk=self.story_feed_id)
Expand Down
9 changes: 5 additions & 4 deletions apps/social/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from utils.view_functions import render_to
from utils.story_functions import format_story_link_date__short
from utils.story_functions import format_story_link_date__long
from utils.story_functions import strip_tags, linkify
from utils import jennyholzer
from vendor.timezones.utilities import localtime_for_timezone

Expand Down Expand Up @@ -139,7 +140,7 @@ def load_social_stories(request, user_id, username=None):
shared_date = localtime_for_timezone(shared_stories[story['id']]['shared_date'],
user.profile.timezone)
story['shared_date'] = format_story_link_date__long(shared_date, now)
story['shared_comments'] = shared_stories[story['id']]['comments']
story['shared_comments'] = strip_tags(shared_stories[story['id']]['comments'])

story['intelligence'] = {
'feed': apply_classifier_feeds(classifier_feeds, story['story_feed_id'],
Expand Down Expand Up @@ -343,7 +344,7 @@ def mark_story_as_shared(request):
stories, profiles = MSharedStory.stories_with_comments_and_profiles([story], request.user.pk,
check_all=check_all)
story = stories[0]
story['shared_comments'] = shared_story['comments'] or ""
story['shared_comments'] = strip_tags(shared_story['comments'] or "")

if post_to_services:
for service in post_to_services:
Expand Down Expand Up @@ -438,7 +439,7 @@ def save_comment_reply(request):
replies = []
for story_reply in shared_story.replies:
if (story_reply.user_id == reply.user_id and
story_reply.comments == original_message):
strip_tags(story_reply.comments) == original_message):
reply.publish_date = story_reply.publish_date
replies.append(reply)
else:
Expand Down Expand Up @@ -900,7 +901,7 @@ def load_activities(request):

public = user_id != request.user.pk
page = max(1, int(request.REQUEST.get('page', 1)))
limit = request.REQUEST.get('limit')
limit = request.REQUEST.get('limit', 4)
activities, has_next_page = MActivity.user(user_id, page=page, limit=limit, public=public)
format = request.REQUEST.get('format', None)

Expand Down
1 change: 1 addition & 0 deletions media/js/newsblur/models/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ NEWSBLUR.Models.Story = Backbone.Model.extend({

initialize: function() {
this.bind('change:selected', this.change_selected);
this.bind('change:shared_comments', this.populate_comments);
this.bind('change:comments', this.populate_comments);
this.bind('change:comment_count', this.populate_comments);
this.populate_comments();
Expand Down
1 change: 1 addition & 0 deletions media/js/newsblur/views/story_share_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ NEWSBLUR.Views.StoryShareView = Backbone.View.extend({
$share_button.removeClass('NB-saving').removeClass('NB-disabled').text('Share');
$unshare_button.removeClass('NB-saving').removeClass('NB-disabled').text('Delete Share');
$share_sideoption.text(shared_text).closest('.NB-sideoption');
$comments_sideoptions.val(this.model.get('shared_comments'));

if (this.options.on_social_page) {
this.model.social_page_story.$el.toggleClass('NB-story-shared', this.model.get('shared'));
Expand Down
5 changes: 4 additions & 1 deletion utils/feed_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,14 @@ def process(self):
start_date = datetime.datetime.utcnow()
# end_date = datetime.datetime.utcnow()
story_guids = []
stories = []
for entry in self.fpf.entries:
story = pre_process_story(entry)
if story.get('published') < start_date:
start_date = story.get('published')
# if story.get('published') > end_date:
# end_date = story.get('published')
stories.append(story)
story_guids.append(story.get('guid') or story.get('link'))

existing_stories = list(MStory.objects(
Expand All @@ -236,7 +238,8 @@ def process(self):
# | (Q(story_guid__in=story_guids)),
# story_feed=self.feed
# ).order_by('-story_date')
ret_values = self.feed.add_update_stories(self.fpf.entries, existing_stories, verbose=self.options['verbose'])
ret_values = self.feed.add_update_stories(stories, existing_stories,
verbose=self.options['verbose'])

if ((not self.feed.is_push or self.options.get('force'))
and hasattr(self.fpf, 'feed') and
Expand Down
12 changes: 12 additions & 0 deletions utils/story_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from HTMLParser import HTMLParser
from itertools import chain
from django.utils.dateformat import DateFormat
from django.utils.html import strip_tags as strip_tags_django
from django.conf import settings
from utils.tornado_escape import linkify as linkify_tornado

def story_score(story, bottom_delta=None):
# A) Date - Assumes story is unread and within unread range
Expand Down Expand Up @@ -110,6 +112,9 @@ def pre_process_story(entry):
story_title = story_title[:80] + '...'
entry['title'] = story_title

entry['title'] = strip_tags(entry.get('title'))
entry['author'] = strip_tags(entry.get('author'))

return entry

class bunch(dict):
Expand Down Expand Up @@ -156,9 +161,16 @@ def get_data(self):
return ' '.join(self.fed)

def strip_tags(html):
if not html:
return ''
return strip_tags_django(html)

s = MLStripper()
s.feed(html)
return s.get_data()

def linkify(*args, **kwargs):
return linkify_tornado(*args, **kwargs)

def truncate_chars(value, max_length):
if len(value) <= max_length:
Expand Down
Loading

0 comments on commit 18428a3

Please sign in to comment.