DEV: Scope topic voting tables to avoid confusion with post voting
This commit is contained in:
parent
fdb1f98a96
commit
05396b6fb4
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
module DiscourseTopicVoting
|
module DiscourseTopicVoting
|
||||||
class CategorySetting < ActiveRecord::Base
|
class CategorySetting < ActiveRecord::Base
|
||||||
self.table_name = "discourse_voting_category_settings"
|
self.table_name = "topic_voting_category_settings"
|
||||||
|
|
||||||
belongs_to :category, inverse_of: :discourse_topic_voting_category_setting
|
belongs_to :category, inverse_of: :discourse_topic_voting_category_setting
|
||||||
|
|
||||||
|
@ -12,24 +12,24 @@ module DiscourseTopicVoting
|
||||||
|
|
||||||
def unarchive_votes
|
def unarchive_votes
|
||||||
DB.exec(<<~SQL, { category_id: self.category_id })
|
DB.exec(<<~SQL, { category_id: self.category_id })
|
||||||
UPDATE discourse_voting_votes
|
UPDATE topic_voting_votes
|
||||||
SET archive=false
|
SET archive=false
|
||||||
FROM topics
|
FROM topics
|
||||||
WHERE topics.category_id = :category_id
|
WHERE topics.category_id = :category_id
|
||||||
AND topics.deleted_at is NULL
|
AND topics.deleted_at is NULL
|
||||||
AND NOT topics.closed
|
AND NOT topics.closed
|
||||||
AND NOT topics.archived
|
AND NOT topics.archived
|
||||||
AND discourse_voting_votes.topic_id = topics.id
|
AND topic_voting_votes.topic_id = topics.id
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
def archive_votes
|
def archive_votes
|
||||||
DB.exec(<<~SQL, { category_id: self.category_id })
|
DB.exec(<<~SQL, { category_id: self.category_id })
|
||||||
UPDATE discourse_voting_votes
|
UPDATE topic_voting_votes
|
||||||
SET archive=true
|
SET archive=true
|
||||||
FROM topics
|
FROM topics
|
||||||
WHERE topics.category_id = :category_id
|
WHERE topics.category_id = :category_id
|
||||||
AND discourse_voting_votes.topic_id = topics.id
|
AND topic_voting_votes.topic_id = topics.id
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
#
|
#
|
||||||
# Table name: discourse_voting_category_settings
|
# Table name: topic_voting_category_settings
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
# category_id :integer
|
# category_id :integer
|
||||||
|
@ -50,5 +50,5 @@ end
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_discourse_voting_category_settings_on_category_id (category_id) UNIQUE
|
# index_topic_voting_category_settings_on_category_id (category_id) UNIQUE
|
||||||
#
|
#
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
module DiscourseTopicVoting
|
module DiscourseTopicVoting
|
||||||
class TopicVoteCount < ActiveRecord::Base
|
class TopicVoteCount < ActiveRecord::Base
|
||||||
self.table_name = "discourse_voting_topic_vote_count"
|
self.table_name = "topic_voting_topic_vote_count"
|
||||||
|
|
||||||
belongs_to :topic
|
belongs_to :topic
|
||||||
end
|
end
|
||||||
|
@ -10,7 +10,7 @@ end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
#
|
#
|
||||||
# Table name: discourse_voting_topic_vote_count
|
# Table name: topic_voting_topic_vote_count
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
# topic_id :integer
|
# topic_id :integer
|
||||||
|
@ -20,5 +20,5 @@ end
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_discourse_voting_topic_vote_count_on_topic_id (topic_id) UNIQUE
|
# index_topic_voting_topic_vote_count_on_topic_id (topic_id) UNIQUE
|
||||||
#
|
#
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
module DiscourseTopicVoting
|
module DiscourseTopicVoting
|
||||||
class Vote < ActiveRecord::Base
|
class Vote < ActiveRecord::Base
|
||||||
self.table_name = "discourse_voting_votes"
|
self.table_name = "topic_voting_votes"
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :topic
|
belongs_to :topic
|
||||||
|
@ -11,7 +11,7 @@ end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
#
|
#
|
||||||
# Table name: discourse_voting_votes
|
# Table name: topic_voting_votes
|
||||||
#
|
#
|
||||||
# id :bigint not null, primary key
|
# id :bigint not null, primary key
|
||||||
# topic_id :integer
|
# topic_id :integer
|
||||||
|
@ -22,5 +22,5 @@ end
|
||||||
#
|
#
|
||||||
# Indexes
|
# Indexes
|
||||||
#
|
#
|
||||||
# index_discourse_voting_votes_on_user_id_and_topic_id (user_id,topic_id) UNIQUE
|
# index_topic_voting_votes_on_user_id_and_topic_id (user_id,topic_id) UNIQUE
|
||||||
#
|
#
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "migration/table_dropper"
|
||||||
|
|
||||||
|
class RenameVotingTables < ActiveRecord::Migration[7.0]
|
||||||
|
def up
|
||||||
|
unless table_exists?(:topic_voting_topic_vote_count)
|
||||||
|
Migration::TableDropper.read_only_table(:discourse_voting_topic_vote_count)
|
||||||
|
execute <<~SQL
|
||||||
|
CREATE TABLE topic_voting_topic_vote_count
|
||||||
|
(LIKE discourse_voting_topic_vote_count INCLUDING ALL);
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
INSERT INTO topic_voting_topic_vote_count
|
||||||
|
SELECT *
|
||||||
|
FROM discourse_voting_topic_vote_count
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
ALTER SEQUENCE discourse_voting_topic_vote_count_id_seq
|
||||||
|
RENAME TO topic_voting_topic_vote_count_id_seq
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
ALTER SEQUENCE topic_voting_topic_vote_count_id_seq
|
||||||
|
OWNED BY topic_voting_topic_vote_count.id
|
||||||
|
SQL
|
||||||
|
|
||||||
|
add_index :topic_voting_topic_vote_count, :topic_id, unique: true
|
||||||
|
end
|
||||||
|
|
||||||
|
unless table_exists?(:topic_voting_votes)
|
||||||
|
Migration::TableDropper.read_only_table(:discourse_voting_votes)
|
||||||
|
execute <<~SQL
|
||||||
|
CREATE TABLE topic_voting_votes
|
||||||
|
(LIKE discourse_voting_votes INCLUDING ALL);
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
INSERT INTO topic_voting_votes
|
||||||
|
SELECT *
|
||||||
|
FROM discourse_voting_votes
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
ALTER SEQUENCE discourse_voting_votes_id_seq
|
||||||
|
RENAME TO topic_voting_votes_id_seq
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
ALTER SEQUENCE topic_voting_votes_id_seq
|
||||||
|
OWNED BY topic_voting_votes.id
|
||||||
|
SQL
|
||||||
|
|
||||||
|
add_index :topic_voting_votes, %i[user_id topic_id], unique: true
|
||||||
|
end
|
||||||
|
|
||||||
|
unless table_exists?(:topic_voting_category_settings)
|
||||||
|
Migration::TableDropper.read_only_table(:discourse_voting_category_settings)
|
||||||
|
execute <<~SQL
|
||||||
|
CREATE TABLE topic_voting_category_settings
|
||||||
|
(LIKE discourse_voting_category_settings INCLUDING ALL);
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
INSERT INTO topic_voting_category_settings
|
||||||
|
SELECT *
|
||||||
|
FROM discourse_voting_category_settings
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
ALTER SEQUENCE discourse_voting_category_settings_id_seq
|
||||||
|
RENAME TO topic_voting_category_settings_id_seq
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
ALTER SEQUENCE topic_voting_category_settings_id_seq
|
||||||
|
OWNED BY topic_voting_category_settings.id
|
||||||
|
SQL
|
||||||
|
|
||||||
|
add_index :topic_voting_category_settings, :category_id, unique: true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
raise ActiveRecord::IrreversibleMigration
|
||||||
|
end
|
||||||
|
end
|
|
@ -35,14 +35,14 @@ module DiscourseTopicVoting
|
||||||
count = self.votes.count
|
count = self.votes.count
|
||||||
|
|
||||||
DB.exec(<<~SQL, topic_id: self.id, votes_count: count)
|
DB.exec(<<~SQL, topic_id: self.id, votes_count: count)
|
||||||
INSERT INTO discourse_voting_topic_vote_count
|
INSERT INTO topic_voting_topic_vote_count
|
||||||
(topic_id, votes_count, created_at, updated_at)
|
(topic_id, votes_count, created_at, updated_at)
|
||||||
VALUES
|
VALUES
|
||||||
(:topic_id, :votes_count, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
(:topic_id, :votes_count, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||||
ON CONFLICT (topic_id) DO UPDATE SET
|
ON CONFLICT (topic_id) DO UPDATE SET
|
||||||
votes_count = :votes_count,
|
votes_count = :votes_count,
|
||||||
updated_at = CURRENT_TIMESTAMP
|
updated_at = CURRENT_TIMESTAMP
|
||||||
WHERE discourse_voting_topic_vote_count.topic_id = :topic_id
|
WHERE topic_voting_topic_vote_count.topic_id = :topic_id
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,15 +5,15 @@ module DiscourseTopicVoting
|
||||||
def list_voted_by(user)
|
def list_voted_by(user)
|
||||||
create_list(:user_topics) do |topics|
|
create_list(:user_topics) do |topics|
|
||||||
topics.joins(
|
topics.joins(
|
||||||
"INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id",
|
"INNER JOIN topic_voting_votes ON topic_voting_votes.topic_id = topics.id",
|
||||||
).where("discourse_voting_votes.user_id = ?", user.id)
|
).where("topic_voting_votes.user_id = ?", user.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_votes
|
def list_votes
|
||||||
create_list(:votes, unordered: true) do |topics|
|
create_list(:votes, unordered: true) do |topics|
|
||||||
topics.joins(
|
topics.joins(
|
||||||
"LEFT JOIN discourse_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
|
"LEFT JOIN topic_voting_topic_vote_count dvtvc ON dvtvc.topic_id = topics.id",
|
||||||
).order("COALESCE(dvtvc.votes_count,'0')::integer DESC, topics.bumped_at DESC")
|
).order("COALESCE(dvtvc.votes_count,'0')::integer DESC, topics.bumped_at DESC")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
12
plugin.rb
12
plugin.rb
|
@ -52,13 +52,13 @@ after_initialize do
|
||||||
if user
|
if user
|
||||||
result =
|
result =
|
||||||
result.select(
|
result.select(
|
||||||
"topics.*, COALESCE((SELECT 1 FROM discourse_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted",
|
"topics.*, COALESCE((SELECT 1 FROM topic_voting_votes WHERE user_id = #{user.id} AND topic_id = topics.id), 0) AS current_user_voted",
|
||||||
)
|
)
|
||||||
|
|
||||||
if options[:state] == "my_votes"
|
if options[:state] == "my_votes"
|
||||||
result =
|
result =
|
||||||
result.joins(
|
result.joins(
|
||||||
"INNER JOIN discourse_voting_votes ON discourse_voting_votes.topic_id = topics.id AND discourse_voting_votes.user_id = #{user.id}",
|
"INNER JOIN topic_voting_votes ON topic_voting_votes.topic_id = topics.id AND topic_voting_votes.user_id = #{user.id}",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -67,9 +67,9 @@ after_initialize do
|
||||||
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
|
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
|
||||||
result =
|
result =
|
||||||
result.joins(
|
result.joins(
|
||||||
"LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id",
|
"LEFT JOIN topic_voting_topic_vote_count ON topic_voting_topic_vote_count.topic_id = topics.id",
|
||||||
).reorder(
|
).reorder(
|
||||||
"COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC",
|
"COALESCE(topic_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -104,14 +104,14 @@ after_initialize do
|
||||||
|
|
||||||
register_search_advanced_filter(/^min_vote_count:(\d+)$/) do |posts, match|
|
register_search_advanced_filter(/^min_vote_count:(\d+)$/) do |posts, match|
|
||||||
posts.where(
|
posts.where(
|
||||||
"(SELECT votes_count FROM discourse_voting_topic_vote_count WHERE discourse_voting_topic_vote_count.topic_id = posts.topic_id) >= ?",
|
"(SELECT votes_count FROM topic_voting_topic_vote_count WHERE topic_voting_topic_vote_count.topic_id = posts.topic_id) >= ?",
|
||||||
match.to_i,
|
match.to_i,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
register_search_advanced_order(:votes) do |posts|
|
register_search_advanced_order(:votes) do |posts|
|
||||||
posts.reorder(
|
posts.reorder(
|
||||||
"COALESCE((SELECT dvtvc.votes_count FROM discourse_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = topics.id), 0) DESC",
|
"COALESCE((SELECT dvtvc.votes_count FROM topic_voting_topic_vote_count dvtvc WHERE dvtvc.topic_id = topics.id), 0) DESC",
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue