|
1 | 1 | class TopicsController < ApplicationController |
2 | 2 | include DraftSidebarLoader |
3 | 3 |
|
4 | | - before_action :set_topic, only: [ :show, :message_batch, :attachments_sidebar, :patchsets_sidebar, :aware, :read_all, :unread_all, :star, :unstar, :latest_patchset, :summary, :messages ] |
5 | | - before_action :require_authentication, only: [ :aware, :read_all, :unread_all, :star, :unstar ] |
| 4 | + before_action :set_topic, only: [ :show, :message_batch, :attachments_sidebar, :patchsets_sidebar, :aware, :read_all, :unread_all, :star, :unstar, :ignore, :unignore, :latest_patchset, :summary, :messages ] |
| 5 | + before_action :require_authentication, only: [ :aware, :read_all, :unread_all, :star, :unstar, :ignore, :unignore ] |
6 | 6 |
|
7 | 7 | TOPIC_LIST_PRELOADS = [ :creator, { creator_person: :default_alias }, { last_sender_person: :default_alias } ].freeze |
8 | 8 |
|
9 | 9 | def index |
10 | 10 | @search_query = nil |
11 | 11 | base_query = Topic.includes(*TOPIC_LIST_PRELOADS) |
| 12 | + base_query = apply_default_ignore_filter(base_query) if user_signed_in? |
12 | 13 |
|
13 | 14 | apply_cursor_pagination(base_query) |
14 | 15 | preload_topic_participants |
@@ -228,6 +229,30 @@ def unstar |
228 | 229 | end |
229 | 230 | end |
230 | 231 |
|
| 232 | + def ignore |
| 233 | + TopicIgnore.find_or_create_by!(user: current_user, topic: @topic) |
| 234 | + respond_to do |format| |
| 235 | + format.turbo_stream { render :update_ignore_state } |
| 236 | + format.json { render json: { ignored: true } } |
| 237 | + format.html { redirect_to topic_path(@topic) } |
| 238 | + end |
| 239 | + rescue ActiveRecord::RecordNotUnique |
| 240 | + respond_to do |format| |
| 241 | + format.turbo_stream { render :update_ignore_state } |
| 242 | + format.json { render json: { ignored: true } } |
| 243 | + format.html { redirect_to topic_path(@topic) } |
| 244 | + end |
| 245 | + end |
| 246 | + |
| 247 | + def unignore |
| 248 | + TopicIgnore.where(user: current_user, topic: @topic).destroy_all |
| 249 | + respond_to do |format| |
| 250 | + format.turbo_stream { render :update_ignore_state } |
| 251 | + format.json { render json: { ignored: false } } |
| 252 | + format.html { redirect_to topic_path(@topic) } |
| 253 | + end |
| 254 | + end |
| 255 | + |
231 | 256 | def latest_patchset |
232 | 257 | latest_message = latest_patchset_message |
233 | 258 | return head :not_found unless latest_message |
@@ -569,6 +594,16 @@ def assign_branch_segments! |
569 | 594 | end |
570 | 595 | end |
571 | 596 |
|
| 597 | + # Starring always wins: an ignored-and-starred topic still appears. |
| 598 | + def apply_default_ignore_filter(base_query) |
| 599 | + user_id = current_user.id |
| 600 | + base_query.where( |
| 601 | + "topics.id NOT IN (SELECT topic_id FROM topic_ignores WHERE user_id = ?) " \ |
| 602 | + "OR topics.id IN (SELECT topic_id FROM topic_stars WHERE user_id = ?)", |
| 603 | + user_id, user_id |
| 604 | + ) |
| 605 | + end |
| 606 | + |
572 | 607 | def apply_cursor_pagination(base_query) |
573 | 608 | @viewing_since = viewing_since_param |
574 | 609 |
|
|
0 commit comments