11class TopicsController < ApplicationController
2- before_action :set_topic , only : [ :show , :message_batch , :aware , :read_all , :unread_all , :star , :unstar , :latest_patchset ]
2+ before_action :set_topic , only : [ :show , :message_batch , :attachments_sidebar , : aware, :read_all , :unread_all , :star , :unstar , :latest_patchset ]
33 before_action :require_authentication , only : [ :aware , :aware_bulk , :aware_all , :read_all , :unread_all , :star , :unstar ]
44
55 def index
@@ -57,8 +57,7 @@ def show
5757 . preload (
5858 :sender ,
5959 :sender_person ,
60- { sender_person : :default_alias } ,
61- :attachments
60+ { sender_person : :default_alias }
6261 )
6362
6463 @messages = messages_scope . order ( created_at : :asc )
@@ -76,7 +75,9 @@ def show
7675 load_star_state
7776 end
7877
79- @has_patches = @messages . any? { |msg | msg . attachments . any? ( &:patch_extension? ) }
78+ @has_patches =
79+ Attachment . joins ( :message ) . where ( messages : { topic_id : @topic . id } )
80+ . where ( "attachments.file_name LIKE '%.patch' OR attachments.file_name LIKE '%.diff'" ) . exists?
8081 if @has_patches
8182 latest_message = latest_patchset_message
8283 if latest_message
@@ -106,6 +107,19 @@ def message_batch
106107 render layout : false
107108 end
108109
110+ def attachments_sidebar
111+ message_columns = ( Message . column_names - %w[ body body_tsv ] ) . map { |c | "messages.#{ c } " }
112+ messages = @topic . messages
113+ . select ( message_columns )
114+ . preload ( :attachments )
115+ . order ( created_at : :asc )
116+
117+ @attachment_messages = messages . select { |msg | msg . attachments . any? }
118+ @message_numbers = messages . each_with_index . to_h { |msg , idx | [ msg . id , idx + 1 ] }
119+
120+ render layout : false
121+ end
122+
109123 def aware
110124 last_id = params [ :up_to_message_id ] . presence &.to_i || @topic . messages . maximum ( :id )
111125 ThreadAwareness . mark_until ( user : current_user , topic : @topic , until_message_id : last_id ) if last_id
@@ -362,10 +376,13 @@ def user_state_frame
362376
363377 def latest_patchset_message
364378 @latest_patchset_message ||= @topic . messages
365- . where ( id : Attachment . where ( message_id : @topic . messages . select ( :id ) )
379+ . where ( id : Attachment . joins ( :message )
380+ . where ( messages : { topic_id : @topic . id } )
381+ . where ( "attachments.file_name LIKE '%.patch' OR attachments.file_name LIKE '%.diff'" )
366382 . select ( :message_id ) )
367383 . order ( created_at : :desc )
368- . find { |msg | msg . attachments . any? ( &:patch_extension? ) }
384+ . preload ( :attachments )
385+ . first
369386 end
370387
371388 def set_topic
0 commit comments