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