コード例 #1
0
def addComment(user,
               thread_id: ObjectId,
               text: str,
               notification_type: str = 'comment_reply',
               use_bleach=True):  # user can add comments
    filterOperation('postComment', user)
    if use_bleach:
        text = bleach.clean(text, tags=[], attributes=[], styles=[])
    l = len(text)
    if l > Comments.MAX_COMMENT_LENGTH_LONG:
        raise UserError('COMMENT_TOO_LONG')
    elif l > Comments.MAX_COMMENT_LENGTH_REGULAR and not filterOperation(
            'postLongComment', user, raise_exception=False):
        raise UserError('COMMENT_TOO_LONG')
    thread_obj = db.comment_threads.find_one({'_id': thread_id})
    if thread_obj is None:
        raise UserError('THREAD_NOT_EXIST')
    with redis_lock.Lock(rdb, "thread:" +
                         str(thread_id)), MongoTransaction(client) as s:
        cid = str(
            db.comment_items.insert_one(
                {
                    'thread': thread_id,
                    'content': text,
                    'hidden': False,
                    'deleted': False,
                    'pinned': False,
                    'upvotes': 0,
                    'downvotes': 0,
                    'meta': makeUserMetaObject(user)
                },
                session=s()).inserted_id)
        db.comment_threads.update_one({'_id': thread_id},
                                      {'$inc': {
                                          'count': int(1)
                                      }},
                                      session=s())
        note_obj = {
            "cid": ObjectId(cid),
            "replied_by": makeUserMeta(user),
            "content": text[:Comments.NOTIFICATION_CONTENT_LENGTH]
        }
        # ===========================================================
        if 'obj_type' in thread_obj and 'obj_id' in thread_obj:
            note_obj['replied_type'] = thread_obj['obj_type']
            note_obj['replied_obj'] = thread_obj['obj_id']
        else:
            obj = db.videos.find_one({'comment_thread': thread_id},
                                     session=s())
            if obj:
                note_obj['replied_type'] = 'video'
                note_obj['replied_obj'] = obj['_id']
                db.comment_threads.update_one(
                    {'_id': thread_id},
                    {'$set': {
                        'obj_type': 'video',
                        'obj_id': obj['_id']
                    }},
                    session=s())
            else:
                obj = playlist_db.retrive_item({'comment_thread': thread_id},
                                               session=s())
                if obj:
                    note_obj['replied_type'] = 'playlist'
                    note_obj['replied_obj'] = obj['_id']
                    db.comment_threads.update_one({'_id': thread_id}, {
                        '$set': {
                            'obj_type': 'playlist',
                            'obj_id': obj['_id']
                        }
                    },
                                                  session=s())
                else:
                    obj = db.users.find_one({'comment_thread': thread_id},
                                            session=s())
                    if obj:
                        note_obj['replied_type'] = 'user'
                        note_obj['replied_obj'] = obj['_id']
                        db.comment_threads.update_one({'_id': thread_id}, {
                            '$set': {
                                'obj_type': 'user',
                                'obj_id': obj['_id']
                            }
                        },
                                                      session=s())
                    else:
                        log(level='ERR',
                            obj={
                                'msg': 'orphan thread found!!',
                                'thread_id': thread_id,
                                'thread_obj': thread_obj
                            })
                        raise UserError('UNKNOWN_ERROR')
        # ===========================================================
        if notification_type and thread_obj['owner'] != user[
                '_id']:  # empty means do not notify user
            createNotification(notification_type,
                               thread_obj['owner'],
                               session=s(),
                               other=note_obj)
        if note_obj[
                'replied_type'] == 'forum':  # forum comment, set modified_at date
            db.forum_threads.update_one({'_id': note_obj['replied_obj']}, {
                '$set': {
                    'meta.modified_at': datetime.now(),
                    'meta.modified_by': user['_id']
                }
            },
                                        session=s())
        s.mark_succeed()
        return cid
