Files
fizzy/app/models/user.rb
T
Kevin McConnell 0820badcc3 Add involvement types to accesses
This provides a way to set the level of involvement that a user has with
a collection, and from which we determine the level of notifications to
send. Users can be access-only, watching, or being notified about
everything.

If you're access-only, you won't get an notifications. If you're
watching, you'll only get notifications for the items you're watching
(which includes the items you've been assigned, have commented on, etc).
If you're set to everything you'll get notifications about all activity
in that collection.

This change replaces our previous concept of subscriptions. Where
previously you'd subscribe to a collection to get notifications in it,
now you'll simply set the notification level on your access.
2025-04-03 16:35:13 +01:00

64 lines
1.7 KiB
Ruby

class User < ApplicationRecord
include Avatar, Role, Transferable
belongs_to :account
has_many :sessions, dependent: :destroy
has_secure_password validations: false
has_many :accesses, dependent: :destroy
has_many :buckets, through: :accesses
has_many :accessible_bubbles, through: :buckets, source: :bubbles
has_many :filters, foreign_key: :creator_id, inverse_of: :creator, dependent: :destroy
has_many :pops, dependent: :nullify
has_many :assignments, foreign_key: :assignee_id, dependent: :destroy
has_many :assignings, foreign_key: :assigner_id, class_name: "Assignment"
has_many :assigned_bubbles, through: :assignments, source: :bubble
has_many :notifications, dependent: :destroy
has_one_attached :avatar
has_many :pins, dependent: :destroy
has_many :pinned_bubbles, through: :pins, source: :bubble
normalizes :email_address, with: ->(value) { value.strip.downcase }
after_create_commit :grant_access_to_buckets
scope :alphabetically, -> { order("lower(name)") }
scope :sorted_with_user_first, ->(user) { order(Arel.sql("users.id != ?, lower(name)", user.id)) }
def initials
name.to_s.scan(/\b\p{L}/).join.upcase
end
def deactivate
transaction do
sessions.destroy_all
accesses.destroy_all
update! active: false, email_address: deactived_email_address
end
end
def current?
Current.user == self
end
def can_remove?(other)
other != self
end
private
def deactived_email_address
email_address.sub(/@/, "-deactivated-#{SecureRandom.uuid}@")
end
def grant_access_to_buckets
Access.insert_all account.buckets.all_access.pluck(:id).collect { |bucket_id| { bucket_id: bucket_id, user_id: id } }
end
end