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/plugins/chat/assets/javascripts/discourse/routes/chat-channel.js
Joffrey JAFFEUX 66130dc8c1
REFACTOR: handles every chat resource as an URL (#18961)
- Note this is also tweaking the UI a little bit as we are now using links/buttons in the header as needed
- It disables the find ideal channel in drawer mode, if loading `/chat` in drawer mode it will either reopen at the last position or just stay on index
2022-11-11 06:39:15 +01:00

64 lines
1.8 KiB
JavaScript

import DiscourseRoute from "discourse/routes/discourse";
import Promise from "rsvp";
import EmberObject, { action } from "@ember/object";
import { ajax } from "discourse/lib/ajax";
import { inject as service } from "@ember/service";
import ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel";
import slugifyChannel from "discourse/plugins/chat/discourse/lib/slugify-channel";
export default class ChatChannelRoute extends DiscourseRoute {
@service chat;
@service router;
async model(params) {
let [chatChannel, channels] = await Promise.all([
this.getChannel(params.channelId),
this.chat.getChannels(),
]);
return EmberObject.create({
chatChannel,
channels,
});
}
async getChannel(id) {
let channel = await this.chat.getChannelBy("id", id);
if (!channel || this.forceRefetchChannel) {
channel = await this.getChannelFromServer(id);
}
return channel;
}
async getChannelFromServer(id) {
return ajax(`/chat/chat_channels/${id}`)
.then((response) => ChatChannel.create(response))
.catch(() => this.replaceWith("/404"));
}
afterModel(model) {
this.chat.setActiveChannel(model?.chatChannel);
const queryParams = this.paramsFor(this.routeName);
const slug = slugifyChannel(model.chatChannel);
if (queryParams?.channelTitle !== slug) {
this.router.replaceWith("chat.channel.index", model.chatChannel.id, slug);
}
}
setupController(controller) {
super.setupController(...arguments);
if (controller.messageId) {
this.chat.set("messageId", controller.messageId);
this.controller.set("messageId", null);
}
}
@action
refreshModel(forceRefetchChannel = false) {
this.forceRefetchChannel = forceRefetchChannel;
this.refresh();
}
}