コード例 #2
0
ファイル: comment.py プロジェクト: shitianshiwa/PatchyVideo
def addComment(user, thread_id: ObjectId, text: str):  # user can add comments
    # TODO notify user being replied to
    filterOperation('postComment', user)
    l = len(text)
    if l > Comments.MAX_COMMENT_LENGTH_LONG:
        raise UserError('COMMENT_TOO_LONG')
    elif l > Comments.MAX_COMMENT_LENGTH_REGULAR and not filterOperation(
            'postLongComment', user, raise_exception=False):
        raise UserError('COMMENT_TOO_LONG')
    thread_obj = db.comment_threads.find_one({'_id': thread_id})
    if thread_obj is None:
        raise UserError('THREAD_NOT_EXIST')
    with redis_lock.Lock(rdb, "thread:" +
                         str(thread_id)), MongoTransaction(client) as s:
        cid = str(
            db.comment_items.insert_one(
                {
                    'thread': thread_id,
                    'content': text,
                    'hidden': False,
                    'deleted': False,
                    'upvotes': 0,
                    'downvotes': 0,
                    'meta': makeUserMetaObject(user)
                },
                session=s()).inserted_id)
        db.comment_threads.update_one({'_id': thread_id},
                                      {'$inc': {
                                          'count': int(1)
                                      }},
                                      session=s())
        note_obj = {
            "cid": ObjectId(cid),
            "replied_by": makeUserMeta(user),
            "content": text[:Comments.NOTIFICATION_CONTENT_LENGTH]
        }
        # ===========================================================
        if 'obj_type' in thread_obj and 'obj_id' in thread_obj:
            note_obj['replied_type'] = thread_obj['obj_type']
            note_obj['replied_obj'] = thread_obj['obj_id']
        else:
            obj = db.items.find_one({'comment_thread': thread_id}, session=s())
            if obj:
                note_obj['replied_type'] = 'video'
                note_obj['replied_obj'] = obj['_id']
                db.comment_threads.update_one(
                    {'_id': thread_id},
                    {'$set': {
                        'obj_type': 'video',
                        'obj_id': obj['_id']
                    }},
                    session=s())
            else:
                obj = db.playlists.find_one({'comment_thread': thread_id},
                                            session=s())
                if obj:
                    note_obj['replied_type'] = 'playlist'
                    note_obj['replied_obj'] = obj['_id']
                    db.comment_threads.update_one({'_id': thread_id}, {
                        '$set': {
                            'obj_type': 'playlist',
                            'obj_id': obj['_id']
                        }
                    },
                                                  session=s())
                else:
                    obj = db.users.find_one({'comment_thread': thread_id},
                                            session=s())
                    if obj:
                        note_obj['replied_type'] = 'user'
                        note_obj['replied_obj'] = obj['_id']
                        db.comment_threads.update_one({'_id': thread_id}, {
                            '$set': {
                                'obj_type': 'user',
                                'obj_id': obj['_id']
                            }
                        },
                                                      session=s())
                    else:
                        log(level='ERR',
                            obj={
                                'msg': 'orphan thread found!!',
                                'thread_id': thread_id,
                                'thread_obj': thread_obj
                            })
                        raise UserError('UNKNOWN_ERROR')
        # ===========================================================
        createNotification('comment_reply',
                           thread_obj['owner'],
                           session=s(),
                           other=note_obj)
        s.mark_succeed()
        return cid
