This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
plugin.rb
148 lines (127 loc) · 5.06 KB
/
plugin.rb
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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
# url: https://github.com/angusmcleod/discourse-quick-messages
register_asset 'stylesheets/common/quick_menu.scss'
register_asset 'stylesheets/common/quick_composer.scss'
register_asset 'stylesheets/mobile/quick_mobile.scss', :mobile
register_asset "stylesheets/colors.scss", :color_definitions
require_relative 'lib/setting_quick_messages_badge'
if respond_to?(:register_svg_icon)
register_svg_icon "angle-up"
register_svg_icon "angle-down"
register_svg_icon "external-link"
register_svg_icon "times"
end
enabled_site_setting :quick_message_enabled
after_initialize do
Post.register_custom_field_type('quick_message', :boolean)
PostRevisor.track_topic_field(:custom_fields)
DiscoursePluginRegistry.serialized_current_user_fields << "show_quick_messages"
DiscoursePluginRegistry.serialized_current_user_fields << "quick_messages_access"
User.register_custom_field_type("show_quick_messages", :boolean)
User.register_custom_field_type("quick_messages_access", :boolean)
add_to_serializer(:current_user, :show_quick_messages) { object.show_quick_messages }
add_to_serializer(:current_user, :quick_messages_access) { object.quick_messages_access }
register_editable_user_custom_field :show_quick_messages if defined? register_editable_user_custom_field
register_editable_user_custom_field :quick_messages_access if defined? register_editable_user_custom_field
SiteSetting.class_eval do
def self.min_personal_message_post_length
quick_message_min_post_length
end
def self.personal_message_post_length
quick_message_min_post_length..max_post_length
end
end
(defined?(PostValidator) == 'constant' ? PostValidator : 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? && 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
class ::User
def show_quick_messages
return false unless SiteSetting.quick_message_enabled
return false unless quick_messages_access
!SiteSetting.quick_message_user_preference || ActiveModel::Type::Boolean.new.cast(custom_fields['show_quick_messages'])
end
def quick_messages_access
SiteSetting.quick_message_required_badge == 0 || self.badge_ids.include?(SiteSetting.quick_message_required_badge)
end
end
module UserUnreadPrivateMessagesExtension
def unread_private_messages
if self.show_quick_messages
@unread_pms ||=
begin
# perf critical, much more efficient than AR
sql = <<~SQL
SELECT COUNT(*)
FROM notifications n
LEFT JOIN topics t ON t.id = n.topic_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
SQL
DB.query_single(sql,
user_id: id,
subtype: TopicSubtype.user_to_user,
type: Notification.types[:private_message]
)[0].to_i
end
else
super
end
end
end
require_dependency 'user'
class ::User
prepend UserUnreadPrivateMessagesExtension
end
require_dependency 'topic_list_item_serializer'
class ::TopicListItemSerializer
attributes :message_excerpt, :subtype
def message_excerpt
if object.custom_fields["quick_message"] || object.archetype == Archetype.private_message
raw = Post.where(topic_id: object.id, post_number: object.highest_post_number).pluck('raw')[0]
raw.gsub!(/(\!\[)(.*?)\)/, "<i class='fa fa-picture-o'></i>")
raw.truncate(150)
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