2015-07-15 12:30:11 +08:00
|
|
|
import Model from 'flarum/Model';
|
2015-04-25 20:58:39 +08:00
|
|
|
import computed from 'flarum/utils/computed';
|
2015-07-15 12:30:11 +08:00
|
|
|
import ItemList from 'flarum/utils/ItemList';
|
2015-09-22 16:18:21 +08:00
|
|
|
import Badge from 'flarum/components/Badge';
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-10-13 14:27:18 +08:00
|
|
|
export default class Discussion extends Model {}
|
|
|
|
|
|
|
|
Object.assign(Discussion.prototype, {
|
2015-07-15 12:30:11 +08:00
|
|
|
title: Model.attribute('title'),
|
2016-02-04 18:46:30 +08:00
|
|
|
slug: Model.attribute('slug'),
|
2015-07-15 12:30:11 +08:00
|
|
|
|
|
|
|
startTime: Model.attribute('startTime', Model.transformDate),
|
|
|
|
startUser: Model.hasOne('startUser'),
|
|
|
|
startPost: Model.hasOne('startPost'),
|
|
|
|
|
|
|
|
lastTime: Model.attribute('lastTime', Model.transformDate),
|
|
|
|
lastUser: Model.hasOne('lastUser'),
|
|
|
|
lastPost: Model.hasOne('lastPost'),
|
|
|
|
lastPostNumber: Model.attribute('lastPostNumber'),
|
|
|
|
|
|
|
|
commentsCount: Model.attribute('commentsCount'),
|
|
|
|
repliesCount: computed('commentsCount', commentsCount => Math.max(0, commentsCount - 1)),
|
|
|
|
posts: Model.hasMany('posts'),
|
|
|
|
relevantPosts: Model.hasMany('relevantPosts'),
|
|
|
|
|
|
|
|
readTime: Model.attribute('readTime', Model.transformDate),
|
|
|
|
readNumber: Model.attribute('readNumber'),
|
|
|
|
isUnread: computed('unreadCount', unreadCount => !!unreadCount),
|
2015-09-08 08:57:50 +08:00
|
|
|
isRead: computed('unreadCount', unreadCount => app.session.user && !unreadCount),
|
2015-07-15 12:30:11 +08:00
|
|
|
|
2015-09-22 16:18:21 +08:00
|
|
|
hideTime: Model.attribute('hideTime', Model.transformDate),
|
|
|
|
hideUser: Model.hasOne('hideUser'),
|
|
|
|
isHidden: computed('hideTime', 'commentsCount', (hideTime, commentsCount) => !!hideTime || commentsCount === 0),
|
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
canReply: Model.attribute('canReply'),
|
|
|
|
canRename: Model.attribute('canRename'),
|
2015-09-22 16:18:21 +08:00
|
|
|
canHide: Model.attribute('canHide'),
|
2015-10-13 14:27:18 +08:00
|
|
|
canDelete: Model.attribute('canDelete'),
|
|
|
|
|
2015-07-08 09:21:30 +08:00
|
|
|
/**
|
|
|
|
* Remove a post from the discussion's posts relationship.
|
|
|
|
*
|
2015-07-15 12:30:11 +08:00
|
|
|
* @param {Integer} id The ID of the post to remove.
|
|
|
|
* @public
|
2015-07-08 09:21:30 +08:00
|
|
|
*/
|
2015-07-06 14:56:27 +08:00
|
|
|
removePost(id) {
|
2015-07-15 12:30:11 +08:00
|
|
|
const relationships = this.data.relationships;
|
2015-07-06 14:56:27 +08:00
|
|
|
const posts = relationships && relationships.posts;
|
|
|
|
|
|
|
|
if (posts) {
|
|
|
|
posts.data.some((data, i) => {
|
|
|
|
if (id === data.id) {
|
|
|
|
posts.data.splice(i, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-10-13 14:27:18 +08:00
|
|
|
},
|
2015-07-06 14:56:27 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Get the estimated number of unread posts in this discussion for the current
|
|
|
|
* user.
|
|
|
|
*
|
|
|
|
* @return {Integer}
|
|
|
|
* @public
|
|
|
|
*/
|
2015-05-02 07:06:59 +08:00
|
|
|
unreadCount() {
|
2015-07-15 12:30:11 +08:00
|
|
|
const user = app.session.user;
|
|
|
|
|
2015-05-02 07:06:59 +08:00
|
|
|
if (user && user.readTime() < this.lastTime()) {
|
2015-09-08 08:57:50 +08:00
|
|
|
return Math.max(0, this.lastPostNumber() - (this.readNumber() || 0));
|
2015-05-02 07:06:59 +08:00
|
|
|
}
|
2015-07-15 12:30:11 +08:00
|
|
|
|
2015-05-02 07:06:59 +08:00
|
|
|
return 0;
|
2015-10-13 14:27:18 +08:00
|
|
|
},
|
2015-05-02 07:06:59 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Get the Badge components that apply to this discussion.
|
|
|
|
*
|
|
|
|
* @return {ItemList}
|
|
|
|
* @public
|
|
|
|
*/
|
2015-05-02 07:06:59 +08:00
|
|
|
badges() {
|
2015-09-22 16:18:21 +08:00
|
|
|
const items = new ItemList();
|
|
|
|
|
|
|
|
if (this.isHidden()) {
|
2015-10-22 08:31:13 +08:00
|
|
|
items.add('hidden', <Badge type="hidden" icon="trash" label={app.translator.trans('core.lib.badge.hidden_tooltip')}/>);
|
2015-09-22 16:18:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
2015-10-13 14:27:18 +08:00
|
|
|
},
|
2015-04-25 20:58:39 +08:00
|
|
|
|
2015-07-15 12:30:11 +08:00
|
|
|
/**
|
|
|
|
* Get a list of all of the post IDs in this discussion.
|
|
|
|
*
|
|
|
|
* @return {Array}
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
postIds() {
|
2016-02-22 19:52:49 +08:00
|
|
|
const posts = this.data.relationships.posts;
|
|
|
|
|
|
|
|
return posts ? posts.data.map(link => link.id) : [];
|
2015-07-15 12:30:11 +08:00
|
|
|
}
|
2015-10-13 14:27:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
export default Discussion;
|