使用电子邮件地址关注主题,无需注册

我花了一些宝贵的时间来研究代码。我认为类似这样的东西(警告:很可能无法正常工作,因为我还没有尝试过)可以允许用户仅通过电子邮件发布回复。我很想听听您的意见。

我尤其不确定这是创建暂存用户的最佳方法。虽然我还没有找到任何专门创建暂存用户的方法。

class SomePluginController < ApplicationController

  # 确保数据库中有一个暂存用户
  def ensure_user

    # 查看暂存用户是否存在
    user = User.where(staged: true).with_email(params[:email].strip.downcase).first

    # 手动创建一个暂存用户
    if !user
      user = User.new
      user.staged = true
      user.email = params[:email]
      user.active = false
      user.save!
    end
    
    user
  end

  # 以暂存用户的身份观看主题
  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

  # 以暂存用户的身份回复主题
  def staged_reply
 
    user = ensure_user

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

  end

end
3 个赞