Retrospring/app/models/inbox.rb

63 lines
1.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2020-04-18 15:59:18 -07:00
class Inbox < ApplicationRecord
2023-02-25 06:46:11 -08:00
belongs_to :user, touch: :inbox_updated_at
2014-11-10 22:10:02 -08:00
belongs_to :question
attr_accessor :returning
2015-01-03 13:27:14 -08:00
before_create do
raise "User does not want to receive anonymous questions" if !returning &&
question.author_is_anonymous &&
(question.author_identifier != "justask") &&
!user.privacy_allow_anonymous_questions?
2015-01-03 13:27:14 -08:00
end
after_create do
2023-10-18 12:57:09 -07:00
user.touch(:inbox_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
after_update do
2023-10-18 12:57:09 -07:00
user.touch(:inbox_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
after_destroy do
# user might not exist at this point (account deleted, records are cleaned up async)
2023-10-18 12:57:09 -07:00
user&.touch(:inbox_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
def answer(answer_content, user)
raise Errors::AnsweringOtherBlockedSelf if question.user&.blocking?(user)
raise Errors::AnsweringSelfBlockedOther if user.blocking?(question.user)
answer = user.answer(self.question, answer_content)
self.destroy
2014-12-12 14:14:32 -08:00
answer
end
def remove
2014-12-14 03:19:52 -08:00
self.question.destroy if self.question.can_be_removed?
self.destroy
end
2023-03-04 09:40:29 -08:00
def notification_icon = question.author_is_anonymous ? "/icons/maskable_icon_x128.png" : question.user.profile_picture.url(:small)
def as_push_notification
{
title: I18n.t(
"frontend.push_notifications.inbox.title",
user: if question.author_is_anonymous
user.profile.anon_display_name || APP_CONFIG["anonymous_name"]
else
question.user.profile.safe_name
2023-10-18 12:53:30 -07:00
end,
),
2023-03-04 09:40:29 -08:00
icon: notification_icon,
2023-02-25 06:46:11 -08:00
body: question.content.truncate(Question::SHORT_QUESTION_MAX_LENGTH),
data: {
click_url: "/inbox",
2023-03-04 09:40:29 -08:00
},
}
end
2014-11-10 14:45:36 -08:00
end