notifications: do not notify anonymous user (#64497)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Frédéric Péters 2022-05-23 15:27:16 +02:00
parent cd71255ae5
commit ed358aed7e
1 changed files with 12 additions and 1 deletions

View File

@ -69,9 +69,20 @@ class Add(GenericAPIView):
return Response(response, status.HTTP_400_BAD_REQUEST)
payload = serializer.validated_data
notification_ids = []
if payload.get('name_ids'):
# if name_ids were given, get the existing users
users = payload.get('users')
elif request.user and request.user.is_authenticated:
# if not, add notification to current user (it will usually be set
# via the custom PublikAuthentication djangorestfamework authentication
# class).
users = [request.user]
else:
response = {'err': 1, 'err_desc': 'no users given'}
return Response(response, status.HTTP_400_BAD_REQUEST)
try:
with transaction.atomic():
for user in payload.get('users') or [request.user]:
for user in users:
notification = Notification.notify(
user=user,
summary=payload['summary'],