diff src/plugins/plugin_misc_radiocol.py @ 765:787ee59dba9c

plugins radiocol, xep-0054: better handling of upload errors: - radiocolSongAdded is now asynchronous - _buildAvatar return a Failure if the image is not readable
author souliane <souliane@mailoo.org>
date Tue, 17 Dec 2013 18:44:53 +0100
parents d0e809014ea2
children bfabeedbf32e
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Tue Dec 17 11:35:41 2013 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Tue Dec 17 18:44:53 2013 +0100
@@ -21,7 +21,8 @@
 from twisted.words.xish import domish
 from twisted.internet import reactor
 from twisted.words.protocols.jabber import jid
-
+from twisted.internet import threads, defer
+from sat.core import exceptions
 import os.path
 import copy
 import time
@@ -65,7 +66,7 @@
         self.host = host
         host.bridge.addMethod("radiocolLaunch", ".plugin", in_sign='asss', out_sign='', method=self.prepareRoom)
         host.bridge.addMethod("radiocolCreate", ".plugin", in_sign='sass', out_sign='', method=self.createGame)
-        host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded)
+        host.bridge.addMethod("radiocolSongAdded", ".plugin", in_sign='sss', out_sign='', method=self.radiocolSongAdded, async=True)
         host.bridge.addSignal("radiocolPlayers", ".plugin", signature='ssass')  # room_jid, referee, players, profile
         host.bridge.addSignal("radiocolStarted", ".plugin", signature='ssasais')  # room_jid, referee, players, [QUEUE_TO_START, QUEUE_LIMIT], profile
         host.bridge.addSignal("radiocolSongRejected", ".plugin", signature='sss')  # room_jid, reason, profile
@@ -94,32 +95,22 @@
         #     check data. Referee will have to parse the song himself to check it
         client = self.host.getClient(profile)
         if not client:
-            error(_("Can't access profile's data"))
-            return
+            raise exceptions.NotConnectedProfileError(_("Can't access profile's data"))
         try:
             song = OggVorbis(song_path)
         except OggVorbisHeaderError:
             #this file is not ogg vorbis, we reject it
             self.deleteFile(song_path)  # FIXME: same host trick (see note above)
-            self.host.bridge.radiocolSongRejected(jid.JID(referee).userhost(),
-                                                  "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable", profile)
-            """mess = self.createGameElt(jid.JID(referee))
-            reject_elt = mess.firstChildElement().addElement(('','song_rejected'))
-            reject_elt['sender'] = client.jid
-            reject_elt['reason'] = "Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"
-            #FIXME: add an error code
-            self.host.profiles[profile].xmlstream.send(mess)"""
-            return
+            return defer.fail(exceptions.DataError("Uploaded file is not Ogg Vorbis song, only Ogg Vorbis songs are acceptable"))
         attrs = {'filename': os.path.basename(song_path),
                  'title': song.get("title", ["Unknown"])[0],
                  'artist': song.get("artist", ["Unknown"])[0],
                  'album': song.get("album", ["Unknown"])[0],
                  'length': str(song.info.length)
                  }
-        self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
-
         radio_data = self.games[jid.JID(referee).userhost()]  # FIXME: referee comes from Libervia's client side, it's unsecure
         radio_data['to_delete'][attrs['filename']] = song_path  # FIXME: works only because of the same host trick, see the note under the docstring
+        return threads.deferToThread(self.send, jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
 
     def playNext(self, room_jid, profile):
         """"Play next song in queue if exists, and put a timer