This repository was archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathusercache.go
More file actions
88 lines (71 loc) · 2.09 KB
/
Copy pathusercache.go
File metadata and controls
88 lines (71 loc) · 2.09 KB
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
85
86
87
88
package usercache
import (
"fmt"
"strconv"
"time"
"github.com/riking/marvin"
"github.com/riking/marvin/slack"
"github.com/riking/marvin/slack/rtm"
)
// interface duplicated in rtm package
type API interface {
marvin.Module
GetEntry(userid slack.UserID) (slack.User, error)
LoadEntries() error
UpdateEntry(userobject *slack.User) error
UpdateEntries(userobjects []*slack.User) error
}
var _ API = &UserCacheModule{}
// ---
func init() {
marvin.RegisterModule(NewUserCacheModule)
}
const Identifier = "usercache"
type UserCacheModule struct {
team marvin.Team
}
func NewUserCacheModule(t marvin.Team) marvin.Module {
mod := &UserCacheModule{
team: t,
}
return mod
}
func (mod *UserCacheModule) Identifier() marvin.ModuleID {
return Identifier
}
func (mod *UserCacheModule) Load(t marvin.Team) {
t.DB().MustMigrate(Identifier, 1505192548, sqlMigrate1)
t.DB().SyntaxCheck(sqlGetAllEntries, sqlGetEntry, sqlUpsertEntry)
t.ModuleConfig(Identifier).Add("last-timestamp", "0")
t.ModuleConfig(Identifier).Add("delay", (72 * time.Hour).String())
}
func (mod *UserCacheModule) Enable(team marvin.Team) {
go func() {
fmt.Printf("Loading user cache entries....\n")
err := mod.LoadEntries()
if err != nil {
fmt.Printf("Error whilst updating entries: %s\n", err.Error())
return
}
fmt.Printf("Loaded all entries from the user cache.\n")
go mod.UpdateTask()
}()
}
func (mod *UserCacheModule) Disable(t marvin.Team) {
}
func (mod *UserCacheModule) UpdateTask() {
rtmClient := mod.team.GetRTMClient().(*rtm.Client)
for {
timestr, _, _ := mod.team.ModuleConfig(Identifier).GetIsDefault("last-timestamp")
delaystr, _, _ := mod.team.ModuleConfig(Identifier).GetIsDefault("delay")
timeint, _ := strconv.ParseInt(timestr, 10, 64)
var timeres = time.Unix(timeint, 0)
delayres, err := time.ParseDuration(delaystr)
if err != nil || timeres.Before(time.Now().Add(-delayres)) {
fmt.Printf("Repolling user list....\n")
go rtmClient.FillUsersList()
err = mod.team.ModuleConfig(Identifier).Set("last-timestamp", strconv.FormatInt(time.Now().Unix(), 10))
}
time.Sleep(1 * time.Hour)
}
}