Skip to content

Commit

Permalink
Merge pull request #59 from dimagi/ce/get-or-create-channel
Browse files Browse the repository at this point in the history
get or create channel
  • Loading branch information
calellowitz authored Dec 11, 2024
2 parents 10824ee + 7feba02 commit d1e168c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions messaging/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_current_message_server(request):
encoded_credentials = auth_header.split(' ')[1]
decoded_credentials = base64.b64decode(encoded_credentials).decode('utf-8')
client_id, client_secret = decoded_credentials.split(':')
server = get_object_or_404(MessageServer, oauth_application__client_id=client_id)
server = get_object_or_404(MessageServer, server_id=client_id)
return server


Expand Down Expand Up @@ -170,18 +170,23 @@ def post(self, request, *args, **kwargs):
channel_source = data["channel_source"]
server = get_current_message_server(request)
user = get_object_or_404(ConnectUser, username=connect_id)
channel = Channel.objects.create(server=server, connect_user=user, channel_source=channel_source)
message = MessageData(
usernames=[channel.connect_user.username],
title="Channel created",
body="Please provide your consent to send/receive message.",
data={"keyUrl": server.key_url},
)
# send fcm notification.
send_bulk_message(message)
return JsonResponse(
{"channel_id": str(channel.channel_id)}, status=status.HTTP_201_CREATED
)
channel, created = Channel.objects.get_or_create(server=server, connect_user=user, channel_source=channel_source)
if created:
message = MessageData(
usernames=[channel.connect_user.username],
title="Channel created",
body="Please provide your consent to send/receive message.",
data={"keyUrl": server.key_url},
)
# send fcm notification.
send_bulk_message(message)
return JsonResponse(
{"channel_id": str(channel.channel_id)}, status=status.HTTP_201_CREATED
)
else:
return JsonResponse(
{"channel_id": str(channel.channel_id)}, status=status.HTTP_200_OK
)


class SendServerConnectMessage(APIView):
Expand Down

0 comments on commit d1e168c

Please sign in to comment.