Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add description to invites (#33207)

Ted Johansson862116a17618
diff --git a/app/assets/javascripts/discourse/app/components/modal/create-invite.gjs b/app/assets/javascripts/discourse/app/components/modal/create-invite.gjs
index c5e744c4b25..4e4d5f36a8e 100644
--- a/app/assets/javascripts/discourse/app/components/modal/create-invite.gjs
+++ b/app/assets/javascripts/discourse/app/components/modal/create-invite.gjs
@@ -12,6 +12,7 @@ import DModal from "discourse/components/d-modal";
import Form from "discourse/components/form";
import FutureDateInput from "discourse/components/future-date-input";
import { extractError } from "discourse/lib/ajax-error";
+import { INVITE_DESCRIPTION_MAX_LENGTH } from "discourse/lib/constants";
import { canNativeShare, nativeShare } from "discourse/lib/pwa-utils";
import { sanitize } from "discourse/lib/text";
import { applyValueTransformer } from "discourse/lib/transformer";
@@ -77,6 +78,7 @@ export default class CreateInvite extends Component {
@cached
get data() {
const data = {
+ description: this.invite.description ?? "",
restrictTo: this.invite.emailOrDomain ?? "",
maxRedemptions:
this.invite.max_redemptions_allowed ?? this.defaultRedemptionsAllowed,
@@ -154,6 +156,10 @@ export default class CreateInvite extends Component {
}
}
+ get descriptionValidation() {
+ return `length:0,${INVITE_DESCRIPTION_MAX_LENGTH}`;
+ }
+
get maxRedemptionsAllowedLimit() {
if (this.currentUser.staff) {
return this.siteSettings.invite_link_max_redemptions_limit;
@@ -196,6 +202,7 @@ export default class CreateInvite extends Component {
@action
async onFormSubmit(data) {
const submitData = {
+ description: data.description,
emailOrDomain: data.restrictTo?.trim(),
group_ids: data.inviteToGroups,
topic_id: data.inviteToTopic,
@@ -331,6 +338,15 @@ export default class CreateInvite extends Component {
@onRegisterApi={{this.registerApi}}
as |form|
>
+ <form.Field
+ @name="description"
+ @title={{i18n "user.invited.invite.description"}}
+ @format="large"
+ @validation={{this.descriptionValidation}}
+ as |field|
+ >
+ <field.Input />
+ </form.Field>
<form.Field
@name="restrictTo"
@title={{i18n "user.invited.invite.restrict"}}
diff --git a/app/assets/javascripts/discourse/app/lib/constants.js b/app/assets/javascripts/discourse/app/lib/constants.js
index 454056fc3c7..0bda47cb1d0 100644
--- a/app/assets/javascripts/discourse/app/lib/constants.js
+++ b/app/assets/javascripts/discourse/app/lib/constants.js
@@ -102,3 +102,5 @@ export const ADMIN_SEARCH_RESULT_TYPES = [
];
export const API_KEY_SCOPE_MODES = ["global", "read_only", "granular"];
+
+export const INVITE_DESCRIPTION_MAX_LENGTH = 100;
diff --git a/app/assets/javascripts/discourse/app/templates/user-invited-show.gjs b/app/assets/javascripts/discourse/app/templates/user-invited-show.gjs
index e31dd1c206d..71ea7898deb 100644
--- a/app/assets/javascripts/discourse/app/templates/user-invited-show.gjs
+++ b/app/assets/javascripts/discourse/app/templates/user-invited-show.gjs
@@ -188,6 +188,11 @@ export default RouteTemplate(
href={{invite.topic.url}}
>{{icon "file"}} {{invite.topic.title}}</a></p>
{{/if}}
+ {{#if invite.description}}
+ <div class="invite-description">
+ {{invite.description}}
+ </div>
+ {{/if}}
</td>
<td class="invite-updated-at">
diff --git a/app/assets/stylesheets/common/base/user.scss b/app/assets/stylesheets/common/base/user.scss
index f1083cb7787..aa6dbbb0e31 100644
--- a/app/assets/stylesheets/common/base/user.scss
+++ b/app/assets/stylesheets/common/base/user.scss
@@ -383,6 +383,10 @@
}
}
+.invite-description {
+ color: var(--primary-medium);
+}
+
.public-user-fields {
margin-top: 8px;
margin-bottom: 8px;
diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index 8cb01bad9c9..d00d6b5df9d 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -91,6 +91,7 @@ class InvitesController < ApplicationController
Invite.generate(
current_user,
email: email,
+ description: params[:description],
domain: params[:domain],
skip_email: params[:skip_email],
invited_by: current_user,
@@ -146,6 +147,7 @@ class InvitesController < ApplicationController
Invite.generate(
current_user,
email: params[:email],
+ description: params[:description],
domain: params[:domain],
skip_email: params[:skip_email],
invited_by: current_user,
@@ -286,7 +288,13 @@ class InvitesController < ApplicationController
begin
invite.update!(
- params.permit(:email, :custom_message, :max_redemptions_allowed, :expires_at),
+ params.permit(
+ :email,
+ :description,
+ :custom_message,
+ :max_redemptions_allowed,
+ :expires_at,
+ ),
)
rescue ActiveRecord::RecordInvalid => e
return render_json_error(e.record.errors.full_messages.first)
diff --git a/app/models/invite.rb b/app/models/invite.rb
index 43b75133583..80641ce788c 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -18,6 +18,7 @@ class Invite < ActiveRecord::Base
BULK_INVITE_EMAIL_LIMIT = 200
DOMAIN_REGEX =
/\A(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])\z/
+ DESCRIPTION_MAX_LENGTH = 100
rate_limit :limit_invites_per_day
@@ -34,6 +35,7 @@ class Invite < ActiveRecord::Base
validates :email, email: true, allow_blank: true, length: { maximum: 500 }
validates :custom_message, length: { maximum: 1000 }
validates :domain, length: { maximum: 500 }
+ validates :description, length: { maximum: DESCRIPTION_MAX_LENGTH }
validate :ensure_max_redemptions_allowed
validate :valid_redemption_count
validate :valid_domain, if: :will_save_change_to_domain?
@@ -177,7 +179,14 @@ class Invite < ActiveRecord::Base
)
else
create_args =
- opts.slice(:email, :domain, :moderator, :custom_message, :max_redemptions_allowed)
+ opts.slice(
+ :email,
+ :description,
+ :domain,
+ :moderator,
+ :custom_message,
+ :max_redemptions_allowed,
+ )
create_args[:invited_by] = invited_by
create_args[:email] = email
create_args[:emailed_status] = emailed_status
@@ -382,6 +391,7 @@ end
# expires_at :datetime not null
# email_token :string
# domain :string
+# description :string(100)
#
# Indexes
#
diff --git a/app/serializers/invite_serializer.rb b/app/serializers/invite_serializer.rb
index 9e49bdfa640..40a3bbd79e0 100644
--- a/app/serializers/invite_serializer.rb
+++ b/app/serializers/invite_serializer.rb
@@ -4,6 +4,7 @@ class InviteSerializer < ApplicationSerializer
attributes :id,
:invite_key,
:link,
+ :description,
:email,
:domain,
:emailed,
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 81a707c779c..ed8cdb90b53 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -2038,6 +2038,8 @@ en:
show_advanced: "Show Advanced Options"
hide_advanced: "Hide Advanced Options"
+ description: "Description"
+
restrict: "Restrict to"
restrict_email: "Restrict to email"
restrict_domain: "Restrict to domain"
diff --git a/db/migrate/20250614020437_add_description_to_invites.rb b/db/migrate/20250614020437_add_description_to_invites.rb
new file mode 100644
index 00000000000..4761d77c396
--- /dev/null
+++ b/db/migrate/20250614020437_add_description_to_invites.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddDescriptionToInvites < ActiveRecord::Migration[7.2]
+ def change
+ add_column :invites, :description, :string, limit: 100
+ end
+end
diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rake
index bc09ea07b1d..92da06f1ffa 100644
--- a/lib/tasks/javascript.rake
+++ b/lib/tasks/javascript.rake
@@ -186,7 +186,9 @@ task "javascript:update_constants" => :environment do
export const ADMIN_SEARCH_RESULT_TYPES = #{Admin::SearchController::RESULT_TYPES.to_json};
- export const API_KEY_SCOPE_MODES = #{ApiKey.scope_modes.keys.to_json}
+ export const API_KEY_SCOPE_MODES = #{ApiKey.scope_modes.keys.to_json};
+
+ export const INVITE_DESCRIPTION_MAX_LENGTH = #{Invite::DESCRIPTION_MAX_LENGTH};
JS
pretty_notifications = Notification.types.map { |n| " #{n[0]}: #{n[1]}," }.join("\n")
diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb
index f4fc8d949ac..f1460c387fc 100644
--- a/spec/models/invite_spec.rb
+++ b/spec/models/invite_spec.rb
@@ -13,6 +13,7 @@ RSpec.describe Invite do
it { is_expected.to validate_presence_of :invited_by_id }
it { is_expected.to rate_limit }
it { is_expected.to validate_length_of(:custom_message).is_at_most(1000) }
+ it { is_expected.to validate_length_of(:description).is_at_most(100) }
it "allows invites with valid emails" do
invite = Fabricate.build(:invite, email: "[email protected]", invited_by: user)
diff --git a/spec/requests/invites_controller_spec.rb b/spec/requests/invites_controller_spec.rb
index dd18dbccede..b8d25f333c8 100644
--- a/spec/requests/invites_controller_spec.rb
+++ b/spec/requests/invites_controller_spec.rb
@@ -826,9 +826,14 @@ RSpec.describe InvitesController do
end
it "does not resend invite email when updating other fields" do
- put "/invites/#{invite.id}", params: { custom_message: "new message" }
+ put "/invites/#{invite.id}",
+ params: {
+ custom_message: "new message",
+ description: "new description",
+ }
expect(response.status).to eq(200)
expect(invite.reload.custom_message).to eq("new message")
+ expect(invite.reload.description).to eq("new description")
expect(Jobs::InviteEmail.jobs.size).to eq(0)
end
diff --git a/spec/system/create_invite_spec.rb b/spec/system/create_invite_spec.rb
index 2839de17843..969498d1390 100644
--- a/spec/system/create_invite_spec.rb
+++ b/spec/system/create_invite_spec.rb
@@ -97,6 +97,7 @@ describe "Creating Invites", type: :system do
open_invite_modal
display_advanced_options
+ create_invite_modal.form.field("description").fill_in("welcome")
create_invite_modal.form.field("restrictTo").fill_in("discourse.org")
create_invite_modal.form.field("maxRedemptions").fill_in("53")
create_invite_modal.form.field("expiresAfterDays").select(90)
@@ -115,6 +116,7 @@ describe "Creating Invites", type: :system do
expect(user_invited_pending_page.invites_list.size).to eq(1)
+ expect(user_invited_pending_page.latest_invite).to have_description("welcome")
expect(user_invited_pending_page.latest_invite).to be_link_type(
key: invite_key,
redemption_count: 0,
diff --git a/spec/system/page_objects/pages/user_invited_pending.rb b/spec/system/page_objects/pages/user_invited_pending.rb
index 876f659395b..7b3053bff35 100644
--- a/spec/system/page_objects/pages/user_invited_pending.rb
+++ b/spec/system/page_objects/pages/user_invited_pending.rb
@@ -25,6 +25,10 @@ module PageObjects
end
end
+ def has_description?(text)
+ invite_type_col.has_css?(".invite-description", text:)
+ end
+
def email_type?(email)
invite_type_col.has_text?(email) && invite_type_col.has_css?(".d-icon-envelope")
end