Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: Various behind-the-scenes improvements to PresenceChannel (#14518)
- Allow the `/presence/get` endpoint to return multiple channels in a single request (limited to 50) - When multiple presence channels are initialized in a single Ember runloop, batch them into a single GET request - Introduce the `presence-pretender` to allow easy testing of PresenceChannel-related features - Introduce a `use_cache` boolean (default true) on the the server-side PresenceChannel initializer. Useful during testing.
- Loading branch information
1 parent
ba380c5
commit a55642a
Showing
7 changed files
with
251 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/assets/javascripts/discourse/tests/helpers/presence-pretender.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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) { | ||
channels[name] ||= { count: 0, users: [], last_message_id: 0 }; | ||
return channels[name]; | ||
} | ||
|
||
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: [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 = {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.