Class: Reply

Inherits:
ApplicationRecord show all
Includes:
BaseModel, Likeable, MarkdownBody, Mentionable, SoftDelete
Defined in:
app/models/reply.rb

Constant Summary collapse

UPVOTES =
%w(+1 :+1: :thumbsup: :plus1: πŸ‘ πŸ‘πŸ» πŸ‘πŸΌ πŸ‘πŸ½ πŸ‘πŸΎ πŸ‘πŸΏ)

Constants included from ApplicationHelper

ApplicationHelper::ALLOW_ATTRIBUTES, ApplicationHelper::ALLOW_TAGS, ApplicationHelper::EMPTY_STRING, ApplicationHelper::LANGUAGES_LISTS, ApplicationHelper::MOBILE_USER_AGENTS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mentionable

#delete_notifiaction_mentions, #extract_mentioned_users, #mentioned_user_logins, #mentioned_users, #no_mention_users, #send_mention_notification

Methods included from Likeable

#liked_by_user?

Methods included from ApplicationHelper

#admin?, #birthday_tag, #cache, #cached_asset_path, #controller_javascript_include_tag, #controller_stylesheet_link_tag, #highlight, #icon_tag, #insert_code_menu_items_tag, #javascript_include_tag_with_cached, #memory_cache, #mobile?, #notice_message, #owner?, #random_tips, #render_list, #render_page_title, #sanitize_markdown, #spaceless, #stylesheet_link_tag_with_cached, #timeago, #wiki_editor?

Methods included from SoftDelete

#deleted?

Methods included from BaseModel

#pull, #push

Class Method Details

.broadcast_to_client(reply) ⇒ Object


99
100
101
# File 'app/models/reply.rb', line 99

def self.broadcast_to_client(reply)
  ActionCable.server.broadcast "topics/#{reply.topic_id}/replies", { id: reply.id, user_id: reply.user_id, action: :create }
end

.notify_reply_created(reply_id) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/reply.rb', line 54

def self.notify_reply_created(reply_id)
  reply = Reply.find_by_id(reply_id)
  return if reply.blank?
  topic = Topic.find_by_id(reply.topic_id)
  return if topic.blank?

  notified_user_ids = reply.mentioned_user_ids

  # η»™ε‘εΈ–δΊΊε‘ε›žεΈ–ι€šηŸ₯
  if reply.user_id != topic.user_id && !notified_user_ids.include?(topic.user_id)
    Notification.create notify_type: 'topic_reply',
                        actor_id: reply.user_id,
                        user_id: topic.user_id,
                        target: reply,
                        second_target: topic
    notified_user_ids << topic.user_id
  end

  follower_ids = topic.follower_ids + (reply.user.try(:follower_ids) || [])
  follower_ids.uniq!

  # η»™ε…³ζ³¨θ€…ε‘ι€šηŸ₯
  default_note = {
    notify_type: 'topic_reply',
    target_type: "Reply", target_id: reply.id,
    second_target_type: 'Topic', second_target_id: topic.id,
    actor_id: reply.user_id
  }
  Notification.bulk_insert(set_size: 100) do |worker|
    follower_ids.each do |uid|
      # ζŽ’ι™€εŒδΈ€δΈͺε›žε€θΏ‡η¨‹δΈ­ε·²η»ζι†’θΏ‡ηš„δΊΊ
      next if notified_user_ids.include?(uid)
      # ζŽ’ι™€ε›žεΈ–δΊΊ
      next if uid == reply.user_id
      logger.debug "Post Notification to: #{uid}"
      note = default_note.merge(user_id: uid)
      worker.add(note)
    end
  end

  self.broadcast_to_client(reply)

  true
end

Instance Method Details

#async_create_reply_notifyObject


44
45
46
# File 'app/models/reply.rb', line 44

def async_create_reply_notify
  NotifyReplyJob.perform_later(id)
end

#check_vote_chars_for_like_topicObject


49
50
51
52
# File 'app/models/reply.rb', line 49

def check_vote_chars_for_like_topic
  return unless self.upvote?
  user.like(topic)
end

#destroyObject


112
113
114
115
116
# File 'app/models/reply.rb', line 112

def destroy
  super
  Notification.where(notify_type: 'topic_reply', target: self).delete_all
  delete_notifiaction_mentions
end

#popular?Boolean

ζ˜―ε¦ηƒ­ι—¨


104
105
106
# File 'app/models/reply.rb', line 104

def popular?
  likes_count >= 5
end

#update_parent_topicObject


30
31
32
# File 'app/models/reply.rb', line 30

def update_parent_topic
  topic.update_last_reply(self) if topic.present?
end

#update_parent_topic_updated_atObject


36
37
38
39
40
41
# File 'app/models/reply.rb', line 36

def update_parent_topic_updated_at
  unless topic.blank?
    topic.update_deleted_last_reply(self)
    true
  end
end

#upvote?Boolean


108
109
110
# File 'app/models/reply.rb', line 108

def upvote?
  body.strip.start_with?(*UPVOTES)
end