Просмотр исходного кода

Refactor AI assist notification logic in project list page (#26613)

GitOrigin-RevId: 849ab62b814f321452e5d6e53d968943cec60417
Domagoj Kriskovic 1 год назад
Родитель
Сommit
48379a9d86
1 измененных файлов с 48 добавлено и 18 удалено
  1. 48 18
      services/web/app/src/Features/Project/ProjectListController.mjs

+ 48 - 18
services/web/app/src/Features/Project/ProjectListController.mjs

@@ -411,24 +411,17 @@ async function projectListPage(req, res, next) {
     'papers-notification-banner'
     'papers-notification-banner'
   )
   )
 
 
-  await SplitTestHandler.promises.getAssignment(
-    req,
-    res,
-    'ai-assist-notification'
-  )
+  const aiAssistNotificationAssignment =
+    await SplitTestHandler.promises.getAssignment(
+      req,
+      res,
+      'ai-assist-notification'
+    )
 
 
-  const canUseAi = await PermissionsManager.promises.checkUserPermissions(
-    user,
-    ['use-ai']
-  )
-  const assistantDisabled = user.aiErrorAssistant?.enabled === false // the assistant has been manually disabled by the user
-  const subscription =
-    await SubscriptionLocator.promises.getUsersSubscription(userId)
-  const hasManuallyCollectedSubscription =
-    subscription?.collectionMethod === 'manual'
-  const canUseAiAssist =
-    canUseAi && !hasManuallyCollectedSubscription && !assistantDisabled
-  const hasAiAssist = user.features?.aiErrorAssistant
+  let showAiAssistNotification = false
+  if (aiAssistNotificationAssignment.variant !== 'default') {
+    showAiAssistNotification = await _showAiAssistNotification(user)
+  }
 
 
   const customerIoEnabled =
   const customerIoEnabled =
     await SplitTestHandler.promises.hasUserBeenAssignedToVariant(
     await SplitTestHandler.promises.hasUserBeenAssignedToVariant(
@@ -471,7 +464,7 @@ async function projectListPage(req, res, next) {
     hasIndividualPaidSubscription,
     hasIndividualPaidSubscription,
     userRestrictions: Array.from(req.userRestrictions || []),
     userRestrictions: Array.from(req.userRestrictions || []),
     customerIoEnabled,
     customerIoEnabled,
-    showAiAssistNotification: canUseAiAssist && !hasAiAssist,
+    showAiAssistNotification,
   })
   })
 }
 }
 
 
@@ -756,6 +749,43 @@ function _hasActiveFilter(filters) {
   )
   )
 }
 }
 
 
+async function _showAiAssistNotification(user) {
+  // Check if the assistant has been manually disabled by the user
+  if (user.aiErrorAssistant?.enabled === false) {
+    return false
+  }
+
+  // Check if the user can use AI features (policy check)
+  const canUseAi = await PermissionsManager.promises.checkUserPermissions(
+    user,
+    ['use-ai']
+  )
+  if (!canUseAi) {
+    return false
+  }
+
+  // Check if the user has a subscription with manually collected group admins (#22822)
+  const subscription = await SubscriptionLocator.promises.getUsersSubscription(
+    user._id
+  )
+  if (subscription?.collectionMethod === 'manual') {
+    return false
+  }
+
+  // Check if the user already has AI Assist via Overleaf
+  if (user.features?.aiErrorAssistant) {
+    return false
+  }
+  // Check if the user already has AI Assist via Writefull
+  const { isPremium: hasAiAssistViaWritefull } =
+    await UserGetter.promises.getWritefullData(user._id)
+  if (hasAiAssistViaWritefull) {
+    return false
+  }
+
+  return true
+}
+
 export default {
 export default {
   projectListPage: expressify(projectListPage),
   projectListPage: expressify(projectListPage),
   getProjectsJson: expressify(getProjectsJson),
   getProjectsJson: expressify(getProjectsJson),