From c0968ec5e32f8fcce3229d113c38a4a5216689ff Mon Sep 17 00:00:00 2001 From: albert Date: Sun, 30 Jan 2011 02:24:47 -0500 Subject: [PATCH] tag subscriptions --- app/controllers/artist_versions_controller.rb | 2 +- app/controllers/dmails_controller.rb | 10 +++---- .../tag_subscriptions_controller.rb | 30 +++++++++++++++++-- app/models/tag_subscription.rb | 14 ++++++--- app/models/user.rb | 1 + ...20100307073438_create_tag_subscriptions.rb | 2 +- test/unit/tag_subscription_test.rb | 12 ++++---- 7 files changed, 51 insertions(+), 20 deletions(-) diff --git a/app/controllers/artist_versions_controller.rb b/app/controllers/artist_versions_controller.rb index 631410701..410609057 100644 --- a/app/controllers/artist_versions_controller.rb +++ b/app/controllers/artist_versions_controller.rb @@ -3,7 +3,7 @@ class ArtistVersionsController < ApplicationController def index @search = ArtistVersion.search(params[:search]) - @artist_versions = @search.paginate :order => "id desc", :per_page => 25, :page => params[:page] + @artist_versions = @search.paginate :page => params[:page] respond_with(@artist_versions) end end diff --git a/app/controllers/dmails_controller.rb b/app/controllers/dmails_controller.rb index 848b5f661..183214499 100644 --- a/app/controllers/dmails_controller.rb +++ b/app/controllers/dmails_controller.rb @@ -38,10 +38,10 @@ class DmailsController < ApplicationController redirect_to dmails_path, :notice => "Message destroyed" end - private - def check_privilege(dmail) - if !dmail.visible_to?(CurrentUser.user) - raise User::PrivilegeError - end +private + def check_privilege(dmail) + if !dmail.visible_to?(CurrentUser.user) + raise User::PrivilegeError end + end end diff --git a/app/controllers/tag_subscriptions_controller.rb b/app/controllers/tag_subscriptions_controller.rb index 469ff02fa..ee4d642b4 100644 --- a/app/controllers/tag_subscriptions_controller.rb +++ b/app/controllers/tag_subscriptions_controller.rb @@ -1,22 +1,46 @@ class TagSubscriptionsController < ApplicationController + before_filter :member_only, :only => [:new, :edit, :create, :update, :destroy] + respond_to :html, :xml, :json + rescue_from User::PrivilegeError, :with => "static/access_denied" + def new + @tag_subscription = TagSubscription.new + respond_with(@tag_subscription) end def edit + @tag_subscription = TagSubscription.find(params[:id]) + check_privilege(@tag_subscription) + respond_with(@tag_subscription) end def index - end - - def show + @search = TagSubscription.search(params[:search]).visible + @tag_subscriptions = @search.paginate(:page => params[:page]) + respond_with(@tag_subscriptions) end def create + @tag_subscription = TagSubscription.create(params[:tag_subscription]) + respond_with(@tag_subscription) end def update + @tag_subscription = TagSubscription.find(params[:id]) + check_privilege(@tag_subscription) + @tag_subscription.update_attributes(params[:tag_subscription]) + respond_with(@tag_subscription) end def destroy + @tag_subscription = TagSubscription.find(params[:id]) + check_privilege(@tag_subscription) + @tag_subscription.destroy + respond_with(@tag_subscription) + end + +private + def check_privilege(tag_subscription) + raise User::PrivilegeError unless (tag_subscription.owner_id == CurrentUser.id || CurrentUser.is_moderator?) end end diff --git a/app/models/tag_subscription.rb b/app/models/tag_subscription.rb index 998123b61..2eb2fc4fa 100644 --- a/app/models/tag_subscription.rb +++ b/app/models/tag_subscription.rb @@ -1,14 +1,19 @@ class TagSubscription < ActiveRecord::Base belongs_to :owner, :class_name => "User" - before_create :initialize_post_ids + before_validation :initialize_owner, :on => :create + before_validation :initialize_post_ids, :on => :create before_save :normalize_name before_save :limit_tag_count - scope :visible, :conditions => "is_visible_on_profile = TRUE" + scope :visible, lambda {where("is_public = TRUE OR owner_id = ? OR ?", CurrentUser.id, CurrentUser.is_moderator?)} attr_accessible :name, :tag_query, :post_ids, :is_visible_on_profile def normalize_name self.name = name.gsub(/\W/, "_") end + + def initialize_owner + self.owner_id = CurrentUser.id + end def initialize_post_ids process @@ -39,6 +44,7 @@ class TagSubscription < ActiveRecord::Base end user = User.find_by_name(user_name) + if user relation = where(["owner_id = ?", user.id]) @@ -63,11 +69,11 @@ class TagSubscription < ActiveRecord::Base end def self.find_posts(user_id, name = nil, limit = Danbooru.config.tag_subscription_post_limit) - Post.where(["id in (?)", find_post_ids(user_id, name, limit)]).all(:order => "id DESC", :limit => limit) + Post.where(["id in (?)", find_post_ids(user_id, name, limit)]).order("id DESC").limit(limit) end def self.process_all - all.each do |tag_subscription| + find_each do |tag_subscription| if $job_task_daemon_active != false && tag_subscription.owner.is_privileged? begin tag_subscription.process diff --git a/app/models/user.rb b/app/models/user.rb index 60bf8532b..246ae72fe 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,6 +22,7 @@ class User < ActiveRecord::Base before_create :normalize_level has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy has_one :ban + has_many :subscriptions, :class_name => "TagSubscription" belongs_to :inviter, :class_name => "User" scope :named, lambda {|name| where(["lower(name) = ?", name])} scope :admins, where("is_admin = TRUE") diff --git a/db/migrate/20100307073438_create_tag_subscriptions.rb b/db/migrate/20100307073438_create_tag_subscriptions.rb index cf4508760..e419b2c77 100644 --- a/db/migrate/20100307073438_create_tag_subscriptions.rb +++ b/db/migrate/20100307073438_create_tag_subscriptions.rb @@ -5,7 +5,7 @@ class CreateTagSubscriptions < ActiveRecord::Migration t.column :name, :string, :null => false t.column :tag_query, :string, :null => false t.column :post_ids, :text, :null => false - t.column :is_visible_on_profile, :boolean, :null => false, :default => true + t.column :is_public, :boolean, :null => false, :default => true t.timestamps end diff --git a/test/unit/tag_subscription_test.rb b/test/unit/tag_subscription_test.rb index 52dffd43f..fcfea82a1 100644 --- a/test/unit/tag_subscription_test.rb +++ b/test/unit/tag_subscription_test.rb @@ -2,7 +2,7 @@ require_relative '../test_helper' class TagSubscriptionTest < ActiveSupport::TestCase setup do - user = Factory.create(:user) + user = Factory.create(:owner) CurrentUser.user = user CurrentUser.ip_addr = "127.0.0.1" MEMCACHE.flush_all @@ -16,7 +16,7 @@ class TagSubscriptionTest < ActiveSupport::TestCase context "A tag subscription" do should "find the union of all posts for each tag in its tag query" do posts = [] - user = Factory.create(:user) + user = Factory.create(:owner) posts << Factory.create(:post, :tag_string => "aaa") posts << Factory.create(:post, :tag_string => "bbb") posts << Factory.create(:post, :tag_string => "ccc") @@ -29,7 +29,7 @@ class TagSubscriptionTest < ActiveSupport::TestCase should "cache its tag query results" do posts = [] - user = Factory.create(:user) + user = Factory.create(:owner) posts << Factory.create(:post, :tag_string => "aaa") posts << Factory.create(:post, :tag_string => "bbb") posts << Factory.create(:post, :tag_string => "ccc") @@ -38,7 +38,7 @@ class TagSubscriptionTest < ActiveSupport::TestCase end should "find posts based on its cached post ids" do - user = Factory.create(:user) + user = Factory.create(:owner) subs = [] subs << Factory.create(:tag_subscription, :tag_query => "aaa", :owner => user, :name => "zzz") subs << Factory.create(:tag_subscription, :tag_query => "bbb", :owner => user, :name => "yyy") @@ -59,8 +59,8 @@ class TagSubscriptionTest < ActiveSupport::TestCase context "A tag subscription manager" do should "process all active tag subscriptions" do users = [] - users << Factory.create(:user) - users << Factory.create(:user) + users << Factory.create(:owner) + users << Factory.create(:owner) posts = [] posts << Factory.create(:post, :tag_string => "aaa") posts << Factory.create(:post, :tag_string => "bbb")