-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels+Contact.go
57 lines (49 loc) · 1.61 KB
/
Models+Contact.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
package main
import "time"
func (me contact) isCurrent() bool {
return me.ID == activeContactID
}
func (me contact) fetchSpacemail() []spacemail {
var mails []spacemail
filter := spacemail{ContactID: me.ID}
database.Where(&filter).Order("date asc").Find(&mails)
return mails
}
func (me contact) numContactUnread() int {
var numUnread uint
database.Model(&spacemail{}).Where("contact_id = ? and inbound = ? and read = ?", me.ID, false, false).Count(&numUnread)
return int(numUnread)
}
func (me contact) sendMessageToContact(text string) {
mail := spacemail{CrewID: me.OwnerID, ContactID: me.ID, Inbound: false, Read: false, Date: int(time.Now().Unix()), Text: text}
database.Create(&mail)
if me.CrewID != 0 {
var mirrorcontact contact
database.Where(&contact{OwnerID: me.CrewID, CrewID: me.OwnerID}).First(&mirrorcontact)
mirrorcontact.sendMessageToCrew(text)
}
if me.isCurrent() {
output(func(print printer) { print(mail.toString()) })
}
}
func (me contact) sendMessageToCrew(text string) {
mirrormail := spacemail{CrewID: me.OwnerID, ContactID: me.ID, Inbound: true, Read: false, Date: int(time.Now().Unix()), Text: text}
database.Create(&mirrormail)
if me.isCurrent() {
output(func(print printer) { print(mirrormail.toString()) })
}
var crew crew
database.Preload("Chat").First(&crew, me.OwnerID)
if crew.ChatID != 0 {
crew.Chat.sendMessage(mirrormail.toString())
}
}
func fetchContactByName(name string, crew crew) *contact {
search := contact{Name: name, OwnerID: crew.ID}
var found contact
database.Where(&search).First(&found)
if database.NewRecord(&found) {
return nil
}
return &found
}