-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
site.js
245 lines (212 loc) · 6.9 KB
/
site.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import EmberObject, { get } from "@ember/object";
import { alias, sort } from "@ember/object/computed";
import { htmlSafe } from "@ember/template";
import { isEmpty } from "@ember/utils";
import PreloadStore from "discourse/lib/preload-store";
import Singleton from "discourse/mixins/singleton";
import Archetype from "discourse/models/archetype";
import Category from "discourse/models/category";
import PostActionType from "discourse/models/post-action-type";
import RestModel from "discourse/models/rest";
import TrustLevel from "discourse/models/trust-level";
import deprecated from "discourse-common/lib/deprecated";
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
import discourseComputed from "discourse-common/utils/decorators";
const Site = RestModel.extend({
isReadOnly: alias("is_readonly"),
init() {
this._super(...arguments);
this.topicCountDesc = ["topic_count:desc"];
},
@discourseComputed("notification_types")
notificationLookup(notificationTypes) {
const result = [];
Object.keys(notificationTypes).forEach(
(k) => (result[notificationTypes[k]] = k)
);
return result;
},
@discourseComputed("post_action_types.[]")
flagTypes() {
const postActionTypes = this.post_action_types;
if (!postActionTypes) {
return [];
}
return postActionTypes.filterBy("is_flag", true);
},
categoriesByCount: sort("categories", "topicCountDesc"),
collectUserFields(fields) {
fields = fields || {};
let siteFields = this.user_fields;
if (!isEmpty(siteFields)) {
return siteFields.map((f) => {
let value = fields ? fields[f.id.toString()] : null;
value = value || htmlSafe("—");
return { name: f.name, value };
});
}
return [];
},
// Sort subcategories under parents
@discourseComputed("categoriesByCount", "categories.[]")
sortedCategories(categories) {
return Category.sortCategories(categories);
},
// Returns it in the correct order, by setting
@discourseComputed("categories.[]")
categoriesList(categories) {
return this.siteSettings.fixed_category_positions
? categories
: this.sortedCategories;
},
@discourseComputed("categories.[]", "categories.@each.notification_level")
trackedCategoriesList(categories) {
const trackedCategories = [];
for (const category of categories) {
if (category.isTracked) {
if (
this.siteSettings.allow_uncategorized_topics ||
!category.isUncategorizedCategory
) {
trackedCategories.push(category);
}
}
}
return trackedCategories;
},
postActionTypeById(id) {
return this.get("postActionByIdLookup.action" + id);
},
topicFlagTypeById(id) {
return this.get("topicFlagByIdLookup.action" + id);
},
removeCategory(id) {
const categories = this.categories;
const existingCategory = categories.findBy("id", id);
if (existingCategory) {
categories.removeObject(existingCategory);
delete this.categoriesById.categoryId;
}
},
updateCategory(newCategory) {
const categories = this.categories;
const categoryId = get(newCategory, "id");
const existingCategory = categories.findBy("id", categoryId);
// Don't update null permissions
if (newCategory.permission === null) {
delete newCategory.permission;
}
if (existingCategory) {
existingCategory.setProperties(newCategory);
return existingCategory;
} else {
// TODO insert in right order?
newCategory = this.store.createRecord("category", newCategory);
categories.pushObject(newCategory);
this.categoriesById[categoryId] = newCategory;
newCategory.set(
"parentCategory",
this.categoriesById[newCategory.parent_category_id]
);
return newCategory;
}
},
});
Site.reopenClass(Singleton, {
// The current singleton will retrieve its attributes from the `PreloadStore`.
createCurrent() {
const store = getOwnerWithFallback(this).lookup("service:store");
const siteAttributes = PreloadStore.get("site");
siteAttributes["isReadOnly"] = PreloadStore.get("isReadOnly");
siteAttributes["isStaffWritesOnly"] = PreloadStore.get("isStaffWritesOnly");
return store.createRecord("site", siteAttributes);
},
create() {
const result = this._super.apply(this, arguments);
const store = result.store;
if (result.categories) {
let subcatMap = {};
result.categoriesById = new Map();
result.categories = result.categories.map((c) => {
if (c.parent_category_id) {
subcatMap[c.parent_category_id] =
subcatMap[c.parent_category_id] || [];
subcatMap[c.parent_category_id].push(c.id);
}
return (result.categoriesById[c.id] = store.createRecord(
"category",
c
));
});
// Associate the categories with their parents
result.categories.forEach((c) => {
let subcategoryIds = subcatMap[c.get("id")];
if (subcategoryIds) {
c.set(
"subcategories",
subcategoryIds.map((id) => result.categoriesById[id])
);
}
if (c.get("parent_category_id")) {
c.set(
"parentCategory",
result.categoriesById[c.get("parent_category_id")]
);
}
});
}
if (result.trust_levels) {
result.trustLevels = Object.entries(result.trust_levels).map(
([key, id]) => {
return new TrustLevel(id, key);
}
);
delete result.trust_levels;
}
if (result.post_action_types) {
result.postActionByIdLookup = EmberObject.create();
result.post_action_types = result.post_action_types.map((p) => {
const actionType = PostActionType.create(p);
result.postActionByIdLookup.set("action" + p.id, actionType);
return actionType;
});
}
if (result.topic_flag_types) {
result.topicFlagByIdLookup = EmberObject.create();
result.topic_flag_types = result.topic_flag_types.map((p) => {
const actionType = PostActionType.create(p);
result.topicFlagByIdLookup.set("action" + p.id, actionType);
return actionType;
});
}
if (result.archetypes) {
result.archetypes = result.archetypes.map((a) => {
a.site = result;
return Archetype.create(a);
});
}
if (result.user_fields) {
result.user_fields = result.user_fields.map((uf) =>
EmberObject.create(uf)
);
}
return result;
},
});
if (typeof Discourse !== "undefined") {
let warned = false;
// eslint-disable-next-line no-undef
Object.defineProperty(Discourse, "Site", {
get() {
if (!warned) {
deprecated("Import the Site class instead of using Discourse.Site", {
since: "2.4.0",
id: "discourse.globals.site",
});
warned = true;
}
return Site;
},
});
}
export default Site;