-
Notifications
You must be signed in to change notification settings - Fork 0
/
conference.go
222 lines (198 loc) · 5.96 KB
/
conference.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"time"
)
type conference struct {
InvolvedCrews []crew
InvolvedNSCs []contact
RingingCrews []crew
}
type conferenceMgr struct {
Conferences []conference
}
var conferences conferenceMgr
func setupConferenceMgr() {
conferences.setup()
}
func (mgr *conferenceMgr) setup() {
mgr.Conferences = make([]conference, 0)
}
func (mgr *conferenceMgr) call(origin crew, crews []crew, nscs []contact) {
originworker := getWorker(origin.ChatID)
originworker.Chat.OpenConnection = true
database.Save(&originworker.Chat)
conference := conference{InvolvedCrews: []crew{origin}, InvolvedNSCs: nscs, RingingCrews: crews}
for _, crew := range conference.RingingCrews {
worker := getWorker(crew.ChatID)
worker.Chat.OpenConnection = true
database.Save(&worker.Chat)
worker.setCommandSet([]botCommand{
botHelpCmd{},
botAcceptCmd{},
botRejectCmd{},
})
worker.Chat.sendMessage(fmt.Sprintf("Eingehende Konferenzanfrage von %s. Mit /accept annehmen, mit /reject ablehnen.", origin.Name))
}
for _, contact := range nscs {
conference.transmitForContact(contact, "*SYSTEM* Verbindung hergestellt.")
}
mgr.Conferences = append(mgr.Conferences, conference)
}
func (mgr *conferenceMgr) findConferenceForCrew(crew crew) *conference {
for i, conference := range mgr.Conferences {
for _, participatingcrew := range conference.InvolvedCrews {
if participatingcrew.ID == crew.ID {
return &mgr.Conferences[i]
}
}
for _, ringingcrew := range conference.RingingCrews {
if ringingcrew.ID == crew.ID {
return &mgr.Conferences[i]
}
}
}
return nil
}
func (mgr *conferenceMgr) findConferenceForContact(contact contact) *conference {
for i, conference := range mgr.Conferences {
for _, oncall := range conference.InvolvedNSCs {
if oncall.ID == contact.ID {
return &mgr.Conferences[i]
}
}
}
return nil
}
func (mgr *conferenceMgr) acceptCall(crew crew) {
conference := mgr.findConferenceForCrew(crew)
if conference == nil {
return
}
conference.accept(crew)
}
func (mgr *conferenceMgr) rejectCall(crew crew) {
conference := mgr.findConferenceForCrew(crew)
if conference == nil {
return
}
conference.reject(crew)
}
func (mgr *conferenceMgr) hangup(crew crew) {
conference := mgr.findConferenceForCrew(crew)
if conference == nil {
return
}
conference.hangup(crew)
if len(conference.InvolvedCrews) == 0 {
for _, crew := range conference.RingingCrews {
conference.reject(crew)
}
for i, conf := range mgr.Conferences {
if len(conf.InvolvedCrews) == 0 && len(conf.RingingCrews) == 0 {
mgr.Conferences = append(mgr.Conferences[:i], mgr.Conferences[i+1:]...)
}
}
}
}
func (mgr *conferenceMgr) isCrewInOngoingCall(crew crew) bool {
return mgr.findConferenceForCrew(crew) != nil
}
func (mgr *conferenceMgr) isContactInOngoingCall(contact contact) bool {
return mgr.findConferenceForContact(contact) != nil
}
func (mgr *conferenceMgr) transmitFromCrew(crew crew, text string) {
conference := mgr.findConferenceForCrew(crew)
if conference != nil {
conference.transmitForCrew(crew, text)
}
}
func (mgr *conferenceMgr) transmitFromContact(contact contact, text string) {
conference := mgr.findConferenceForContact(contact)
if conference != nil {
conference.transmitForContact(contact, text)
}
}
// conference API
func (cf *conference) accept(crew crew) {
for i, ringing := range cf.RingingCrews {
if ringing.ID == crew.ID {
cf.RingingCrews = append(cf.RingingCrews[:i], cf.RingingCrews[i+1:]...)
cf.InvolvedCrews = append(cf.InvolvedCrews, crew)
worker := getWorker(crew.ChatID)
worker.setCommandSet([]botCommand{
botHelpCmd{},
botHangupCmd{},
})
return
}
}
}
func (cf *conference) reject(crew crew) {
for i, calling := range cf.RingingCrews {
if calling.ID == crew.ID {
var chat chat
database.First(&chat, crew.ChatID)
chat.OpenConnection = false
database.Save(&chat)
cf.transmitForCrew(crew, "*SYSTEM* Verbindungsaufbau abgelehnt.")
cf.RingingCrews = append(cf.RingingCrews[:i], cf.RingingCrews[i+1:]...)
worker := getWorker(crew.ChatID)
worker.setDefaultCommandSet()
return
}
}
}
func (cf *conference) hangup(crew crew) {
for i, calling := range cf.InvolvedCrews {
if calling.ID == crew.ID {
var chat chat
database.First(&chat, crew.ChatID)
chat.OpenConnection = false
database.Save(&chat)
cf.InvolvedCrews = append(cf.InvolvedCrews[:i], cf.InvolvedCrews[i+1:]...)
cf.transmitForCrew(crew, "*SYSTEM* Verbindung beendet.")
worker := getWorker(crew.ChatID)
worker.setDefaultCommandSet()
return
}
}
}
func (cf *conference) transmitForCrew(crew crew, text string) {
for _, oncall := range cf.InvolvedCrews {
if oncall.ID != crew.ID && oncall.ChatID != 0 {
var chat chat
database.First(&chat, oncall.ChatID)
chat.sendMessage(fmt.Sprintf("<%s> %s", crew.Name, text))
}
}
for _, contact := range cf.InvolvedNSCs {
spacemail := spacemail{CrewID: crew.ID, ContactID: contact.ID, Text: text, Inbound: false, Read: false, Date: int(time.Now().Unix())}
database.Create(&spacemail)
updateSidebar()
if contact.ID == activeContactID {
output(func(print printer) { print(spacemail.toString()) })
}
}
}
func (cf *conference) transmitForContact(contact contact, text string) {
protocol := spacemail{CrewID: contact.OwnerID, ContactID: contact.ID, Text: text, Date: int(time.Now().Unix()), Inbound: true, Read: true}
database.Create(&protocol)
for _, oncall := range cf.InvolvedCrews {
if oncall.ChatID != 0 {
var chat chat
database.First(&chat, oncall.ChatID)
chat.sendMessage(fmt.Sprintf("<%s> %s", contact.Name, text))
}
}
for _, oncall := range cf.InvolvedNSCs {
if oncall.ID != contact.ID {
spacemail := spacemail{CrewID: oncall.OwnerID, ContactID: oncall.ID, Text: fmt.Sprintf("<%s> %s", contact.Name, text), Inbound: false, Read: false, Date: int(time.Now().Unix())}
database.Create(&spacemail)
updateSidebar()
if contact.ID == activeContactID {
output(func(print printer) { print(spacemail.toString()) })
}
}
}
}