2019-08-24 16:51:25 +02:00
|
|
|
class Post < ApplicationRecord
|
2022-07-18 10:47:54 +02:00
|
|
|
include TenantOwnable
|
2024-12-20 14:06:48 +01:00
|
|
|
include ApplicationHelper
|
|
|
|
|
include Rails.application.routes.url_helpers
|
2024-04-05 18:23:31 +02:00
|
|
|
extend FriendlyId
|
2022-07-18 10:47:54 +02:00
|
|
|
|
2019-08-24 16:51:25 +02:00
|
|
|
belongs_to :board
|
2024-07-12 20:38:46 +02:00
|
|
|
belongs_to :user, optional: true
|
2019-08-24 16:51:25 +02:00
|
|
|
belongs_to :post_status, optional: true
|
2022-05-28 11:03:36 +02:00
|
|
|
|
2019-09-27 12:32:30 +02:00
|
|
|
has_many :likes, dependent: :destroy
|
2022-05-28 11:03:36 +02:00
|
|
|
has_many :follows, dependent: :destroy
|
|
|
|
|
has_many :followers, through: :follows, source: :user
|
2019-09-24 21:16:51 +02:00
|
|
|
has_many :comments, dependent: :destroy
|
2022-05-28 11:03:36 +02:00
|
|
|
has_many :post_status_changes, dependent: :destroy
|
2019-08-24 16:51:25 +02:00
|
|
|
|
2025-01-28 16:55:48 +01:00
|
|
|
has_many_attached :attachments
|
|
|
|
|
|
2024-12-20 14:06:48 +01:00
|
|
|
after_create :run_new_post_webhooks
|
|
|
|
|
after_destroy :run_delete_post_webhooks
|
|
|
|
|
|
2024-07-12 20:38:46 +02:00
|
|
|
enum approval_status: [
|
|
|
|
|
:approved,
|
|
|
|
|
:pending,
|
|
|
|
|
:rejected
|
|
|
|
|
]
|
|
|
|
|
|
2024-01-24 18:37:54 +01:00
|
|
|
validates :title, presence: true, length: { in: 4..128 }
|
2025-01-28 16:55:48 +01:00
|
|
|
validates :attachments,
|
|
|
|
|
content_type: Rails.application.accepted_image_types,
|
2025-02-05 13:47:01 +01:00
|
|
|
size: { less_than: 2048.kilobytes },
|
2025-01-28 16:55:48 +01:00
|
|
|
limit: { max: 5 }
|
2019-09-04 21:12:07 +02:00
|
|
|
|
2019-09-23 16:10:25 +02:00
|
|
|
paginates_per Rails.application.posts_per_page
|
2019-09-05 17:11:07 +02:00
|
|
|
|
2024-04-07 12:32:57 +02:00
|
|
|
friendly_id :title, use: :scoped, scope: :tenant_id
|
2024-04-05 18:23:31 +02:00
|
|
|
|
2024-12-20 14:06:48 +01:00
|
|
|
def url
|
|
|
|
|
get_url_for(method(:post_url), resource: self)
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-09 16:50:33 +02:00
|
|
|
class << self
|
|
|
|
|
def find_with_post_status_in(post_statuses)
|
|
|
|
|
where(post_status_id: post_statuses.pluck(:id))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def search_by_name_or_description(s)
|
|
|
|
|
s = s || ''
|
|
|
|
|
s = sanitize_sql_like(s)
|
|
|
|
|
where("posts.title ILIKE ? OR posts.description ILIKE ?", "%#{s}%", "%#{s}%")
|
|
|
|
|
end
|
2024-01-26 17:43:24 +01:00
|
|
|
|
|
|
|
|
def order_by(sort_by)
|
|
|
|
|
case sort_by
|
|
|
|
|
when 'newest'
|
|
|
|
|
order(created_at: :desc)
|
|
|
|
|
when 'trending'
|
|
|
|
|
order(hotness: :desc)
|
|
|
|
|
when 'most_voted'
|
|
|
|
|
order(likes_count: :desc)
|
|
|
|
|
when 'oldest'
|
|
|
|
|
order(created_at: :asc)
|
|
|
|
|
else
|
|
|
|
|
order(created_at: :desc)
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-07-12 20:38:46 +02:00
|
|
|
|
|
|
|
|
def pending
|
|
|
|
|
where(approval_status: "pending")
|
|
|
|
|
end
|
2019-09-05 17:11:07 +02:00
|
|
|
end
|
2024-12-20 14:06:48 +01:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def run_new_post_webhooks
|
|
|
|
|
entities = {
|
|
|
|
|
post: self.id,
|
|
|
|
|
board: self.board.id
|
|
|
|
|
}
|
|
|
|
|
entities[:post_author] = self.user.id if self.user_id
|
|
|
|
|
|
|
|
|
|
# New post (approved)
|
|
|
|
|
if self.approval_status == 'approved'
|
|
|
|
|
Webhook.where(trigger: :new_post, is_enabled: true).each do |webhook|
|
|
|
|
|
RunWebhook.perform_later(
|
|
|
|
|
webhook_id: webhook.id,
|
|
|
|
|
current_tenant_id: Current.tenant.id,
|
|
|
|
|
entities: entities
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# New post (pending approval)
|
|
|
|
|
if self.approval_status == 'pending'
|
|
|
|
|
Webhook.where(trigger: :new_post_pending_approval, is_enabled: true).each do |webhook|
|
|
|
|
|
RunWebhook.perform_later(
|
|
|
|
|
webhook_id: webhook.id,
|
|
|
|
|
current_tenant_id: Current.tenant.id,
|
|
|
|
|
entities: entities
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def run_delete_post_webhooks
|
|
|
|
|
# Since the post has already been deleted from DB
|
|
|
|
|
# we only provide its ID
|
|
|
|
|
entities = { post_id: self.id }
|
|
|
|
|
|
|
|
|
|
Webhook.where(trigger: :delete_post, is_enabled: true).each do |webhook|
|
|
|
|
|
RunWebhook.perform_later(
|
|
|
|
|
webhook_id: webhook.id,
|
|
|
|
|
current_tenant_id: Current.tenant.id,
|
|
|
|
|
entities: entities
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-08-24 16:51:25 +02:00
|
|
|
end
|