aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormms <git@sapka.me>2024-11-22 23:35:51 +0100
committermms <git@sapka.me>2024-11-22 23:37:42 +0100
commitc2623f1aa3638c988026b28f6a4131df6c61e0c6 (patch)
treec9a685d2997214bcb90f3659febc56f7ac85cbbb /lib
parent64ab548e16fdc8fa862e2b432464f3d67f5d9db8 (diff)
feat: allow to search for thread
Diffstat (limited to 'lib')
-rw-r--r--lib/.chotto.rb.swpbin12288 -> 0 bytes
-rw-r--r--lib/chotto.rb5
-rw-r--r--lib/chotto/message.rb10
-rw-r--r--lib/chotto/message_thread.rb22
-rw-r--r--lib/chotto/messages.rb76
-rw-r--r--lib/chotto/ruleset.rb4
-rw-r--r--lib/chotto/token.rb12
-rw-r--r--lib/chotto/token_group.rb30
8 files changed, 106 insertions, 53 deletions
diff --git a/lib/.chotto.rb.swp b/lib/.chotto.rb.swp
deleted file mode 100644
index 288ee5d..0000000
--- a/lib/.chotto.rb.swp
+++ /dev/null
Binary files differ
diff --git a/lib/chotto.rb b/lib/chotto.rb
index 62e9a0d..61f149b 100644
--- a/lib/chotto.rb
+++ b/lib/chotto.rb
@@ -9,6 +9,9 @@ require_relative 'chotto/helpers'
require_relative 'chotto/message'
require_relative 'chotto/messages'
require_relative 'chotto/ruleset'
+require_relative 'chotto/token'
+require_relative 'chotto/token_group'
+require_relative 'chotto/message_thread'
module Chotto
class << self
@@ -34,7 +37,7 @@ module Chotto
end
def rule_set(name, &block)
- @rule_sets << RuleSet.new(name, db, block)
+ @rule_sets << RuleSet.new(name, db, config.only_new, block)
end
def close_db
diff --git a/lib/chotto/message.rb b/lib/chotto/message.rb
index 282a030..cc3a134 100644
--- a/lib/chotto/message.rb
+++ b/lib/chotto/message.rb
@@ -2,11 +2,12 @@
module Chotto
class Message
+ attr_reader :message, :messages, :db
attr_accessor :tags
- attr_reader :message
- def initialize(msg:)
+ def initialize(msg:, db:)
@message = msg
+ @db = db
@tags = @message.tags
end
@@ -21,6 +22,11 @@ module Chotto
end
end
+ def thread
+ thread_id = message.thread_id
+ MessageThread.new(thread_id: thread_id, db: db)
+ end
+
private
def handle_get_header(header_name)
diff --git a/lib/chotto/message_thread.rb b/lib/chotto/message_thread.rb
new file mode 100644
index 0000000..87227cd
--- /dev/null
+++ b/lib/chotto/message_thread.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+module Chotto
+ class MessageThread
+ attr_reader :thread_id, :db
+
+ def initialize(thread_id:, db:)
+ @thread_id = thread_id
+ @db = db
+ end
+
+ def each
+ db.search_messages("thread:#{thread_id}").each do |msg|
+ yield(
+ Message.new(
+ msg: msg,
+ db: db
+ ))
+ end
+ end
+ end
+end
diff --git a/lib/chotto/messages.rb b/lib/chotto/messages.rb
index 0d48c72..ce2cb55 100644
--- a/lib/chotto/messages.rb
+++ b/lib/chotto/messages.rb
@@ -1,59 +1,26 @@
# frozen_string_literal: true
module Chotto
- class Messages
- AND_CONJUCTION = :and
- OR_CONJUCTION = :or
- DEFAULT_CONJUCTION = AND_CONJUCTION
- OLDEST_FIRST_ORDER = 'oldest_first'
- NEWEST_FIRST_ORDER = 'newest_first'
- DEFAULT_ORDER = OLDEST_FIRST_ORDER
-
- Token = Struct.new(:conjuction, :field, :value, :messages) do
- def to_query
- conjuction_to_use = messages.next_token_id > 1 ? conjuction : ''
- return "#{conjuction_to_use} (#{value})" if field == :direct
-
- "#{conjuction_to_use} (#{field}:#{value})"
- end
- end
-
- TokenGroup = Struct.new(:conjuction, :tokens, :messages) do
- def initialize(*)
- super
- end
+ AND_CONJUCTION = :and
+ OR_CONJUCTION = :or
+ DEFAULT_CONJUCTION = AND_CONJUCTION
+ OLDEST_FIRST_ORDER = 'oldest_first'
+ NEWEST_FIRST_ORDER = 'newest_first'
+ DEFAULT_ORDER = OLDEST_FIRST_ORDER
- def to_query
- tokens.flat_map(&:to_query).join(' ').to_s
- end
-
- def push_token(field, value)
- if value.is_a? String
- tokens.push(Token.new(conjuction, field, value, messages))
- elsif value.is_a? Array
- group = TokenGroup.new(OR_CONJUCTION, [], messages)
- value.each do |val|
- group.push_token(field, val)
- end
- tokens.push group
-
- end
- end
-
- def next_token_id
- @token_count += 1
- end
- end
-
- attr_accessor :query, :current_conjuction, :token_count, :order
+ class Messages
+ attr_accessor :query, :current_conjuction, :token_count, :order, :only_new
attr_reader :db
- def initialize(db:)
+ include Enumerable
+
+ def initialize(db:, only_new:)
@db = db
- @query = []
@current_conjuction = AND_CONJUCTION
@token_count = 0
@order = DEFAULT_ORDER
+ @only_new = only_new
+ clear_filter!
end
def or
@@ -77,6 +44,10 @@ module Chotto
self
end
+ def clear_filter!
+ @query = []
+ end
+
def newest_first
@order = NEWEST_FIRST_ORDER
@@ -91,7 +62,11 @@ module Chotto
def each
db.search_messages(query_string).each do |msg|
- yield(Message.new(msg: msg))
+ yield(
+ Message.new(
+ msg: msg,
+ db: db
+ ))
end
end
@@ -108,7 +83,12 @@ module Chotto
end
def query_string
- query.map(&:to_query).join(' ')
+ string_from_filters = query.map(&:to_query).join(' ')
+ if only_new
+ "tag:new AND (#{string_from_filters})"
+ else
+ string_from_filters
+ end
end
def next_token_id
diff --git a/lib/chotto/ruleset.rb b/lib/chotto/ruleset.rb
index 08c9dbd..b76da1e 100644
--- a/lib/chotto/ruleset.rb
+++ b/lib/chotto/ruleset.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true
module Chotto
- RuleSet = Struct.new(:name, :db, :rule) do
+ RuleSet = Struct.new(:name, :db, :only_new, :rule) do
def messages
- Messages.new(db: db)
+ Messages.new(db: db, only_new: only_new)
end
def run
diff --git a/lib/chotto/token.rb b/lib/chotto/token.rb
new file mode 100644
index 0000000..fe46877
--- /dev/null
+++ b/lib/chotto/token.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+module Chotto
+ Token = Struct.new(:conjuction, :field, :value, :messages) do
+ def to_query
+ conjuction_to_use = messages.next_token_id > 1 ? conjuction : ''
+ return "#{conjuction_to_use} (#{value})" if field == :direct
+
+ "#{conjuction_to_use} (#{field}:#{value})"
+ end
+ end
+end
diff --git a/lib/chotto/token_group.rb b/lib/chotto/token_group.rb
new file mode 100644
index 0000000..7f8085e
--- /dev/null
+++ b/lib/chotto/token_group.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module Chotto
+ TokenGroup = Struct.new(:conjuction, :tokens, :messages) do
+ def initialize(*)
+ super
+ end
+
+ def to_query
+ tokens.flat_map(&:to_query).join(' ').to_s
+ end
+
+ def push_token(field, value)
+ if value.is_a? String
+ tokens.push(Token.new(conjuction, field, value, messages))
+ elsif value.is_a? Array
+ group = TokenGroup.new(OR_CONJUCTION, [], messages)
+ value.each do |val|
+ group.push_token(field, val)
+ end
+ tokens.push group
+
+ end
+ end
+
+ def next_token_id
+ @token_count += 1
+ end
+ end
+end