Skip to content

Commit

Permalink
sync validation config
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalraja committed Jan 5, 2022
1 parent 70d076b commit 8b87da2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
16 changes: 16 additions & 0 deletions backend/api/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ func (s *Server) handleCreateSyncLocation() http.HandlerFunc {
if err := s.bind(r, req); err != nil {
return err
}
_, err := sync.SyncFromLocation(req)
if err != nil {
if err == sync.ErrType {
return newValidationErr("type", "invalid")
}
return newValidationErr("config", "invalid")
}
ctx := r.Context()
u := s.currentUser(ctx)
if u == nil {
Expand Down Expand Up @@ -48,6 +55,15 @@ func (s *Server) handleSaveSyncLocation() http.HandlerFunc {
loc.Type = req.Type
loc.Config = req.Config
loc.Deleted = req.Deleted

_, err = sync.SyncFromLocation(loc)
if err != nil {
if err == sync.ErrType {
return newValidationErr("type", "invalid")
}
return newValidationErr("config", "invalid")
}

if err := loc.Save(u); err != nil {
return err
}
Expand Down
31 changes: 22 additions & 9 deletions backend/sync/sync.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sync

import (
"fmt"
"log"

"github.com/altlimit/dmedia/model"
Expand All @@ -16,6 +17,8 @@ type (

var (
SyncChannel = make(chan int64)
ErrType = fmt.Errorf("invalid type")
ErrConfig = fmt.Errorf("invlid config")
)

func syncListener() {
Expand Down Expand Up @@ -46,6 +49,22 @@ func ScheduleSync(userID int64) {
SyncChannel <- userID
}

func SyncFromLocation(loc *model.SyncLocation) (Sync, error) {
var syncer Sync
if loc.Type == "telegram" {
syncer = &Telegram{
Token: loc.Config["token"].(string),
Channel: loc.Config["channel"].(string),
}
}
if syncer == nil {
return nil, ErrType
} else if !syncer.Valid() {
return nil, ErrConfig
}
return syncer, nil
}

func SyncUser(userID int64) {
locs, err := model.GetSyncs(userID, true)
if err != nil {
Expand All @@ -54,17 +73,11 @@ func SyncUser(userID int64) {
}

for _, loc := range locs {
var syncer Sync
if loc.Type == "telegram" {
syncer = &Telegram{
Token: loc.Config["token"].(string),
Channel: loc.Config["channel"].(string),
}
}
if syncer == nil {
syncer, err := SyncFromLocation(&loc)
if err == ErrType {
log.Println("SyncUser", userID, "location type", loc.Type, "invalid")
continue
} else if !syncer.Valid() {
} else if err == ErrConfig {
log.Println("SyncUser", userID, "invalid config", loc.Name)
continue
}
Expand Down
17 changes: 16 additions & 1 deletion backend/sync/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
Expand All @@ -21,7 +22,21 @@ type Telegram struct {
}

func (t *Telegram) Valid() bool {
return t.Token != "" && t.Channel != ""
if t.Token != "" && t.Channel != "" {
resp, err := http.Get(t.getURL("getMe"))
if err != nil {
log.Println("valid error", err)
return false
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("valid read body error", err)
}
json := string(body)
ok := gjson.Get(json, "ok")
return ok.Bool()
}
return false
}

func (t *Telegram) getURL(method string) string {
Expand Down

0 comments on commit 8b87da2

Please sign in to comment.