Files
once-campfire/app/models/user.rb
Kevin McConnell df76a227dc Hello world
First open source release of Campfire 🎉
2025-08-21 09:31:59 +01:00

68 lines
1.7 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class User < ApplicationRecord
include Avatar, Bot, Mentionable, Role, Transferable
has_many :memberships, dependent: :delete_all
has_many :rooms, through: :memberships
has_many :reachable_messages, through: :rooms, source: :messages
has_many :messages, dependent: :destroy, foreign_key: :creator_id
has_many :push_subscriptions, class_name: "Push::Subscription", dependent: :delete_all
has_many :boosts, dependent: :destroy, foreign_key: :booster_id
has_many :searches, dependent: :delete_all
has_many :sessions, dependent: :destroy
scope :active, -> { where(active: true) }
has_secure_password validations: false
after_create_commit :grant_membership_to_open_rooms
scope :ordered, -> { order("LOWER(name)") }
scope :filtered_by, ->(query) { where("name like ?", "%#{query}%") }
def initials
name.scan(/\b\w/).join
end
def title
[ name, bio ].compact_blank.join(" ")
end
def deactivate
transaction do
close_remote_connections
memberships.without_direct_rooms.delete_all
push_subscriptions.delete_all
searches.delete_all
sessions.delete_all
update! active: false, email_address: deactived_email_address
end
end
def deactivated?
!active?
end
def reset_remote_connections
close_remote_connections reconnect: true
end
private
def grant_membership_to_open_rooms
Membership.insert_all(Rooms::Open.pluck(:id).collect { |room_id| { room_id: room_id, user_id: id } })
end
def deactived_email_address
email_address&.gsub(/@/, "-deactivated-#{SecureRandom.uuid}@")
end
def close_remote_connections(reconnect: false)
ActionCable.server.remote_connections.where(current_user: self).disconnect reconnect: reconnect
end
end