class User < ActiveRecord::Base enum role: [:user, :vip, :admin] after_initialize :set_default_role, if: :new_record? validates :email, presence: true, uniqueness: true validates :soft_token, presence: true, uniqueness: true has_many :canisters has_many :doodads has_many :projects has_many :profiles has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :microposts, as: :micropostable def icid stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize if stp.principal.nil? #TODO use principal if !soft_token.nil? if `dfx identity use #{soft_token}; echo $?`.to_i == 255 `dfx identity new #{soft_token} --disable-encryption` `dfx identity use #{soft_token}` @principal = `dfx identity get-principal` @icid = `dfx ledger account-id` `dfx identity use anonymous` stp.principal = @principal stp.save end end elsif `dfx identity use #{soft_token}` @icid = `dfx ledger account-id` `dfx identity use anonymous` end return @icid.strip end def principal stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize if stp.principal.nil? #TODO use principal if !soft_token.nil? if `dfx identity use #{soft_token}; echo $?`.to_i == 255 `dfx identity new #{soft_token} --disable-encryption` `dfx identity use #{soft_token}` @principal = `dfx identity get-principal` `dfx identity use anonymous` stp.principal = @principal stp.save end end end return stp.principal.strip end def icp_balance `dfx identity use #{soft_token}` balance = `dfx ledger --network ic balance` `dfx identity use anonymous` return balance.strip end def set_default_role self.role ||= :user end def soft_user? self.email.empty? end def signed_in? !soft_user? end def to_s display_name end def display_name self.email.split("@")[0] || "Guest#{self.soft_token[0..5]}" end def sit_in(seat) seat.sit self end def stand_from(table) return if table == nil @seats = [] if self.id @seats << table.seats.where(user_id: self.id) end if self.soft_token @seats << table.seats.where(user_soft_token: self.soft_token) end @seats.flatten.each do |seat| seat.stand(self) end end # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable # Follows a user. def follow(leader) active_relationships.create(followed_id: leader.id) end # Unfollows a user. def unfollow(leader) active_relationships.find_by(followed_id: leader.id).destroy end # Returns true if the current user is following the leader. def following?(leader) following.include?(leader) end def feed Micropost.where("user_id = ?", id) end end