Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

2.9 KB
Raw Blame
# See https://zulip.readthedocs.io/en/latest/subsystems/onboarding-steps.html
# for documentation on this subsystem.
from dataclasses import dataclass

from django.conf import settings

from zerver.models import OnboardingStep, UserProfile


@dataclass
class APIOnboardingStep:
    type: str
    name: str


@dataclass
class OneTimeNotice:
    name: str

    def to_dict(self) -> APIOnboardingStep:
        return APIOnboardingStep(
            type="one_time_notice",
            name=self.name,
        )


@dataclass
class OneTimeAction:
    name: str

    def to_dict(self) -> APIOnboardingStep:
        return APIOnboardingStep(
            type="one_time_action",
            name=self.name,
        )


ONE_TIME_NOTICES: list[OneTimeNotice] = [
    OneTimeNotice(
        name="visibility_policy_banner",
    ),
    OneTimeNotice(
        name="intro_inbox_view_modal",
    ),
    OneTimeNotice(
        name="intro_recent_view_modal",
    ),
    OneTimeNotice(
        name="first_stream_created_banner",
    ),
    OneTimeNotice(
        name="jump_to_conversation_banner",
    ),
    OneTimeNotice(
        name="non_interleaved_view_messages_fading",
    ),
    OneTimeNotice(
        name="interleaved_view_messages_fading",
    ),
    OneTimeNotice(
        name="intro_resolve_topic",
    ),
    OneTimeNotice(
        name="navigation_tour_video",
    ),
    OneTimeNotice(
        name="intro_go_to_conversation_tooltip",
    ),
]

ONE_TIME_ACTIONS = [OneTimeAction(name="narrow_to_dm_with_welcome_bot_new_user")]

ALL_ONBOARDING_STEPS: list[OneTimeNotice | OneTimeAction] = ONE_TIME_NOTICES + ONE_TIME_ACTIONS


def get_next_onboarding_steps(user: UserProfile) -> list[APIOnboardingStep]:
    # If a Zulip server has disabled the tutorial, never send any
    # onboarding steps.
    if not settings.TUTORIAL_ENABLED:
        return []

    seen_onboarding_steps: list[str] = list(
        OnboardingStep.objects.filter(user=user).values_list("onboarding_step", flat=True)
    )
    if settings.NAVIGATION_TOUR_VIDEO_URL is None:
        # Server admin disabled navigation tour video, treat it as seen.
        seen_onboarding_steps.append("navigation_tour_video")
    seen_onboarding_steps_set = frozenset(seen_onboarding_steps)

    onboarding_steps: list[APIOnboardingStep] = []
    for onboarding_step in ALL_ONBOARDING_STEPS:
        if onboarding_step.name in seen_onboarding_steps_set:
            continue
        onboarding_steps.append(onboarding_step.to_dict())

    return onboarding_steps


def copy_onboarding_steps(source_profile: UserProfile, target_profile: UserProfile) -> None:
    source_steps = OnboardingStep.objects.filter(user=source_profile)
    OnboardingStep.objects.bulk_create(
        [
            OnboardingStep(
                user=target_profile,
                onboarding_step=step.onboarding_step,
                timestamp=step.timestamp,
            )
            for step in source_steps
        ]
    )