@urugus/slack-cli 0.2.6 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/history.d.ts.map +1 -1
- package/dist/commands/history.js +7 -7
- package/dist/commands/history.js.map +1 -1
- package/dist/commands/send.d.ts.map +1 -1
- package/dist/commands/send.js +5 -18
- package/dist/commands/send.js.map +1 -1
- package/dist/utils/profile-config.d.ts.map +1 -1
- package/dist/utils/profile-config.js +2 -6
- package/dist/utils/profile-config.js.map +1 -1
- package/dist/utils/slack-operations/channel-operations.d.ts +9 -0
- package/dist/utils/slack-operations/channel-operations.d.ts.map +1 -1
- package/dist/utils/slack-operations/channel-operations.js +77 -50
- package/dist/utils/slack-operations/channel-operations.js.map +1 -1
- package/dist/utils/token-utils.d.ts +7 -0
- package/dist/utils/token-utils.d.ts.map +1 -0
- package/dist/utils/token-utils.js +18 -0
- package/dist/utils/token-utils.js.map +1 -0
- package/dist/utils/validators.d.ts +79 -0
- package/dist/utils/validators.d.ts.map +1 -0
- package/dist/utils/validators.js +175 -0
- package/dist/utils/validators.js.map +1 -0
- package/package.json +1 -1
- package/src/commands/history.ts +17 -12
- package/src/commands/send.ts +8 -19
- package/src/utils/profile-config.ts +3 -15
- package/src/utils/slack-operations/channel-operations.ts +91 -54
- package/src/utils/token-utils.ts +17 -0
- package/src/utils/validators.ts +212 -0
- package/tests/utils/option-parsers.test.ts +173 -0
- package/tests/utils/profile-config.test.ts +282 -0
- package/tests/utils/slack-operations/channel-operations-refactored.test.ts +179 -0
- package/tests/utils/token-utils.test.ts +33 -0
- package/tests/utils/validators.test.ts +307 -0
- package/src/utils/profile-config-refactored.ts +0 -161
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.optionValidators = exports.formatValidators = void 0;
|
|
4
|
+
exports.validateRequired = validateRequired;
|
|
5
|
+
exports.validateMutuallyExclusive = validateMutuallyExclusive;
|
|
6
|
+
exports.validateFormat = validateFormat;
|
|
7
|
+
exports.validateRange = validateRange;
|
|
8
|
+
exports.validateDateFormat = validateDateFormat;
|
|
9
|
+
exports.createValidationHook = createValidationHook;
|
|
10
|
+
exports.createOptionParser = createOptionParser;
|
|
11
|
+
const constants_1 = require("./constants");
|
|
12
|
+
/**
|
|
13
|
+
* Validates that a value exists (not undefined, null, or empty string)
|
|
14
|
+
*/
|
|
15
|
+
function validateRequired(value, fieldName) {
|
|
16
|
+
if (value === undefined || value === null || value === '') {
|
|
17
|
+
return `${fieldName} is required`;
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Validates mutually exclusive options
|
|
23
|
+
*/
|
|
24
|
+
function validateMutuallyExclusive(options, fields, errorMessage) {
|
|
25
|
+
const presentFields = fields.filter((field) => options[field] !== undefined);
|
|
26
|
+
if (presentFields.length > 1) {
|
|
27
|
+
return errorMessage || `Cannot use both ${presentFields.join(' and ')}`;
|
|
28
|
+
}
|
|
29
|
+
if (presentFields.length === 0) {
|
|
30
|
+
return errorMessage || `Must specify one of: ${fields.join(', ')}`;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Validates string format using regex
|
|
36
|
+
*/
|
|
37
|
+
function validateFormat(value, pattern, errorMessage) {
|
|
38
|
+
if (!pattern.test(value)) {
|
|
39
|
+
return errorMessage;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Validates numeric range
|
|
45
|
+
*/
|
|
46
|
+
function validateRange(value, min, max, fieldName = 'Value') {
|
|
47
|
+
if (min !== undefined && value < min) {
|
|
48
|
+
return `${fieldName} must be at least ${min}`;
|
|
49
|
+
}
|
|
50
|
+
if (max !== undefined && value > max) {
|
|
51
|
+
return `${fieldName} must be at most ${max}`;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Validates date format
|
|
57
|
+
*/
|
|
58
|
+
function validateDateFormat(dateString) {
|
|
59
|
+
const date = new Date(dateString);
|
|
60
|
+
if (isNaN(date.getTime())) {
|
|
61
|
+
return 'Invalid date format';
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Common format validators
|
|
67
|
+
*/
|
|
68
|
+
exports.formatValidators = {
|
|
69
|
+
/**
|
|
70
|
+
* Validates Slack thread timestamp format (1234567890.123456)
|
|
71
|
+
*/
|
|
72
|
+
threadTimestamp: (value) => {
|
|
73
|
+
const pattern = /^\d{10}\.\d{6}$/;
|
|
74
|
+
return validateFormat(value, pattern, constants_1.ERROR_MESSAGES.INVALID_THREAD_TIMESTAMP);
|
|
75
|
+
},
|
|
76
|
+
/**
|
|
77
|
+
* Validates Slack channel ID format (C1234567890, D1234567890, G1234567890)
|
|
78
|
+
*/
|
|
79
|
+
channelId: (value) => {
|
|
80
|
+
const pattern = /^[CDG][A-Z0-9]{10,}$/;
|
|
81
|
+
return validateFormat(value, pattern, 'Invalid channel ID format');
|
|
82
|
+
},
|
|
83
|
+
/**
|
|
84
|
+
* Validates output format options
|
|
85
|
+
*/
|
|
86
|
+
outputFormat: (value) => {
|
|
87
|
+
const validFormats = ['table', 'simple', 'json', 'compact'];
|
|
88
|
+
if (!validFormats.includes(value)) {
|
|
89
|
+
return `Invalid format. Must be one of: ${validFormats.join(', ')}`;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Creates a preAction hook for command validation
|
|
96
|
+
*/
|
|
97
|
+
function createValidationHook(validations) {
|
|
98
|
+
return (thisCommand) => {
|
|
99
|
+
const options = thisCommand.opts();
|
|
100
|
+
for (const validation of validations) {
|
|
101
|
+
const error = validation(options, thisCommand);
|
|
102
|
+
if (error) {
|
|
103
|
+
thisCommand.error(`Error: ${error}`);
|
|
104
|
+
break; // Stop processing after first error
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Common command option validators
|
|
111
|
+
*/
|
|
112
|
+
exports.optionValidators = {
|
|
113
|
+
/**
|
|
114
|
+
* Validates message/file options for send command
|
|
115
|
+
*/
|
|
116
|
+
messageOrFile: (options) => {
|
|
117
|
+
if (!options.message && !options.file) {
|
|
118
|
+
return constants_1.ERROR_MESSAGES.NO_MESSAGE_OR_FILE;
|
|
119
|
+
}
|
|
120
|
+
if (options.message && options.file) {
|
|
121
|
+
return constants_1.ERROR_MESSAGES.BOTH_MESSAGE_AND_FILE;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
},
|
|
125
|
+
/**
|
|
126
|
+
* Validates thread timestamp if provided
|
|
127
|
+
*/
|
|
128
|
+
threadTimestamp: (options) => {
|
|
129
|
+
if (options.thread) {
|
|
130
|
+
return exports.formatValidators.threadTimestamp(options.thread);
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
},
|
|
134
|
+
/**
|
|
135
|
+
* Validates message count for history command
|
|
136
|
+
*/
|
|
137
|
+
messageCount: (options) => {
|
|
138
|
+
if (options.number) {
|
|
139
|
+
const count = parseInt(options.number, 10);
|
|
140
|
+
if (isNaN(count)) {
|
|
141
|
+
return 'Message count must be a number';
|
|
142
|
+
}
|
|
143
|
+
return validateRange(count, 1, 1000, 'Message count');
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
},
|
|
147
|
+
/**
|
|
148
|
+
* Validates date format for history command
|
|
149
|
+
*/
|
|
150
|
+
sinceDate: (options) => {
|
|
151
|
+
if (options.since) {
|
|
152
|
+
const error = validateDateFormat(options.since);
|
|
153
|
+
if (error) {
|
|
154
|
+
return 'Invalid date format. Use YYYY-MM-DD HH:MM:SS';
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
/**
|
|
161
|
+
* Creates a validated option parser
|
|
162
|
+
*/
|
|
163
|
+
function createOptionParser(parser, validator) {
|
|
164
|
+
return (value, defaultValue) => {
|
|
165
|
+
const parsed = parser(value, defaultValue);
|
|
166
|
+
if (validator) {
|
|
167
|
+
const error = validator(parsed);
|
|
168
|
+
if (error) {
|
|
169
|
+
throw new Error(error);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return parsed;
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["../../src/utils/validators.ts"],"names":[],"mappings":";;;AAoBA,4CAKC;AAKD,8DAaC;AAKD,wCASC;AAKD,sCAaC;AAKD,gDAMC;AAqCD,oDAcC;AA4DD,gDAcC;AAlND,2CAA6C;AAgB7C;;GAEG;AACH,SAAgB,gBAAgB,CAAC,KAAc,EAAE,SAAiB;IAChE,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,EAAE,EAAE,CAAC;QAC1D,OAAO,GAAG,SAAS,cAAc,CAAC;IACpC,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;GAEG;AACH,SAAgB,yBAAyB,CACvC,OAAgC,EAChC,MAAgB,EAChB,YAAqB;IAErB,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,CAAC;IAC7E,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC7B,OAAO,YAAY,IAAI,mBAAmB,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;IAC1E,CAAC;IACD,IAAI,aAAa,CAAC,MAAM,KAAK,CAAC,EAAE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BACV,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
package/src/commands/history.ts
CHANGED
|
@@ -4,11 +4,9 @@ import { wrapCommand } from '../utils/command-wrapper';
|
|
|
4
4
|
import { createSlackClient } from '../utils/client-factory';
|
|
5
5
|
import { HistoryOptions } from '../types/commands';
|
|
6
6
|
import { API_LIMITS } from '../utils/constants';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
prepareSinceTimestamp,
|
|
11
|
-
} from './history-validators';
|
|
7
|
+
import { parseCount, parseProfile } from '../utils/option-parsers';
|
|
8
|
+
import { createValidationHook, optionValidators } from '../utils/validators';
|
|
9
|
+
import { prepareSinceTimestamp } from './history-validators';
|
|
12
10
|
import { displayHistoryResults } from './history-display';
|
|
13
11
|
|
|
14
12
|
export function setupHistoryCommand(): Command {
|
|
@@ -22,17 +20,24 @@ export function setupHistoryCommand(): Command {
|
|
|
22
20
|
)
|
|
23
21
|
.option('--since <date>', 'Get messages since specific date (YYYY-MM-DD HH:MM:SS)')
|
|
24
22
|
.option('--profile <profile>', 'Use specific workspace profile')
|
|
25
|
-
.hook(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
})
|
|
23
|
+
.hook(
|
|
24
|
+
'preAction',
|
|
25
|
+
createValidationHook([optionValidators.messageCount, optionValidators.sinceDate])
|
|
26
|
+
)
|
|
30
27
|
.action(
|
|
31
28
|
wrapCommand(async (options: HistoryOptions) => {
|
|
32
|
-
const
|
|
29
|
+
const profile = parseProfile(options.profile);
|
|
30
|
+
const client = await createSlackClient(profile);
|
|
31
|
+
|
|
32
|
+
const limit = parseCount(
|
|
33
|
+
options.number,
|
|
34
|
+
API_LIMITS.DEFAULT_MESSAGE_COUNT,
|
|
35
|
+
API_LIMITS.MIN_MESSAGE_COUNT,
|
|
36
|
+
API_LIMITS.MAX_MESSAGE_COUNT
|
|
37
|
+
);
|
|
33
38
|
|
|
34
39
|
const historyOptions: ApiHistoryOptions = {
|
|
35
|
-
limit
|
|
40
|
+
limit,
|
|
36
41
|
};
|
|
37
42
|
|
|
38
43
|
const oldest = prepareSinceTimestamp(options.since);
|
package/src/commands/send.ts
CHANGED
|
@@ -6,14 +6,10 @@ import { createSlackClient } from '../utils/client-factory';
|
|
|
6
6
|
import { FileError } from '../utils/errors';
|
|
7
7
|
import { SendOptions } from '../types/commands';
|
|
8
8
|
import { extractErrorMessage } from '../utils/error-utils';
|
|
9
|
+
import { parseProfile } from '../utils/option-parsers';
|
|
10
|
+
import { createValidationHook, optionValidators } from '../utils/validators';
|
|
9
11
|
import * as fs from 'fs/promises';
|
|
10
12
|
|
|
11
|
-
function isValidThreadTimestamp(timestamp: string): boolean {
|
|
12
|
-
// Slack timestamp format: 1234567890.123456
|
|
13
|
-
const timestampRegex = /^\d{10}\.\d{6}$/;
|
|
14
|
-
return timestampRegex.test(timestamp);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
13
|
export function setupSendCommand(): Command {
|
|
18
14
|
const sendCommand = new Command('send')
|
|
19
15
|
.description('Send a message to a Slack channel')
|
|
@@ -22,18 +18,10 @@ export function setupSendCommand(): Command {
|
|
|
22
18
|
.option('-f, --file <file>', 'File containing message content')
|
|
23
19
|
.option('-t, --thread <thread>', 'Thread timestamp to reply to')
|
|
24
20
|
.option('--profile <profile>', 'Use specific workspace profile')
|
|
25
|
-
.hook(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
if (options.message && options.file) {
|
|
31
|
-
thisCommand.error(`Error: ${ERROR_MESSAGES.BOTH_MESSAGE_AND_FILE}`);
|
|
32
|
-
}
|
|
33
|
-
if (options.thread && !isValidThreadTimestamp(options.thread)) {
|
|
34
|
-
thisCommand.error(`Error: ${ERROR_MESSAGES.INVALID_THREAD_TIMESTAMP}`);
|
|
35
|
-
}
|
|
36
|
-
})
|
|
21
|
+
.hook(
|
|
22
|
+
'preAction',
|
|
23
|
+
createValidationHook([optionValidators.messageOrFile, optionValidators.threadTimestamp])
|
|
24
|
+
)
|
|
37
25
|
.action(
|
|
38
26
|
wrapCommand(async (options: SendOptions) => {
|
|
39
27
|
// Get message content
|
|
@@ -51,7 +39,8 @@ export function setupSendCommand(): Command {
|
|
|
51
39
|
}
|
|
52
40
|
|
|
53
41
|
// Send message
|
|
54
|
-
const
|
|
42
|
+
const profile = parseProfile(options.profile);
|
|
43
|
+
const client = await createSlackClient(profile);
|
|
55
44
|
await client.sendMessage(options.channel, messageContent, options.thread);
|
|
56
45
|
|
|
57
46
|
console.log(chalk.green(`✓ ${SUCCESS_MESSAGES.MESSAGE_SENT(options.channel)}`));
|
|
@@ -2,13 +2,8 @@ import * as fs from 'fs/promises';
|
|
|
2
2
|
import * as path from 'path';
|
|
3
3
|
import * as os from 'os';
|
|
4
4
|
import type { Config, ConfigOptions, ConfigStore, Profile } from '../types/config';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
TOKEN_MIN_LENGTH,
|
|
8
|
-
DEFAULT_PROFILE_NAME,
|
|
9
|
-
ERROR_MESSAGES,
|
|
10
|
-
FILE_PERMISSIONS,
|
|
11
|
-
} from './constants';
|
|
5
|
+
import { DEFAULT_PROFILE_NAME, ERROR_MESSAGES, FILE_PERMISSIONS } from './constants';
|
|
6
|
+
import { maskToken } from './token-utils';
|
|
12
7
|
|
|
13
8
|
export class ProfileConfigManager {
|
|
14
9
|
private configPath: string;
|
|
@@ -98,14 +93,7 @@ export class ProfileConfigManager {
|
|
|
98
93
|
}
|
|
99
94
|
|
|
100
95
|
maskToken(token: string): string {
|
|
101
|
-
|
|
102
|
-
return '****';
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const prefix = token.substring(0, TOKEN_MASK_LENGTH);
|
|
106
|
-
const suffix = token.substring(token.length - TOKEN_MASK_LENGTH);
|
|
107
|
-
|
|
108
|
-
return `${prefix}-****-****-${suffix}`;
|
|
96
|
+
return maskToken(token);
|
|
109
97
|
}
|
|
110
98
|
|
|
111
99
|
private async getConfigStore(): Promise<ConfigStore> {
|
|
@@ -2,6 +2,7 @@ import { BaseSlackClient } from './base-client';
|
|
|
2
2
|
import { channelResolver } from '../channel-resolver';
|
|
3
3
|
import { DEFAULTS } from '../constants';
|
|
4
4
|
import { Channel, ListChannelsOptions } from '../slack-api-client';
|
|
5
|
+
import { WebClient } from '@slack/web-api';
|
|
5
6
|
|
|
6
7
|
interface ChannelWithUnreadInfo extends Channel {
|
|
7
8
|
unread_count: number;
|
|
@@ -10,6 +11,15 @@ interface ChannelWithUnreadInfo extends Channel {
|
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export class ChannelOperations extends BaseSlackClient {
|
|
14
|
+
constructor(tokenOrClient: string | WebClient) {
|
|
15
|
+
if (typeof tokenOrClient === 'string') {
|
|
16
|
+
super(tokenOrClient);
|
|
17
|
+
} else {
|
|
18
|
+
super('dummy-token'); // Call parent constructor
|
|
19
|
+
this.client = tokenOrClient; // Override the client for testing
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
13
23
|
async listChannels(options: ListChannelsOptions): Promise<Channel[]> {
|
|
14
24
|
const channels: Channel[] = [];
|
|
15
25
|
let cursor: string | undefined;
|
|
@@ -34,65 +44,15 @@ export class ChannelOperations extends BaseSlackClient {
|
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
async listUnreadChannels(): Promise<Channel[]> {
|
|
37
|
-
|
|
38
|
-
const response = await this.client.conversations.list({
|
|
39
|
-
types: 'public_channel,private_channel,im,mpim',
|
|
40
|
-
exclude_archived: true,
|
|
41
|
-
limit: 1000,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const channels = response.channels as Channel[];
|
|
47
|
+
const channels = await this.fetchAllChannels();
|
|
45
48
|
const channelsWithUnread: Channel[] = [];
|
|
46
49
|
|
|
47
50
|
// Process channels one by one with delay to avoid rate limits
|
|
48
51
|
for (const channel of channels) {
|
|
49
52
|
try {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
const channelInfo = info.channel as ChannelWithUnreadInfo;
|
|
55
|
-
|
|
56
|
-
// Get the latest message in the channel
|
|
57
|
-
const history = await this.client.conversations.history({
|
|
58
|
-
channel: channel.id,
|
|
59
|
-
limit: 1,
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// Always check for messages after last_read timestamp
|
|
63
|
-
if (channelInfo.last_read) {
|
|
64
|
-
// Fetch messages after last_read
|
|
65
|
-
const unreadHistory = await this.client.conversations.history({
|
|
66
|
-
channel: channel.id,
|
|
67
|
-
oldest: channelInfo.last_read,
|
|
68
|
-
limit: 100, // Get up to 100 unread messages
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const unreadCount = unreadHistory.messages?.length || 0;
|
|
72
|
-
if (unreadCount > 0) {
|
|
73
|
-
channelsWithUnread.push({
|
|
74
|
-
...channel,
|
|
75
|
-
unread_count: unreadCount,
|
|
76
|
-
unread_count_display: unreadCount,
|
|
77
|
-
last_read: channelInfo.last_read,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
} else if (history.messages && history.messages.length > 0) {
|
|
81
|
-
// If no last_read, all messages are unread
|
|
82
|
-
const allHistory = await this.client.conversations.history({
|
|
83
|
-
channel: channel.id,
|
|
84
|
-
limit: 100,
|
|
85
|
-
});
|
|
86
|
-
const unreadCount = allHistory.messages?.length || 0;
|
|
87
|
-
|
|
88
|
-
if (unreadCount > 0) {
|
|
89
|
-
channelsWithUnread.push({
|
|
90
|
-
...channel,
|
|
91
|
-
unread_count: unreadCount,
|
|
92
|
-
unread_count_display: unreadCount,
|
|
93
|
-
last_read: channelInfo.last_read,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
53
|
+
const unreadInfo = await this.getChannelUnreadInfo(channel);
|
|
54
|
+
if (unreadInfo) {
|
|
55
|
+
channelsWithUnread.push(unreadInfo);
|
|
96
56
|
}
|
|
97
57
|
|
|
98
58
|
// Add delay between API calls to avoid rate limiting
|
|
@@ -106,6 +66,83 @@ export class ChannelOperations extends BaseSlackClient {
|
|
|
106
66
|
return channelsWithUnread;
|
|
107
67
|
}
|
|
108
68
|
|
|
69
|
+
private async fetchAllChannels(): Promise<Channel[]> {
|
|
70
|
+
const response = await this.client.conversations.list({
|
|
71
|
+
types: 'public_channel,private_channel,im,mpim',
|
|
72
|
+
exclude_archived: true,
|
|
73
|
+
limit: 1000,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return response.channels as Channel[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private async getChannelUnreadInfo(channel: Channel): Promise<Channel | null> {
|
|
80
|
+
const channelInfo = await this.fetchChannelInfo(channel.id);
|
|
81
|
+
const unreadCount = await this.calculateUnreadCount(channel.id, channelInfo);
|
|
82
|
+
|
|
83
|
+
if (unreadCount > 0) {
|
|
84
|
+
return {
|
|
85
|
+
...channel,
|
|
86
|
+
unread_count: unreadCount,
|
|
87
|
+
unread_count_display: unreadCount,
|
|
88
|
+
last_read: channelInfo.last_read,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private async fetchChannelInfo(channelId: string): Promise<ChannelWithUnreadInfo> {
|
|
96
|
+
const info = await this.client.conversations.info({
|
|
97
|
+
channel: channelId,
|
|
98
|
+
include_num_members: false,
|
|
99
|
+
});
|
|
100
|
+
return info.channel as ChannelWithUnreadInfo;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private async calculateUnreadCount(
|
|
104
|
+
channelId: string,
|
|
105
|
+
channelInfo: ChannelWithUnreadInfo
|
|
106
|
+
): Promise<number> {
|
|
107
|
+
// Get the latest message to check if channel has any messages
|
|
108
|
+
const latestMessage = await this.fetchLatestMessage(channelId);
|
|
109
|
+
if (!latestMessage) {
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (channelInfo.last_read) {
|
|
114
|
+
return await this.fetchUnreadMessageCount(channelId, channelInfo.last_read);
|
|
115
|
+
} else {
|
|
116
|
+
// If no last_read, all messages are unread
|
|
117
|
+
return await this.fetchAllMessageCount(channelId);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private async fetchLatestMessage(channelId: string): Promise<any> {
|
|
122
|
+
const history = await this.client.conversations.history({
|
|
123
|
+
channel: channelId,
|
|
124
|
+
limit: 1,
|
|
125
|
+
});
|
|
126
|
+
return history.messages && history.messages.length > 0 ? history.messages[0] : null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private async fetchUnreadMessageCount(channelId: string, lastRead: string): Promise<number> {
|
|
130
|
+
const unreadHistory = await this.client.conversations.history({
|
|
131
|
+
channel: channelId,
|
|
132
|
+
oldest: lastRead,
|
|
133
|
+
limit: 100, // Get up to 100 unread messages
|
|
134
|
+
});
|
|
135
|
+
return unreadHistory.messages?.length || 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private async fetchAllMessageCount(channelId: string): Promise<number> {
|
|
139
|
+
const allHistory = await this.client.conversations.history({
|
|
140
|
+
channel: channelId,
|
|
141
|
+
limit: 100,
|
|
142
|
+
});
|
|
143
|
+
return allHistory.messages?.length || 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
109
146
|
async getChannelInfo(channelNameOrId: string): Promise<ChannelWithUnreadInfo> {
|
|
110
147
|
const channelId = await channelResolver.resolveChannelId(channelNameOrId, () =>
|
|
111
148
|
this.listChannels({
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TOKEN_MASK_LENGTH, TOKEN_MIN_LENGTH } from './constants';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Masks a token for display purposes, showing only first and last few characters
|
|
5
|
+
* @param token The token to mask
|
|
6
|
+
* @returns Masked token in format "xoxb-****-****-abcd"
|
|
7
|
+
*/
|
|
8
|
+
export function maskToken(token: string): string {
|
|
9
|
+
if (token.length <= TOKEN_MIN_LENGTH) {
|
|
10
|
+
return '****';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const prefix = token.substring(0, TOKEN_MASK_LENGTH);
|
|
14
|
+
const suffix = token.substring(token.length - TOKEN_MASK_LENGTH);
|
|
15
|
+
|
|
16
|
+
return `${prefix}-****-****-${suffix}`;
|
|
17
|
+
}
|