A platform for community discussion. Free, open, simple.
FEATURE: Add presence indicators for translating posts (#35857)
Keegan George400b57ac64e4
diff --git a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.gjs b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.gjsindex f8525019290..0f75afe74c2 100644--- a/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.gjs+++ b/plugins/discourse-presence/assets/javascripts/discourse/components/composer-presence-display.gjs@@ -5,6 +5,7 @@ import { gt } from "truth-helpers";import UserLink from "discourse/components/user-link";import avatar from "discourse/helpers/avatar";import helperFn from "discourse/helpers/helper-fn";+import Composer from "discourse/models/composer";import { i18n } from "discourse-i18n";export default class ComposerPresenceDisplay extends Component {@@ -16,6 +17,7 @@ export default class ComposerPresenceDisplay extends Component {@tracked replyChannel;@tracked whisperChannel;@tracked editChannel;+ @tracked translateChannel;setupReplyChannel = helperFn((_, on) => {const { topic } = this.args.model;@@ -63,9 +65,24 @@ export default class ComposerPresenceDisplay extends Component {on.cleanup(() => editChannel.unsubscribe());});+ setupTranslateChannel = helperFn((_, on) => {+ const { post } = this.args.model;++ if (!post || !this.isTranslate) {+ return;+ }++ const name = `/discourse-presence/translate/${post.id}`;+ const translateChannel = this.presence.getChannel(name);+ this.translateChannel = translateChannel;++ translateChannel.subscribe();+ on.cleanup(() => translateChannel.unsubscribe());+ });+notifyState = helperFn((_, on) => {const { topic, post, replyDirty } = this.args.model;- const entity = this.isEdit ? post : topic;+ const entity = this.isEdit || this.isTranslate ? post : topic;if (entity) {const name = `/discourse-presence/${this.state}/${entity.id}`;@@ -83,11 +100,17 @@ export default class ComposerPresenceDisplay extends Component {return this.state === "edit";}+ get isTranslate() {+ return this.state === "translate";+ }+@cachedget state() {- const { editingPost, whisper, replyingToTopic } = this.args.model;+ const { editingPost, whisper, replyingToTopic, action } = this.args.model;- if (editingPost) {+ if (action === Composer.ADD_TRANSLATION) {+ return "translate";+ } else if (editingPost) {return "edit";} else if (whisper) {return "whisper";@@ -102,6 +125,8 @@ export default class ComposerPresenceDisplay extends Component {if (this.isEdit) {users = this.editChannel?.users || [];+ } else if (this.isTranslate) {+ users = this.translateChannel?.users || [];} else {const replyUsers = this.replyChannel?.users || [];const whisperUsers = this.whisperChannel?.users || [];@@ -118,6 +143,7 @@ export default class ComposerPresenceDisplay extends Component {{{this.setupReplyChannel}}{{this.setupWhisperChannel}}{{this.setupEditChannel}}+ {{this.setupTranslateChannel}}{{this.notifyState}}{{#if (gt this.users.length 0)}}@@ -134,6 +160,8 @@ export default class ComposerPresenceDisplay extends Component {<span class="description">{{~#if this.isReply~}}{{i18n "presence.replying" count=this.users.length}}+ {{~else if this.isTranslate~}}+ {{i18n "presence.translating" count=this.users.length}}{{~else~}}{{i18n "presence.editing" count=this.users.length}}{{~/if~}}diff --git a/plugins/discourse-presence/config/locales/client.en.yml b/plugins/discourse-presence/config/locales/client.en.ymlindex dfcc58ad529..8a94963cfad 100644--- a/plugins/discourse-presence/config/locales/client.en.yml+++ b/plugins/discourse-presence/config/locales/client.en.yml@@ -7,6 +7,9 @@ en:editing:one: "editing"other: "editing"+ translating:+ one: "translating"+ other: "translating"replying_to_topic:one: "replying"other: "replying"diff --git a/plugins/discourse-presence/plugin.rb b/plugins/discourse-presence/plugin.rbindex fe5aad41043..de872e358a1 100644--- a/plugins/discourse-presence/plugin.rb+++ b/plugins/discourse-presence/plugin.rb@@ -71,6 +71,27 @@ after_initialize doconfig.allowed_group_ids.uniq!+ config+ elsif post_id = channel_name[%r{/discourse-presence/translate/(\d+)}, 1]+ post = Post.find(post_id)+ topic = Topic.find(post.topic_id)++ config = PresenceChannel::Config.new+ config.allowed_group_ids = staff_groups++ if SiteSetting.content_localization_enabled+ config.allowed_group_ids += SiteSetting.content_localization_allowed_groups_map+ end++ if topic.private_message?+ config.allowed_user_ids = topic.allowed_users.pluck(:id)+ config.allowed_group_ids += topic.allowed_groups.pluck(:group_id)+ elsif secure_group_ids = topic.secure_group_ids+ config.allowed_group_ids += secure_group_ids+ end++ config.allowed_group_ids.uniq!+configendrescue ActiveRecord::RecordNotFounddiff --git a/plugins/discourse-presence/spec/integration/presence_spec.rb b/plugins/discourse-presence/spec/integration/presence_spec.rbindex 73959b53b3e..31389631ce8 100644--- a/plugins/discourse-presence/spec/integration/presence_spec.rb+++ b/plugins/discourse-presence/spec/integration/presence_spec.rb@@ -208,5 +208,63 @@ RSpec.describe "discourse-presence" do*private_message.allowed_users.pluck(:id),)end++ it "handles permissions for translate channel on public topics" do+ post = Fabricate(:post, topic: public_topic, user: user)+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_allowed_groups = group.id++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ group.id,+ )+ expect(c.config.allowed_user_ids).to eq(nil)+ end++ it "handles permissions for translate channel on secure category topics" do+ post = Fabricate(:post, topic: private_topic, user: user)+ SiteSetting.content_localization_enabled = true++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ group.id,+ )+ expect(c.config.allowed_user_ids).to eq(nil)+ end++ it "handles permissions for translate channel on private messages" do+ post = Fabricate(:post, topic: private_message, user: user)+ SiteSetting.content_localization_enabled = true++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ group.id,+ )+ expect(c.config.allowed_user_ids).to contain_exactly(+ *private_message.topic_allowed_users.pluck(:user_id),+ )+ end++ it "allows only staff for translate channel when content localization is disabled" do+ post = Fabricate(:post, topic: public_topic, user: user)+ SiteSetting.content_localization_enabled = false++ c = PresenceChannel.new("/discourse-presence/translate/#{post.id}")+ expect(c.config.public).to eq(false)+ expect(c.config.allowed_group_ids).to contain_exactly(+ Group::AUTO_GROUPS[:admins],+ Group::AUTO_GROUPS[:moderators],+ )+ expect(c.config.allowed_user_ids).to eq(nil)+ endendenddiff --git a/plugins/discourse-presence/test/javascripts/acceptance/discourse-presence-test.js b/plugins/discourse-presence/test/javascripts/acceptance/discourse-presence-test.jsindex d552efca757..81b6baa85e6 100644--- a/plugins/discourse-presence/test/javascripts/acceptance/discourse-presence-test.js+++ b/plugins/discourse-presence/test/javascripts/acceptance/discourse-presence-test.js@@ -248,4 +248,35 @@ acceptance("Discourse Presence Plugin", function (needs) {assert.dom(avatarSelector).doesNotExist("whisper avatar removed");});++ test("Uses the translate channel for translating", async function (assert) {+ await visit("/t/internationalization-localization/280");++ const avatarSelector = ".reply-to .presence-avatars .avatar";++ await joinChannel("/discourse-presence/translate/398", {+ id: 999,+ avatar_template: "/images/avatar.png",+ username: "translator",+ });++ await click(".topic-post[data-post-number='1'] button.show-more-actions");+ await click(".topic-post[data-post-number='1'] button.edit");++ assert.dom(avatarSelector).doesNotExist("editor does not see translator");++ assert.deepEqual(+ presentUserIds("/discourse-presence/translate/398"),+ [999],+ "translator is in translate channel"+ );++ assert.deepEqual(+ presentUserIds("/discourse-presence/edit/398"),+ [],+ "no one in edit channel initially"+ );++ await leaveChannel("/discourse-presence/translate/398", { id: 999 });+ });});