A platform for community discussion. Free, open, simple.
DEV: add chat presence active transformer (#44541)
Kris11db1f80e20d
diff --git a/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-transformers.js b/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-transformers.jsnew file mode 100644index 00000000000..bd35647bad7--- /dev/null+++ b/plugins/chat/assets/javascripts/discourse/pre-initializers/chat-transformers.js@@ -0,0 +1,11 @@+import { withPluginApi } from "discourse/lib/plugin-api";++export default {+ before: "freeze-valid-transformers",++ initialize() {+ withPluginApi((api) => {+ api.addValueTransformerName("chat-presence-active");+ });+ },+};diff --git a/plugins/chat/assets/javascripts/discourse/services/chat.js b/plugins/chat/assets/javascripts/discourse/services/chat.jsindex ce01890a1c6..1b327f51467 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat.js@@ -9,6 +9,7 @@ import { AUTO_GROUPS } from "discourse/lib/constants";import { bind } from "discourse/lib/decorators";import deprecated from "discourse/lib/deprecated";import EmbedMode from "discourse/lib/embed-mode";+import { applyValueTransformer } from "discourse/lib/transformer";import {onPresenceChange,removeOnPresenceChange,@@ -279,7 +280,12 @@ export default class Chat extends Service {return;}- if (this.chatStateManager.isActive) {+ const active = applyValueTransformer(+ "chat-presence-active",+ this.chatStateManager.isActive+ );++ if (active) {this.presenceChannel.enter({ activeOptions: CHAT_ONLINE_OPTIONS });} else {this.presenceChannel.leave();diff --git a/plugins/chat/test/javascripts/unit/services/chat-test.js b/plugins/chat/test/javascripts/unit/services/chat-test.jsindex 3f001505aac..49eae23489e 100644--- a/plugins/chat/test/javascripts/unit/services/chat-test.js+++ b/plugins/chat/test/javascripts/unit/services/chat-test.js@@ -1,5 +1,7 @@+import { settled } from "@ember/test-helpers";import { test } from "qunit";import sinon from "sinon";+import { withPluginApi } from "discourse/lib/plugin-api";import { acceptance } from "discourse/tests/helpers/qunit-helpers";acceptance("Unit | Service | chat", function (needs) {@@ -53,4 +55,49 @@ acceptance("Unit | Service | chat", function (needs) {assert.strictEqual(count, 10, "returns pending count");});++ test("updatePresence leaves chat presence while chat isn't open", async function (assert) {+ const { enter, leave } = (this.chat.presenceChannel = {+ enter: sinon.stub(),+ leave: sinon.stub(),+ });++ this.chat.updatePresence();+ await settled();++ assert.false(enter.called, "doesn't enter");+ assert.true(leave.calledOnce, "leaves");+ });++ test("updatePresence counts what chat-presence-active says is active", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("chat-presence-active", () => true);+ });+ const { enter, leave } = (this.chat.presenceChannel = {+ enter: sinon.stub(),+ leave: sinon.stub(),+ });++ this.chat.updatePresence();+ await settled();++ assert.true(enter.calledOnce, "enters");+ assert.false(leave.called, "doesn't leave");+ });++ test("updatePresence still respects hidden presence", async function (assert) {+ withPluginApi((api) => {+ api.registerValueTransformer("chat-presence-active", () => true);+ });+ this.currentUser.user_option.hide_presence = true;+ const { enter } = (this.chat.presenceChannel = {+ enter: sinon.stub(),+ leave: sinon.stub(),+ });++ this.chat.updatePresence();+ await settled();++ assert.false(enter.called, "doesn't enter");+ });});