UX: improve messaging so notifications list is far more stable PERF: improve performance of notifcation lookup queries - Add feature "SetTransientHeader" that allows shipping info to server in the next Ajax request - remove local storage hack used for notifications - amend lookupStale to return hydrated objects, move logic into store - stop magically clearing various notifications (likes, invitee accepted, group_summary, granted badge)
103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
import { hashString } from 'discourse/lib/hash';
|
|
|
|
const ADMIN_MODELS = ['plugin', 'site-customization', 'embeddable-host'];
|
|
|
|
|
|
export function Result(payload, responseJson) {
|
|
this.payload = payload;
|
|
this.responseJson = responseJson;
|
|
this.target = null;
|
|
}
|
|
|
|
const ajax = Discourse.ajax;
|
|
|
|
// We use this to make sure 404s are caught
|
|
function rethrow(error) {
|
|
if (error.status === 404) {
|
|
throw "404: " + error.responseText;
|
|
}
|
|
throw(error);
|
|
}
|
|
|
|
export default Ember.Object.extend({
|
|
|
|
|
|
storageKey(type, findArgs, options) {
|
|
if (options && options.cacheKey) {
|
|
return options.cacheKey;
|
|
}
|
|
const hashedArgs = Math.abs(hashString(JSON.stringify(findArgs)));
|
|
return `${type}_${hashedArgs}`;
|
|
},
|
|
|
|
basePath(store, type) {
|
|
if (ADMIN_MODELS.indexOf(type.replace('_', '-')) !== -1) { return "/admin/"; }
|
|
return "/";
|
|
},
|
|
|
|
appendQueryParams(path, findArgs) {
|
|
if (findArgs) {
|
|
if (typeof findArgs === "object") {
|
|
const queryString = Object.keys(findArgs)
|
|
.reject(k => !findArgs[k])
|
|
.map(k => k + "=" + encodeURIComponent(findArgs[k]));
|
|
|
|
if (queryString.length) {
|
|
return path + "?" + queryString.join('&');
|
|
}
|
|
} else {
|
|
// It's serializable as a string if not an object
|
|
return path + "/" + findArgs;
|
|
}
|
|
}
|
|
return path;
|
|
},
|
|
|
|
pathFor(store, type, findArgs) {
|
|
let path = this.basePath(store, type, findArgs) + Ember.String.underscore(store.pluralize(type));
|
|
return this.appendQueryParams(path, findArgs);
|
|
},
|
|
|
|
findAll(store, type) {
|
|
return ajax(this.pathFor(store, type)).catch(rethrow);
|
|
},
|
|
|
|
|
|
find(store, type, findArgs) {
|
|
return ajax(this.pathFor(store, type, findArgs)).catch(rethrow);
|
|
},
|
|
|
|
findStale(store, type, findArgs, options) {
|
|
if (this.cached) {
|
|
return this.cached[this.storageKey(type, findArgs, options)];
|
|
}
|
|
},
|
|
|
|
cacheFind(store, type, findArgs, opts, hydrated) {
|
|
this.cached = this.cached || {};
|
|
this.cached[this.storageKey(type,findArgs,opts)] = hydrated;
|
|
},
|
|
|
|
update(store, type, id, attrs) {
|
|
const data = {};
|
|
const typeField = Ember.String.underscore(type);
|
|
data[typeField] = attrs;
|
|
return ajax(this.pathFor(store, type, id), { method: 'PUT', data }).then(function(json) {
|
|
return new Result(json[typeField], json);
|
|
});
|
|
},
|
|
|
|
createRecord(store, type, attrs) {
|
|
const data = {};
|
|
const typeField = Ember.String.underscore(type);
|
|
data[typeField] = attrs;
|
|
return ajax(this.pathFor(store, type), { method: 'POST', data }).then(function (json) {
|
|
return new Result(json[typeField], json);
|
|
});
|
|
},
|
|
|
|
destroyRecord(store, type, record) {
|
|
return ajax(this.pathFor(store, type, record.get('id')), { method: 'DELETE' });
|
|
}
|
|
|
|
});
|