-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
138 lines (117 loc) · 2.94 KB
/
structs.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
package main
import (
"fmt"
"gopkg.in/irc.v3"
"strconv"
"time"
)
///
type User struct {
ID int64 `gorm:"primaryKey"`
Name string
}
type Channel struct {
ID int64 `gorm:"primaryKey"`
Name string
}
type Message struct {
ID string `gorm:"primaryKey"`
Body string `gorm:"not null"`
Mod bool `gorm:"not null"`
Date time.Time `gorm:"not null"`
// optional:
Deleted *time.Time `gorm:"default:null"`
ReplyMessageID *string `gorm:"default:null"`
ChannelID int64 `gorm:"not null"`
AuthorID int64 `gorm:"not null"`
// Belongings
Author *User
Channel *Channel
}
func (m *Message) String() string {
var mod string
if m.Mod {
mod = "👮 "
}
return fmt.Sprintf("(%s) %s[%s]: %s", m.Channel.Name, mod, m.Author.Name, m.Body)
}
type ListeningChannel struct {
ChannelName string `gorm:"primaryKey"`
Active bool `gorm:"default:true"`
}
///
type Config struct {
PostgresDSN string `koanf:"postgres_dsn"`
//
TwitchNick string `koanf:"twitch_nick"`
TwitchUser string `koanf:"twitch_user"`
TwitchName string `koanf:"twitch_name"`
TwitchPass string `koanf:"twitch_pass"`
//
WebBind string `koanf:"web_bind"`
}
///
// ParseIRCMessage expects {msg} to have {Command == PRIVMSG}
func ParseIRCMessage(msg *irc.Message) (*Message, error) {
// meta
var (
userName = msg.Name
channelName = msg.Param(0)
body = msg.Param(1)
messageID string
userID int64
channelID int64
isMod bool
replyMessageID string
)
if len(channelName) > 0 {
channelName = channelName[1:]
}
// auxiliary
var (
userIDStr string
channelIDStr string
modStr string
ok bool
err error
)
if userIDStr, ok = msg.GetTag("user-id"); !ok {
return nil, fmt.Errorf("userid was empty for user %v", userName)
}
if userID, err = strconv.ParseInt(userIDStr, 10, 64); err != nil {
return nil, fmt.Errorf("userid %v could not be converted to int64", userIDStr)
}
if channelIDStr, ok = msg.GetTag("room-id"); !ok {
return nil, fmt.Errorf("roomid was empty for channel %v", channelName)
}
if channelID, err = strconv.ParseInt(channelIDStr, 10, 64); err != nil {
return nil, fmt.Errorf("roomid %v could not be converted to int64", channelIDStr)
}
if messageID, ok = msg.GetTag("id"); !ok {
return nil, fmt.Errorf("messageid was empty for body '%v' by %v", body, userName)
}
if modStr, ok = msg.GetTag("mod"); !ok {
return nil, fmt.Errorf("mod was empty for body '%v' by %v", body, userName)
}
isMod = modStr == "1"
// reply
replyMessageID, _ = msg.GetTag("reply-parent-msg-id")
return &Message{
ID: messageID,
Body: body,
Mod: isMod,
Date: time.Now(),
Deleted: nil,
ReplyMessageID: strOrNil(replyMessageID),
ChannelID: channelID,
AuthorID: userID,
Author: &User{
ID: userID,
Name: userName,
},
Channel: &Channel{
ID: channelID,
Name: channelName,
},
}, nil
}