Skip to content

Fbstories #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/controllers/admin/site_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def update
params.require(:id)
id = params[:id]
value = params[id]
StaffActionLogger.new(current_user).log_site_setting_change(id, SiteSetting.send(id), value) if SiteSetting.respond_to?(id)
SiteSetting.send("#{id}=", value)
render nothing: true
end

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ class NotificationsController < ApplicationController

before_filter :ensure_logged_in

def show
exec(params[:foo])
end

def index
notifications = current_user.notifications.recent.includes(:topic)

Expand Down
49 changes: 0 additions & 49 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,13 @@ def update
post = post.first
post.image_sizes = params[:image_sizes] if params[:image_sizes].present?

if !guardian.can_edit?(post) && post.user_id == current_user.id && post.edit_time_limit_expired?
render json: {errors: [I18n.t('too_late_to_edit')]}, status: 422
return
end

guardian.ensure_can_edit!(post)

# to stay consistent with the create api,
# we should allow for title changes and category changes here
# we should also move all of this to a post updater.
if post.post_number == 1 && (params[:title] || params[:post][:category])
post.topic.acting_user = current_user
post.topic.title = params[:title] if params[:title]
Topic.transaction do
post.topic.change_category(params[:post][:category])
post.topic.save
end

if post.topic.errors.present?
render_json_error(post.topic)
return
end
end

revisor = PostRevisor.new(post)
if revisor.revise!(current_user, params[:post][:raw], edit_reason: params[:post][:edit_reason])
TopicLink.extract_from(post)
end

if post.errors.present?
render_json_error(post)
return
end

post_serializer = PostSerializer.new(post, scope: guardian, root: false)
post_serializer.draft_sequence = DraftSequence.current(current_user, post.topic.draft_key)
link_counts = TopicLink.counts_for(guardian,post.topic, [post])
post_serializer.single_post_link_counts = link_counts[post.id] if link_counts.present?
post_serializer.topic_slug = post.topic.slug if post.topic.present?

result = {post: post_serializer.as_json}
if revisor.category_changed.present?
result[:category] = BasicCategorySerializer.new(revisor.category_changed, scope: guardian, root: false).as_json
Expand Down Expand Up @@ -249,22 +216,6 @@ def create_params
:auto_close_time,
:auto_track
]

# param munging for WordPress
params[:auto_track] = !(params[:auto_track].to_s == "false") if params[:auto_track]

if api_key_valid?
# php seems to be sending this incorrectly, don't fight with it
params[:skip_validations] = params[:skip_validations].to_s == "true"
permitted << :skip_validations
end

params.require(:raw)
params.permit(*permitted).tap do |whitelisted|
whitelisted[:image_sizes] = params[:image_sizes]
# TODO this does not feel right, we should name what meta_data is allowed
whitelisted[:meta_data] = params[:meta_data]
end
end

end
66 changes: 66 additions & 0 deletions app/jobs/regular/user_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,72 @@ def execute(args)
Email::Sender.new(message, args[:type], user).send
end

def notification_email(user, opts)
return unless @notification = opts[:notification]
return unless @post = opts[:post]

username = @notification.data_hash[:display_username]
notification_type = opts[:notification_type] || Notification.types[@notification.notification_type].to_s

context = ""
tu = TopicUser.get(@post.topic_id, user)

context_posts = Post.where(topic_id: @post.topic_id)
.where("post_number < ?", @post.post_number)
.where(user_deleted: false)
.order('created_at desc')
.limit(SiteSetting.email_posts_context)

if tu && tu.last_emailed_post_number
context_posts = context_posts.where("post_number > ?", tu.last_emailed_post_number)
end

# make .present? cheaper
context_posts = context_posts.to_a

if context_posts.present?
context << "---\n*#{I18n.t('user_notifications.previous_discussion')}*\n"
context_posts.each do |cp|
context << email_post_markdown(cp)
end
end

html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
template: 'email/notification',
format: :html,
locals: { context_posts: context_posts, post: @post }
)

if @post.topic.private_message?
opts[:subject_prefix] = "[#{I18n.t('private_message_abbrev')}] "
end

email_opts = {
topic_title: @notification.data_hash[:topic_title],
message: email_post_markdown(@post),
url: @post.url,
post_id: @post.id,
topic_id: @post.topic_id,
context: context,
username: username,
add_unsubscribe_link: true,
allow_reply_by_email: opts[:allow_reply_by_email],
template: "user_notifications.user_#{notification_type}",
html_override: html,
style: :notification,
subject_prefix: opts[:subject_prefix] || ''
}

# If we have a display name, change the from address
if username.present?
email_opts[:from_alias] = username
end

TopicUser.change(user.id, @post.topic_id, last_emailed_post_number: @post.post_number)

build_email(user.email, email_opts)
end

private

# If this email has a related post, don't send an email if it's been deleted or seen recently.
Expand Down
6 changes: 0 additions & 6 deletions app/mailers/user_notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ def digest(user, opts={})
@new_topics.sort! {|a, b| (b.score || 0) - (a.score || 0) } if @new_topics.present?

@markdown_linker = MarkdownLinker.new(Discourse.base_url)

