summary refs log tree commit diff stats
path: root/registry/query.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-14 03:08:07 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-14 03:08:07 -0400
commit2492014051e5cd4c37287a09639d18f85e1349dc (patch)
treefa3af5377e10f6f6748390d1969b5879bc21593a /registry/query.go
parenta8ba50b53f45e06af03f5ef98e759539394a3c0c (diff)
downloadgetwtxt-2492014051e5cd4c37287a09639d18f85e1349dc.tar.gz
moved query functions to own file, made SortByTime query-agnostic
Diffstat (limited to 'registry/query.go')
-rw-r--r--registry/query.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/registry/query.go b/registry/query.go
new file mode 100644
index 0000000..e024bc6
--- /dev/null
+++ b/registry/query.go
@@ -0,0 +1,93 @@
+package registry
+
+import (
+	"sort"
+	"strings"
+	"time"
+)
+
+// QueryUser checks the user index for nicknames that contain the
+// nickname provided as an argument. Entries are returned sorted
+// by the date they were added to the index.
+func (index UserIndex) QueryUser(name string) []string {
+	var timekey = map[time.Time]string{}
+	var keys TimeSlice
+	var users []string
+	imutex.RLock()
+	for k, v := range index {
+		if strings.Contains(v.nick, name) {
+			timekey[v.date] = v.nick + "\t" + k + "\t" + string(v.apidate)
+			keys = append(keys, v.date)
+		}
+	}
+	imutex.RUnlock()
+	sort.Sort(keys)
+	for _, e := range keys {
+		users = append(users, timekey[e])
+	}
+
+	return users
+}
+
+// QueryTag returns all the known statuses that
+// contain the provided tag.
+func (index UserIndex) QueryTag(tag string) []string {
+	var statusmap TimeMapSlice
+	i := 0
+	imutex.RLock()
+	for _, v := range index {
+		statusmap[i] = v.FindTag(tag)
+		i++
+	}
+	imutex.RUnlock()
+
+	return statusmap.SortByTime()
+}
+
+// FindTag takes a user's tweets and looks for a given tag.
+// Returns the tweets with the tag as a []string.
+func (userdata *Data) FindTag(tag string) TimeMap {
+	var statuses TimeMap
+	for k, e := range userdata.status {
+		parts := strings.Split(e, "\t")
+		statusslice := strings.Split(parts[3], " ")
+		for _, v := range statusslice {
+			if v[1:] == tag {
+				statuses[k] = e
+				break
+			}
+		}
+	}
+
+	return statuses
+}
+
+// SortByTime returns a string slice of the query results
+// sorted by time.Time
+func (tm TimeMapSlice) SortByTime() []string {
+	var unionmap TimeMap
+	var times TimeSlice
+	var data []string
+	for _, e := range tm {
+		for k, v := range e {
+			unionmap[k] = v
+		}
+	}
+	for k := range unionmap {
+		times = append(times, k)
+	}
+	sort.Sort(times)
+	for _, e := range times {
+		data = append(data, unionmap[e])
+	}
+
+	return data
+}
+
+// GetStatuses returns the string slice containing a user's statuses
+func (index UserIndex) GetStatuses(url string) TimeMap {
+	imutex.RLock()
+	status := index[url].status
+	imutex.RUnlock()
+	return status
+}