Repository: toeverything/AFFiNE
File: packages/backend/server/src/core/notification/service.ts (TypeScript)

Code before:
      if (!receiver) {
        return;
      }
      const user = await this.models.user.getWorkspaceUser(
        input.body.createdByUserId
      );
      if (!user) {
        return;
      }
      const doc = await this.models.doc.getMeta(
        input.body.workspaceId,
        input.body.doc.id
      );
      const title = doc?.title ?? input.body.doc.title;
      const url = this.url.link(
        generateDocPath({
          workspaceId: input.body.workspaceId,
          docId: input.body.doc.id,
          mode: input.body.doc.mode,
          blockId: input.body.doc.blockId,
          elementId: input.body.doc.elementId,
        })
      );
      await this.mailer.sendMentionMail(receiver.email, {
        user,
        doc: {
          title,
          url,
        },
      });
      this.logger.log(`Mention email sent to user ${receiver.id}`);
    }
  }

  async createInvitation(input: InvitationNotificationCreate) {
    const isActive = await this.models.workspaceUser.getActive(
      input.body.workspaceId,
      input.userId
    );
    if (isActive) {
      this.logger.debug(
        `User ${input.userId} is already a active member of workspace ${input.body.workspaceId}, skip creating notification`
      );
      return;
    }
    await this.ensureWorkspaceContentExists(input.body.workspaceId);
    return await this.models.notification.createInvitation(
      input,
      NotificationType.Invitation
    );
  }

Review comment (refactor):
<!--__GRAPHITE_HTML_TAG_START__--><p class='graphite__hidden'><i>[Re: line +131]</i></p><!--__GRAPHITE_HTML_TAG_END__-->
https://app.graphite.dev/github/pr/toeverything/AFFiNE/10964/refactor(server)-improve-workspace-content-retrieval-from-database
<!--__GRAPHITE_HTML_TAG_START__--><p class='graphite__hidden'>See this comment inline on <a href="https://app.graphite.dev/github/pr/toeverything/AFFiNE/10859?utm_source=unchanged-line-comment">Graphite</a>.</p><!--__GRAPHITE_HTML_TAG_END__-->

Code after:
    const url = this.url.link(
      generateDocPath({
        workspaceId: input.body.workspaceId,
        docId: input.body.doc.id,
        mode: input.body.doc.mode,
        blockId: input.body.doc.blockId,
        elementId: input.body.doc.elementId,
      })
    );
    await this.mailer.send({
      name: 'Mention',
      to: receiver.email,
      props: {
        user: {
          $$userId: input.body.createdByUserId,
        },
        doc: {
          title,
          url,
        },
      },
    });
    this.logger.log(`Mention email sent to user ${receiver.id}`);
  }

  async createInvitation(input: InvitationNotificationCreate) {
    const isActive = await this.models.workspaceUser.getActive(
      input.body.workspaceId,
      input.userId
    );
    if (isActive) {
      this.logger.debug(
        `User ${input.userId} is already a active member of workspace ${input.body.workspaceId}, skip creating notification`
      );
      return;
    }
    await this.ensureWorkspaceContentExists(input.body.workspaceId);
    return await this.models.notification.createInvitation(
      input,
      NotificationType.Invitation
    );
  }

  async createInvitationAccepted(input: InvitationNotificationCreate) {
    await this.ensureWorkspaceContentExists(input.body.workspaceId);
    return await this.models.notification.createInvitation(
      input,
      NotificationType.InvitationAccepted
    );
  }