• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gcivil-nyu-org / INT2-Monday-Spring2024-Team-1 / 968

26 Apr 2024 07:41PM UTC coverage: 90.503% (-1.0%) from 91.473%
968

Pull #261

travis-pro

jazwu
requirement
Pull Request #261: Chat Rebuilt with Pusher

15 of 37 new or added lines in 2 files covered. (40.54%)

1 existing line in 1 file now uncovered.

1782 of 1969 relevant lines covered (90.5%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.36
/chat/views.py
1
from django.http import JsonResponse
1✔
2
from django.shortcuts import render, get_object_or_404, redirect
1✔
3
from django.contrib.auth.decorators import login_required
1✔
4
from healthConfig.settings import (
1✔
5
    PUSHER_APP_ID,
6
    PUSHER_KEY,
7
    PUSHER_SECRET,
8
    PUSHER_CLUSTER,
9
)
10
from healthScore.models import User, Hospital
1✔
11
from .models import ChatSession, Message
1✔
12
from pusher import Pusher
1✔
13
from django.views.decorators.csrf import csrf_exempt
1✔
14

15
pusher = Pusher(
1✔
16
    app_id=PUSHER_APP_ID,
17
    key=PUSHER_KEY,
18
    secret=PUSHER_SECRET,
19
    cluster=PUSHER_CLUSTER,
20
    ssl=True,
21
)
22

23

24
@login_required(login_url="/")
1✔
25
def get_chat_session(request, receiver_id):
1✔
26
    sender = request.user
×
27
    receiver = get_object_or_404(User, id=receiver_id)
×
28

29
    if sender.is_patient:
×
30
        # current chat and messages
31
        chat, created = ChatSession.objects.get_or_create(
×
32
            patient=sender, healthcareWorker=receiver
33
        )
34
        messages = chat.messages.all()
×
35
        # users that have been chatted with current user before
36
        receiver_ids = (
×
37
            ChatSession.objects.filter(patient_id=sender.id)
38
            .order_by("-createdAt")
39
            .values_list("healthcareWorker_id", flat=True)
40
            .distinct()
41
        )
42
        receivers = User.objects.filter(id__in=receiver_ids)
×
43
        receivers = list(receivers)
×
44
        receivers.sort(key=lambda receiver: list(receiver_ids).index(receiver.id))
×
45
    elif sender.is_healthcare_worker:
×
46
        # current chat and messages
47
        chat, created = ChatSession.objects.get_or_create(
×
48
            patient=receiver, healthcareWorker=sender
49
        )
50
        messages = chat.messages.all()
×
51
        # users that have been chatted with current user before
52
        receiver_ids = (
×
53
            ChatSession.objects.filter(healthcareWorker_id=sender.id)
54
            .order_by("-createdAt")
55
            .values_list("patient_id", flat=True)
56
            .distinct()
57
        )
58
        receivers = User.objects.filter(id__in=receiver_ids)
×
59
        receivers = list(receivers)
×
60
        receivers.sort(key=lambda receiver: list(receiver_ids).index(receiver.id))
×
61

62
    context = {
×
63
        "receiver_id": receiver_id,
64
        "receiver": receiver,
65
        "receivers": receivers,
66
        "messages": messages,
67
        "pusher_key": PUSHER_KEY,
68
    }
69

70
    return render(request, "chat/chat.html", context)
×
71

72

73
def chat_view(request):
1✔
74
    # get all chat sessions related to current user
75
    chat_log = (
×
76
        ChatSession.objects.filter(patient=request.user)
77
        .union(ChatSession.objects.filter(healthcareWorker=request.user))
78
        .order_by("-createdAt")
79
    )
80

81
    if chat_log.exists():
×
82
        # if it exists, load the latest chat
83
        first_chat = chat_log.first()
×
84
        print(first_chat.healthcareWorker.id, first_chat.patient.id)
×
85
        return redirect(
×
86
            "chat:get_chat_session",
87
            receiver_id=(
88
                first_chat.healthcareWorker.id
89
                if request.user == first_chat.patient
90
                else first_chat.patient.id
91
            ),
92
        )
93
    else:
94
        return render(request, "chat/no_chat.html")
×
95

96

97
@login_required(login_url="/")
1✔
98
def select_user_view(request):
1✔
99
    hospitals = Hospital.objects.all()
×
100
    return render(request, "chat/select_user.html", {"hospitals": hospitals})
×
101

102

103
@csrf_exempt
1✔
104
def pusher_authentication(request):
1✔
NEW
105
    channel_name = request.POST["channel_name"]
×
NEW
106
    print(channel_name)
×
NEW
107
    if channel_name.startswith(
×
108
        f"private-chat-{request.user.id}-"
109
    ) or channel_name.endswith(f"-{request.user.id}"):
NEW
110
        auth = pusher.authenticate(
×
111
            channel=channel_name, socket_id=request.POST["socket_id"]
112
        )
NEW
113
        return JsonResponse(auth)
×
NEW
114
    return JsonResponse({"message": "Forbidden"}, status=403)
×
115

116

117
def get_channel_name(author_id, other_user_id):
1✔
NEW
118
    if int(author_id) < int(other_user_id):
×
NEW
119
        return f"private-chat-{author_id}-{other_user_id}"
×
120
    else:
NEW
121
        return f"private-chat-{other_user_id}-{author_id}"
×
122

123

124
@csrf_exempt
1✔
125
def send_message(request):
1✔
NEW
126
    if request.method == "POST":
×
NEW
127
        content = request.POST.get("message")
×
NEW
128
        author_id = request.POST.get("author_id")
×
NEW
129
        other_user_id = request.POST.get("receiver_id")
×
130

NEW
131
        channel_name = get_channel_name(author_id, other_user_id)
×
132

NEW
133
        pusher.trigger(
×
134
            channel_name,
135
            "new_message",
136
            {"author_id": author_id, "receiver_id": other_user_id, "message": content},
137
        )
138

NEW
139
        author = User.objects.get(id=author_id)
×
NEW
140
        other_user = User.objects.get(id=other_user_id)
×
141

NEW
142
        new_message = Message.objects.create(author=author, content=content)
×
NEW
143
        chat_session, created = ChatSession.objects.get_or_create(
×
144
            patient=author if author.is_patient else other_user,
145
            healthcareWorker=author if author.is_healthcare_worker else other_user,
146
        )
147

NEW
148
        chat_session.messages.add(new_message)
×
149

NEW
150
        return JsonResponse(
×
151
            {"status": "success", "message": "Message sent successfully"}
152
        )
153
    else:
NEW
154
        return JsonResponse(
×
155
            {"status": "error", "message": "Invalid request"}, status=400
156
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc