slack-rs 0.1.70

A Slack CLI tool with OAuth authentication, profile management, and API access
Documentation
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! OAuth scope preset definitions and expansion utilities
//!
//! This module provides preset scope definitions (like "all") and utilities
//! to expand them into concrete scope lists.

use std::collections::BTreeSet;

/// Returns the comprehensive bot scopes preset
///
/// This includes common bot scopes but excludes admin/Enterprise-only scopes.
/// The list is stable and sorted alphabetically.
///
/// Note: Some scopes are User-only and should not be included here:
/// - channels:write (User only)
/// - dnd:write (User only)
/// - users.profile:write (User only)
pub fn bot_all_scopes() -> Vec<String> {
    vec![
        "channels:history",
        "channels:join",
        "channels:manage",
        "channels:read",
        "channels:write.invites",
        "channels:write.topic",
        "chat:write",
        "chat:write.customize",
        "chat:write.public",
        "commands",
        "conversations.connect:manage",
        "conversations.connect:read",
        "conversations.connect:write",
        "dnd:read",
        "emoji:read",
        "files:read",
        "files:write",
        "groups:history",
        "groups:read",
        "groups:write",
        "groups:write.invites",
        "groups:write.topic",
        "im:history",
        "im:read",
        "im:write",
        "im:write.topic",
        "links:read",
        "links:write",
        "mpim:history",
        "mpim:read",
        "mpim:write",
        "mpim:write.topic",
        "pins:read",
        "pins:write",
        "reactions:read",
        "reactions:write",
        "reminders:read",
        "reminders:write",
        "team:read",
        "usergroups:read",
        "usergroups:write",
        "users.profile:read",
        "users:read",
        "users:read.email",
        "users:write",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect()
}

/// Returns the comprehensive user scopes preset
///
/// This includes common user scopes but excludes admin/Enterprise-only scopes.
/// The list is stable and sorted alphabetically.
///
/// Note: Some scopes are User-only and cannot be used as bot scopes:
/// - channels:write
/// - dnd:write
/// - users.profile:write
pub fn user_all_scopes() -> Vec<String> {
    vec![
        "channels:history",
        "channels:read",
        "channels:write",
        "chat:write",
        "dnd:read",
        "dnd:write",
        "emoji:read",
        "files:read",
        "files:write",
        "groups:history",
        "groups:read",
        "groups:write",
        "identify",
        "im:history",
        "im:read",
        "im:write",
        "mpim:history",
        "mpim:read",
        "mpim:write",
        "pins:read",
        "pins:write",
        "reactions:read",
        "reactions:write",
        "reminders:read",
        "reminders:write",
        "search:read",
        "stars:read",
        "stars:write",
        "team:read",
        "usergroups:read",
        "usergroups:write",
        "users.profile:read",
        "users.profile:write",
        "users:read",
        "users:read.email",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect()
}

/// Returns the comprehensive "all" scope preset (legacy, defaults to bot scopes)
///
/// This includes common scopes for bot/user tokens but excludes admin/Enterprise-only scopes.
/// The list is stable and sorted alphabetically.
/// For new code, prefer bot_all_scopes() or user_all_scopes() explicitly.
pub fn all_scopes() -> Vec<String> {
    bot_all_scopes()
}

/// Returns bot-specific scopes
///
/// This includes scopes typically used for bot tokens.
#[allow(dead_code)]
pub fn bot_scopes() -> Vec<String> {
    vec![
        "channels:history",
        "channels:read",
        "channels:write",
        "chat:write",
        "conversations.connect:read",
        "conversations.connect:write",
        "emoji:read",
        "files:read",
        "files:write",
        "groups:history",
        "groups:read",
        "groups:write",
        "im:history",
        "im:read",
        "im:write",
        "mpim:history",
        "mpim:read",
        "mpim:write",
        "pins:read",
        "pins:write",
        "reactions:read",
        "reactions:write",
        "reminders:read",
        "reminders:write",
        "usergroups:read",
        "usergroups:write",
        "users:read",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect()
}

/// Returns user-specific scopes
///
/// This includes scopes typically used for user tokens.
#[allow(dead_code)]
pub fn user_scopes() -> Vec<String> {
    vec![
        "channels:history",
        "channels:read",
        "channels:write",
        "chat:write",
        "dnd:read",
        "dnd:write",
        "files:read",
        "files:write",
        "groups:history",
        "groups:read",
        "groups:write",
        "im:history",
        "im:read",
        "im:write",
        "mpim:history",
        "mpim:read",
        "mpim:write",
        "pins:read",
        "pins:write",
        "reactions:read",
        "reactions:write",
        "reminders:read",
        "reminders:write",
        "search:read",
        "stars:read",
        "stars:write",
        "team:read",
        "users.profile:read",
        "users.profile:write",
        "users:read",
    ]
    .iter()
    .map(|s| s.to_string())
    .collect()
}

/// Expands preset names (like "all", "bot:all", "user:all") in a scope list and removes duplicates
///
/// # Arguments
/// * `input_scopes` - List of scopes which may include preset names (case-insensitive)
///
/// # Returns
/// A deduplicated, sorted list of concrete scopes with presets expanded
///
/// # Presets
/// - "all": Expands to bot_all_scopes() (legacy behavior, for backward compatibility)
/// - "bot:all": Expands to bot_all_scopes()
/// - "user:all": Expands to user_all_scopes()
///
/// # Example
/// ```
/// use slack_rs::oauth::scopes::expand_scopes;
///
/// let result = expand_scopes(&["bot:all".to_string(), "custom:scope".to_string()]);
/// assert!(result.contains(&"chat:write".to_string()));
/// assert!(result.contains(&"custom:scope".to_string()));
/// ```
pub fn expand_scopes(input_scopes: &[String]) -> Vec<String> {
    let mut expanded = BTreeSet::new();

    for scope in input_scopes {
        let normalized = scope.trim().to_lowercase();

        match normalized.as_str() {
            "all" => {
                // Legacy: expand to bot scopes for backward compatibility
                for preset_scope in all_scopes() {
                    expanded.insert(preset_scope);
                }
            }
            "bot:all" => {
                // Explicit bot scopes preset
                for preset_scope in bot_all_scopes() {
                    expanded.insert(preset_scope);
                }
            }
            "user:all" => {
                // Explicit user scopes preset
                for preset_scope in user_all_scopes() {
                    expanded.insert(preset_scope);
                }
            }
            _ => {
                // Keep individual scopes as-is (preserving original case)
                expanded.insert(scope.trim().to_string());
            }
        }
    }

    expanded.into_iter().collect()
}

/// Expands scopes with context-aware "all" preset
///
/// # Arguments
/// * `input_scopes` - List of scopes which may include preset names
/// * `is_bot_context` - true for bot scope context, false for user scope context
///
/// # Returns
/// A deduplicated, sorted list of concrete scopes with presets expanded
///
/// # Context-aware behavior
/// - In bot context, "all" expands to bot_all_scopes()
/// - In user context, "all" expands to user_all_scopes()
/// - "bot:all" and "user:all" always expand to their respective presets regardless of context
pub fn expand_scopes_with_context(input_scopes: &[String], is_bot_context: bool) -> Vec<String> {
    let mut expanded = BTreeSet::new();

    for scope in input_scopes {
        let normalized = scope.trim().to_lowercase();

        match normalized.as_str() {
            "all" => {
                // Context-aware expansion
                let preset = if is_bot_context {
                    bot_all_scopes()
                } else {
                    user_all_scopes()
                };
                for preset_scope in preset {
                    expanded.insert(preset_scope);
                }
            }
            "bot:all" => {
                for preset_scope in bot_all_scopes() {
                    expanded.insert(preset_scope);
                }
            }
            "user:all" => {
                for preset_scope in user_all_scopes() {
                    expanded.insert(preset_scope);
                }
            }
            _ => {
                expanded.insert(scope.trim().to_string());
            }
        }
    }

    expanded.into_iter().collect()
}

/// Expands preset names for bot scopes with context awareness
///
/// # Arguments
/// * `input_scopes` - List of scopes which may include preset names like "all", "bot:all"
///
/// # Returns
/// A deduplicated, sorted list of concrete scopes with presets expanded
///
/// "all" is interpreted as "bot:all" in bot context
#[allow(dead_code)]
pub fn expand_bot_scopes(input_scopes: &[String]) -> Vec<String> {
    let mut expanded = BTreeSet::new();

    for scope in input_scopes {
        let normalized = scope.trim().to_lowercase();

        if normalized == "all" || normalized == "bot:all" {
            // Expand the bot preset
            for preset_scope in bot_scopes() {
                expanded.insert(preset_scope);
            }
        } else if normalized == "user:all" {
            // User scopes should not be in bot scopes, but handle gracefully
            for preset_scope in user_scopes() {
                expanded.insert(preset_scope);
            }
        } else {
            // Keep individual scopes as-is (preserving original case)
            expanded.insert(scope.trim().to_string());
        }
    }

    expanded.into_iter().collect()
}

/// Expands preset names for user scopes with context awareness
///
/// # Arguments
/// * `input_scopes` - List of scopes which may include preset names like "all", "user:all"
///
/// # Returns
/// A deduplicated, sorted list of concrete scopes with presets expanded
///
/// "all" is interpreted as "user:all" in user context
#[allow(dead_code)]
pub fn expand_user_scopes(input_scopes: &[String]) -> Vec<String> {
    let mut expanded = BTreeSet::new();

    for scope in input_scopes {
        let normalized = scope.trim().to_lowercase();

        if normalized == "all" || normalized == "user:all" {
            // Expand the user preset
            for preset_scope in user_scopes() {
                expanded.insert(preset_scope);
            }
        } else if normalized == "bot:all" {
            // Bot scopes should not be in user scopes, but handle gracefully
            for preset_scope in bot_scopes() {
                expanded.insert(preset_scope);
            }
        } else {
            // Keep individual scopes as-is (preserving original case)
            expanded.insert(scope.trim().to_string());
        }
    }

    expanded.into_iter().collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_all_scopes_returns_stable_list() {
        let scopes1 = all_scopes();
        let scopes2 = all_scopes();
        assert_eq!(scopes1, scopes2, "all_scopes() should return a stable list");
        assert!(!scopes1.is_empty(), "all_scopes() should not be empty");
    }

    #[test]
    fn test_all_scopes_is_sorted() {
        let scopes = all_scopes();
        let mut sorted = scopes.clone();
        sorted.sort();
        assert_eq!(
            scopes, sorted,
            "all_scopes() should be alphabetically sorted"
        );
    }

    #[test]
    fn test_all_scopes_contains_expected_scopes() {
        let scopes = all_scopes();
        assert!(scopes.contains(&"chat:write".to_string()));
        assert!(scopes.contains(&"users:read".to_string()));
        assert!(scopes.contains(&"channels:read".to_string()));
    }

    #[test]
    fn test_expand_scopes_with_all_only() {
        let input = vec!["all".to_string()];
        let result = expand_scopes(&input);

        assert!(result.contains(&"chat:write".to_string()));
        assert!(result.contains(&"users:read".to_string()));
        assert!(result.len() > 10, "all should expand to many scopes");
    }

    #[test]
    fn test_expand_scopes_case_insensitive() {
        let input1 = vec!["all".to_string()];
        let input2 = vec!["ALL".to_string()];
        let input3 = vec!["AlL".to_string()];

        let result1 = expand_scopes(&input1);
        let result2 = expand_scopes(&input2);
        let result3 = expand_scopes(&input3);

        assert_eq!(result1, result2);
        assert_eq!(result2, result3);
    }

    #[test]
    fn test_expand_scopes_mixed_with_duplicates() {
        let input = vec![
            "all".to_string(),
            "chat:write".to_string(), // duplicate with "all"
            "custom:scope".to_string(),
        ];
        let result = expand_scopes(&input);

        // Should contain all scopes from "all" plus custom:scope
        assert!(result.contains(&"chat:write".to_string()));
        assert!(result.contains(&"users:read".to_string()));
        assert!(result.contains(&"custom:scope".to_string()));

        // Should deduplicate - chat:write appears only once
        assert_eq!(
            result.iter().filter(|s| *s == "chat:write").count(),
            1,
            "chat:write should appear only once"
        );
    }

    #[test]
    fn test_expand_scopes_no_presets() {
        let input = vec!["chat:write".to_string(), "users:read".to_string()];
        let result = expand_scopes(&input);

        assert_eq!(result.len(), 2);
        assert!(result.contains(&"chat:write".to_string()));
        assert!(result.contains(&"users:read".to_string()));
    }

    #[test]
    fn test_expand_scopes_empty() {
        let input: Vec<String> = vec![];
        let result = expand_scopes(&input);

        assert!(result.is_empty());
    }

    #[test]
    fn test_expand_scopes_preserves_case_for_non_presets() {
        let input = vec!["Custom:Scope".to_string()];
        let result = expand_scopes(&input);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0], "Custom:Scope");
    }

    #[test]
    fn test_expand_scopes_trims_whitespace() {
        let input = vec![" chat:write ".to_string(), "  all  ".to_string()];
        let result = expand_scopes(&input);

        assert!(result.contains(&"chat:write".to_string()));
        assert!(result.contains(&"users:read".to_string())); // from "all"
    }

    #[test]
    fn test_expand_scopes_with_bot_all_preset() {
        let input = vec!["bot:all".to_string()];
        let result = expand_scopes(&input);

        assert!(result.contains(&"chat:write".to_string()));
        assert!(result.contains(&"channels:read".to_string()));
        assert!(!result.contains(&"search:read".to_string())); // user-only scope
    }

    #[test]
    fn test_expand_scopes_with_user_all_preset() {
        let input = vec!["user:all".to_string()];
        let result = expand_scopes(&input);

        assert!(result.contains(&"search:read".to_string()));
        assert!(result.contains(&"users:read".to_string()));
        assert!(result.contains(&"chat:write".to_string()));
    }

    #[test]
    fn test_expand_scopes_with_context_bot() {
        let input = vec!["all".to_string()];
        let result = expand_scopes_with_context(&input, true);

        // In bot context, "all" expands to bot scopes
        assert!(result.contains(&"chat:write".to_string()));
        assert!(!result.contains(&"search:read".to_string())); // user-only
    }

    #[test]
    fn test_expand_scopes_with_context_user() {
        let input = vec!["all".to_string()];
        let result = expand_scopes_with_context(&input, false);

        // In user context, "all" expands to user scopes
        assert!(result.contains(&"search:read".to_string()));
        assert!(result.contains(&"users:read".to_string()));
    }

    #[test]
    fn test_bot_all_scopes_no_user_only_scopes() {
        let scopes = bot_all_scopes();
        assert!(!scopes.contains(&"search:read".to_string()));
        assert!(!scopes.contains(&"stars:read".to_string()));
    }

    #[test]
    fn test_user_all_scopes_includes_user_scopes() {
        let scopes = user_all_scopes();
        assert!(scopes.contains(&"search:read".to_string()));
        assert!(scopes.contains(&"stars:read".to_string()));
        assert!(scopes.contains(&"users:read".to_string()));
    }
}