@@ -5,7 +5,6 @@ import { makeUserId } from '../api/idTypes';
5
5
import type { Narrow , Stream , UserId , Message , Outbox , PmMessage , PmOutbox } from '../types' ;
6
6
import { topicNarrow , streamNarrow , specialNarrow , pmNarrowFromRecipients } from './narrow' ;
7
7
import { pmKeyRecipientsFromIds , recipientsOfPrivateMessage } from './recipient' ;
8
- import { ensureUnreachable } from '../generics' ;
9
8
import { isUrlOnRealm } from './url' ;
10
9
11
10
/**
@@ -48,51 +47,6 @@ export const isNarrowLink = (url: URL, realm: URL): boolean =>
48
47
&& url . search === ''
49
48
&& / ^ # n a r r o w \/ / i. test ( url . hash ) ;
50
49
51
- /* PRIVATE: Exported only for tests. */
52
- export type LinkType = 'home' | 'pm' | 'topic' | 'stream' | 'special' ;
53
-
54
- /**
55
- * PRIVATE -- exported only for tests.
56
- *
57
- * The passed `url` must appear to be a link to a Zulip narrow on the given
58
- * `realm`. In particular, `isNarrowLink(url, realm)` must be true.
59
- */
60
- // TODO: Work out what this does, write a jsdoc for its interface, and
61
- // reimplement using URL object (not just for the realm)
62
- export const getLinkType = ( url : URL , realm : URL ) : LinkType => {
63
- // isNarrowLink(…) is true, by jsdoc, so this call is OK.
64
- const hashSegments = getHashSegmentsFromNarrowLink ( url , realm ) ;
65
-
66
- if (
67
- ( hashSegments . length === 2 && hashSegments [ 0 ] === 'pm-with' )
68
- || ( hashSegments . length === 4 && hashSegments [ 0 ] === 'pm-with' && hashSegments [ 2 ] === 'near' )
69
- ) {
70
- return 'pm' ;
71
- }
72
-
73
- if (
74
- ( hashSegments . length === 4 || hashSegments . length === 6 )
75
- && hashSegments [ 0 ] === 'stream'
76
- && ( hashSegments [ 2 ] === 'subject' || hashSegments [ 2 ] === 'topic' )
77
- ) {
78
- return 'topic' ;
79
- }
80
-
81
- if ( hashSegments . length === 2 && hashSegments [ 0 ] === 'stream' ) {
82
- return 'stream' ;
83
- }
84
-
85
- if (
86
- hashSegments . length === 2
87
- && hashSegments [ 0 ] === 'is'
88
- && / ^ ( p r i v a t e | s t a r r e d | m e n t i o n e d ) / i. test ( hashSegments [ 1 ] )
89
- ) {
90
- return 'special' ;
91
- }
92
-
93
- return 'home' ;
94
- } ;
95
-
96
50
/** Decode a dot-encoded string. */
97
51
// The Zulip webapp uses this encoding in narrow-links:
98
52
// https://github.com/zulip/zulip/blob/1577662a6/static/js/hash_util.js#L18-L25
@@ -167,41 +121,49 @@ export const getNarrowFromLink = (
167
121
return null ;
168
122
}
169
123
170
- const type = getLinkType ( url , realm ) ;
171
-
172
124
// isNarrowLink(…) is true, by early return above, so this call is OK.
173
125
const hashSegments = getHashSegmentsFromNarrowLink ( url , realm ) ;
174
126
175
- switch ( type ) {
176
- case 'pm' : {
177
- // TODO: This case is pretty useless in practice, due to basically a
178
- // bug in the webapp: the URL that appears in the location bar for a
179
- // group PM conversation excludes self, so it's unusable for anyone
180
- // else. In particular this will foil you if, say, you try to give
181
- // someone else in the conversation a link to a particular message.
182
- const ids = parsePmOperand ( hashSegments [ 1 ] ) ;
183
- return pmNarrowFromRecipients ( pmKeyRecipientsFromIds ( ids , ownUserId ) ) ;
184
- }
185
- case 'topic' : {
186
- const stream = parseStreamOperand ( hashSegments [ 1 ] , streamsById , streamsByName ) ;
187
- return stream && topicNarrow ( stream . stream_id , parseTopicOperand ( hashSegments [ 3 ] ) ) ;
188
- }
189
- case 'stream' : {
190
- const stream = parseStreamOperand ( hashSegments [ 1 ] , streamsById , streamsByName ) ;
191
- return stream && streamNarrow ( stream . stream_id ) ;
192
- }
193
- case 'special' :
194
- try {
195
- return specialNarrow ( hashSegments [ 1 ] ) ;
196
- } catch {
197
- return null ;
198
- }
199
- case 'home' :
200
- return null ; // TODO(?): Give HOME_NARROW
201
- default :
202
- ensureUnreachable ( type ) ;
127
+ if (
128
+ ( hashSegments . length === 2 && hashSegments [ 0 ] === 'pm-with' )
129
+ || ( hashSegments . length === 4 && hashSegments [ 0 ] === 'pm-with' && hashSegments [ 2 ] === 'near' )
130
+ ) {
131
+ // TODO: This case is pretty useless in practice, due to basically a
132
+ // bug in the webapp: the URL that appears in the location bar for a
133
+ // group PM conversation excludes self, so it's unusable for anyone
134
+ // else. In particular this will foil you if, say, you try to give
135
+ // someone else in the conversation a link to a particular message.
136
+ const ids = parsePmOperand ( hashSegments [ 1 ] ) ;
137
+ return pmNarrowFromRecipients ( pmKeyRecipientsFromIds ( ids , ownUserId ) ) ;
138
+ }
139
+
140
+ if (
141
+ ( hashSegments . length === 4 || hashSegments . length === 6 )
142
+ && hashSegments [ 0 ] === 'stream'
143
+ && ( hashSegments [ 2 ] === 'subject' || hashSegments [ 2 ] === 'topic' )
144
+ ) {
145
+ const stream = parseStreamOperand ( hashSegments [ 1 ] , streamsById , streamsByName ) ;
146
+ return stream && topicNarrow ( stream . stream_id , parseTopicOperand ( hashSegments [ 3 ] ) ) ;
147
+ }
148
+
149
+ if ( hashSegments . length === 2 && hashSegments [ 0 ] === 'stream' ) {
150
+ const stream = parseStreamOperand ( hashSegments [ 1 ] , streamsById , streamsByName ) ;
151
+ return stream && streamNarrow ( stream . stream_id ) ;
152
+ }
153
+
154
+ if (
155
+ hashSegments . length === 2
156
+ && hashSegments [ 0 ] === 'is'
157
+ && / ^ ( p r i v a t e | s t a r r e d | m e n t i o n e d ) / i. test ( hashSegments [ 1 ] )
158
+ ) {
159
+ try {
160
+ return specialNarrow ( hashSegments [ 1 ] ) ;
161
+ } catch {
203
162
return null ;
163
+ }
204
164
}
165
+
166
+ return null ; // TODO(?) Give HOME_NARROW
205
167
} ;
206
168
207
169
/**
0 commit comments