This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/helpers/presence-pretender.js
Joffrey JAFFEUX 9e19b22f64
DEV: prevents Firefox ESR tests to crash on ||= (#14758)
A follow up PR should investigate why `proposal-logical-assignment-operators` is not getting used here (test file?) but this should be enough to get things running.
2021-10-28 10:55:22 +02:00

87 lines
2.3 KiB
JavaScript

import { publishToMessageBus } from "discourse/tests/helpers/qunit-helpers";
import User from "discourse/models/user";
import { settled } from "@ember/test-helpers";
let channels = {};
export default function (helper) {
this.post("/presence/update", (request) => {
const params = new URLSearchParams(request.requestBody);
const presentChannels = params.getAll("present_channels[]");
const leaveChannels = params.getAll("leave_channels[]");
const user = User.current();
if (!user) {
return helper.response(403, {});
}
const userInfo = {
id: user.id,
username: user.username,
name: user.name,
avatar_template: "/letter_avatar_proxy/v4/letter/b/35a633/{size}.png",
};
presentChannels.forEach((c) => joinChannel(c, userInfo));
leaveChannels.forEach((c) => leaveChannel(c, userInfo));
return helper.response({ success: "OK" });
});
this.get("/presence/get", (request) => {
const channelNames = request.queryParams.channels;
const response = {};
channelNames.forEach((c) => (response[c] = getChannelInfo(c)));
return helper.response(response);
});
}
export function getChannelInfo(name) {
return (
channels[name] ||
(channels[name] = { count: 0, users: [], last_message_id: 0 })
);
}
export function joinChannel(name, user) {
const channel = getChannelInfo(name);
if (!channel.users.any((u) => u.id === user.id)) {
channel.users.push(user);
channel.count += 1;
channel.last_message_id += 1;
publishToMessageBus(
`/presence${name}`,
{
entering_users: [Object.assign({}, user)],
},
0,
channel.last_message_id
);
}
return settled();
}
export function leaveChannel(name, user) {
const channel = getChannelInfo(name);
if (channel.users.any((u) => u.id === user.id)) {
channel.users = channel.users.reject((u) => u.id === user.id);
channel.count -= 1;
channel.last_message_id += 1;
publishToMessageBus(
`/presence${name}`,
{
leaving_user_ids: [user.id],
},
0,
channel.last_message_id
);
}
return settled();
}
export function presentUserIds(channelName) {
return getChannelInfo(channelName).users.map((u) => u.id);
}
export function clearState() {
channels = {};
}