コード例 #3
0
def addReply(user,
             reply_to: ObjectId,
             text: str,
             notification_type: str = 'comment_reply',
             use_bleach=True):  # user can add comments
    """
	reply_to: comment id
	"""
    filterOperation('postComment', user)
    if use_bleach:
        text = bleach.clean(text, tags=[], attributes=[], styles=[])
    l = len(text)
    if l > Comments.MAX_COMMENT_LENGTH_LONG:
        raise UserError('COMMENT_TOO_LONG')
    elif l > Comments.MAX_COMMENT_LENGTH_REGULAR and not filterOperation(
            'postLongComment', user, raise_exception=False):
        raise UserError('COMMENT_TOO_LONG')
    parent_obj = db.comment_items.find_one({'_id': reply_to})
    if parent_obj is None:
        raise UserError('PARENT_NOT_EXIST')
    with MongoTransaction(client) as s:
        if 'thread' in parent_obj:  # reply to primary comment
            cid = str(
                db.comment_items.insert_one(
                    {
                        'parent': reply_to,
                        'content': text,
                        'hidden': False,
                        'deleted': False,
                        'pinned': False,
                        'upvotes': 0,
                        'downvotes': 0,
                        'meta': makeUserMetaObject(user)
                    },
                    session=s()).inserted_id)
            thread_obj = db.comment_threads.find_one(
                {'_id': parent_obj['thread']}, session=s())
            if thread_obj is None:
                log(level='ERR',
                    obj={
                        'msg': 'orphan comment found!!',
                        'cid': parent_obj['_id'],
                        'obj': parent_obj
                    })
                raise UserError('UNKNOWN_ERROR')
            thread_id = thread_obj['_id']
        else:  # reply to secondary comment
            cid = str(
                db.comment_items.insert_one(
                    {
                        'parent': parent_obj['parent'],
                        'reply_to': reply_to,
                        'content': text,
                        'hidden': False,
                        'deleted': False,
                        'pinned': False,
                        'upvotes': 0,
                        'downvotes': 0,
                        'meta': makeUserMetaObject(user)
                    },
                    session=s()).inserted_id)
            parent_parent_obj = db.comment_items.find_one(
                {'_id': parent_obj['parent']}, session=s())
            if parent_parent_obj is None:
                log(level='ERR',
                    obj={
                        'msg': 'orphan comment found!!',
                        'cid': parent_obj['_id'],
                        'obj': parent_obj
                    })
                raise UserError('UNKNOWN_ERROR')
            thread_obj = db.comment_threads.find_one(
                {'_id': parent_parent_obj['thread']}, session=s())
            if thread_obj is None:
                log(level='ERR',
                    obj={
                        'msg': 'orphan comment found!!',
                        'cid': parent_parent_obj['_id'],
                        'obj': parent_parent_obj
                    })
                raise UserError('UNKNOWN_ERROR')
            thread_id = thread_obj['_id']
        note_obj = {
            "cid": ObjectId(cid),
            "replied_by": makeUserMeta(user),
            "content": text[:Comments.NOTIFICATION_CONTENT_LENGTH]
        }
        # ===========================================================
        if 'obj_type' in thread_obj and 'obj_id' in thread_obj:
            note_obj['replied_type'] = thread_obj['obj_type']
            note_obj['replied_obj'] = thread_obj['obj_id']
        else:
            obj = db.videos.find_one({'comment_thread': thread_id},
                                     session=s())
            if obj:
                note_obj['replied_type'] = 'video'
                note_obj['replied_obj'] = obj['_id']
                db.comment_threads.update_one(
                    {'_id': thread_id},
                    {'$set': {
                        'obj_type': 'video',
                        'obj_id': obj['_id']
                    }},
                    session=s())
            else:
                obj = playlist_db.retrive_item({'comment_thread': thread_id},
                                               session=s())
                if obj:
                    note_obj['replied_type'] = 'playlist'
                    note_obj['replied_obj'] = obj['_id']
                    db.comment_threads.update_one({'_id': thread_id}, {
                        '$set': {
                            'obj_type': 'playlist',
                            'obj_id': obj['_id']
                        }
                    },
                                                  session=s())
                else:
                    obj = db.users.find_one({'comment_thread': thread_id},
                                            session=s())
                    if obj:
                        note_obj['replied_type'] = 'user'
                        note_obj['replied_obj'] = obj['_id']
                        db.comment_threads.update_one({'_id': thread_id}, {
                            '$set': {
                                'obj_type': 'user',
                                'obj_id': obj['_id']
                            }
                        },
                                                      session=s())
                    else:
                        log(level='ERR',
                            obj={
                                'msg': 'orphan thread found!!',
                                'thread_id': thread_id,
                                'thread_obj': thread_obj
                            })
                        raise UserError('UNKNOWN_ERROR')
        # ===========================================================
        if notification_type and parent_obj['meta']['created_by'] != user[
                '_id']:  # empty means do not notify user and we don't want to notify users who reply to themselves
            createNotification(notification_type,
                               parent_obj['meta']['created_by'],
                               session=s(),
                               other=note_obj)
        if note_obj[
                'replied_type'] == 'forum':  # forum reply, set modified_at date
            db.forum_threads.update_one({'_id': note_obj['replied_obj']}, {
                '$set': {
                    'meta.modified_at': datetime.now(),
                    'meta.modified_by': user['_id']
                }
            },
                                        session=s())
        s.mark_succeed()
コード例 #4
0
ファイル: comment.py プロジェクト: shitianshiwa/PatchyVideo
def addReply(user, reply_to: ObjectId, text: str):  # user can add comments
    """
    reply_to: comment id
    """
    filterOperation('postComment', user)
    # TODO notify user being replied to
    l = len(text)
    if l > Comments.MAX_COMMENT_LENGTH_LONG:
        raise UserError('COMMENT_TOO_LONG')
    elif l > Comments.MAX_COMMENT_LENGTH_REGULAR and not filterOperation(
            'postLongComment', user, raise_exception=False):
        raise UserError('COMMENT_TOO_LONG')
    parent_obj = db.comment_items.find_one({'_id': reply_to})
    if parent_obj is None:
        raise UserError('PARENT_NOT_EXIST')
    with MongoTransaction(client) as s:
        if 'thread' in parent_obj:  # reply to primary comment
            cid = str(
                db.comment_items.insert_one(
                    {
                        'parent': reply_to,
                        'content': text,
                        'hidden': False,
                        'deleted': False,
                        'upvotes': 0,
                        'downvotes': 0,
                        'meta': makeUserMetaObject(user)
                    },
                    session=s()).inserted_id)
            thread_obj = db.comment_threads.find_one(
                {'_id': parent_obj['thread']}, session=s())
            if thread_obj is None:
                log(level='ERR',
                    obj={
                        'msg': 'orphan comment found!!',
                        'cid': parent_obj['_id'],
                        'obj': parent_obj
                    })
                raise UserError('UNKNOWN_ERROR')
            thread_id = thread_obj['_id']
        else:  # reply to secondary comment
            cid = str(
                db.comment_items.insert_one(
                    {
                        'parent': parent_obj['parent'],
                        'reply_to': reply_to,
                        'content': text,
                        'hidden': False,
                        'deleted': False,
                        'upvotes': 0,
                        'downvotes': 0,
                        'meta': makeUserMetaObject(user)
                    },
                    session=s()).inserted_id)
            parent_parent_obj = db.comment_items.find_one(
                {'_id': parent_obj['parent']}, session=s())
            if parent_parent_obj is None:
                log(level='ERR',
                    obj={
                        'msg': 'orphan comment found!!',
                        'cid': parent_obj['_id'],
                        'obj': parent_obj
                    })
                raise UserError('UNKNOWN_ERROR')
            thread_obj = db.comment_threads.find_one(
                {'_id': parent_parent_obj['thread']}, session=s())
            if thread_obj is None:
                log(level='ERR',
                    obj={
                        'msg': 'orphan comment found!!',
                        'cid': parent_parent_obj['_id'],
                        'obj': parent_parent_obj
                    })
                raise UserError('UNKNOWN_ERROR')
            thread_id = thread_obj['_id']
        note_obj = {
            "cid": ObjectId(cid),
            "replied_by": makeUserMeta(user),
            "content": text[:Comments.NOTIFICATION_CONTENT_LENGTH]
        }
        # ===========================================================
        if 'obj_type' in thread_obj and 'obj_id' in thread_obj:
            note_obj['replied_type'] = thread_obj['obj_type']
            note_obj['replied_obj'] = thread_obj['obj_id']
        else:
            obj = db.items.find_one({'comment_thread': thread_id}, session=s())
            if obj:
                note_obj['replied_type'] = 'video'
                note_obj['replied_obj'] = obj['_id']
                db.comment_threads.update_one(
                    {'_id': thread_id},
                    {'$set': {
                        'obj_type': 'video',
                        'obj_id': obj['_id']
                    }},
                    session=s())
            else:
                obj = db.playlists.find_one({'comment_thread': thread_id},
                                            session=s())
                if obj:
                    note_obj['replied_type'] = 'playlist'
                    note_obj['replied_obj'] = obj['_id']
                    db.comment_threads.update_one({'_id': thread_id}, {
                        '$set': {
                            'obj_type': 'playlist',
                            'obj_id': obj['_id']
                        }
                    },
                                                  session=s())
                else:
                    obj = db.users.find_one({'comment_thread': thread_id},
                                            session=s())
                    if obj:
                        note_obj['replied_type'] = 'user'
                        note_obj['replied_obj'] = obj['_id']
                        db.comment_threads.update_one({'_id': thread_id}, {
                            '$set': {
                                'obj_type': 'user',
                                'obj_id': obj['_id']
                            }
                        },
                                                      session=s())
                    else:
                        log(level='ERR',
                            obj={
                                'msg': 'orphan thread found!!',
                                'thread_id': thread_id,
                                'thread_obj': thread_obj
                            })
                        raise UserError('UNKNOWN_ERROR')
        # ===========================================================
        createNotification('comment_reply',
                           parent_obj['meta']['created_by'],
                           session=s(),
                           other=note_obj)
        s.mark_succeed()