Add user avatars

This commit is contained in:
Râu Cao
2023-09-06 12:15:25 +02:00
parent 86f85a90f4
commit 9e5aeaf572
3 changed files with 82 additions and 24 deletions

View File

@@ -5,6 +5,12 @@ class User < ApplicationRecord
serialize :preferences, UserPreferences
#
# File attachments
#
has_one_attached :avatar
# Relations
has_many :invitations, dependent: :destroy
has_one :invitation, inverse_of: :invitee, foreign_key: 'invited_user_id'
@@ -40,6 +46,7 @@ class User < ApplicationRecord
validates_uniqueness_of :nostr_pubkey, allow_blank: true
validate :acceptable_avatar
scope :confirmed, -> { where.not(confirmed_at: nil) }
scope :pending, -> { where(confirmed_at: nil) }
scope :all_except, -> (user) { where.not(id: user) }
@@ -140,6 +147,14 @@ class User < ApplicationRecord
@display_name ||= ldap_entry[:display_name]
end
def avatar_64px
avatar.variant(resize_to_fill: [64, 64])
end
def avatar_256px
avatar.variant(resize_to_fill: [256, 256])
end
def services_enabled
ldap_entry[:service] || []
end
@@ -168,4 +183,17 @@ class User < ApplicationRecord
return @ldap_service if defined?(@ldap_service)
@ldap_service = LdapService.new
end
def acceptable_avatar
return unless avatar.attached?
if avatar.blob.byte_size > 1.megabyte
errors.add(:avatar, "file size is too large")
end
acceptable_types = ["image/jpeg", "image/png"]
unless acceptable_types.include?(avatar.content_type)
errors.add(:avatar, "must be a JPEG or PNG file")
end
end
end