Repository: gpodder/mygpo

Issue:
API: Device Synchronization API - Start / Stop Sync returning HTTP status 500
During my work on PR https://github.com/gpodder/mygpo/pull/122 is was testing the Device Synchronization API - Start / Stop Sync (https://gpoddernet.readthedocs.io/en/latest/api/reference/sync.html#post--api-2-sync-devices-(username).json)

I sent the following request
```json
{
  "synchronize": [
    [
      "my-desktop", "cellphone"
    ]
  ]
}
```

and it is returning HTTP 500
```html
<html>
    <head>
        <title>500 Internal server error (gpodder.net)</title>
        <link rel="stylesheet" type="text/css" href="/static/css/fail.css" />
    </head>
    <body>
        <div id="c">
            <div id="fail">
                <h1>500 - Internal server error.</h1>
                <p>
                The service is currently overloaded.
                Please try again later or contact us.
                </p>
            </div>
        </div>
        <img id="icon" src="/static/failpodder.png">
    </body>
</html>
```

as a reference, a previous call to https://gpoddernet.readthedocs.io/en/latest/api/reference/sync.html#get--api-2-sync-devices-(username).json was returning:
```json
{
  "synchronized": [],
  "not-synchronized": [
    "cellphone",
    "my-desktop"
  ]
}
```

I'm able ot sync this devices on the web ui though.


Discussion:
The cause for this is:

```
File "mygpo/api/advanced/sync.py", line 35, in main
    update_sync_status(request.user, synclist, stopsync)
  File "mygpo/api/advanced/sync.py", line 80, in update_sync_status
    other = user.get_device_by_uid(other_uid)
  File "venv/lib/python3.8/site-packages/django/utils/functional.py", line 241, in inner
    return func(self._wrapped, *args)
AttributeError: 'User' object has no attribute 'get_device_by_uid'
```

Replacing `user.get_device_by_uid(other_uid)` with `Client.objects.get(user=user, uid=other_uid)` leads to more crashes. 

@SiqingYu I do not know much about python. Could you please point me into the right direction to fix this? Synchronizing devices using the API will make AntennaPod's sync feature a lot more useful. Our users are currently pretty confused that they need to log into the website to make sync work.

Resolving patch:
diff --git a/mygpo/api/advanced/sync.py b/mygpo/api/advanced/sync.py
--- a/mygpo/api/advanced/sync.py
+++ b/mygpo/api/advanced/sync.py
@@ -38,7 +38,7 @@ def main(request, username):
         except Client.DoesNotExist as e:
             return HttpResponseNotFound(str(e))
 
-        return JsonResponse(get_sync_status(user))
+        return JsonResponse(get_sync_status(request.user))
 
 
 def get_sync_status(user):
@@ -77,11 +77,11 @@ def update_sync_status(user, synclist, stopsync):
         dev = user.client_set.get(uid=uid)
 
         for other_uid in devlist[1:]:
-            other = user.get_device_by_uid(other_uid)
+            other = user.client_set.get(uid=other_uid)
             dev.sync_with(other)
 
     for uid in stopsync:
-        dev = user.get_device_by_uid(uid)
+        dev = user.client_set.get(uid=uid)
         try:
             dev.stop_sync()
         except ValueError:


Test patch: