Esempio n. 1
0
def updatePlaylistCoverFromFile(pid, user, file_key):
    log(obj={'pid': pid, 'file_key': file_key})

    photo_file = None
    if file_key.startswith("upload-image-"):
        filename = rdb.get(file_key)
        if filename:
            photo_file = filename.decode('ascii')
    if photo_file is None:
        raise UserError('NO_COVER')

    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        list_obj = playlist_db.retrive_item(pid)
        log(obj={'playlist': list_obj})
        if list_obj is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, list_obj)
        playlist_db.update_item_query(list_obj,
                                      {'$set': {
                                          "item.cover": photo_file
                                      }},
                                      user=makeUserMeta(user),
                                      session=s())
        s.mark_succeed()
Esempio n. 2
0
def addToPlaylist(user, pid: ObjectId, text: str, use_bleach=True):
    filterOperation('postComment', user)
    playlist_obj = playlist_db.retrive_item(pid)
    if playlist_obj is None:
        raise UserError('PLAYLIST_NOT_EXIST')
    with redis_lock.Lock(rdb, "playlistEdit:" + str(pid)):
        if 'comment_thread' in playlist_obj:
            cid = addComment(user,
                             playlist_obj['comment_thread'],
                             text,
                             use_bleach=use_bleach)
            return playlist_obj['comment_thread'], cid
        else:
            with MongoTransaction(client) as s:
                tid = createThread('playlist',
                                   playlist_obj['_id'],
                                   playlist_obj['meta']['created_by'],
                                   session=s())
                playlist_db.update_item_query(
                    playlist_obj, {'$set': {
                        'comment_thread': tid
                    }},
                    session=s())
                s.mark_succeed()
            cid = addComment(user, tid, text)
            return tid, cid
Esempio n. 3
0
def insertIntoPlaylistLockFree(pid, vid, rank, user, session):
    log(obj={'pid': pid, 'vid': vid, 'rank': rank})
    if tagdb.retrive_item({'_id': ObjectId(vid)}, session=session) is None:
        #raise UserError('VIDEO_NOT_EXIST')
        return False
    conflicting_item = db.playlist_items.find_one(
        {
            'pid': ObjectId(pid),
            'vid': ObjectId(vid)
        }, session=session)
    if conflicting_item is not None:
        editPlaylist_MoveLockFree(pid, conflicting_item, rank, session=session)
        playlist_db.update_item_query(pid, {},
                                      user=makeUserMeta(user),
                                      session=session)
        if conflicting_item['rank'] >= rank:
            return True
        else:
            return False
    playlists = tagdb.retrive_item({'_id': ObjectId(vid)},
                                   session=session)['item']['series']
    playlists.append(ObjectId(pid))
    playlists = list(set(playlists))
    tagdb.update_item_query(ObjectId(vid),
                            {'$set': {
                                'item.series': playlists
                            }},
                            user=makeUserMeta(user),
                            session=session)
    playlist_db.update_item_query(pid, {"$inc": {
        "item.videos": int(1)
    }},
                                  user=makeUserMeta(user),
                                  session=session)
    db.playlist_items.update_many(
        {
            'pid': ObjectId(pid),
            'rank': {
                '$gte': rank
            }
        }, {'$inc': {
            'rank': int(1)
        }},
        session=session)
    db.playlist_items.insert_one(
        {
            "pid": ObjectId(pid),
            "vid": ObjectId(vid),
            "rank": int(rank),
            "meta": makeUserMeta(user)
        },
        session=session)
    return True
Esempio n. 4
0
def addVideoToPlaylist(pid, vid, user):
    log(obj={'pid': pid, 'vid': vid})
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        playlist = playlist_db.retrive_item(pid)
        if playlist is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, playlist)
        if tagdb.retrive_item({'_id': ObjectId(vid)}, session=s()) is None:
            raise UserError('VIDEO_NOT_EXIST')
        if playlist['item']["videos"] > PlaylistConfig.MAX_VIDEO_PER_PLAYLIST:
            raise UserError('VIDEO_LIMIT_EXCEEDED')
        conflicting_item = db.playlist_items.find_one(
            {
                'pid': ObjectId(pid),
                'vid': ObjectId(vid)
            }, session=s())
        if conflicting_item is not None:
            editPlaylist_MoveLockFree(pid,
                                      conflicting_item,
                                      int(playlist['item']["videos"]),
                                      session=s())
            playlist_db.update_item_query(playlist, {},
                                          user=makeUserMeta(user),
                                          session=s())
            s.mark_succeed()
            return
        playlists = tagdb.retrive_item({'_id': ObjectId(vid)},
                                       session=s())['item']['series']
        playlists.append(ObjectId(pid))
        playlists = list(set(playlists))
        tagdb.update_item_query(ObjectId(vid),
                                {'$set': {
                                    'item.series': playlists
                                }},
                                user=makeUserMeta(user),
                                session=s())
        db.playlist_items.insert_one(
            {
                "pid": ObjectId(pid),
                "vid": ObjectId(vid),
                "rank": int(playlist['item']["videos"]),
                "meta": makeUserMeta(user)
            },
            session=s())
        playlist_db.update_item_query(playlist,
                                      {"$inc": {
                                          "item.videos": int(1)
                                      }},
                                      user=makeUserMeta(user),
                                      session=s())
        s.mark_succeed()
Esempio n. 5
0
def updatePlaylistInfo(pid,
                       title,
                       desc,
                       cover,
                       user,
                       private=False,
                       privateEdit=True):
    log(
        obj={
            'title': title,
            'desc': desc,
            'cover': cover,
            'private': private,
            'privateEdit': privateEdit
        })
    if len(title) > PlaylistConfig.MAX_TITLE_LENGTH:
        raise UserError('TITLE_TOO_LONG')
    if len(desc) > PlaylistConfig.MAX_DESC_LENGTH:
        raise UserError('DESC_TOO_LONG')
    if cover and len(cover) > PlaylistConfig.MAX_COVER_URL_LENGTH:
        raise UserError('URL_TOO_LONG')
    if not title:
        raise UserError('EMPTY_TITLE')
    if not desc:
        raise UserError('EMPTY_DESC')
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        list_obj = playlist_db.retrive_item(pid)
        log(obj={'playlist': list_obj})
        if list_obj is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, list_obj)
        if cover:
            playlist_db.update_item_query(list_obj,
                                          {'$set': {
                                              "item.cover": cover
                                          }},
                                          user=makeUserMeta(user),
                                          session=s())
        playlist_db.update_item_query(
            list_obj, {
                '$set': {
                    "item.title": title,
                    "item.desc": desc,
                    "item.private": private,
                    "item.privateEdit": privateEdit
                }
            },
            fields_to_index=['item.title', 'item.desc'],
            user=makeUserMeta(user),
            session=s())
        s.mark_succeed()
Esempio n. 6
0
def updatePlaylistCover(pid, cover, user):
    log(obj={'pid': pid, 'cover': cover})
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        list_obj = playlist_db.retrive_item(pid)
        log(obj={'playlist': list_obj})
        if list_obj is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, list_obj)
        playlist_db.update_item_query(list_obj,
                                      {'$set': {
                                          "item.cover": cover
                                      }},
                                      user=makeUserMeta(user),
                                      session=s())
        s.mark_succeed()
Esempio n. 7
0
def editPlaylist_MoveUp(pid, vid, offset, limit, user):
    log(obj={'pid': pid, 'vid': vid})
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        playlist = playlist_db.retrive_item(pid, session=s())
        if playlist is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, playlist)
        if playlist['item']["videos"] > 0:
            entry = db.playlist_items.find_one(
                {
                    "pid": ObjectId(pid),
                    "vid": ObjectId(vid)
                }, session=s())
            if entry is None:
                s.mark_failover()
                raise UserError('VIDEO_NOT_EXIST_OR_NOT_IN_PLAYLIST')
            if entry['rank'] <= 0:
                return None
            exchange_entry = db.playlist_items.find_one(
                {
                    "pid": ObjectId(pid),
                    "rank": int(entry['rank'] - 1)
                },
                session=s())
            db.playlist_items.update_one(
                {'_id': entry['_id']},
                {'$set': {
                    'rank': int(entry['rank'] - 1)
                }},
                session=s())
            db.playlist_items.update_one(
                {'_id': exchange_entry['_id']},
                {'$set': {
                    'rank': int(entry['rank'])
                }},
                session=s())
            playlist_db.update_item_query(playlist, {},
                                          user=makeUserMeta(user),
                                          session=s())
            #video_page, video_count = listPlaylistVideos(pid, page - 1, page_size, user)
            s.mark_succeed()
            #return {'videos': video_page, 'video_count': video_count, 'page': page}
        else:
            raise UserError('EMPTY_PLAYLIST')
Esempio n. 8
0
def removeVideoFromPlaylist(pid, vid, offset, limit, user):
    log(obj={'pid': pid, 'vid': vid})
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        playlist = playlist_db.retrive_item(pid, session=s())
        if playlist is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, playlist)
        if playlist['item']["videos"] > 0:
            entry = db.playlist_items.find_one(
                {
                    "pid": ObjectId(pid),
                    "vid": ObjectId(vid)
                }, session=s())
            if entry is None:
                raise UserError('VIDEO_NOT_EXIST_OR_NOT_IN_PLAYLIST')
            db.playlist_items.update_many(
                {
                    'pid': ObjectId(pid),
                    'rank': {
                        '$gt': entry['rank']
                    }
                }, {'$inc': {
                    'rank': int(-1)
                }},
                session=s())
            db.playlist_items.delete_one({'_id': entry['_id']}, session=s())
            playlist_db.update_item_query(playlist,
                                          {"$inc": {
                                              "item.videos": int(-1)
                                          }},
                                          user=makeUserMeta(user),
                                          session=s())
        else:
            raise UserError('EMPTY_PLAYLIST')
        """
		video_page, video_count = listPlaylistVideos(pid, page - 1, page_size, user)
		if len(video_page) == 0 and page > 1 and video_count > 0 :
			# in case deleting video results in current page becomes empty, show the previous page
			video_page, video_count = listPlaylistVideos(pid, page - 2, page_size, user)
			s.mark_succeed()
			return {'videos': video_page, 'video_count': video_count, 'page': page - 1}
		"""
        s.mark_succeed()
Esempio n. 9
0
def editPlaylist_Move(pid, vid, to_rank, user):
    log(obj={'pid': pid, 'vid': vid, 'to_rank': to_rank})
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        playlist = playlist_db.retrive_item(pid, session=s())
        if playlist is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, playlist)
        if tagdb.retrive_item({'_id': ObjectId(vid)}, session=s()) is None:
            raise UserError('VIDEO_NOT_EXIST')
        if to_rank < 0:
            raise UserError('OUT_OF_RANGE')
        if to_rank > playlist['item']['videos']:
            to_rank = int(playlist['item']['videos'])
        editPlaylist_MoveLockFree(pid, vid, to_rank, session=s())
        playlist_db.update_item_query(playlist, {},
                                      user=makeUserMeta(user),
                                      session=s())
        s.mark_succeed()
Esempio n. 10
0
def updatePlaylistCoverVID(pid, vid, offset, limit, user):
    log(obj={'pid': pid, 'vid': vid})
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        list_obj = playlist_db.retrive_item(pid)
        log(obj={'playlist': list_obj})
        if list_obj is None:
            raise UserError('PLAYLIST_NOT_EXIST')
        filterOperation('editPlaylist', user, list_obj)
        video_obj = filterSingleVideo(vid, user)
        if video_obj is None:
            raise UserError('VIDEO_NOT_EXIST')
        cover = video_obj['item']['cover_image']
        playlist_db.update_item_query(list_obj,
                                      {'$set': {
                                          "item.cover": cover
                                      }},
                                      user=makeUserMeta(user),
                                      session=s())
        #video_page, video_count = listPlaylistVideos(pid, page - 1, page_size, user)
        s.mark_succeed()
Esempio n. 11
0
def ratePlaylist(user, pid: ObjectId, stars: int):
    stars = max(min(int(stars), 10), 1)
    playlist_obj = playlist_db.retrive_item(pid)
    if playlist_obj is None:
        raise UserError('PLAYLIST_NOT_EXIST')
    with redis_lock.Lock(rdb, "playlistEdit:" +
                         str(pid)), MongoTransaction(client) as s:
        rating_obj = db.playlist_ratings.find_one(
            {
                'pid': pid,
                'uid': ObjectId(user['_id'])
            }, session=s())
        user_rated = 0
        if rating_obj:
            user_rated = 1
            db.playlist_ratings.update_one(
                {
                    'pid': pid,
                    'uid': ObjectId(user['_id'])
                }, {'$set': {
                    'v': int(stars)
                }},
                session=s())
        else:
            user_rated = 0
            db.playlist_ratings.insert_one(
                {
                    'pid': pid,
                    'uid': ObjectId(user['_id']),
                    'v': int(stars)
                },
                session=s())
        if 'total_rating' in playlist_obj:
            if rating_obj:
                playlist_db.update_item_query(playlist_obj, {
                    '$inc': {
                        'total_rating': int(stars - rating_obj['v']),
                        'total_rating_user': int(1 - user_rated)
                    }
                },
                                              session=s())
            else:
                playlist_db.update_item_query(playlist_obj, {
                    '$inc': {
                        'total_rating': int(stars),
                        'total_rating_user': int(1 - user_rated)
                    }
                },
                                              session=s())
        else:
            playlist_db.update_item_query(playlist_obj, {
                '$set': {
                    'total_rating': int(stars),
                    'total_rating_user': int(1)
                }
            },
                                          session=s())
        s.mark_succeed()