A platform for community discussion. Free, open, simple.
FEATURE: Store composer version and device info on post stat (#31600)
Martin Brennan223b40de1cf3
diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.jsindex eba1ce83737..ace3eef9c4c 100644--- a/app/assets/javascripts/discourse/app/models/composer.js+++ b/app/assets/javascripts/discourse/app/models/composer.js@@ -66,6 +66,7 @@ const CLOSED = "closed",target_recipients: "targetRecipients",typing_duration_msecs: "typingTime",composer_open_duration_msecs: "composerTime",+ composer_version: "composerVersion",tags: "tags",featured_link: "featuredLink",shared_draft: "sharedDraft",@@ -194,6 +195,8 @@ export default class Composer extends RestModel {}@service dialog;+ @service siteSettings;+ @service keyValueStore;@tracked topic;@tracked post;@@ -351,6 +354,17 @@ export default class Composer extends RestModel {return total;}+ get composerVersion() {+ if (+ this.siteSettings.rich_editor &&+ this.keyValueStore.get("d-editor-prefers-rich-editor") === "true"+ ) {+ return 2;+ }++ return 1;+ }+@discourseComputed("archetypeId")archetype(archetypeId) {return this.archetypes.findBy("id", archetypeId);diff --git a/app/assets/javascripts/discourse/tests/unit/models/composer-test.js b/app/assets/javascripts/discourse/tests/unit/models/composer-test.jsindex 820f9e0e7cb..c1677428dcb 100644--- a/app/assets/javascripts/discourse/tests/unit/models/composer-test.js+++ b/app/assets/javascripts/discourse/tests/unit/models/composer-test.js@@ -30,6 +30,7 @@ module("Unit | Model | composer", function (hooks) {hooks.beforeEach(function () {this.siteSettings = getOwner(this).lookup("service:site-settings");+ this.keyValueStore = getOwner(this).lookup("service:key-value-store");});test("replyLength", function (assert) {@@ -482,4 +483,19 @@ module("Unit | Model | composer", function (hooks) {assert.true(saved);});++ test("composerVersion is correct when using 'rich text' composer", async function (assert) {+ this.siteSettings.rich_editor = true;+ this.keyValueStore.set({+ key: "d-editor-prefers-rich-editor",+ value: "true",+ });+ const composer = createComposer.call(this, {});+ assert.strictEqual(composer.composerVersion, 2);+ });++ test("composerVersion is correct when using 'classic' composer", async function (assert) {+ const composer = createComposer.call(this, {});+ assert.strictEqual(composer.composerVersion, 1);+ });});diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rbindex e903fc573e3..eaca9faec90 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -850,6 +850,7 @@ class PostsController < ApplicationControllercomposer_open_duration_msecsvisibledraft_key+ composer_version]Post.plugin_permitted_create_params.each do |key, value|@@ -950,6 +951,7 @@ class PostsController < ApplicationControllerresult[:ip_address] = request.remote_ipresult[:user_agent] = request.user_agentresult[:referrer] = request.env["HTTP_REFERER"]+ result[:writing_device] = BrowserDetection.device(request.user_agent)recipients = result[:target_recipients]diff --git a/app/models/post_stat.rb b/app/models/post_stat.rbindex e2b684666f3..6fc438b2759 100644--- a/app/models/post_stat.rb+++ b/app/models/post_stat.rb@@ -1,6 +1,15 @@# frozen_string_literal: trueclass PostStat < ActiveRecord::Base+ # Version 1 is the original textarea composer for Discourse,+ # which has been around since its inception and uses a split+ # pane between markdown and preview.+ #+ # Version 2 is the new rich text composer, which is a single+ # contendedibale using ProseMirror which is more of a WYSIWYG+ # experience.+ COMPOSER_VERSIONS = { 1 => "classic", 2 => "rich_text" }+belongs_to :postend@@ -15,8 +24,13 @@ end# composer_open_duration_msecs :integer# created_at :datetime not null# updated_at :datetime not null+# composer_version :integer+# writing_device :string+# writing_device_user_agent :string## Indexes#-# index_post_stats_on_post_id (post_id)+# index_post_stats_on_composer_version (composer_version)+# index_post_stats_on_post_id (post_id)+# index_post_stats_on_writing_device (writing_device)#diff --git a/db/migrate/20250304034313_add_composer_columns_to_post_stat.rb b/db/migrate/20250304034313_add_composer_columns_to_post_stat.rbnew file mode 100644index 00000000000..5a086a34cbc--- /dev/null+++ b/db/migrate/20250304034313_add_composer_columns_to_post_stat.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true++class AddComposerColumnsToPostStat < ActiveRecord::Migration[7.2]+ def change+ add_column :post_stats, :composer_version, :integer+ add_column :post_stats, :writing_device, :string+ add_column :post_stats, :writing_device_user_agent, :string+ end+enddiff --git a/db/migrate/20250304041850_add_index_to_post_stat_composer_columns.rb b/db/migrate/20250304041850_add_index_to_post_stat_composer_columns.rbnew file mode 100644index 00000000000..22bf20b135b--- /dev/null+++ b/db/migrate/20250304041850_add_index_to_post_stat_composer_columns.rb@@ -0,0 +1,19 @@+# frozen_string_literal: true++class AddIndexToPostStatComposerColumns < ActiveRecord::Migration[7.2]+ disable_ddl_transaction!++ def up+ if !index_exists?(:post_stats, :composer_version)+ add_index :post_stats, :composer_version, algorithm: :concurrently+ end+ if !index_exists?(:post_stats, :writing_device)+ add_index :post_stats, :writing_device, algorithm: :concurrently+ end+ end++ def down+ remove_index :post_stats, :composer_version if index_exists?(:post_stats, :composer_version)+ remove_index :post_stats, :writing_device if index_exists?(:post_stats, :writing_device)+ end+enddiff --git a/lib/post_creator.rb b/lib/post_creator.rbindex d486fffb801..548819777bd 100644--- a/lib/post_creator.rb+++ b/lib/post_creator.rb@@ -342,6 +342,9 @@ class PostCreatordrafts_saved: revisions,typing_duration_msecs: @opts[:typing_duration_msecs] || 0,composer_open_duration_msecs: @opts[:composer_open_duration_msecs] || 0,+ writing_device: @opts[:writing_device]&.to_s,+ writing_device_user_agent: @opts[:user_agent]&.truncate(400),+ composer_version: @opts[:composer_version],)endenddiff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rbindex d175e180f27..f7466c64adc 100644--- a/spec/lib/post_creator_spec.rb+++ b/spec/lib/post_creator_spec.rb@@ -13,7 +13,16 @@ RSpec.describe PostCreator dodescribe "new topic" dofab!(:category) { Fabricate(:category, user: user) }let(:basic_topic_params) do- { title: "hello world topic", raw: "my name is fred", archetype_id: 1, advance_draft: true }+ {+ title: "hello world topic",+ raw: "my name is fred",+ archetype_id: 1,+ advance_draft: true,+ writing_device: "linux",+ user_agent:+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",+ composer_version: 2,+ }endlet(:image_sizes) do{ "http://an.image.host/image.jpg" => { "width" => 111, "height" => 222 } }@@ -365,6 +374,11 @@ RSpec.describe PostCreator dopost = creator.createexpect(post.post_stat.typing_duration_msecs).to eq(0)expect(post.post_stat.drafts_saved).to eq(2)+ expect(post.post_stat.writing_device).to eq("linux")+ expect(post.post_stat.writing_device_user_agent).to eq(+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",+ )+ expect(post.post_stat.composer_version).to eq(2)expect(user.reload.user_stat.draft_count).to eq(0)ensurePostCreator.track_post_stats = false