This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
269 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ rvm: | |
notifications: | ||
email: false | ||
|
||
services: | ||
- elasticsearch | ||
|
||
before_install: | ||
- gem install bundler | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Monkey-patch the String class. | ||
class String | ||
# Random index in string. | ||
def random_index | ||
rand(size - 1) | ||
end | ||
|
||
# Random substring of string. | ||
def random_substring | ||
a = [0, 0].map { random_index }.sort | ||
self[a[0]..a[1]] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ minimal_password_length: 8 | |
|
||
blocked_usernames: | ||
- dummy | ||
|
||
elasticsearch: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,26 @@ | ||
# Search for resources. | ||
class Search | ||
module Search | ||
# Results for query. | ||
def self.for(mailbox, query) | ||
results = {} | ||
query = "%#{query}%" | ||
def self.for(mailbox, query, sql: !Settings.elasticsearch) | ||
haystack = ::Domain.managable(mailbox) | ||
haystack_ids = haystack.pluck(:id) | ||
|
||
haystack = Domain.managable(mailbox) | ||
query = "%#{query}%" if sql | ||
|
||
domains = haystack.where('name like ?', query) | ||
domains.each { |domain| results[domain] ||= [] } | ||
results = {} | ||
|
||
haystack.all.each do |domain| | ||
[:mailboxes, :aliases].each do |model| | ||
domain.send(model).where('username like ?', query).each do |result| | ||
results[result.domain] ||= [] | ||
results[result.domain] << result | ||
end | ||
Search::Domain.search(haystack, query, sql: sql).each do |domain| | ||
results[domain] ||= [] | ||
end | ||
|
||
[Alias, Mailbox].each do |model| | ||
a = model.where(domain_id: haystack_ids) | ||
Search::Address.search(a, query, sql: sql).each do |result| | ||
results[result.domain] ||= [] | ||
results[result.domain] << result | ||
end | ||
end | ||
|
||
results | ||
end | ||
|
||
# Random substring of string. | ||
def self.random_substring(string) | ||
x = [0, 0].map { random_index(string) }.sort | ||
string[x[0]..x[1]] | ||
end | ||
|
||
private | ||
|
||
# Random index in string. | ||
def self.random_index(string) | ||
rand(string.size - 1) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Search for aliases or mailboxes. | ||
module Search::Address | ||
@model = nil | ||
|
||
# Search either via SQL or indexed (depending on settings or argument). | ||
def self.search(haystack, needle, sql: !Settings.elasticsearch) | ||
@model = Object.const_get haystack.model_name | ||
|
||
if sql | ||
sql_search(haystack, needle) | ||
else | ||
indexed_search(haystack, needle) | ||
end | ||
end | ||
|
||
private | ||
|
||
# Search indexed. | ||
def self.indexed_search(haystack, needle) | ||
haystack.search(needle, fields: @model.const_get('SEARCH_FIELDS')) | ||
end | ||
|
||
# Search via SQL. | ||
def self.sql_search(haystack, needle) | ||
haystack.where('username like ?', needle) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Search for domains. | ||
module Search::Domain | ||
# Search either via SQL or indexed (depending on settings or argument). | ||
def self.search(haystack, needle, sql: !Settings.elasticsearch) | ||
if sql | ||
sql_search(haystack, needle) | ||
else | ||
indexed_search(haystack, needle) | ||
end | ||
end | ||
|
||
private | ||
|
||
# Search indexed. | ||
def self.indexed_search(haystack, needle) | ||
haystack.search(needle, fields: Domain::SEARCH_FIELDS) | ||
end | ||
|
||
# Search via SQL. | ||
def self.sql_search(haystack, needle) | ||
haystack.where('name like ?', needle) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.