lib/ezmlm/list/thread.rb
changeset 17 23c7f5c8ee39
parent 15 a38e6916504c
child 20 9d59d30685cb
equal deleted inserted replaced
16:e135ccae6783 17:23c7f5c8ee39
     1 #!/usr/bin/ruby
     1 #!/usr/bin/ruby
     2 # vim: set nosta noet ts=4 sw=4:
     2 # vim: set nosta noet ts=4 sw=4:
     3 #
     3 
       
     4 
     4 # A collection of messages for a specific archive thread.
     5 # A collection of messages for a specific archive thread.
     5 #
     6 #
     6 #    thread = Ezmlm::List::Thread.new( list, 'acgcbmbmeapgpfckcdol' )
     7 #    thread = Ezmlm::List::Thread.new( list, 'acgcbmbmeapgpfckcdol' )
     7 #    thread.subject         #=> "Help - navigate on interface?"
     8 #    thread.subject         #=> "Help - navigate on interface?"
     8 #    thread.first.date.to_s #=> "2017-05-07T14:55:05-07:00"
     9 #    thread.first.date.to_s #=> "2017-05-07T14:55:05-07:00"
    27 	### a +list+ and a +thread_id+.
    28 	### a +list+ and a +thread_id+.
    28 	###
    29 	###
    29 	def initialize( list, thread_id )
    30 	def initialize( list, thread_id )
    30 		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
    31 		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
    31 		raise ArgumentError, "Malformed Thread ID." unless thread_id =~ /^\w{20}$/
    32 		raise ArgumentError, "Malformed Thread ID." unless thread_id =~ /^\w{20}$/
    32 		raise "Thread indexing is not enabled." unless list.threaded?
    33 		raise "Archiving is not enabled." unless list.archived?
    33 
    34 
    34 		@list     = list
    35 		@list     = list
    35 		@id       = thread_id
    36 		@id       = thread_id
    36 		@subject  = nil
    37 		@subject  = nil
    37 		@messages = nil
    38 		@messages = nil
    63 		self.load_thread # refresh for any thread updates since object was created
    64 		self.load_thread # refresh for any thread updates since object was created
    64 		self.messages.each do |id|
    65 		self.messages.each do |id|
    65 			yield Ezmlm::List::Message.new( self.list, id )
    66 			yield Ezmlm::List::Message.new( self.list, id )
    66 		end
    67 		end
    67 	end
    68 	end
       
    69 	alias_method :each_message, :each
       
    70 
       
    71 
       
    72 	### Lazy load each author ID as a Ezmlm::List::Author, yielding it
       
    73 	### to the block.
       
    74 	###
       
    75 	def each_author
       
    76 		self.load_thread # refresh for any thread updates since object was created
       
    77 		self.authors.each do |id|
       
    78 			yield Ezmlm::List::Author.new( self.list, id )
       
    79 		end
       
    80 	end
    68 
    81 
    69 
    82 
    70 	#########
    83 	#########
    71 	protected
    84 	protected
    72 	#########
    85 	#########
    77 		@messages = []
    90 		@messages = []
    78 		@authors  = []
    91 		@authors  = []
    79 		path = self.thread_path
    92 		path = self.thread_path
    80 		raise "Unknown thread: %p" % [ self.id ] unless path.exist?
    93 		raise "Unknown thread: %p" % [ self.id ] unless path.exist?
    81 
    94 
    82 		path.each_line.with_index do |line, i|
    95 		path.open( 'r', encoding: Encoding::ISO8859_1 ) do |fh|
    83 			if i.zero?
    96 			fh.each_line.with_index do |line, i|
    84 				@subject = line.match( /^\w+ (.+)/ )[1]
    97 				if i.zero?
    85 			else
    98 					@subject = line.match( /^\w+ (.+)/ )[1]
    86 				match = line.match( /^(\d+):\d+:(\w+) / ) or next
    99 				else
    87 				self.messages << match[1].to_i
   100 					match = line.match( /^(\d+):\d+:(\w+) / ) or next
    88 				self.authors  << match[2]
   101 					self.messages << match[1].to_i
       
   102 					self.authors  << match[2]
       
   103 				end
    89 			end
   104 			end
    90 		end
   105 		end
    91 	end
   106 	end
    92 
   107 
    93 
   108