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-stream.js
Jarek Radosz 87a6cf0133
DEV: Try reducing RSVP usage
Native Promises ftw?
2022-08-17 12:54:35 +02:00

123 lines
3.1 KiB
JavaScript

import discourseComputed, { on } from "discourse-common/utils/decorators";
import { A } from "@ember/array";
import RestModel from "discourse/models/rest";
import UserAction from "discourse/models/user-action";
import { ajax } from "discourse/lib/ajax";
import { emojiUnescape } from "discourse/lib/text";
import { escapeExpression } from "discourse/lib/utilities";
import { url } from "discourse/lib/computed";
export default RestModel.extend({
loaded: false,
@on("init")
_initialize() {
this.setProperties({ itemsLoaded: 0, content: [] });
},
@discourseComputed("filter")
filterParam(filter) {
if (filter === UserAction.TYPES.replies) {
return [UserAction.TYPES.replies, UserAction.TYPES.quotes].join(",");
}
if (!filter) {
return [UserAction.TYPES.topics, UserAction.TYPES.posts].join(",");
}
return filter;
},
baseUrl: url(
"itemsLoaded",
"user.username_lower",
"/user_actions.json?offset=%@&username=%@"
),
filterBy(opts) {
this.setProperties(
Object.assign(
{
itemsLoaded: 0,
content: [],
lastLoadedUrl: null,
},
opts
)
);
return this.findItems();
},
@discourseComputed("loaded", "content.[]")
noContent(loaded, content) {
return loaded && content.length === 0;
},
remove(userAction) {
// 1) remove the user action from the child groups
this.content.forEach((ua) => {
["likes", "stars", "edits", "bookmarks"].forEach((group) => {
const items = ua.get(`childGroups.${group}.items`);
if (items) {
items.removeObject(userAction);
}
});
});
// 2) remove the parents that have no children
const content = this.content.filter((ua) => {
return ["likes", "stars", "edits", "bookmarks"].some((group) => {
return ua.get(`childGroups.${group}.items.length`) > 0;
});
});
this.setProperties({ content, itemsLoaded: content.length });
},
findItems() {
let findUrl = this.baseUrl;
if (this.filterParam) {
findUrl += `&filter=${this.filterParam}`;
}
if (this.actingUsername) {
findUrl += `&acting_username=${this.actingUsername}`;
}
// Don't load the same stream twice. We're probably at the end.
const lastLoadedUrl = this.lastLoadedUrl;
if (lastLoadedUrl === findUrl) {
return Promise.resolve();
}
if (this.loading) {
return Promise.resolve();
}
this.set("loading", true);
return ajax(findUrl)
.then((result) => {
if (result && result.user_actions) {
const copy = A();
result.user_actions.forEach((action) => {
action.title = emojiUnescape(escapeExpression(action.title));
copy.pushObject(UserAction.create(action));
});
this.content.pushObjects(UserAction.collapseStream(copy));
this.setProperties({
itemsLoaded: this.itemsLoaded + result.user_actions.length,
});
}
})
.finally(() =>
this.setProperties({
loaded: true,
loading: false,
lastLoadedUrl: findUrl,
})
);
},
});