summary refs log tree commit diff stats
path: root/registry/query.go
blob: 26d61c44f30e83d085f19756e68e1f7141e271e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package registry // import "github.com/gbmor/getwtxt/registry"

import (
	"sort"
	"strings"
)

// 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 {
	timekey := NewTimeMap()
	keys := make(TimeSlice, 0)
	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 {
	statusmap := NewTimeMapSlice()
	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 {
	statuses := NewTimeMap()
	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 = NewTimeMap()
	var times = make(TimeSlice, 0)
	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
}