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/app/models/user-drafts-stream.js
Andrei Prigorshnev 477bbc372e
FEATURE: improve blank page syndrome on the user activity pages (#14311)
This improves blank page syndrome on the next pages:
* activity
* activity/replies
* activity/drafts
* activity/likes-given
2021-09-16 21:35:34 +04:00

97 lines
2.4 KiB
JavaScript

import discourseComputed from "discourse-common/utils/decorators";
import { ajax } from "discourse/lib/ajax";
import { cookAsync, emojiUnescape, excerpt } from "discourse/lib/text";
import { escapeExpression } from "discourse/lib/utilities";
import {
NEW_PRIVATE_MESSAGE_KEY,
NEW_TOPIC_KEY,
} from "discourse/models/composer";
import RestModel from "discourse/models/rest";
import UserDraft from "discourse/models/user-draft";
import { Promise } from "rsvp";
export default RestModel.extend({
limit: 30,
loading: false,
hasMore: false,
content: null,
init() {
this._super(...arguments);
this.reset();
},
reset() {
this.setProperties({
loading: false,
hasMore: true,
content: [],
});
},
@discourseComputed("content.length", "loading")
noContent(contentLength, loading) {
return contentLength === 0 && !loading;
},
remove(draft) {
this.set(
"content",
this.content.filter((item) => item.draft_key !== draft.draft_key)
);
},
findItems(site) {
if (site) {
this.set("site", site);
}
if (this.loading || !this.hasMore) {
return Promise.resolve();
}
this.set("loading", true);
const url = `/drafts.json?offset=${this.content.length}&limit=${this.limit}`;
return ajax(url)
.then((result) => {
if (!result) {
return;
}
if (!result.drafts) {
return;
}
this.set("hasMore", result.drafts.size >= this.limit);
const promises = result.drafts.map((draft) => {
draft.data = JSON.parse(draft.data);
return cookAsync(draft.data.reply).then((cooked) => {
draft.excerpt = excerpt(cooked.string, 300);
draft.post_number = draft.data.postId || null;
if (
draft.draft_key === NEW_PRIVATE_MESSAGE_KEY ||
draft.draft_key === NEW_TOPIC_KEY
) {
draft.title = draft.data.title;
}
draft.title = emojiUnescape(escapeExpression(draft.title));
if (draft.data.categoryId) {
draft.category =
this.site.categories.findBy("id", draft.data.categoryId) ||
null;
}
this.content.push(UserDraft.create(draft));
});
});
return Promise.all(promises);
})
.finally(() => {
this.set("loading", false);
});
},
});