-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.js
141 lines (131 loc) · 4 KB
/
status.js
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
const { getCreds, appSetup } = require( './utils' );
const chrono = require( 'chrono-node' );
const colors = require( 'colors' );
const { getUserConfig } = require( './manageConfig' );
async function setRefiners() {
const userConfig = await getUserConfig();
// Retrieve the current day of the week
const currentDOW = new Date().getDay();
// Customize chrono refiner to adjust results to our needs
const custom = chrono.casual.clone();
custom.refiners.push( {
refine: ( context, results ) => {
results.forEach( ( result ) => {
// If there's no explicitly stated time (by checking for an hour) and the day of the week matches today,
// Assume the user wants next week, and not later today
if (
! result.start.isCertain( 'hour' ) &&
result.start.date().getDay() === currentDOW
) {
result.start.assign( 'day', result.start.get( 'day' ) + 7 );
}
// If no explicit time was stated, default to 9AM (this aligns with Slack's existing defaults)
if ( ! result.start.isCertain( 'hour' ) ) {
result.start.assign(
'hour',
userConfig.defaultActionTime.hour
);
result.start.assign(
'minute',
userConfig.defaultActionTime.minute
);
}
} );
return results;
},
} );
return custom;
}
async function parseExpiration( expiration ) {
const custom = await setRefiners();
// Get current timestamp for reference
const now = Date.now();
// Parse input date/time
const parseDate = new Date(
custom.parseDate( expiration, now, { forwardDate: true } )
);
// Convert parsed date/time to a timestamp
let expireTimestamp = Math.floor( parseDate.getTime() / 1000 );
// Confirm timestamp returned isn't in the past. If it is, add 24 hours
// This is needed because of how chronos ignores the reference date when parsing an explicit time
if ( expireTimestamp < now / 1000 ) {
expireTimestamp += 60 * 60 * 24;
}
return expireTimestamp;
}
// Update user status using emoji and status text
// @param args Object workspace String (optional) default = ''
// emoji String (optional)
// text String (optinoal)
// expiration String (optional) default = null
async function setStatus( args ) {
let defaults = { workspace: '', expiration: null };
args = { ...defaults, ...args };
// Replace an empty expiration string with null
args.expiration = args.expiration === '' ? null : args.expiration;
// If expiration is null, and text parses as a date string,
// use 'text' as the expiration and pass an empty 'text' string instead.
if (
args.expiration === null &&
chrono.parseDate( args.text, Date.now(), { forwardDate: true } ) !==
null
) {
args.expiration = args.text;
args.text = '';
}
const creds = await getCreds( args.workspace );
const app = await appSetup( creds );
try {
await app.client.users.profile.set( {
token: creds.token,
profile: {
status_text: args.text,
status_emoji: args.emoji,
status_expiration:
args.expiration === null
? 0
: await parseExpiration( args.expiration ),
},
} );
} catch ( error ) {
console.error(
'😢 Slack status was not able to be updated:'.red,
`"${ error.data.error }"\n`.grey
);
}
}
async function clearStatus( workspace ) {
const creds = await getCreds( workspace );
const app = await appSetup( creds );
await app.client.users.profile.set( {
token: creds.token,
profile: {
status_text: '',
status_emoji: '',
status_expiration: '',
},
} );
}
async function setPresence( workspace, presence ) {
if ( arguments.length === 1 ) {
presence = workspace;
workspace = '';
}
if ( presence === 'active' ) {
presence = 'auto';
}
const creds = await getCreds( workspace );
const app = await appSetup( creds );
try {
await app.client.users.setPresence( {
token: creds.token,
presence: presence,
} );
} catch ( error ) {
console.error(
'😢 Slack presence was not able to be updated:'.red,
`"${ error.data.error }"\n`.grey
);
}
}
module.exports = { setStatus, setPresence, parseExpiration, clearStatus };