2015-04-25 20:58:39 +08:00
|
|
|
import Model from 'flarum/model';
|
|
|
|
import computed from 'flarum/utils/computed';
|
|
|
|
import ItemList from 'flarum/utils/item-list';
|
|
|
|
|
2015-05-02 07:06:59 +08:00
|
|
|
class Discussion extends Model {
|
2015-05-29 17:26:29 +08:00
|
|
|
pushData(newData) {
|
|
|
|
super.pushData(newData);
|
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
// var links = this.data().links;
|
|
|
|
// var posts = links && links.posts;
|
|
|
|
// if (posts) {
|
|
|
|
// if (newData.removedPosts) {
|
|
|
|
// posts.linkage.forEach((linkage, i) => {
|
|
|
|
// if (newData.removedPosts.indexOf(linkage.id) !== -1) {
|
|
|
|
// posts.linkage.splice(i, 1);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
2015-05-29 17:26:29 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
// if (newData.links && newData.links.addedPosts) {
|
|
|
|
// newData.links.addedPosts.linkage.forEach(linkage => {
|
|
|
|
// if (posts.linkage[posts.linkage.length - 1].id != linkage.id) {
|
|
|
|
// posts.linkage.push(linkage);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
2015-05-29 17:26:29 +08:00
|
|
|
}
|
|
|
|
|
2015-05-02 07:06:59 +08:00
|
|
|
unreadCount() {
|
|
|
|
var user = app.session.user();
|
|
|
|
if (user && user.readTime() < this.lastTime()) {
|
|
|
|
return Math.max(0, this.lastPostNumber() - (this.readNumber() || 0))
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
badges() {
|
|
|
|
return new ItemList();
|
|
|
|
}
|
|
|
|
}
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.title = Model.attribute('title');
|
2015-05-07 15:23:57 +08:00
|
|
|
Discussion.prototype.slug = computed('title', title => title.toLowerCase().replace(/[^a-z0-9]/gi, '-').replace(/-+/g, '-').replace(/-$|^-/g, '') || '-');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.startTime = Model.attribute('startTime', Model.transformDate);
|
|
|
|
Discussion.prototype.startUser = Model.hasOne('startUser');
|
|
|
|
Discussion.prototype.startPost = Model.hasOne('startPost');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.lastTime = Model.attribute('lastTime', Model.transformDate);
|
|
|
|
Discussion.prototype.lastUser = Model.hasOne('lastUser');
|
|
|
|
Discussion.prototype.lastPost = Model.hasOne('lastPost');
|
|
|
|
Discussion.prototype.lastPostNumber = Model.attribute('lastPostNumber');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.canReply = Model.attribute('canReply');
|
|
|
|
Discussion.prototype.canRename = Model.attribute('canRename');
|
|
|
|
Discussion.prototype.canDelete = Model.attribute('canDelete');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.commentsCount = Model.attribute('commentsCount');
|
2015-06-23 09:00:06 +08:00
|
|
|
Discussion.prototype.repliesCount = computed('commentsCount', commentsCount => Math.max(0, commentsCount - 1));
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.posts = Model.hasMany('posts');
|
|
|
|
Discussion.prototype.postIds = function() { return this.data().relationships.posts.data.map((link) => link.id); };
|
|
|
|
Discussion.prototype.relevantPosts = Model.hasMany('relevantPosts');
|
|
|
|
Discussion.prototype.addedPosts = Model.hasMany('addedPosts');
|
|
|
|
Discussion.prototype.removedPosts = Model.attribute('removedPosts');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-04 10:54:48 +08:00
|
|
|
Discussion.prototype.readTime = Model.attribute('readTime', Model.transformDate);
|
|
|
|
Discussion.prototype.readNumber = Model.attribute('readNumber');
|
2015-04-25 20:58:39 +08:00
|
|
|
|
|
|
|
Discussion.prototype.isUnread = computed('unreadCount', unreadCount => !!unreadCount);
|
|
|
|
|
|
|
|
export default Discussion;
|