File size: 2,711 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# == Schema Information
#
# Table name: users
#
#  id              :bigint(8)        not null, primary key
#  email           :string           not null
#  fname           :string
#  lname           :string
#  user_url        :string           not null
#  birthday        :date
#  sex             :string
#  password_digest :string           not null
#  session_token   :string           not null
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#

class User < ApplicationRecord
  validates :email, :session_token, :user_url, presence: true, uniqueness: true
  validates :password_digest, presence: true
  validates :password, length: { minimum: 6, allow_nil: true }
  after_initialize :ensure_session_token, :ensure_user_url
  before_create :attach_default_photos

  attr_reader :password

  has_many :authored_posts,
    foreign_key: :author_id,
    class_name: :Post

  has_one_attached :profile_picture
  has_one_attached :cover_photo

  def friends
    Friend.where("requesting_id = ? AND accepted = ?", self.id, true).or(
      Friend.where("requested_id = ? AND accepted = ?", self.id, true)
    ).map do |request| 
      if request.requesting_user.id == self.id
        request.requested_user
      else
        request.requesting_user
      end
    end
  end
  
  def wall_posts
    Post.where("recipient_id = ?", self.id)
      .order(created_at: :desc)
  end

  def feed_posts
    friend_ids = friends.map { |user| user.id } << self.id
    
    Post.where("author_id IN (?) AND recipient_id IN (?)", friend_ids, friend_ids)
  end

  def attach_default_photos
    self.profile_picture.attach(
      io: File.open(Rails.root.join('app', 'assets', 'images', 'profile.png')), 
      filename: 'profile.png', content_type: 'image/png'
    )

    self.cover_photo.attach(
      io: File.open(Rails.root.join('app', 'assets', 'images', 'cover.jpg')), 
      filename: 'cover.jpg', content_type: 'image/png'
    )
  end
    
  def self.generate_session_token
    SecureRandom.urlsafe_base64(16)
  end

  def self.find_by_credentials(email, password)
    user = User.find_by(email: email)
    user && user.is_password?(password) ? user : nil
  end

  def reset_session_token!
    self.session_token = User.generate_session_token;
    self.save
    self.session_token
  end

  def ensure_session_token
    self.session_token ||= User.generate_session_token
  end

  def ensure_user_url
    self.user_url ||= User.generate_session_token
  end

  def password=(password)
    @password = password
    self.password_digest = BCrypt::Password.create(password)
  end

  def is_password?(password)
    BCrypt::Password.new(password_digest).is_password?(password)
  end
end