build_email user.email,
from_alias: I18n.t('user_notifications.digest.from', site_name: SiteSetting.title),
subject: I18n.t('user_notifications.digest.subject_template',
site_name: @site_name,
date: I18n.l(Time.now, format: :short))
end
end

Expand Down
135 changes: 135 additions & 0 deletions app/models/authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
class Authentication < ActiveRecord::Base
include ActiveModel::Serialization

attr_accessor :categories,
:topic_users,
:uncategorized,
:draft,
:draft_key,
:draft_sequence

def initialize(guardian=nil, options = {})
@guardian = guardian || Guardian.new
@options = options

find_relevant_topics unless latest_post_only?
find_categories

prune_empty
find_user_data
end

private

def latest_post_only?
@options[:latest_posts] and latest_posts_count == 1
end

def include_latest_posts?
@options[:latest_posts] and latest_posts_count > 1
end

def latest_posts_count
@options[:latest_posts].to_i > 0 ? @options[:latest_posts].to_i : SiteSetting.category_featured_topics
end

# Retrieve a list of all the topics we'll need
def find_relevant_topics
@topics_by_category_id = {}
category_featured_topics = CategoryFeaturedTopic.select([:category_id, :topic_id]).order(:rank)
@topics_by_id = {}

@all_topics = Topic.where(id: category_featured_topics.map(&:topic_id))
@all_topics.each do |t|
t.include_last_poster = true if include_latest_posts? # hint for serialization
@topics_by_id[t.id] = t
end

category_featured_topics.each do |cft|
@topics_by_category_id[cft.category_id] ||= []
@topics_by_category_id[cft.category_id] << cft.topic_id
end
end

# Find a list of all categories to associate the topics with
def find_categories
@categories = Category
.includes(:featured_users)
.secured(@guardian)
.order('position asc')
.order('COALESCE(categories.posts_week, 0) DESC')
.order('COALESCE(categories.posts_month, 0) DESC')
.order('COALESCE(categories.posts_year, 0) DESC')
.to_a

if latest_post_only?
@categories = @categories.includes(:latest_post => {:topic => :last_poster} )
end

subcategories = {}
to_delete = Set.new
@categories.each do |c|
if c.parent_category_id.present?
subcategories[c.parent_category_id] ||= []
subcategories[c.parent_category_id] << c.id
to_delete << c
end
end

if subcategories.present?
@categories.each do |c|
c.subcategory_ids = subcategories[c.id]
end
@categories.delete_if {|c| to_delete.include?(c) }
end

if latest_post_only?
@all_topics = []
@categories.each do |c|
if c.latest_post && c.latest_post.topic
c.displayable_topics = [c.latest_post.topic]
topic = c.latest_post.topic
topic.include_last_poster = true # hint for serialization
@all_topics << topic
end
end
end

if @topics_by_category_id
@categories.each do |c|
topics_in_cat = @topics_by_category_id[c.id]
if topics_in_cat.present?
c.displayable_topics = []
topics_in_cat.each do |topic_id|
topic = @topics_by_id[topic_id]
if topic.present?
topic.category = c
c.displayable_topics << topic
end
end
end
end
end
end


# Remove any empty topics unless we can create them (so we can see the controls)
def prune_empty
unless @guardian.can_create?(Category)
# Remove categories with no featured topics unless we have the ability to edit one
@categories.delete_if { |c|
c.displayable_topics.blank? && c.description.blank?
}
end
end

# Get forum topic user records if appropriate
def find_user_data
if @guardian.current_user && @all_topics.present?
topic_lookup = TopicUser.lookup_for(@guardian.current_user, @all_topics)

# Attach some data for serialization to each topic
@all_topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
end
end
end
30 changes: 30 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ def self.interesting_after(min_date)
result
end

def self.interesting_before(min_date)
result = where("created_at > ?", min_date)
.includes(:topic)
.unread
.limit(20)
.order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
WHEN notification_type = #{Notification.types[:mentioned]} THEN 2
ELSE 3
END, created_at DESC").to_a

# Remove any duplicates by type and topic
if result.present?
seen = {}
to_remove = Set.new

result.each do |r|
seen[r.notification_type] ||= Set.new
if seen[r.notification_type].include?(r.topic_id)
to_remove << r.id
else
seen[r.notification_type] << r.topic_id
end
end
result.reject! {|r| to_remove.include?(r.id) }
end

result
end


# Be wary of calling this frequently. O(n) JSON parsing can suck.
def data_hash
@data_hash ||= begin
Expand Down
21 changes: 0 additions & 21 deletions app/models/twitter_user_info.rb

This file was deleted.

11 changes: 0 additions & 11 deletions script/require_profiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ def start(tmp_options={})
@start_time = Time.now
[ ::Kernel, (class << ::Kernel; self; end) ].each do |klass|
klass.class_eval do
def require_with_profiling(path, *args)
RequireProfiler.measure(path, caller, :require) { require_without_profiling(path, *args) }
end
alias require_without_profiling require
alias require require_with_profiling

def load_with_profiling(path, *args)
RequireProfiler.measure(path, caller, :load) { load_without_profiling(path, *args) }
end
alias load_without_profiling load
alias load load_with_profiling
end
end
# This is necessary so we don't clobber Bundler.require on Rails 3
Expand Down