mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-26 11:12:53 +08:00
54 lines
1.2 KiB
JavaScript
Vendored
54 lines
1.2 KiB
JavaScript
Vendored
import { cancel } from "@ember/runloop";
|
|
import Service, { service } from "@ember/service";
|
|
|
|
export default class ChatDraftsManager extends Service {
|
|
@service chatApi;
|
|
|
|
drafts = {};
|
|
|
|
willDestroy() {
|
|
super.willDestroy(...arguments);
|
|
cancel(this._persistHandler);
|
|
}
|
|
|
|
async add(message, channelId, threadId) {
|
|
try {
|
|
this.drafts[this.key(channelId, threadId)] = message;
|
|
await this.persistDraft(message, channelId, threadId);
|
|
} catch (e) {
|
|
// eslint-disable-next-line no-console
|
|
console.log("Couldn't save draft", e);
|
|
}
|
|
}
|
|
|
|
get(channelId, threadId) {
|
|
return this.drafts[this.key(channelId, threadId)];
|
|
}
|
|
|
|
remove(channelId, threadId) {
|
|
delete this.drafts[this.key(channelId, threadId)];
|
|
}
|
|
|
|
reset() {
|
|
this.drafts = {};
|
|
}
|
|
|
|
key(channelId, threadId) {
|
|
let key = `c-${channelId}`;
|
|
if (threadId) {
|
|
key += `:t-${threadId}`;
|
|
}
|
|
return key.toString();
|
|
}
|
|
|
|
async persistDraft(message, channelId, threadId) {
|
|
try {
|
|
await this.chatApi.saveDraft(channelId, message.toJSONDraft(), {
|
|
threadId,
|
|
});
|
|
message.draftSaved = true;
|
|
} catch {
|
|
// We don't want to throw an error if the draft fails to save
|
|
}
|
|
}
|
|
}
|