Watch topic using email address without requiring registration

I’ve spent some quality time digging through the code. I think something like this (warning: very likely not working as I haven’t tried it yet) would work to allow the user to post a reply with just an email. I’d appreciate any comments you have.

I’m especially not sure if this is the best way to create a staged user. I haven’t found any method that specifically creates staged users though.

class SomePluginController < ApplicationController

  # Make sure there is a staged user in the db
  def ensure_user

    # See if staged user exists
    user = User.where(staged: true).with_email(params[:email].strip.downcase).first

    # Create a staged user manually
    if !user
      user = User.new
      user.staged = true
      user.email = params[:email]
      user.active = false
      user.save!
    end
    
    user
  end

  # Watch topic as a staged user
  def staged_watch

    user = ensure_user

    topic = Topic.find(params[:topic_id].to_i)
    TopicUser.change(user, topic.id, notification_level: params[:notification_level].to_i)

  end

  # Reply to topic as a staged user
  def staged_reply
 
    user = ensure_user

    manager = NewPostManager.new(user,
                             raw: params[:body],
                             topic_id: params[:topic_id])
    result = manager.perform

  end

end
3 Likes