Skip to content

Commit 76062d2

Browse files
feat: add Subject search and wildcard support
Adds support for searching by subject and using % as wildcard in to, from, and subject search fields. Cherry-picked from postalserver/postal PR postalserver#3399 Co-authored-by: matige <matige@users.noreply.github.com> Co-authored-by: openhands <openhands@all-hands.dev>
1 parent a86f2a2 commit 76062d2

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

app/controllers/messages_controller.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ def get_messages(scope)
170170
options[:direction] = "asc"
171171
end
172172

173-
options[:where][:rcpt_to] = qs[:to] if qs[:to]
174-
options[:where][:mail_from] = qs[:from] if qs[:from]
175173
options[:where][:status] = qs[:status] if qs[:status]
176174
options[:where][:token] = qs[:token] if qs[:token]
177175

@@ -180,6 +178,15 @@ def get_messages(scope)
180178
options[:where].delete(:spam)
181179
options[:where].delete(:scope)
182180
end
181+
[[:subject, :subject], [:to, :rcpt_to], [:from, :mail_from]].each do |qs_key, where_key|
182+
if qs[qs_key]
183+
if qs[qs_key].include?('%')
184+
options[:where][where_key] = { like: qs[qs_key] }
185+
else
186+
options[:where][where_key] = qs[qs_key]
187+
end
188+
end
189+
end
183190
options[:where][:tag] = qs[:tag] if qs[:tag]
184191
options[:where][:id] = qs[:id] if qs[:id]
185192
options[:where][:spam] = true if qs[:spam] == "yes" || qs[:spam] == "y"

app/views/messages/_search.html.haml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
as shown opposite into the box above and press enter.
1414
.messageSearch__right
1515
%dl.messageSearch__definition
16-
%dt to: rachel@example.com
17-
%dd Returns all mail addressed to the address provided.
16+
%dt to: rachel@example.com or to: %rachel%
17+
%dd Returns all mail addressed to the address provided. Use % as wildcard anywhere.
1818
%dl.messageSearch__definition
19-
%dt from: tom@example.com
20-
%dd Returns all mail sent from to the address provided.
19+
%dt from: tom@example.com or from: %@example.com
20+
%dd Returns all mail sent from to the address provided. Use % as wildcard anywhere.
21+
%dl.messageSearch__definition
22+
%dt subject: "any string" or subject: "%any string%"
23+
%dd Returns all mail which subject contains the substring provided. Use % as wildcard anywhere.
2124
%dl.messageSearch__definition
2225
%dt status: pending
2326
%dd Returns all messages with the status provided. The suitable statuses are: <code>pending</code>, <code>sent</code>, <code>held</code>, <code>softfail</code>, <code>hardfail</code> and <code>bounced</code>.

lib/postal/message_db/database.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ def hash_to_sql(hash, joiner = ", ")
368368
sql << "`#{key}` <= #{escape(inner_value)}"
369369
when :greater_than_or_equal_to
370370
sql << "`#{key}` >= #{escape(inner_value)}"
371+
when :like
372+
sql << "`#{key}` LIKE #{escape(inner_value)}"
371373
end
372374
end
373375
sql.empty? ? "1=1" : sql.join(joiner)

0 commit comments

Comments
 (0)