-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
Discussion.tsx
146 lines (128 loc) · 4.09 KB
/
Discussion.tsx
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
import app from '../../common/app';
import Model from '../Model';
import computed from '../utils/computed';
import ItemList from '../utils/ItemList';
import Badge from '../components/Badge';
import Mithril from 'mithril';
import Post from './Post';
import User from './User';
export default class Discussion extends Model {
title() {
return Model.attribute<string>('title').call(this);
}
slug() {
return Model.attribute<string>('slug').call(this);
}
createdAt() {
return Model.attribute<Date | undefined, string | undefined>('createdAt', Model.transformDate).call(this);
}
user() {
return Model.hasOne<User | null>('user').call(this);
}
firstPost() {
return Model.hasOne<Post | null>('firstPost').call(this);
}
lastPostedAt() {
return Model.attribute('lastPostedAt', Model.transformDate).call(this);
}
lastPostedUser() {
return Model.hasOne<User | null>('lastPostedUser').call(this);
}
lastPost() {
return Model.hasOne<Post | null>('lastPost').call(this);
}
lastPostNumber() {
return Model.attribute<number | null | undefined>('lastPostNumber').call(this);
}
commentCount() {
return Model.attribute<number | undefined>('commentCount').call(this);
}
replyCount() {
return computed<number, this>('commentCount', (commentCount) => Math.max(0, ((commentCount as number) ?? 0) - 1)).call(this);
}
posts() {
return Model.hasMany<Post>('posts').call(this);
}
mostRelevantPost() {
return Model.hasOne<Post | null>('mostRelevantPost').call(this);
}
lastReadAt() {
return Model.attribute('lastReadAt', Model.transformDate).call(this);
}
lastReadPostNumber() {
return Model.attribute<number | null | undefined>('lastReadPostNumber').call(this);
}
isUnread() {
return computed<boolean, this>('unreadCount', (unreadCount) => !!unreadCount).call(this);
}
isRead() {
return computed<boolean, this>('unreadCount', (unreadCount) => !!(app.session.user && !unreadCount)).call(this);
}
hiddenAt() {
return Model.attribute('hiddenAt', Model.transformDate).call(this);
}
hiddenUser() {
return Model.hasOne<User | null>('hiddenUser').call(this);
}
isHidden() {
return computed<boolean, this>('hiddenAt', (hiddenAt) => !!hiddenAt).call(this);
}
canReply() {
return Model.attribute<boolean | undefined>('canReply').call(this);
}
canRename() {
return Model.attribute<boolean | undefined>('canRename').call(this);
}
canHide() {
return Model.attribute<boolean | undefined>('canHide').call(this);
}
canDelete() {
return Model.attribute<boolean | undefined>('canDelete').call(this);
}
/**
* Remove a post from the discussion's posts relationship.
*/
removePost(id: string): void {
const posts = this.rawRelationship<Post[]>('posts');
if (!posts) {
return;
}
posts.some((data, i) => {
if (id === data.id) {
posts.splice(i, 1);
return true;
}
return false;
});
}
/**
* Get the estimated number of unread posts in this discussion for the current
* user.
*/
unreadCount(): number {
const user = app.session.user;
if (user && (user.markedAllAsReadAt()?.getTime() ?? 0) < this.lastPostedAt()?.getTime()!) {
const unreadCount = Math.max(0, (this.lastPostNumber() ?? 0) - (this.lastReadPostNumber() || 0));
// If posts have been deleted, it's possible that the unread count could exceed the
// actual post count. As such, we take the min of the two to ensure this isn't an issue.
return Math.min(unreadCount, this.commentCount() ?? 0);
}
return 0;
}
/**
* Get the Badge components that apply to this discussion.
*/
badges(): ItemList<Mithril.Children> {
const items = new ItemList<Mithril.Children>();
if (this.isHidden()) {
items.add('hidden', <Badge type="hidden" icon="fas fa-trash" label={app.translator.trans('core.lib.badge.hidden_tooltip')} />);
}
return items;
}
/**
* Get a list of all of the post IDs in this discussion.
*/
postIds(): string[] {
return this.rawRelationship<Post[]>('posts')?.map((link) => link.id) ?? [];
}
}