summaryrefslogtreecommitdiffstats
path: root/model/search_params.go
diff options
context:
space:
mode:
authorDmitry Samuylov <dsamuylov@pharo.com>2018-08-28 13:09:32 -0400
committerHarrison Healey <harrisonmhealey@gmail.com>2018-08-28 13:09:32 -0400
commit42806ae965b861955235698f247df862fd655d09 (patch)
tree4a92f098976b089dc832f3f0dc5ecfc0392690ed /model/search_params.go
parent61e27beabc9804fdcf59ed9df2180802175a4f70 (diff)
downloadchat-42806ae965b861955235698f247df862fd655d09.tar.gz
chat-42806ae965b861955235698f247df862fd655d09.tar.bz2
chat-42806ae965b861955235698f247df862fd655d09.zip
Feature/search after before on (#9219)
* initial implementation of after, before, on search flags allowing to restrict the search to a specific day or a date range * missed setting beforeDate in SearchParams in one place * fixed condition when only flags are used for search without any plain terms * changed date format used for after/before/on flags to be in ISO8601 format as suggested in PR comments, added a helper function to pad month and day with zeroes allowing the user user either format, with or without leading zeroes * corrected expected compare to date setting for the TestParseDateFilterToTimeISO8601 test * fixed a bug for the scenario when you only have the date flags without any terms, added a couple of tests for that scenario * updated the date filter logic to use parameters to construct the query instead of simply appending strings together, as suggested in the pull request comments * added search unit test using date flags * added a helper function to create a test post with a createat date manually set, updated the test for search using date flags to create test posts with different createat dates to be able to better test the functionality * MM-11817 Add support for after/before/on search flags with Elasticsearch * add support to search posts to perform the search in context of the client's timezone when filtering by createat date using on: after: before: flags * updated tests to match the new signature
Diffstat (limited to 'model/search_params.go')
-rw-r--r--model/search_params.go73
1 files changed, 58 insertions, 15 deletions
diff --git a/model/search_params.go b/model/search_params.go
index 21aa69a0d..8ed1fccfb 100644
--- a/model/search_params.go
+++ b/model/search_params.go
@@ -16,11 +16,33 @@ type SearchParams struct {
IsHashtag bool
InChannels []string
FromUsers []string
+ AfterDate string
+ BeforeDate string
+ OnDate string
OrTerms bool
IncludeDeletedChannels bool
+ TimeZoneOffset int
}
-var searchFlags = [...]string{"from", "channel", "in"}
+// Returns the epoch timestamp of the start of the day specified by SearchParams.AfterDate
+func (p *SearchParams) GetAfterDateMillis() int64 {
+ date := ParseDateFilterToTime(p.AfterDate)
+ return GetStartOfDayMillis(date, p.TimeZoneOffset)
+}
+
+// Returns the epoch timestamp of the end of the day specified by SearchParams.BeforeDate
+func (p *SearchParams) GetBeforeDateMillis() int64 {
+ date := ParseDateFilterToTime(p.BeforeDate)
+ return GetEndOfDayMillis(date, p.TimeZoneOffset)
+}
+
+// Returns the epoch timestamps of the start and end of the day specified by SearchParams.OnDate
+func (p *SearchParams) GetOnDateMillis() (int64, int64) {
+ date := ParseDateFilterToTime(p.OnDate)
+ return GetStartOfDayMillis(date, p.TimeZoneOffset), GetEndOfDayMillis(date, p.TimeZoneOffset)
+}
+
+var searchFlags = [...]string{"from", "channel", "in", "before", "after", "on"}
func splitWords(text string) []string {
words := []string{}
@@ -101,7 +123,7 @@ func parseSearchFlags(input []string) ([]string, [][2]string) {
return words, flags
}
-func ParseSearchParams(text string) []*SearchParams {
+func ParseSearchParams(text string, timeZoneOffset int) []*SearchParams {
words, flags := parseSearchFlags(splitWords(text))
hashtagTermList := []string{}
@@ -120,6 +142,9 @@ func ParseSearchParams(text string) []*SearchParams {
inChannels := []string{}
fromUsers := []string{}
+ afterDate := ""
+ beforeDate := ""
+ onDate := ""
for _, flagPair := range flags {
flag := flagPair[0]
@@ -129,6 +154,12 @@ func ParseSearchParams(text string) []*SearchParams {
inChannels = append(inChannels, value)
} else if flag == "from" {
fromUsers = append(fromUsers, value)
+ } else if flag == "after" {
+ afterDate = value
+ } else if flag == "before" {
+ beforeDate = value
+ } else if flag == "on" {
+ onDate = value
}
}
@@ -136,29 +167,41 @@ func ParseSearchParams(text string) []*SearchParams {
if len(plainTerms) > 0 {
paramsList = append(paramsList, &SearchParams{
- Terms: plainTerms,
- IsHashtag: false,
- InChannels: inChannels,
- FromUsers: fromUsers,
+ Terms: plainTerms,
+ IsHashtag: false,
+ InChannels: inChannels,
+ FromUsers: fromUsers,
+ AfterDate: afterDate,
+ BeforeDate: beforeDate,
+ OnDate: onDate,
+ TimeZoneOffset: timeZoneOffset,
})
}
if len(hashtagTerms) > 0 {
paramsList = append(paramsList, &SearchParams{
- Terms: hashtagTerms,
- IsHashtag: true,
- InChannels: inChannels,
- FromUsers: fromUsers,
+ Terms: hashtagTerms,
+ IsHashtag: true,
+ InChannels: inChannels,
+ FromUsers: fromUsers,
+ AfterDate: afterDate,
+ BeforeDate: beforeDate,
+ OnDate: onDate,
+ TimeZoneOffset: timeZoneOffset,
})
}
// special case for when no terms are specified but we still have a filter
- if len(plainTerms) == 0 && len(hashtagTerms) == 0 && (len(inChannels) != 0 || len(fromUsers) != 0) {
+ if len(plainTerms) == 0 && len(hashtagTerms) == 0 && (len(inChannels) != 0 || len(fromUsers) != 0 || len(afterDate) != 0 || len(beforeDate) != 0 || len(onDate) != 0) {
paramsList = append(paramsList, &SearchParams{
- Terms: "",
- IsHashtag: false,
- InChannels: inChannels,
- FromUsers: fromUsers,
+ Terms: "",
+ IsHashtag: false,
+ InChannels: inChannels,
+ FromUsers: fromUsers,
+ AfterDate: afterDate,
+ BeforeDate: beforeDate,
+ OnDate: onDate,
+ TimeZoneOffset: timeZoneOffset,
})
}