-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomation.go
More file actions
77 lines (68 loc) · 2.1 KB
/
automation.go
File metadata and controls
77 lines (68 loc) · 2.1 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
package main
import "fmt"
type automationitem struct {
Chat chat
Message message
}
var automationqueue chan automationitem
var automationenabled map[uint]bool
var automationqueues map[uint]*automationworker
func setupAutomation() {
automationenabled = make(map[uint]bool)
automationqueues = make(map[uint]*automationworker)
automationqueue = make(chan automationitem, 100)
go automationWorker()
}
func automationWorker() {
for item := range automationqueue {
enabled, ok := automationenabled[item.Chat.ID]
if !ok {
automationenabled[item.Chat.ID] = true
enabled = true
}
if enabled {
if item.Chat.isLinked() == false {
//this is an unlinked chat. we have to check things first.
handleUnlinkedMessage(item)
} else {
//here we will handle linked accounts. As this got more complex we will spin off goroutines here, but not now.
worker := getWorker(item.Chat.ID)
worker.Queue <- item.Message
}
}
}
}
func getWorker(chatid uint) *automationworker {
worker, ok := automationqueues[chatid]
if !ok {
var chat chat
database.First(&chat, chatid)
worker = newWorker(chat)
automationqueues[chatid] = worker
}
return worker
}
func handleUnlinkedMessage(item automationitem) {
text := item.Message.Text
var foundCrew crew
database.Where(&crew{Code: text}).First(&foundCrew)
var settings globalSettings
database.First(&settings)
if text == "" || database.NewRecord(foundCrew) || foundCrew.Code != text {
item.Chat.sendMessage(settings.UnauthenticatedGreeting)
} else {
foundCrew.Chat = item.Chat
database.Save(&foundCrew)
updateSidebar()
item.Chat.sendMessage(fmt.Sprintf(settings.AuthenticatedGreeting, foundCrew.Name))
item.Chat.sendMessage(settings.AuthenticatedIntro)
item.Chat.sendMessage(foundCrew.Description)
item.Chat.sendMessage(fmt.Sprintf("Du verfügst über %.2f AU", foundCrew.balance()))
var count uint
database.Model(&spacemail{}).Where("crew_id = ? and inbound = ? and read = ?", foundCrew.ID, true, false).Count(&count)
if count > 0 {
item.Chat.sendMessage("Es gibt ungelesene Nachrichten.")
//TODO: should we resend those?
}
}
}