Exemple #1
0
def add_reply_notification(sender, instance, created, **kwargs):
    if created:
        reply = instance
        if not reply.from_user_id == reply.to_user_id:
            r = add_notification(one_or_many='one',
                                 from_user_id=reply.from_user_id,
                                 to_user_id=reply.to_user_id,
                                 action='reply',
                                 body='{}评论了你发表的图片:{}'.format(
                                     reply.from_user.last_name, reply.body),
                                 app='photo:{}'.format(reply.photo.id))
            print(r)
Exemple #2
0
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({'code': 2, 'msg': '用户尚未登录'})

        redis = get_redis()
        identity = request.GET['identity']
        fOrUnf = request.GET['fOrUnf']

        if fOrUnf == 'follow':
            # 判断被关注用户是否设置了隐私账号
            if int(
                    redis.hget("user:{}:detail".format(identity),
                               'private').decode()):
                redis.sadd("user:{}:followRequests".format(identity),
                           request.user.username)
                add_notification(one_or_many='one',
                                 from_user_id=request.user.username,
                                 to_user_id=identity,
                                 action='follow',
                                 body='{}请求关注你'.format(request.user.last_name),
                                 app='user:{}'.format(request.user.username))
            else:
                redis.sadd("user:{}:follows".format(request.user.username),
                           identity)
                redis.sadd("user:{}:fans".format(identity),
                           request.user.username)
                add_notification(one_or_many='one',
                                 from_user_id=request.user.username,
                                 to_user_id=identity,
                                 action='follow',
                                 body='{}关注了你'.format(request.user.last_name),
                                 app='user:{}'.format(request.user.username))

        if fOrUnf == 'unfollow':
            redis.srem("user:{}:follows".format(request.user.username),
                       identity)
            redis.srem("user:{}:fans".format(identity), request.user.username)

        return JsonResponse({'code': 1, 'msg': '操作成功'})
Exemple #3
0
def add_sub_reply_notification(sender, instance, created, **kwargs):
    if created:
        sub_reply = instance
        if not sub_reply.from_user_id == sub_reply.to_user_id:
            r = add_notification(
                one_or_many='one',
                from_user_id=sub_reply.from_user_id,
                to_user_id=sub_reply.to_user_id,
                action='reply',
                body='{}回复了你的评论:{}'.format(
                    sub_reply.from_user.last_name, sub_reply.body),
                app='blog:{}'.format(sub_reply.blog.id)
            )
            print(r)
Exemple #4
0
def add_photo_notification(sender, instance, created, **kwargs):
    if created:
        photo = instance
        redis = get_redis()
        redis_key = 'user:{}:fans'.format(photo.author_id)
        fans = redis.smembers(redis_key)
        fans = [fan.decode() for fan in fans]
        r = add_notification(one_or_many='many',
                             from_user_id=photo.author_id,
                             to_users=fans,
                             action='post',
                             body='{}发表了新图片'.format(photo.author.last_name),
                             app='photo:{}'.format(photo.id))
        print(r)
Exemple #5
0
def add_blog_notification(sender, instance, created, **kwargs):
    if created:
        blog = instance
        redis = get_redis()
        redis_key = 'user:{}:fans'.format(blog.author_id)
        fans = redis.smembers(redis_key)
        fans = [fan.decode() for fan in fans]
        r = add_notification(
            one_or_many='many',
            from_user_id=blog.author_id,
            to_users=fans,
            action='post',
            body='{}发表了新博客:《{}》'.format(blog.author.last_name, blog.title),
            app='blog:{}'.format(blog.id)
        )
        print(r)
Exemple #6
0
    def get(self, request):
        if not request.user.is_authenticated:
            return JsonResponse({'code': 4, 'msg': '请先登录再进行操作'})
        way = request.GET['way']
        identity = request.GET['id']
        addOrRem = request.GET['aor']

        redis = get_redis()
        support_ways = ['blog', 'breply', 'bsreply']
        if way not in support_ways:
            return JsonResponse({'code': 2, 'msg': '出错,找不到所给参数的有效集合'})

        key = way + ':' + identity + ':likes'
        if addOrRem == 'add':
            if way == 'blog':
                blog = Blog.objects.filter(pk=identity).first()
                action = 'bloglike'
                to_user_id = blog.author_id
                body = '{}给你的博客《{}》点了个赞'.format(request.user.last_name,
                                                blog.title)
                app = 'blog:{}'.format(blog.id)
            else:
                if way == 'breply':
                    reply = BlogReply.objects.filter(pk=identity).first()
                    action = 'replylike'
                if way == 'bsreply':
                    reply = BlogSubReply.objects.filter(pk=identity).first()
                    action = 'subreplylike'
                to_user_id = reply.from_user_id
                body = '{}赞了你的评论:{}'.format(request.user.last_name, reply.body)
                app = 'blog:{}'.format(reply.blog_id)
            if not to_user_id == request.user.username:
                notification = Notification.objects.filter(
                    Q(action=action) & Q(app=app)).last()
                if notification:
                    if notification.viewed:
                        add_notification(one_or_many='one',
                                         from_user_id=request.user.username,
                                         to_user_id=to_user_id,
                                         action=action,
                                         body=body,
                                         app=app)
                    else:
                        if way == 'blog':
                            body = '{}等多人给你的博客《{}》点赞'.format(
                                request.user.last_name, blog.title)
                        else:
                            body = '{}等多人赞了你的评论:“{}”'.format(
                                request.user.last_name, reply.body)
                        notification.body = body if len(
                            body) < 150 else body[:147] + '...'
                        notification.save()
                else:
                    add_notification(one_or_many='one',
                                     from_user_id=request.user.username,
                                     to_user_id=to_user_id,
                                     action=action,
                                     body=body,
                                     app=app)
            redis.sadd(key, request.user.username)
        if addOrRem == 'rem':
            redis.srem(key, request.user.username)
        return JsonResponse({'code': 1, 'msg': '操作成功'})