FEATURE: Redesign discourse-presence to track state on the client side. (#9487)

Before this commit, the presence state of users were stored on the
server side and any updates to the state meant we had to publish the
entire state to the clients. Also, the way the state of users were
stored on the server side meant we didn't have a way to differentiate
between replying users and whispering users.

In this redesign, we decided to move the tracking of users state to the client
side and have the server publish client events instead. As a result of
this change, we're able to remove the number of opened connections
needed to track presence and also reduce the payload that is sent for
each event.

At the same time, we've also improved on the restrictions when publishing message_bus messages. Users that
do not have permission to see certain events will not receive messages
for those events.
This commit is contained in:
Alan Guo Xiang Tan
2020-04-29 12:48:55 +08:00
committed by GitHub
parent 5503eba924
commit 301a0fa54e
11 changed files with 859 additions and 543 deletions

View File

@ -0,0 +1,42 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import PresenceManager from "../discourse/lib/presence-manager";
function initializeDiscoursePresence(api) {
const currentUser = api.getCurrentUser();
const siteSettings = api.container.lookup("site-settings:main");
if (currentUser && !currentUser.hide_profile_and_presence) {
api.modifyClass("model:topic", {
presenceManager: null
});
api.modifyClass("route:topic-from-params", {
setupController() {
this._super(...arguments);
this.modelFor("topic").set(
"presenceManager",
PresenceManager.create({
topic: this.modelFor("topic"),
currentUser,
messageBus: api.container.lookup("message-bus:main"),
siteSettings
})
);
}
});
}
}
export default {
name: "discourse-presence",
after: "message-bus",
initialize(container) {
const siteSettings = container.lookup("site-settings:main");
if (siteSettings.presence_enabled) {
withPluginApi("0.8.40", initializeDiscoursePresence);
}
}
};