forked from paviliondev/discourse-quick-messages
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
104 lines (89 loc) · 3.27 KB
/
plugin.rb
File metadata and controls
104 lines (89 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
98
99
100
101
102
103
104
# name: discourse-quick-messages
# about: A Discourse plugin that adds a menu and a chat-like compose for private messages
# version: 0.1
# authors: Angus McLeod
register_asset 'stylesheets/quick.scss', :desktop
after_initialize do
Post.register_custom_field_type('quick_message', :boolean)
PostRevisor.track_topic_field(:custom_fields)
SiteSetting.class_eval do
def self.min_private_message_post_length
quick_message_min_post_length
end
def self.private_message_post_length
quick_message_min_post_length..max_post_length
end
end
Validators::PostValidator.class_eval do
def private_message?(post)
post.topic.try(:private_message?) || post.custom_fields['quick_message']
end
end
Post.class_eval do
def default_rate_limiter
return @rate_limiter if @rate_limiter.present?
if custom_fields["quick_message"] || archetype == Archetype.private_message
limit_key = "create_quick_message"
max_setting = SiteSetting.send("quick_message_rate_limit_create")
else
limit_key = "create_#{self.class.name.underscore}"
max_setting = if user && user.new_user? and SiteSetting.has_setting?("rate_limit_new_user_#{limit_key}")
SiteSetting.send("rate_limit_new_user_#{limit_key}")
else
SiteSetting.send("rate_limit_#{limit_key}")
end
end
@rate_limiter = RateLimiter.new(user, limit_key, 1, max_setting)
end
def limit_posts_per_day
unless custom_fields["quick_message"] || archetype == Archetype.private_message
if user && user.new_user_posting_on_first_day? && post_number && post_number > 1
RateLimiter.new(user, "first-day-replies-per-day", SiteSetting.max_replies_in_first_day, 1.day.to_i)
end
end
end
end
User.class_eval do
def unread_private_messages
@unread_pms ||=
begin
# perf critical, much more efficient than AR
sql = "
SELECT COUNT(*) FROM notifications n
LEFT JOIN topics t ON n.topic_id = t.id
WHERE
t.deleted_at IS NULL AND
t.subtype = :subtype AND
n.notification_type = :type AND
n.user_id = :user_id AND
NOT read"
User.exec_sql(sql, user_id: id,
subtype: TopicSubtype.user_to_user,
type: Notification.types[:private_message])
.getvalue(0,0).to_i
end
end
end
require 'topic_list_item_serializer'
class ::TopicListItemSerializer
attributes :message_excerpt,
:subtype
def message_excerpt
if object.custom_fields["quick_message"] || object.archetype == Archetype.private_message
cooked = Post.where(topic_id: object.id, post_number: object.highest_post_number).pluck('cooked')
excerpt = PrettyText.excerpt(cooked[0], 200, keep_emoji_images: true)
excerpt.gsub!(/(\[#{I18n.t 'excerpt_image'}\])/, "<i class='fa fa-picture-o'></i>") if excerpt
excerpt
else
return false
end
end
def include_message_excerpt?
!!message_excerpt
end
def subtype
object.subtype
end
end
TopicList.preloaded_custom_fields << "quick_message" if TopicList.respond_to? :preloaded_custom_fields
end