diff --git a/api/server.go b/api/server.go index 03551c2..b6c1057 100644 --- a/api/server.go +++ b/api/server.go @@ -28,13 +28,13 @@ type Server struct { logPath string bot *bot.Bot cfg *config.Config - fileLogger *filelog.Logger + fileLogger filelog.Logger helixClient helix.TwitchApiClient assetsHandler http.Handler } // NewServer create api Server -func NewServer(cfg *config.Config, bot *bot.Bot, fileLogger *filelog.Logger, helixClient helix.TwitchApiClient, assets fs.FS) Server { +func NewServer(cfg *config.Config, bot *bot.Bot, fileLogger filelog.Logger, helixClient helix.TwitchApiClient, assets fs.FS) Server { build, err := fs.Sub(assets, "web/build") if err != nil { log.Fatal("failed to read public assets") diff --git a/bot/main.go b/bot/main.go index 9eb43f3..2b3ba92 100644 --- a/bot/main.go +++ b/bot/main.go @@ -19,7 +19,7 @@ type Bot struct { startTime time.Time cfg *config.Config helixClient helix.TwitchApiClient - fileLogger *filelog.Logger + logger filelog.Logger worker []*worker channels map[string]helix.UserData clearchats sync.Map @@ -39,7 +39,7 @@ func newWorker(client *twitch.Client) *worker { } // NewBot create new bot instance -func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger *filelog.Logger) *Bot { +func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger filelog.Logger) *Bot { channels, err := helixClient.GetUsersByUserIds(cfg.Channels) if err != nil { log.Fatalf("[bot] failed to load configured channels %s", err.Error()) @@ -48,7 +48,7 @@ func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger *f return &Bot{ cfg: cfg, helixClient: helixClient, - fileLogger: fileLogger, + logger: fileLogger, channels: channels, worker: []*worker{}, OptoutCodes: sync.Map{}, @@ -150,14 +150,14 @@ func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) { } go func() { - err := b.fileLogger.LogPrivateMessageForUser(message.User, message) + err := b.logger.LogPrivateMessageForUser(message.User, message) if err != nil { log.Error(err.Error()) } }() go func() { - err := b.fileLogger.LogPrivateMessageForChannel(message) + err := b.logger.LogPrivateMessageForChannel(message) if err != nil { log.Error(err.Error()) } @@ -170,7 +170,7 @@ func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) { } go func() { - err := b.fileLogger.LogUserNoticeMessageForUser(message.User.ID, message) + err := b.logger.LogUserNoticeMessageForUser(message.User.ID, message) if err != nil { log.Error(err.Error()) } @@ -178,7 +178,7 @@ func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) { if _, ok := message.Tags["msg-param-recipient-id"]; ok { go func() { - err := b.fileLogger.LogUserNoticeMessageForUser(message.Tags["msg-param-recipient-id"], message) + err := b.logger.LogUserNoticeMessageForUser(message.Tags["msg-param-recipient-id"], message) if err != nil { log.Error(err.Error()) } @@ -186,7 +186,7 @@ func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) { } go func() { - err := b.fileLogger.LogUserNoticeMessageForChannel(message) + err := b.logger.LogUserNoticeMessageForChannel(message) if err != nil { log.Error(err.Error()) } @@ -223,14 +223,14 @@ func (b *Bot) handleClearChat(message twitch.ClearChatMessage) { } go func() { - err := b.fileLogger.LogClearchatMessageForUser(message.TargetUserID, message) + err := b.logger.LogClearchatMessageForUser(message.TargetUserID, message) if err != nil { log.Error(err.Error()) } }() go func() { - err := b.fileLogger.LogClearchatMessageForChannel(message) + err := b.logger.LogClearchatMessageForChannel(message) if err != nil { log.Error(err.Error()) } diff --git a/filelog/channellog.go b/filelog/channellog.go index 5793a64..29b8ab8 100644 --- a/filelog/channellog.go +++ b/filelog/channellog.go @@ -15,7 +15,7 @@ import ( log "github.com/sirupsen/logrus" ) -func (l *Logger) LogPrivateMessageForChannel(message twitch.PrivateMessage) error { +func (l *FileLogger) LogPrivateMessageForChannel(message twitch.PrivateMessage) error { year := message.Time.Year() month := int(message.Time.Month()) day := message.Time.Day() @@ -38,7 +38,7 @@ func (l *Logger) LogPrivateMessageForChannel(message twitch.PrivateMessage) erro return nil } -func (l *Logger) LogClearchatMessageForChannel(message twitch.ClearChatMessage) error { +func (l *FileLogger) LogClearchatMessageForChannel(message twitch.ClearChatMessage) error { year := message.Time.Year() month := int(message.Time.Month()) day := message.Time.Day() @@ -61,7 +61,7 @@ func (l *Logger) LogClearchatMessageForChannel(message twitch.ClearChatMessage) return nil } -func (l *Logger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error { +func (l *FileLogger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error { year := message.Time.Year() month := int(message.Time.Month()) day := message.Time.Day() @@ -84,7 +84,7 @@ func (l *Logger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage return nil } -func (l *Logger) ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error) { +func (l *FileLogger) ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error) { filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", channelID, year, month, day) if _, err := os.Stat(filename); err != nil { @@ -126,7 +126,7 @@ func (l *Logger) ReadLogForChannel(channelID string, year int, month int, day in return content, nil } -func (l *Logger) ReadRandomMessageForChannel(channelID string) (string, error) { +func (l *FileLogger) ReadRandomMessageForChannel(channelID string) (string, error) { var dayFileList []string var lines []string diff --git a/filelog/userlog.go b/filelog/userlog.go index d47060a..0db5a7c 100644 --- a/filelog/userlog.go +++ b/filelog/userlog.go @@ -17,17 +17,34 @@ import ( log "github.com/sirupsen/logrus" ) -type Logger struct { +type Logger interface { + LogPrivateMessageForUser(user twitch.User, message twitch.PrivateMessage) error + LogClearchatMessageForUser(userID string, message twitch.ClearChatMessage) error + LogUserNoticeMessageForUser(userID string, message twitch.UserNoticeMessage) error + GetLastLogYearAndMonthForUser(channelID, userID string) (int, int, error) + GetAvailableLogsForUser(channelID, userID string) ([]UserLogFile, error) + ReadLogForUser(channelID, userID string, year string, month string) ([]string, error) + ReadRandomMessageForUser(channelID, userID string) (string, error) + + LogPrivateMessageForChannel(message twitch.PrivateMessage) error + LogClearchatMessageForChannel(message twitch.ClearChatMessage) error + LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error + ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error) + ReadRandomMessageForChannel(channelID string) (string, error) + GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, error) +} + +type FileLogger struct { logPath string } -func NewFileLogger(logPath string) Logger { - return Logger{ +func NewFileLogger(logPath string) FileLogger { + return FileLogger{ logPath: logPath, } } -func (l *Logger) LogPrivateMessageForUser(user twitch.User, message twitch.PrivateMessage) error { +func (l *FileLogger) LogPrivateMessageForUser(user twitch.User, message twitch.PrivateMessage) error { year := message.Time.Year() month := int(message.Time.Month()) @@ -49,7 +66,7 @@ func (l *Logger) LogPrivateMessageForUser(user twitch.User, message twitch.Priva return nil } -func (l *Logger) LogClearchatMessageForUser(userID string, message twitch.ClearChatMessage) error { +func (l *FileLogger) LogClearchatMessageForUser(userID string, message twitch.ClearChatMessage) error { year := message.Time.Year() month := int(message.Time.Month()) @@ -71,7 +88,7 @@ func (l *Logger) LogClearchatMessageForUser(userID string, message twitch.ClearC return nil } -func (l *Logger) LogUserNoticeMessageForUser(userID string, message twitch.UserNoticeMessage) error { +func (l *FileLogger) LogUserNoticeMessageForUser(userID string, message twitch.UserNoticeMessage) error { year := message.Time.Year() month := int(message.Time.Month()) @@ -99,7 +116,7 @@ type UserLogFile struct { Month string `json:"month"` } -func (l *Logger) GetLastLogYearAndMonthForUser(channelID, userID string) (int, int, error) { +func (l *FileLogger) GetLastLogYearAndMonthForUser(channelID, userID string) (int, int, error) { if channelID == "" || userID == "" { return 0, 0, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID) } @@ -148,7 +165,7 @@ func (l *Logger) GetLastLogYearAndMonthForUser(channelID, userID string) (int, i return 0, 0, errors.New("No logs file") } -func (l *Logger) GetAvailableLogsForUser(channelID, userID string) ([]UserLogFile, error) { +func (l *FileLogger) GetAvailableLogsForUser(channelID, userID string) ([]UserLogFile, error) { if channelID == "" || userID == "" { return []UserLogFile{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID) } @@ -201,7 +218,7 @@ type ChannelLogFile struct { Day string `json:"day"` } -func (l *Logger) GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, error) { +func (l *FileLogger) GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, error) { if channelID == "" { return []ChannelLogFile{}, fmt.Errorf("Invalid channelID: %s", channelID) } @@ -263,7 +280,7 @@ func (l *Logger) GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, } // ReadLogForUser fetch logs -func (l *Logger) ReadLogForUser(channelID, userID string, year string, month string) ([]string, error) { +func (l *FileLogger) ReadLogForUser(channelID, userID string, year string, month string) ([]string, error) { if channelID == "" || userID == "" { return []string{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID) } @@ -310,7 +327,7 @@ func (l *Logger) ReadLogForUser(channelID, userID string, year string, month str return content, nil } -func (l *Logger) ReadRandomMessageForUser(channelID, userID string) (string, error) { +func (l *FileLogger) ReadRandomMessageForUser(channelID, userID string) (string, error) { var userLogs []string var lines []string