Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
231 lines (187 loc) · 6.25 KB
/
Copy pathindex.js
File metadata and controls
231 lines (187 loc) · 6.25 KB
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
#! /usr/bin/env node
// micli-send: note, renote, quote, and reaction
// Geminiさんにfixをお願いしました maybe it works :p
import { parseArgs } from 'node:util';
import { readFileSync } from 'node:fs';
import { homedir } from 'node:os';
const version = '0.1.0';
const path2env = '~/.config/micli/env.json'
const usage = `
micli Misskey CLI Client: send a note / reaction command v${version}
Usage: $ micli-send --name <profile_name> [options]
--env <path_to_file>: Specify a path to the environment file
(default = ~/.config/micli/env.json)
--name <profile_name>: (Required) Specify the account profile name to use
## Reaction
-s | --shoot <URL>: Send a Reaction to the note (with "-e") specified by the <URL>
-e <:custom_emoji_name:>: Specify the emoji to reaction
## Note
-m <"message">: Use the <message> as main text of note, reply or quote
-b | --renote <URL>: Renote (share) the note specified by the <URL> (with '-m' option to quote)
-r | --reply <URL>: Reply to the specified note
-v <visibility>: Specify the visibility range (default = public)
* public: Will appear in all timelines
* home: Will limited to the Home TL
* follower: Only visible to your follower, and disable renoting (shareing)
-t <user>: Specify the user to send, and limit the visibility to only this user(s)
--localOnly: In this noting (posting), turn off the federation to the ActivityPub Server
-w | --contentsWarning <explain>: Add a CW hiding with explain
## Other
-d | --delete <URL>: Delete the specified note or reaction
-h | --help: Show this help
`;
const options = {
name: { type: 'string' },
env: { type: 'string' },
help: { type: 'boolean', short: 'h' },
delete: { type: 'string', short: 'd' },
shoot: { type: 'string', short: 's' },
emote: { type: 'string', short: 'e' },
message: { type: 'string', short: 'm' },
renote: { type: 'string', short: 'b' },
reply: { type: 'string', short: 'r' },
visibility: { type: 'string', short: 'v' },
to: { type: 'string', short: 't', multiple: true },
localOnly: { type: 'boolean' },
contentsWarning: { type: 'string', short: 'w' }
};
const expandPath = path => path.replace(/^~(?=$|\/|\\)/, homedir());
const file2obj = (path) => {
let result;
try {
const txt = readFileSync(expandPath(path));
result = JSON.parse(txt);
} catch (err) {
console.error(`Error: Could not read or parse ${path}`);
console.error(err.message);
process.exit(1);
}
return result;
};
const post2mi = async (prof, endpoint, data = {}) => {
const url = `https://${prof.server}/api/${endpoint}`;
let response;
try {
response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
...data,
i: prof.token,
}),
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`API Error (${response.status}): ${errorBody}`);
}
} catch (err) {
console.error('Failed to request to Misskey server: ', endpoint);
console.error(err.message);
process.exit(1);
}
if (response.status === 204) return {};
const text = await response.text();
return text ? JSON.parse(text) : {};
};
const getNoteId = async (prof, uri) => {
const res = await post2mi(prof, 'ap/show', { uri });
return res.object.id;
};
const isContained = args => args.some(v => v != null);
const isOwnNote = async (prof, noteId) => {
const note = await post2mi(prof, 'notes/show', { noteId });
const me = await post2mi(prof, 'i');
return note.user.id === me.id;
}
const main = async () => {
const { values: args } = parseArgs({ options });
if (args.help || !args.name) {
console.log(usage);
process.exit(0);
}
const env = file2obj(args.env || path2env);
const prof = env.profiles.find(p => p.name === args.name);
if (!prof) {
console.error(`Error: Profile "${args.name}" not found in ${args.env || path2env}`);
process.exit(1);
}
const visibility = (args.to != null && args.to.length > 0) ? 'specified'
: args.visibility || 'public';
let noteId;
if (isContained([ args.shoot, args.renote, args.reply, args.delete ])) {
noteId = await getNoteId(prof, args.shoot || args.renote || args.reply || args.delete);
}
if (args.shoot != null) {
if (isContained([ args.message, args.renote, args.reply ])) {
console.error('Error: Cannot reaction and note at the same time!');
process.exit(1);
}
await post2mi(prof, 'notes/reactions/create', {
noteId: noteId,
reaction: args.emote
});
} else if (args.reply != null) {
if (args.message == null) {
console.error('Error: Contents is empty');
process.exit(1);
}
// reply
await post2mi(prof, 'notes/create', {
visibility: visibility,
visibleUserIds: args.to || [],
cw: args.contentsWarning || null,
localOnly: args.localOnly || false,
replyId: noteId,
text: args.message,
});
} else if (args.renote != null) {
if (args.message != null) {
// quote
await post2mi(prof, 'notes/create', {
visibility: visibility,
visibleUserIds: args.to || [],
cw: args.contentsWarning || null,
localOnly: args.localOnly || false,
renoteId: noteId,
text: args.message,
});
} else {
// rn
await post2mi(prof, 'notes/create', {
visibility: visibility,
visibleUserIds: args.to || [],
localOnly: args.localOnly || false,
renoteId: noteId
});
}
} else if (args.delete != null) {
if (!(await isOwnNote(prof, noteId))) {
await post2mi(prof, 'notes/reactions/delete', {
noteId: noteId
});
} else {
await post2mi(prof, 'notes/delete', {
noteId: noteId
});
}
} else {
if (args.message == null) {
console.error('Error: Contents is empty');
process.exit(1);
}
// plain-vanilla note
await post2mi(prof, 'notes/create', {
visibility: visibility,
visibleUserIds: args.to || [],
cw: args.contentsWarning || null,
localOnly: args.localOnly || false,
text: args.message,
});
}
};
main().catch(err => {
console.error(err);
process.exit(1);
});