Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Comment serialization for deleted users #154

Merged
merged 1 commit into from Aug 16, 2023
Merged

Conversation

s3lase
Copy link
Contributor

@s3lase s3lase commented Aug 14, 2023

Serializing topics with deleted comment users results in unhandled errors.

This change guards the user details access on QuestionAnswerComment records and also cleans up post voting related records when a user is destroyed via UserDestroyer.

Serializing topics with deleted comment users results in unhandled
errors.

This change guards the user details access on `QuestionAnswerComment`
records and also cleans up post voting related records when a user is
destroyed via UserDestroyer.
@jjaffeux
Copy link

Did you consider using some null object pattern here?

# frozen_string_literal: true

module DiscoursePostVoting
  class DeletedCommentUser < User
    def username
      I18n.t("discourse_post_voting.deleted_user_username")
    end
    
    def name
      I18n.t("discourse_post_voting.deleted_user_name")
    end

    def avatar_template
      "/plugins/discourse-post-voting/images/deleted-user-avatar.png"
    end
  end
end

class QuestionAnswerCommentSerializer < ApplicationSerializer
  def user
    object.user || DiscoursePostVoting::DeletedCommentUser.new
  end
  
  def username
    user.username
  end
  
  def name
    user.name
  end
end

This might not suit your use case, but it's an easy solution to have a system working in every cases without having to clean the DB and maintaining state. eg: should we really remove the votes of a deleted user?

@s3lase
Copy link
Contributor Author

s3lase commented Aug 15, 2023

Did you consider using some null object pattern here?

Yeah. I considered that but tabled it for much later when we have more cases of details of possibly deleted users being accessed. Returning a null user's username would have required additional plumbing work in core to have user cards work with a null user's usernames.

This might not suit your use case, but it's an easy solution to have a system working in every cases without having to clean the DB and maintaining state. eg: should we really remove the votes of a deleted user?

I say yes. Looking at UserDestroy we seem to delete a bunch of other records associated with the user. The vote deletion is an extension of that.

@s3lase s3lase merged commit 97d468c into main Aug 16, 2023
4 checks passed
@s3lase s3lase deleted the fix/deleted-commenter branch August 16, 2023 10:45
@s3lase
Copy link
Contributor Author

s3lase commented Aug 16, 2023

thanks @ZogStriP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants