def upvote_song(room_number, url, cookie): user_id = SecurityUtils.get_userId(cookie) url = SecurityUtils.encrypt_url(url) pending_songs = DBUtils.get_pending_songs(room_number) # Check if a song is in the queue/pending songs if url not in pending_songs: return False, ErrorMsg.NO_SONG.value songs = DBUtils.get_votes_per_user(room_number, user_id) if url not in songs: result = DBUtils.upvote(room_number, url, user_id) msg = 'Something went wrong, please vote again!' pending_songs = DBUtils.get_pending_songs(room_number) sorted_queue = QueueModerator.sort_pending_songs(pending_songs) return (True, sorted_queue, None) if result else (False, sorted_queue, msg) elif songs[url]: sorted_queue = QueueModerator.sort_pending_songs(pending_songs) msg = 'User has already voted for this song' return False, sorted_queue, msg sorted_queue = QueueModerator.sort_pending_songs(pending_songs) return False, sorted_queue, None
def enqueue_song(room_number, url, name, duration, userId): url = SecurityUtils.encrypt_url(url) room = DBUtils.get_room(room_number) queue = room['queue'] # Assume a song does not exist exists = False if url in queue: exists = True queue_list = QueueModerator.sort_pending_songs(queue) return False, queue_list song = { 'name': name, 'score': 0, # initial score is always 0 'duration': duration, 'nickname': DBUtils.get_member(userId, room_number)[userId]['nickname'], 'userId': userId } result = DBUtils.enqueue_song(room['_id'], url, song, len(queue.keys()) + 1) unsorted_queue = DBUtils.get_pending_songs(room_number) queue_list = QueueModerator.sort_pending_songs(unsorted_queue) return result, queue_list
def pending_songs(room_number): # print("whats going on") try: unsorted_queue = DBUtils.get_pending_songs(room_number) # possible type error - idk if unsorted_queue is of type dict sorted_queue = QueueModerator.sort_pending_songs(unsorted_queue) return True, sorted_queue except: return False, []
def unvote_song(room_number, url, cookie): user_id = SecurityUtils.get_userId(cookie) url = SecurityUtils.encrypt_url(url) pending_songs = DBUtils.get_pending_songs(room_number) # Check if a song is in the queue/pending songs if url not in pending_songs: sorted_queue = QueueModerator.sort_pending_songs(pending_songs) return False, sorted_queue, ErrorMsg.NO_SONG.value songs = DBUtils.get_votes_per_user(room_number, user_id) if url not in songs or songs[url] == 0: sorted_queue = QueueModerator.sort_pending_songs(pending_songs) return False, sorted_queue, ErrorMsg.NO_VOTE.value result, err = DBUtils.unvote(room_number, url, user_id) pending_songs = DBUtils.get_pending_songs(room_number) sorted_queue = QueueModerator.sort_pending_songs(pending_songs) if result: return True, sorted_queue, None else: return False, sorted_queue, err.value