def like(request):
    user = KeyBackend().authenticate(request)
    if request.user.is_authenticated:
        try:
            vit = Vit.objects.get(id=request.GET.get('vit_pk'))
            if vit.likes.filter(id=request.user.id).exists():
                vit.likes.remove(request.user)
                vit.like_count -= 1
                vit.save()
                return JsonResponse({'status': 'success', 'likes': vit.likes.count(), 'liked': False}, status=200)
            else:
                vit.likes.add(request.user)
                vit.like_count += 1
                vit.save()
                if (vit.user != request.user):
                    notify(message=f"{request.user.username.title()} liked your Vit - '{vit.body}'", notification_type='like', to_user=vit.user, by_user=request.user, link=reverse('vit_detail', kwargs={'pk': vit.id}))
                return JsonResponse({'status': 'success', 'likes': vit.likes.count(), 'liked': True}, status=200)
        except Vit.DoesNotExist:
            return JsonResponse({'error': 'Vit not found'}, status=404)
    else:
        return JsonResponse({'error': 'You must be logged in'}, status=401)
def like(request):
    user = KeyBackend().authenticate(request)
    if request.user.is_authenticated:
        try:
            vit = Vit.objects.get(id=request.GET.get('vit_pk'))
            if vit.likes.filter(id=request.user.id).exists():
                vit.likes.remove(request.user)
                vit.like_count -= 1
                vit.save()
                return JsonResponse({'status': 'success', 'likes': vit.likes.count(), 'liked': False}, status=200)
            else:
                vit.likes.add(request.user)
                vit.like_count += 1
                vit.save()
                if (vit.user != request.user):
                    notify(message=f"{request.user.username.title()} liked your Vit - '{vit.body}'", notification_type='like', to_user=vit.user, by_user=request.user, link=reverse('vit_detail', kwargs={'pk': vit.id}))
                return JsonResponse({'status': 'success', 'likes': vit.likes.count(), 'liked': True}, status=200)
        except Vit.DoesNotExist:
            return JsonResponse({'error': 'Vit not found'}, status=404)
    else:
        return JsonResponse({'error': 'You must be logged in'}, status=401)