Skip to content

Commit

Permalink
Add sync-device GET endpoint and fix old references
Browse files Browse the repository at this point in the history
- Modify old references to legacy implementation of sync
  • Loading branch information
oxtyped committed Apr 8, 2024
1 parent d541c1d commit a877db6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
39 changes: 37 additions & 2 deletions pkg/apis/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,43 @@ func (e *EpisodeAPI) HandleUploadEpisodeAction(w http.ResponseWriter, r *http.Re
// GET /api/2/sync-devices/{username}.json
func (s *SyncAPI) HandleGetSync(w http.ResponseWriter, r *http.Request) {

// username := chi.URLParam(r, "username")
w.Write([]byte(`{}`))
username := chi.URLParam(r, "username")

syncStatus := &SyncDeviceStatus{}

db := s.Data

syncIds, err := db.GetDeviceSyncGroupIds(username)
if err != nil {
log.Printf("error getting sync devices for username (%s): %s", username, err)
w.WriteHeader(500)
return
}
notsyncDevices, err := db.GetNotSyncedDevices(username)
if err != nil {
log.Printf("error getting not_synced devices for username (%s): %s", username, err)
w.WriteHeader(500)
return
}

syncStatus.NotSynchronize = notsyncDevices

for _, id := range syncIds {
sync, err := db.GetDeviceNameFromDeviceSyncGroupId(id)
if err != nil {
log.Printf("error retrieving devices from sync group_id: (%d): %s", id, err)
}

syncStatus.Synchronized = append(syncStatus.Synchronized, sync)
}

jsonBytes, err := json.Marshal(syncStatus)
if err != nil {
log.Printf("error marshalling sync status: %#v", err)
w.WriteHeader(500)
}

w.Write(jsonBytes)
}

// POST /api/2/sync-devices/{username}.json
Expand Down
25 changes: 21 additions & 4 deletions pkg/data/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -589,7 +590,7 @@ func (s *SQLite) GetDeviceSyncGroupIds(username string) ([]int, error) {

db := s.db

rows, err := db.Query("select distinct device_sync_group_id from devices WHERE user_id = (select id from users WHERE username = ?);", username)
rows, err := db.Query("select device_sync_group_id from device_sync_group_devices WHERE device_id = (SELECT id from devices WHERE user_id = (select id from users WHERE username = ?));", username)
if err != nil {
return ids, err
}
Expand All @@ -614,7 +615,7 @@ func (s *SQLite) GetDeviceNameFromDeviceSyncGroupId(id int) ([]string, error) {
var names []string
db := s.db

rows, err := db.Query("select name from devices where device_sync_group_id = ?;", id)
rows, err := db.Query("select name from devices where id IN (select device_id FROM device_sync_group_devices WHERE device_sync_group_id = ?);", id)
if err != nil {
return names, err
}
Expand All @@ -639,15 +640,31 @@ func (s *SQLite) GetNotSyncedDevices(username string) ([]string, error) {
var devices []string

db := s.db
rows, err := db.Query("select name from devices where device_sync_group_id = null;")

userId, err := s.GetUserIdFromName(username)
if err != nil {
return nil, err
}
deviceGroupIds, err := s.GetDeviceSyncGroupIds(username)
if err != nil {
return nil, err
}
stringValues := []string{}
for _, v := range deviceGroupIds {
stringValues = append(stringValues, strconv.Itoa(v))
}
values := strings.Join(stringValues, ",")

rows, err := db.Query("select name from devices where user_id = $1 AND id NOT IN (select device_id FROM device_sync_group_devices WHERE device_sync_group_id IN ($2));", userId, values)
if err != nil {
return devices, err
}

for rows.Next() {

var name string
if err := rows.Scan(name); err != nil {
if err := rows.Scan(&name); err != nil {
fmt.Printf("error scanning rows: %#v", err)
return devices, err
}

Expand Down

0 comments on commit a877db6

Please sign in to comment.