tag subscriptions

This commit is contained in:
albert
2011-01-30 02:24:47 -05:00
parent ee9aca973f
commit c0968ec5e3
7 changed files with 51 additions and 20 deletions

View File

@@ -3,7 +3,7 @@ class ArtistVersionsController < ApplicationController
def index def index
@search = ArtistVersion.search(params[:search]) @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) respond_with(@artist_versions)
end end
end end

View File

@@ -38,10 +38,10 @@ class DmailsController < ApplicationController
redirect_to dmails_path, :notice => "Message destroyed" redirect_to dmails_path, :notice => "Message destroyed"
end end
private private
def check_privilege(dmail) def check_privilege(dmail)
if !dmail.visible_to?(CurrentUser.user) if !dmail.visible_to?(CurrentUser.user)
raise User::PrivilegeError raise User::PrivilegeError
end
end end
end
end end

View File

@@ -1,22 +1,46 @@
class TagSubscriptionsController < ApplicationController 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 def new
@tag_subscription = TagSubscription.new
respond_with(@tag_subscription)
end end
def edit def edit
@tag_subscription = TagSubscription.find(params[:id])
check_privilege(@tag_subscription)
respond_with(@tag_subscription)
end end
def index def index
end @search = TagSubscription.search(params[:search]).visible
@tag_subscriptions = @search.paginate(:page => params[:page])
def show respond_with(@tag_subscriptions)
end end
def create def create
@tag_subscription = TagSubscription.create(params[:tag_subscription])
respond_with(@tag_subscription)
end end
def update def update
@tag_subscription = TagSubscription.find(params[:id])
check_privilege(@tag_subscription)
@tag_subscription.update_attributes(params[:tag_subscription])
respond_with(@tag_subscription)
end end
def destroy 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
end end

View File

@@ -1,14 +1,19 @@
class TagSubscription < ActiveRecord::Base class TagSubscription < ActiveRecord::Base
belongs_to :owner, :class_name => "User" 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 :normalize_name
before_save :limit_tag_count 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 attr_accessible :name, :tag_query, :post_ids, :is_visible_on_profile
def normalize_name def normalize_name
self.name = name.gsub(/\W/, "_") self.name = name.gsub(/\W/, "_")
end end
def initialize_owner
self.owner_id = CurrentUser.id
end
def initialize_post_ids def initialize_post_ids
process process
@@ -39,6 +44,7 @@ class TagSubscription < ActiveRecord::Base
end end
user = User.find_by_name(user_name) user = User.find_by_name(user_name)
if user if user
relation = where(["owner_id = ?", user.id]) relation = where(["owner_id = ?", user.id])
@@ -63,11 +69,11 @@ class TagSubscription < ActiveRecord::Base
end end
def self.find_posts(user_id, name = nil, limit = Danbooru.config.tag_subscription_post_limit) 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 end
def self.process_all 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? if $job_task_daemon_active != false && tag_subscription.owner.is_privileged?
begin begin
tag_subscription.process tag_subscription.process

View File

@@ -22,6 +22,7 @@ class User < ActiveRecord::Base
before_create :normalize_level before_create :normalize_level
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
has_one :ban has_one :ban
has_many :subscriptions, :class_name => "TagSubscription"
belongs_to :inviter, :class_name => "User" belongs_to :inviter, :class_name => "User"
scope :named, lambda {|name| where(["lower(name) = ?", name])} scope :named, lambda {|name| where(["lower(name) = ?", name])}
scope :admins, where("is_admin = TRUE") scope :admins, where("is_admin = TRUE")

View File

@@ -5,7 +5,7 @@ class CreateTagSubscriptions < ActiveRecord::Migration
t.column :name, :string, :null => false t.column :name, :string, :null => false
t.column :tag_query, :string, :null => false t.column :tag_query, :string, :null => false
t.column :post_ids, :text, :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 t.timestamps
end end

View File

@@ -2,7 +2,7 @@ require_relative '../test_helper'
class TagSubscriptionTest < ActiveSupport::TestCase class TagSubscriptionTest < ActiveSupport::TestCase
setup do setup do
user = Factory.create(:user) user = Factory.create(:owner)
CurrentUser.user = user CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all MEMCACHE.flush_all
@@ -16,7 +16,7 @@ class TagSubscriptionTest < ActiveSupport::TestCase
context "A tag subscription" do context "A tag subscription" do
should "find the union of all posts for each tag in its tag query" do should "find the union of all posts for each tag in its tag query" do
posts = [] posts = []
user = Factory.create(:user) user = Factory.create(:owner)
posts << Factory.create(:post, :tag_string => "aaa") posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb") posts << Factory.create(:post, :tag_string => "bbb")
posts << Factory.create(:post, :tag_string => "ccc") posts << Factory.create(:post, :tag_string => "ccc")
@@ -29,7 +29,7 @@ class TagSubscriptionTest < ActiveSupport::TestCase
should "cache its tag query results" do should "cache its tag query results" do
posts = [] posts = []
user = Factory.create(:user) user = Factory.create(:owner)
posts << Factory.create(:post, :tag_string => "aaa") posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb") posts << Factory.create(:post, :tag_string => "bbb")
posts << Factory.create(:post, :tag_string => "ccc") posts << Factory.create(:post, :tag_string => "ccc")
@@ -38,7 +38,7 @@ class TagSubscriptionTest < ActiveSupport::TestCase
end end
should "find posts based on its cached post ids" do should "find posts based on its cached post ids" do
user = Factory.create(:user) user = Factory.create(:owner)
subs = [] subs = []
subs << Factory.create(:tag_subscription, :tag_query => "aaa", :owner => user, :name => "zzz") subs << Factory.create(:tag_subscription, :tag_query => "aaa", :owner => user, :name => "zzz")
subs << Factory.create(:tag_subscription, :tag_query => "bbb", :owner => user, :name => "yyy") 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 context "A tag subscription manager" do
should "process all active tag subscriptions" do should "process all active tag subscriptions" do
users = [] users = []
users << Factory.create(:user) users << Factory.create(:owner)
users << Factory.create(:user) users << Factory.create(:owner)
posts = [] posts = []
posts << Factory.create(:post, :tag_string => "aaa") posts << Factory.create(:post, :tag_string => "aaa")
posts << Factory.create(:post, :tag_string => "bbb") posts << Factory.create(:post, :tag_string => "bbb")