2019-05-13 11:02:53 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-28 20:55:31 -03:00
|
|
|
# name: discourse-signatures
|
2021-10-06 16:53:59 -07:00
|
|
|
# about: Adds signatures to Discourse posts
|
2022-01-19 15:04:31 +01:00
|
|
|
# version: 2.1.0
|
2015-09-28 20:55:31 -03:00
|
|
|
# author: Rafael Silva <xfalcox@gmail.com>
|
2018-09-11 11:26:11 +01:00
|
|
|
# url: https://github.com/discourse/discourse-signatures
|
2022-04-06 12:19:24 +02:00
|
|
|
# transpile_js: true
|
2015-09-28 20:55:31 -03:00
|
|
|
|
|
|
|
enabled_site_setting :signatures_enabled
|
|
|
|
|
2015-12-06 15:56:46 -02:00
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields << "see_signatures"
|
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields << "signature_url"
|
2016-04-08 17:30:39 -04:00
|
|
|
DiscoursePluginRegistry.serialized_current_user_fields << "signature_raw"
|
2015-09-28 20:55:31 -03:00
|
|
|
|
|
|
|
after_initialize do
|
2024-01-08 15:40:10 -06:00
|
|
|
register_user_custom_field_type("see_signatures", :boolean)
|
2024-01-08 16:50:29 -05:00
|
|
|
register_user_custom_field_type("signature_url", :string, max_length: 32_000)
|
2024-01-08 15:40:10 -06:00
|
|
|
register_user_custom_field_type("signature_raw", :string, max_length: 1000)
|
2015-09-28 20:55:31 -03:00
|
|
|
|
2022-01-19 15:04:31 +01:00
|
|
|
# add to class and serializer to allow for default value for the setting
|
|
|
|
add_to_class(:user, :see_signatures) do
|
2022-12-29 12:34:38 +00:00
|
|
|
if custom_fields["see_signatures"] != nil
|
|
|
|
custom_fields["see_signatures"]
|
2022-01-19 15:04:31 +01:00
|
|
|
else
|
|
|
|
SiteSetting.signatures_visible_by_default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-29 12:34:38 +00:00
|
|
|
add_to_serializer(:user, :see_signatures) { object.see_signatures }
|
2022-01-19 15:04:31 +01:00
|
|
|
|
2022-12-29 12:34:38 +00:00
|
|
|
register_editable_user_custom_field %i[see_signatures signature_url signature_raw]
|
2020-07-15 09:32:45 +10:00
|
|
|
|
2022-04-06 12:19:24 +02:00
|
|
|
allow_public_user_custom_field :signature_cooked
|
|
|
|
allow_public_user_custom_field :signature_url
|
2018-09-11 10:57:50 +01:00
|
|
|
|
2022-04-06 12:19:24 +02:00
|
|
|
add_to_serializer(:post, :user_signature) do
|
|
|
|
if SiteSetting.signatures_advanced_mode
|
2022-12-29 12:34:38 +00:00
|
|
|
object.user.custom_fields["signature_cooked"] if object.user
|
2019-08-13 11:08:28 +01:00
|
|
|
else
|
2022-12-29 12:34:38 +00:00
|
|
|
object.user.custom_fields["signature_url"] if object.user
|
2019-08-13 11:08:28 +01:00
|
|
|
end
|
2022-04-06 12:19:24 +02:00
|
|
|
end
|
2019-08-13 11:08:28 +01:00
|
|
|
|
2017-07-24 21:18:11 -03:00
|
|
|
# This is the code responsible for cooking a new advanced mode sig on user update
|
|
|
|
DiscourseEvent.on(:user_updated) do |user|
|
2022-12-29 12:34:38 +00:00
|
|
|
if SiteSetting.signatures_enabled? && SiteSetting.signatures_advanced_mode &&
|
|
|
|
user.custom_fields["signature_raw"]
|
|
|
|
cooked_sig =
|
|
|
|
PrettyText.cook(
|
|
|
|
user.custom_fields["signature_raw"],
|
|
|
|
omit_nofollow: user.has_trust_level?(TrustLevel[3]) && !SiteSetting.tl3_links_no_follow,
|
|
|
|
)
|
2018-04-03 19:55:00 +06:00
|
|
|
# avoid infinite recursion
|
2022-12-29 12:34:38 +00:00
|
|
|
if cooked_sig != user.custom_fields["signature_cooked"]
|
|
|
|
user.custom_fields["signature_cooked"] = cooked_sig
|
2017-08-28 18:01:17 -04:00
|
|
|
user.save
|
|
|
|
end
|
2017-07-24 21:18:11 -03:00
|
|
|
end
|
|
|
|
end
|
2015-09-28 20:55:31 -03:00
|
|
|
end
|
2015-12-06 15:56:46 -02:00
|
|
|
|
|
|
|
register_asset "stylesheets/common/signatures.scss"
|