-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels+Chat.go
53 lines (45 loc) · 1.41 KB
/
Models+Chat.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
package main
import (
"fmt"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func (this_chat chat) hasUnread() bool {
var unread uint
database.Model(&message{}).Where("chat_id = ? and read = ?", this_chat.ID, false).Count(&unread)
return unread > 0
}
func (this_chat chat) isLinked() bool {
var count uint
database.Model(&crew{}).Where("chat_id = ?", this_chat.ID).Count(&count)
return count > 0
}
func (this_chat chat) FetchCrew() crew {
var foundCrew crew
database.Model(&crew{}).Where("chat_id = ?", this_chat.ID).First(&foundCrew)
return foundCrew
}
func (this_chat chat) isCurrent() bool {
return this_chat.ID == activeChatID
}
func (this_chat chat) fetchMessages() []message {
var messages []message
database.Model(&message{}).Where(&message{ChatID: this_chat.ID}).Order("id asc").Find(&messages)
return messages
}
func (this_chat chat) sendMessage(text string) {
msg := tgbotapi.NewMessage(this_chat.TelegramChatID, text)
tgbot.Send(msg)
newmsg := message{Inbound: false, ChatID: this_chat.ID, Text: text, Read: true, Date: int(time.Now().Unix())}
database.Create(&newmsg)
if activeChatID == this_chat.ID {
output(func(print printer) { print(newmsg.toString()) })
}
if this_chat.Admin == false {
var chats []chat
database.Where(&chat{Admin: true}).Find(&chats)
for _, admin := range chats {
admin.sendMessage(fmt.Sprintf("[O] <%s> %s", this_chat.TelegramUserName, text))
}
}
}