lib/ezmlm/list/thread.rb
author Mahlon E. Smith <mahlon@laika.com>
Fri, 12 May 2017 11:09:36 -0700
changeset 15 a38e6916504c
child 17 23c7f5c8ee39
permissions -rw-r--r--
Everything is workin! - Add a corpus of test messages to the spec data, with the script that generated them. - Add native objects for Authors, Messages, and Threads. - Tests!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     1
#!/usr/bin/ruby
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     2
# vim: set nosta noet ts=4 sw=4:
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     4
# A collection of messages for a specific archive thread.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     5
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     6
#    thread = Ezmlm::List::Thread.new( list, 'acgcbmbmeapgpfckcdol' )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     7
#    thread.subject         #=> "Help - navigate on interface?"
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     8
#    thread.first.date.to_s #=> "2017-05-07T14:55:05-07:00"
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     9
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    10
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    11
# == Version
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    12
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    13
#  $Id$
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    14
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    15
#---
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    16
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
require 'pathname'
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    18
require 'ezmlm' unless defined?( Ezmlm )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    20
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
### A collection of messages for a specific archive thread.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    23
class Ezmlm::List::Thread
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    24
	include Enumerable
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
	### Instantiate a new thread of messages given
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
	### a +list+ and a +thread_id+.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    29
	def initialize( list, thread_id )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    30
		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    31
		raise ArgumentError, "Malformed Thread ID." unless thread_id =~ /^\w{20}$/
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
		raise "Thread indexing is not enabled." unless list.threaded?
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    33
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    34
		@list     = list
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
		@id       = thread_id
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    36
		@subject  = nil
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    37
		@messages = nil
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
		self.load_thread
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    41
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    42
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    43
	# The list object this message is stored in.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
	attr_reader :list
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    46
	# The thread's identifier.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
	attr_reader :id
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    48
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
	# The subject line of the thread.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
	attr_reader :subject
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    51
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
	# An array of member messages.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
	attr_reader :messages
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
	# An array of member authors.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
	attr_reader :authors
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
	### Enumerable API:  Lazy load each message ID as a
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
	### Ezmlm::List::Message, yielding it to the block.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
	def each
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
		self.load_thread # refresh for any thread updates since object was created
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
		self.messages.each do |id|
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
			yield Ezmlm::List::Message.new( self.list, id )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
		end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    68
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
	#########
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
	protected
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
	#########
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    74
	### Parse the subject index into an array of Messages.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    75
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
	def load_thread
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
		@messages = []
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
		@authors  = []
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
		path = self.thread_path
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
		raise "Unknown thread: %p" % [ self.id ] unless path.exist?
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
		path.each_line.with_index do |line, i|
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
			if i.zero?
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
				@subject = line.match( /^\w+ (.+)/ )[1]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
			else
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
				match = line.match( /^(\d+):\d+:(\w+) / ) or next
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
				self.messages << match[1].to_i
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    88
				self.authors  << match[2]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    89
			end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
		end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    94
	### Return the path on disk for the thread index.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    96
	def thread_path
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    97
		prefix = self.id[ 0 ..  1 ]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    98
		hash   = self.id[ 2 .. -1 ]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    99
		return self.list.listdir + 'archive' + 'subjects' + prefix + hash
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   101
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
end # class Ezmlm::List::Thread