From 30419f0fbf44514d36e8d213745f4959bfa87210 Mon Sep 17 00:00:00 2001
From: gregor <gregor.schulz@holi.social>
Date: Fri, 6 Dec 2024 10:43:43 +0100
Subject: [PATCH] trust user.identity to imply matrix user presence

---
 src/handlers/user-name-updated-handler.ts | 18 ++++++------------
 src/helpers/matrixClient.ts               |  5 -----
 2 files changed, 6 insertions(+), 17 deletions(-)

diff --git a/src/handlers/user-name-updated-handler.ts b/src/handlers/user-name-updated-handler.ts
index 6d369ad..3e6efdd 100644
--- a/src/handlers/user-name-updated-handler.ts
+++ b/src/handlers/user-name-updated-handler.ts
@@ -1,9 +1,5 @@
-import {
-  isUsernameAvailable,
-  getMatrixId,
-  setAvatarUrl,
-  setDisplayName
-} from '../helpers/matrixClient'
+import * as matrix from '../helpers/matrixClient'
+import { getMatrixId } from '../helpers/matrixClient'
 import { logPhase, logWarn } from '../logger'
 import { UserNameUpdatedDataPayload } from '../types'
 
@@ -14,12 +10,10 @@ export const userNameUpdatedHandler = async (
   try {
     await logPhase(messageId, 'userNameUpdateHandler.updateName for identity ' + user.identity + ' to ' + user.name + ' and avatar to ' + user.avatar, async () => {
       if (user.identity) {
-        if (!(await isUsernameAvailable(user.identity))) {
-          logWarn(messageId, `Could not update user profile for ${user.identity}, because the user was not found.`)
-          return
-        }
-        await setDisplayName(user.name, getMatrixId(user.identity))
-        await setAvatarUrl(user.avatar, getMatrixId(user.identity))
+        await matrix.setDisplayName(user.name, getMatrixId(user.identity))
+        await matrix.setAvatarUrl(user.avatar, getMatrixId(user.identity))
+      } else {
+        logWarn(messageId, `Could not update user profile for ${user.identity}, missing matrix identity.`)
       }
     })
   } catch (error) {
diff --git a/src/helpers/matrixClient.ts b/src/helpers/matrixClient.ts
index 32b2198..4c55c02 100644
--- a/src/helpers/matrixClient.ts
+++ b/src/helpers/matrixClient.ts
@@ -200,11 +200,6 @@ export const deactivateAccount = async (matrixId: string) => {
     throw new Error(`Deactivating account did not succeed ${response.status}: ${response.json}`)
 }
 
-export const isUsernameAvailable = async (localpart: string) => {
-  const response = await authedFetchResponse(`/_synapse/admin/v1/username_available?username=${localpart}`, 'GET')
-  return response.status === 200
-}
-
 export const getRoomStateEvents = async (roomId: string): Promise<UknownRoomStateEvent[]> => {
   if (!roomId) return []
   return (await authedFetchJson('/_matrix/client/v3/rooms/' + roomId + '/state', 'GET')) as UknownRoomStateEvent[]
-- 
GitLab