lib/ezmlm/list/author.rb
changeset 15 a38e6916504c
child 17 23c7f5c8ee39
equal deleted inserted replaced
14:cba9fb39bcdb 15:a38e6916504c
       
     1 #!/usr/bin/ruby
       
     2 # vim: set nosta noet ts=4 sw=4:
       
     3 #
       
     4 # A collection of messages authored from a unique user.
       
     5 #
       
     6 # Note that Ezmlm uses the "real name" part of an address
       
     7 # to identify an author.
       
     8 #
       
     9 #    author = Ezmlm::List::Author.new( list, 'acgcbmbmeapgpfckcdol' )
       
    10 #    author.name            #=> "Help - navigate on interface?"
       
    11 #    author.first.date.to_s #=> "2017-05-07T14:55:05-07:00"
       
    12 #
       
    13 #
       
    14 # == Version
       
    15 #
       
    16 #  $Id$
       
    17 #
       
    18 #---
       
    19 
       
    20 require 'pathname'
       
    21 require 'ezmlm' unless defined?( Ezmlm )
       
    22 
       
    23 
       
    24 ### A collection of messages for a specific author.
       
    25 ###
       
    26 class Ezmlm::List::Author
       
    27 	include Enumerable
       
    28 
       
    29 	### Instantiate a new list of messages given
       
    30 	### a +list+ and a +author_id+.
       
    31 	###
       
    32 	def initialize( list, author_id )
       
    33 		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
       
    34 		raise ArgumentError, "Malformed Author ID." unless author_id =~ /^\w{20}$/
       
    35 		raise "Thread indexing is not enabled." unless list.threaded?
       
    36 
       
    37 		@list     = list
       
    38 		@id       = author_id
       
    39 		@messages = nil
       
    40 
       
    41 		self.load_index
       
    42 	end
       
    43 
       
    44 
       
    45 	# The list object this message is stored in.
       
    46 	attr_reader :list
       
    47 
       
    48 	# The author's identifier.
       
    49 	attr_reader :id
       
    50 
       
    51 	# The author's name.
       
    52 	attr_reader :name
       
    53 
       
    54 	# An array of messages this author has sent.
       
    55 	attr_reader :messages
       
    56 
       
    57 	# An array of threads this author has participated in.
       
    58 	attr_reader :threads
       
    59 
       
    60 
       
    61 	### Enumerable API:  Lazy load each message ID as a
       
    62 	### Ezmlm::List::Message, yielding it to the block.
       
    63 	###
       
    64 	def each
       
    65 		self.load_index # refresh for any updates since object was created
       
    66 		self.messages.each do |id|
       
    67 			yield Ezmlm::List::Message.new( self.list, id )
       
    68 		end
       
    69 	end
       
    70 
       
    71 
       
    72 	### Lazy load each thread ID as a Ezmlm::List::Thread, yielding it to the block.
       
    73 	###
       
    74 	def each_thread
       
    75 		self.load_index # refresh for any updates since object was created
       
    76 		self.threads.each do |id|
       
    77 			yield Ezmlm::List::Thread.new( self.list, id )
       
    78 		end
       
    79 	end
       
    80 
       
    81 
       
    82 	#########
       
    83 	protected
       
    84 	#########
       
    85 
       
    86 	### Parse the author index into an array of Messages.
       
    87 	###
       
    88 	def load_index
       
    89 		@messages = []
       
    90 		@threads  = []
       
    91 
       
    92 		path = self.author_path
       
    93 		raise "Unknown author: %p" % [ self.id ] unless path.exist?
       
    94 
       
    95 		path.each_line.with_index do |line, i|
       
    96 			if i.zero?
       
    97 				@name = line.match( /^\w+ (.+)/ )[1]
       
    98 			else
       
    99 				match = line.match( /^(\d+):\d+:(\w+) / ) or next
       
   100 				self.messages << match[1].to_i
       
   101 				self.threads  << match[2]
       
   102 			end
       
   103 		end
       
   104 	end
       
   105 
       
   106 
       
   107 	### Return the path on disk for the author index.
       
   108 	###
       
   109 	def author_path
       
   110 		prefix = self.id[ 0 ..  1 ]
       
   111 		hash   = self.id[ 2 .. -1 ]
       
   112 		return self.list.listdir + 'archive' + 'authors' + prefix + hash
       
   113 	end
       
   114 
       
   115 end # class Ezmlm::List::Author