1
1
import $ from "jquery" ;
2
+ import { z } from "zod" ;
2
3
3
4
import render_topic_muted from "../templates/topic_muted.hbs" ;
4
5
@@ -15,7 +16,32 @@ import * as timerender from "./timerender";
15
16
import * as ui_report from "./ui_report" ;
16
17
import { get_time_from_date_muted } from "./util" ;
17
18
18
- const all_user_topics = new Map ( ) ;
19
+ type ServerUserTopic = z . infer < typeof user_topic_schema > ;
20
+
21
+ export type UserTopic = {
22
+ stream_id : number ;
23
+ stream : string | undefined ;
24
+ topic : string ;
25
+ date_updated : number ;
26
+ date_updated_str : string ;
27
+ visibility_policy : number ;
28
+ } ;
29
+
30
+ const user_topic_schema = z . object ( {
31
+ stream_id : z . number ( ) ,
32
+ topic_name : z . string ( ) ,
33
+ last_updated : z . number ( ) ,
34
+ visibility_policy : z . number ( ) ,
35
+ stream__name : z . string ( ) . optional ( ) ,
36
+ } ) ;
37
+
38
+ const all_user_topics = new Map <
39
+ number ,
40
+ FoldDict < {
41
+ date_updated : number ;
42
+ visibility_policy : number ;
43
+ } >
44
+ > ( ) ;
19
45
20
46
export const all_visibility_policies = {
21
47
INHERIT : 0 ,
@@ -24,7 +50,12 @@ export const all_visibility_policies = {
24
50
FOLLOWED : 3 ,
25
51
} ;
26
52
27
- export function update_user_topics ( stream_id , topic , visibility_policy , date_updated ) {
53
+ export function update_user_topics (
54
+ stream_id : number ,
55
+ topic : string ,
56
+ visibility_policy : number ,
57
+ date_updated : number ,
58
+ ) : void {
28
59
let sub_dict = all_user_topics . get ( stream_id ) ;
29
60
if ( visibility_policy === all_visibility_policies . INHERIT && sub_dict ) {
30
61
sub_dict . delete ( topic ) ;
@@ -38,41 +69,41 @@ export function update_user_topics(stream_id, topic, visibility_policy, date_upd
38
69
}
39
70
}
40
71
41
- export function get_topic_visibility_policy ( stream_id , topic ) {
72
+ export function get_topic_visibility_policy ( stream_id : number , topic : string ) : number | boolean {
42
73
if ( stream_id === undefined ) {
43
74
return false ;
44
75
}
45
76
const sub_dict = all_user_topics . get ( stream_id ) ;
46
77
if ( sub_dict && sub_dict . get ( topic ) ) {
47
- return sub_dict . get ( topic ) . visibility_policy ;
78
+ return sub_dict . get ( topic ) ! . visibility_policy ;
48
79
}
49
80
50
81
return all_visibility_policies . INHERIT ;
51
82
}
52
83
53
- export function is_topic_followed ( stream_id , topic ) {
84
+ export function is_topic_followed ( stream_id : number , topic : string ) : boolean {
54
85
return get_topic_visibility_policy ( stream_id , topic ) === all_visibility_policies . FOLLOWED ;
55
86
}
56
87
57
- export function is_topic_unmuted ( stream_id , topic ) {
88
+ export function is_topic_unmuted ( stream_id : number , topic : string ) : boolean {
58
89
return get_topic_visibility_policy ( stream_id , topic ) === all_visibility_policies . UNMUTED ;
59
90
}
60
91
61
- export function is_topic_muted ( stream_id , topic ) {
92
+ export function is_topic_muted ( stream_id : number , topic : string ) : boolean {
62
93
return get_topic_visibility_policy ( stream_id , topic ) === all_visibility_policies . MUTED ;
63
94
}
64
95
65
- export function is_topic_unmuted_or_followed ( stream_id , topic ) {
96
+ export function is_topic_unmuted_or_followed ( stream_id : number , topic : string ) : boolean {
66
97
return is_topic_unmuted ( stream_id , topic ) || is_topic_followed ( stream_id , topic ) ;
67
98
}
68
99
69
- export function get_user_topics_for_visibility_policy ( visibility_policy ) {
70
- const topics = [ ] ;
100
+ export function get_user_topics_for_visibility_policy ( visibility_policy : number ) : UserTopic [ ] {
101
+ const topics : UserTopic [ ] = [ ] ;
71
102
for ( const [ stream_id , sub_dict ] of all_user_topics ) {
72
103
const stream = sub_store . maybe_get_stream_name ( stream_id ) ;
73
104
for ( const topic of sub_dict . keys ( ) ) {
74
- if ( sub_dict . get ( topic ) . visibility_policy === visibility_policy ) {
75
- const date_updated = sub_dict . get ( topic ) . date_updated ;
105
+ if ( sub_dict . get ( topic ) ! . visibility_policy === visibility_policy ) {
106
+ const date_updated = sub_dict . get ( topic ) ! . date_updated ;
76
107
const date_updated_str = timerender . render_now ( new Date ( date_updated ) ) . time_str ;
77
108
topics . push ( {
78
109
stream_id,
@@ -89,27 +120,27 @@ export function get_user_topics_for_visibility_policy(visibility_policy) {
89
120
}
90
121
91
122
export function set_user_topic_visibility_policy (
92
- stream_id ,
93
- topic ,
94
- visibility_policy ,
95
- from_hotkey ,
96
- from_banner ,
97
- status_element ,
98
- ) {
123
+ stream_id : number ,
124
+ topic : string ,
125
+ visibility_policy : number ,
126
+ from_hotkey ?: boolean ,
127
+ from_banner ?: boolean ,
128
+ status_element ?: JQuery ,
129
+ ) : void {
99
130
const data = {
100
131
stream_id,
101
132
topic,
102
133
visibility_policy,
103
134
} ;
104
135
105
- let $spinner ;
136
+ let $spinner : JQuery ;
106
137
if ( status_element ) {
107
138
$spinner = $ ( status_element ) . expectOne ( ) ;
108
139
$spinner . fadeTo ( 0 , 1 ) ;
109
140
loading . make_indicator ( $spinner , { text : settings_ui . strings . saving } ) ;
110
141
}
111
142
112
- channel . post ( {
143
+ void channel . post ( {
113
144
url : "/json/user_topics" ,
114
145
data,
115
146
success ( ) {
@@ -146,9 +177,9 @@ export function set_user_topic_visibility_policy(
146
177
const stream_name = sub_store . maybe_get_stream_name ( stream_id ) ;
147
178
feedback_widget . show ( {
148
179
populate ( $container ) {
149
- const rendered_html = render_topic_muted ( ) ;
180
+ const rendered_html = render_topic_muted ( { } ) ;
150
181
$container . html ( rendered_html ) ;
151
- $container . find ( ".stream" ) . text ( stream_name ) ;
182
+ $container . find ( ".stream" ) . text ( stream_name ?? "" ) ;
152
183
$container . find ( ".topic" ) . text ( topic ) ;
153
184
} ,
154
185
on_undo ( ) {
@@ -166,13 +197,13 @@ export function set_user_topic_visibility_policy(
166
197
} ) ;
167
198
}
168
199
169
- export function set_visibility_policy_for_element ( $elt , visibility_policy ) {
170
- const stream_id = Number . parseInt ( $elt . attr ( "data-stream-id" ) , 10 ) ;
171
- const topic = $elt . attr ( "data-topic-name" ) ;
200
+ export function set_visibility_policy_for_element ( $elt : JQuery , visibility_policy : number ) : void {
201
+ const stream_id = Number . parseInt ( $elt . attr ( "data-stream-id" ) ! , 10 ) ;
202
+ const topic = $elt . attr ( "data-topic-name" ) ! ;
172
203
set_user_topic_visibility_policy ( stream_id , topic , visibility_policy ) ;
173
204
}
174
205
175
- export function set_user_topic ( user_topic ) {
206
+ export function set_user_topic ( user_topic : ServerUserTopic ) : void {
176
207
const stream_id = user_topic . stream_id ;
177
208
const topic = user_topic . topic_name ;
178
209
const date_updated = user_topic . last_updated ;
@@ -187,14 +218,16 @@ export function set_user_topic(user_topic) {
187
218
update_user_topics ( stream_id , topic , user_topic . visibility_policy , date_updated ) ;
188
219
}
189
220
190
- export function set_user_topics ( user_topics ) {
221
+ export function set_user_topics ( user_topics : ServerUserTopic [ ] ) : void {
191
222
all_user_topics . clear ( ) ;
192
223
193
224
for ( const user_topic of user_topics ) {
194
225
set_user_topic ( user_topic ) ;
195
226
}
196
227
}
197
228
198
- export function initialize ( params ) {
199
- set_user_topics ( params . user_topics ) ;
229
+ export function initialize ( params : { user_topics : ServerUserTopic [ ] } ) : void {
230
+ const user_topics = user_topic_schema . array ( ) . parse ( params . user_topics ) ;
231
+
232
+ set_user_topics ( user_topics ) ;
200
233
}
0 commit comments