Sync DM chat name/icon on user changes
Add a signal to keep direct-message chat name and icon in sync when a user updates their username or avatar, and register it in AppConfig.ready. Also initialize a DM chat's name/icon on creation from the other member's username/avatar if those fields are missing, saving only the changed fields.
This commit is contained in:
@@ -53,7 +53,19 @@ class ChatViewSet(viewsets.ModelViewSet):
|
||||
return Chat.objects.filter(Q(members=user) | Q(owner=user)).distinct()
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(owner=self.request.user)
|
||||
chat = serializer.save(owner=self.request.user)
|
||||
if chat.chat_type == Chat.ChatType.DM:
|
||||
other = chat.members.exclude(pk=self.request.user.pk).first()
|
||||
if other:
|
||||
update_fields = []
|
||||
if not chat.name:
|
||||
chat.name = other.username
|
||||
update_fields.append('name')
|
||||
if not chat.icon and other.avatar:
|
||||
chat.icon = other.avatar
|
||||
update_fields.append('icon')
|
||||
if update_fields:
|
||||
chat.save(update_fields=update_fields)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Member management
|
||||
|
||||
Reference in New Issue
Block a user