Exemple #1
0
def cmd_recommend(request, message="", comment="", unrecommend=""):
        """Recommend or unrecommend message."""
        message_id = canonic_message(message).upper()
        message_obj = yield objs.Message.find_one({'id': message_id})
        if not message_obj:
            defer.returnValue(dict(
                ok=False,
                desc='No such message.'))
        if unrecommend:
            if request.user['name'] in message_obj['recommendations']:
                yield objs.Message.mupdate(
                    {'id': message_id},
                    {'$pull': {'recommendations': request.user['name']}})
                rcount = len(message_obj['recommendations']) - 1
                all_recos = list(message_obj['recommendations'])
                all_recos.remove(request.user['name'])
                publish('upd_recommendations_count',
                        message_id, rcount, all_recos)
                publish('upd_recommendations_count_in_' + message_id,
                        message_id, rcount, all_recos)
                defer.returnValue(dict(
                    ok=True,
                    desc='Message deleted from your recommendations list.'))
            else:
                defer.returnValue(dict(
                    ok=False,
                    desc='You haven\'t recommended this message.'))

        post_throttle = yield throttle_check(request.user['name'])
        ok, rest = yield bnw.core.post.recommendMessage(
            request.user, message_obj, comment)
        yield throttle_update(request.user['name'], post_throttle)
        if ok:
            qn, recepients, replies = rest
            defer.returnValue(dict(
                ok=True,
                desc='Recommended and delivered to %d users (%d replies).' % (
                    recepients, replies)))
        else:
            defer.returnValue(dict(
                ok=False,
                desc=rest))
Exemple #2
0
def cmd_recommend(request, message="", comment="", unrecommend=""):
    """Recommend or unrecommend message."""
    message_id = canonic_message(message).upper()
    message_obj = yield objs.Message.find_one({'id': message_id})
    if not message_obj:
        defer.returnValue(dict(ok=False, desc='No such message.'))
    if unrecommend:
        if request.user['name'] in message_obj['recommendations']:
            yield objs.Message.mupdate(
                {'id': message_id},
                {'$pull': {
                    'recommendations': request.user['name']
                }})
            rcount = len(message_obj['recommendations']) - 1
            all_recos = list(message_obj['recommendations'])
            all_recos.remove(request.user['name'])
            publish('upd_recommendations_count', message_id, rcount, all_recos)
            publish('upd_recommendations_count_in_' + message_id, message_id,
                    rcount, all_recos)
            defer.returnValue(
                dict(ok=True,
                     desc='Message deleted from your recommendations list.'))
        else:
            defer.returnValue(
                dict(ok=False, desc='You haven\'t recommended this message.'))

    yield throttle_check(request.user['name'])
    ok, rest = yield bnw.core.post.recommendMessage(request.user, message_obj,
                                                    comment)
    if ok:
        qn, recipients, replies = rest
        defer.returnValue(
            dict(ok=True,
                 desc='Recommended and delivered to %d users (%d replies).' %
                 (recipients, replies)))
    else:
        defer.returnValue(dict(ok=False, desc=rest))
Exemple #3
0
def cmd_delete(request, message="", last=False):
    """ Удаление

    Удаление поста или коммента.

    redeye: delete --message=123456, d -m ABCDEF/123, d --last, d -l
    simple: D #123456, D #ABDEF/123, D L"""
    message = canonic_message_comment(message).upper()
    if last:
        lastcomment = list((yield objs.Comment.find_sort({'user': request.user['name']}, [('date', -1)], limit=1)))
        lastmessage = list((yield objs.Message.find_sort({'user': request.user['name']}, [('date', -1)], limit=1)))
        if lastcomment:
            if lastmessage:
                message = lastmessage[0]['id'] if lastmessage[0][
                    'date'] > lastcomment[0]['date'] else lastcomment[0]['id']
            else:
                message = lastcomment[0]['id']
        else:
            if lastmessage:
                message = lastmessage[0]['id']
            else:
                defer.returnValue(
                    dict(ok=False, desc='Nothing to delete.')
                )
    if not message:
        defer.returnValue(
            dict(ok=False, desc='Usage: delete -m POST[/COMMENT]')
        )
    splitpost = message.split('/')
    message_id = splitpost[0].upper()
    comment_id = message if len(splitpost) > 1 else None
    if comment_id:
        comment = yield objs.Comment.find_one({'id': comment_id, 'message': message_id})
    post = yield objs.Message.find_one({'id': message_id})
    if comment_id:
        if not comment:
            defer.returnValue(
                dict(ok=False, desc='No such comment')
            )
        if request.user['name'] not in (comment['user'], post['user'], comment.get('real_user'), post.get('real_user')):
            defer.returnValue(
                dict(ok=False, desc='Not your comment and not your message.')
            )
        _ = (yield objs.Message.mupdate({'id': message_id}, {'$inc': {'replycount': -1}}))
        _ = yield objs.Comment.remove({'id': comment['id'], 'message': comment['message'], 'user': comment['user']})
        publish('del_comment_in_' + message_id, comment_id)
        publish('upd_comments_count', message_id, post['replycount'] - 1)
        publish('upd_comments_count_in_' + message_id,
                message_id, post['replycount'] - 1)
        defer.returnValue(
            dict(ok=True, desc='Comment %s removed.' % (comment_id,))
        )
    else:
        if not post:
            defer.returnValue(
                dict(ok=False, desc='No such message.')
            )
        if request.user['name'] not in (post['user'], post.get('real_user')):
            defer.returnValue(
                dict(ok=False, desc='Not your message.')
            )
        _ = yield objs.Message.remove({'id': post['id'], 'user': post['user']})
        _ = yield objs.Comment.remove({'message': post['id']})
        publish('del_message', message_id)
        publish('del_message_on_user_' + post['user'], message_id)
        defer.returnValue(
            dict(ok=True, desc='Message %s removed.' % (message,))
        )