Skip to content

Commit

Permalink
fix(notification): Trigger notification if set in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Dec 13, 2021
1 parent d237b5d commit 151b0d2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
25 changes: 14 additions & 11 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ version: '3'
vars:
TEMP:
sh: mktemp -d
VERSION:
sh: git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(git rev-parse --short HEAD)"

tasks:
default:
- task: clean
- task: lint
- task: tests
- task: build

clean:
desc: Clean output folder
cmds:
- rm -rf out/*

build:
cmds:
- go mod tidy
Expand Down Expand Up @@ -38,15 +46,11 @@ tasks:
- rsrc_windows_amd64.syso
- internal/icon/icon_unix.go
- internal/icon/icon_windows.go
vars:
VERSION:
sh: git describe --tags --exact-match 2>/dev/null || git describe --tags 2>/dev/null || echo "v0.0.0-$(git rev-parse --short HEAD)"

build:darwin:
deps: [ mod, generate ]
deps: [ generate ]
cmds:
- go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}'" -tags production -o "{{.TEMP}}/twitch_clip_${GOOS}_${GOARCH}" .
- echo "Binary built at {{.TEMP}}/twitch_clip_${GOOS}_${GOARCH}"
env:
GOOS: darwin
GOARCH: amd64
Expand All @@ -56,15 +60,15 @@ tasks:
deps: [ build:darwin ]
cmds:
- go run hack/macapp.go -assets "{{.TEMP}}" -bin "twitch_clip_${GOOS}_${GOARCH}" -icon ./assets/icon1080.png -identifier com.skynewz.twitchclip -name "Twitch Clip" -o ./out/
- zip -r ./out/twitch_clip_${GOOS}_${GOARCH}_{{ .VERSION }}.zip "./out/Twitch Clip.app" README.md LICENSE && rm -r "./out/Twitch Clip.app"
env:
GOOS: darwin
GOARCH: amd64

build:windows:
deps: [ mod, generate ]
deps: [ generate ]
cmds:
- go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}' -H=windowsgui" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}.exe" .
- echo "Binary built at out/twitch_clip_${GOOS}_${GOARCH}.exe"
- go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}' -H=windowsgui" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}_{{ .VERSION }}.exe" .
env:
GOOS: windows
GOARCH: amd64
Expand All @@ -73,10 +77,9 @@ tasks:
CXX: x86_64-w64-mingw32-g++

build:linux:
deps: [ mod, generate ]
deps: [ generate ]
cmds:
- go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}'" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}.exe" .
- echo "Binary built at out/twitch_clip_${GOOS}_${GOARCH}.exe"
- go build -ldflags="-s -w -X 'main.twitchClientID=${TWITCH_CLIENT_ID}' -X 'main.twitchClientSecret=${TWITCH_SECRET_ID}'" -tags production -o "out/twitch_clip_${GOOS}_${GOARCH}_{{ .VERSION }}.exe" .
env:
GOOS: linux
GOARCH: amd64
Expand Down
12 changes: 9 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"context"
"fmt"
"github.com/SkYNewZ/twitch-clip/internal/config"
"os"
"sync"
"time"
Expand Down Expand Up @@ -59,6 +60,8 @@ type Application struct {

// Each string in this chan will be send to system clipboard
ClipboardListener chan string

config *config.Config
}

// New creates a new Application
Expand Down Expand Up @@ -95,6 +98,7 @@ func New() *Application {
NotificationCallbackCh: notificationCh,
State: make(map[string]*Item),
ClipboardListener: make(chan string, 1),
config: config.Parse(),
}
}

Expand Down Expand Up @@ -359,9 +363,11 @@ func (a *Application) NewItem(ctx context.Context, s *twitch.Stream) *Item {
// Start routine click for this Item
go item.Click(ctx)

// New item appear, so notify
if err := a.Notifier.Notify(username, item.Game, item.UserLogin); err != nil {
log.Errorf("fail to notify for [%s]: %s", item.UserLogin, err)
// New item appear, so notify if configured
if item.ShouldNotify() {
if err := a.Notifier.Notify(username, item.Game, item.UserLogin); err != nil {
log.Errorf("fail to notify for [%s]: %s", item.UserLogin, err)
}
}

return item
Expand Down
49 changes: 49 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package config

import (
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
)

const (
configFileName = "config.yaml"
configDirectoryName = "Twitch Clip"
)

type Config struct {
Notifications []string `json:"notifications,omitempty" yaml:"notifications,flow"`
}

func defaultConfig() *Config {
return &Config{}
}

// Parse load config stored on disk
func Parse() *Config {
var config = defaultConfig()

dir, err := os.UserConfigDir()
if err != nil {
log.Errorf("cannot load config: %v", err)
return config
}

var file = filepath.Join(dir, configDirectoryName, configFileName)
log.Printf("using config file: %s", file)

data, err := os.ReadFile(file)
if err != nil {
log.Errorf("cannot load config: %v", err)
return config
}

if err := yaml.Unmarshal(data, config); err != nil {
log.Errorf("cannot load config: %v", err)
return config
}

log.Printf("%d notification(s) has been configured", len(config.Notifications))
return config
}
10 changes: 10 additions & 0 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,13 @@ func (i *Item) SetIcon() {
// set icon
i.Item.SetIcon(img)
}

// ShouldNotify send true if current item is configured to send notifications
func (i *Item) ShouldNotify() bool {
for _, user := range i.Application.config.Notifications {
if i.UserLogin == strings.ToLower(user) {
return true
}
}
return false
}
4 changes: 4 additions & 0 deletions pkg/notifier/notification_windows.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package notifier

import (
"errors"
"fmt"
"github.com/phayes/freeport"
"net/http"
"net/url"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -47,6 +50,7 @@ func (s *service) makeNotificationURL(streamer string) string {
func (s *service) startServer() {
var port int
once.Do(func() {
var err error
port, err = freeport.GetFreePort()
if err != nil {
log.Errorf("notification service: cannot find free port, notifications click will not works: %s", err)
Expand Down

0 comments on commit 151b0d2

Please sign in to comment.