2018-06-20 11:50:31 +08:00
|
|
|
import Model from '../Model';
|
|
|
|
import computed from '../utils/computed';
|
|
|
|
import ItemList from '../utils/ItemList';
|
|
|
|
import Badge from '../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
|
|
|
|
2018-08-24 19:18:04 +08:00
|
|
|
createdAt: Model.attribute('createdAt', Model.transformDate),
|
2018-08-24 19:13:52 +08:00
|
|
|
user: Model.hasOne('user'),
|
2018-08-24 19:12:07 +08:00
|
|
|
firstPost: Model.hasOne('firstPost'),
|
2015-07-15 12:30:11 +08:00
|
|
|
|
2018-08-24 19:19:06 +08:00
|
|
|
lastPostedAt: Model.attribute('lastPostedAt', Model.transformDate),
|
2018-08-24 19:14:42 +08:00
|
|
|
lastPostedUser: Model.hasOne('lastPostedUser'),
|
2015-07-15 12:30:11 +08:00
|
|
|
lastPost: Model.hasOne('lastPost'),
|
|
|
|
lastPostNumber: Model.attribute('lastPostNumber'),
|
|
|
|
|
2018-08-24 19:07:04 +08:00
|
|
|
commentCount: Model.attribute('commentCount'),
|
|
|
|
repliesCount: computed('commentCount', commentCount => Math.max(0, commentCount - 1)),
|
2015-07-15 12:30:11 +08:00
|
|
|
posts: Model.hasMany('posts'),
|
2018-02-08 04:08:08 +08:00
|
|
|
mostRelevantPost: Model.hasOne('mostRelevantPost'),
|
2015-07-15 12:30:11 +08:00
|
|
|
|
2018-08-24 19:21:58 +08:00
|
|
|
lastReadAt: Model.attribute('lastReadAt', Model.transformDate),
|
2018-08-24 19:23:28 +08:00
|
|
|
lastReadPostNumber: Model.attribute('lastReadPostNumber'),
|
2015-07-15 12:30:11 +08:00
|
|
|
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
|
|
|
|
2018-08-24 19:20:00 +08:00
|
|
|
hiddenAt: Model.attribute('hiddenAt', Model.transformDate),
|
2015-09-22 16:18:21 +08:00
|
|
|
hideUser: Model.hasOne('hideUser'),
|
2018-08-24 19:20:00 +08:00
|
|
|
isHidden: computed('hiddenAt', hiddenAt => !!hiddenAt),
|
2015-09-22 16:18:21 +08:00
|
|
|
|
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;
|
|
|
|
|
2018-08-24 19:19:06 +08:00
|
|
|
if (user && user.readTime() < this.lastPostedAt()) {
|
2018-08-24 19:23:28 +08:00
|
|
|
return Math.max(0, this.lastPostNumber() - (this.lastReadPostNumber() || 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()) {
|
2018-05-09 14:56:30 +08:00
|
|
|
items.add('hidden', <Badge type="hidden" icon="fas fa-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
|
|
|
});
|