Repository navigation
Expand file tree
/
Copy pathhandler_drafts.py
More file actions
454 lines (412 loc) · 16.5 KB
/
Copy pathhandler_drafts.py
File metadata and controls
454 lines (412 loc) · 16.5 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
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
# ruff: noqa: F403, F405
from typing import Any, Optional
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import ContextTypes, ConversationHandler
from constants import *
from handler_ui import (
build_drafts_message,
cancel_keyboard,
drafting_keyboard,
publish_choice_keyboard,
render_create_preview,
render_create_preview_plain,
)
from handler_state import (
_active_draft_data,
_clear_active_draft,
_drafts_map,
_has_active_draft,
_save_draft_snapshot,
_set_active_draft,
gate_callback,
get_draft_parts,
set_draft_parts,
)
from handler_utils import (
ensure_allowed,
ensure_api_key_or_prompt,
ensure_private,
extract_message_text,
get_preview_format,
schedule_users_data_save,
stale_action_text,
)
from storage import users_data
async def post(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return ConversationHandler.END
if not await ensure_private(update, context):
return ConversationHandler.END
user_id = await ensure_api_key_or_prompt(update, context)
if not user_id:
return ConversationHandler.END
# Resume last draft if exists
u = users_data[user_id]
set_draft_parts(context, list(u.draft_parts) if u.draft_parts else [])
context.user_data[K_UNDO_STACK] = list(u.undo_stack) if u.undo_stack else []
if u.draft_title:
context.user_data[K_TITLE] = u.draft_title
await update.message.reply_text(
MESSAGES["RESUME_DRAFT"].format(title=u.draft_title),
reply_markup=drafting_keyboard(),
)
return ENTER_BODY
await update.message.reply_text(MESSAGES["ENTER_TITLE_PROMPT"], reply_markup=cancel_keyboard())
return ENTER_TITLE
async def new_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
# Fast path: /new Title | Body
if not await ensure_allowed(update, context):
return ConversationHandler.END
if not await ensure_private(update, context):
return ConversationHandler.END
user_id = await ensure_api_key_or_prompt(update, context)
if not user_id:
return ConversationHandler.END
text = update.message.text or ""
parts = text.split(" ", 1)
arg = parts[1] if len(parts) > 1 else ""
title = ""
body = ""
if "|" in arg:
t, b = arg.split("|", 1)
title = t.strip()
body = b.strip()
else:
title = arg.strip()
if title:
context.user_data[K_TITLE] = title
users_data[user_id].draft_title = title
await schedule_users_data_save(context)
if body:
# Direct to publish choice
set_draft_parts(context, [body])
context.user_data[K_UNDO_STACK] = [body]
context.user_data[K_BODY] = body
users_data[user_id].draft_parts = [body]
users_data[user_id].undo_stack = [body]
await schedule_users_data_save(context)
default_mode = users_data[user_id].settings.get("default_publish_mode", "draft")
await update.message.reply_text(
MESSAGES["CHOOSE_PUBLICATION_OPTION"],
reply_markup=publish_choice_keyboard(default_mode=default_mode),
)
return ENTER_PUBLISH_CHOICE
else:
await update.message.reply_text(
MESSAGES["NOW_SEND_BODY"],
reply_markup=drafting_keyboard(),
)
return ENTER_BODY
else:
# Fallback to normal /post flow
return await post(update, context)
async def enter_title(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return ENTER_TITLE
title = extract_message_text(update.message).strip()
if not title:
await update.message.reply_text(MESSAGES["PROMPT_VALID_TITLE"], reply_markup=cancel_keyboard())
return ENTER_TITLE
context.user_data[K_TITLE] = title
# Persist as draft title for resume
u = users_data[update.message.from_user.id]
u.draft_title = title
await schedule_users_data_save(context)
await update.message.reply_text(
MESSAGES["SEND_BODY_MULTIMSG"],
reply_markup=drafting_keyboard(),
)
return ENTER_BODY
async def enter_body(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return ENTER_BODY
text = extract_message_text(update.message)
if not text.strip():
await update.message.reply_text(MESSAGES["PROMPT_VALID_CONTENT"], reply_markup=drafting_keyboard())
return ENTER_BODY
parts = get_draft_parts(context)
parts.append(text)
set_draft_parts(context, parts)
# Maintain undo stack
undo_stack = context.user_data.get(K_UNDO_STACK, [])
undo_stack.append(text)
context.user_data[K_UNDO_STACK] = undo_stack
# Persist draft to users_data for resume
u = users_data[update.message.from_user.id]
u.draft_parts = list(parts)
u.undo_stack = list(undo_stack)
await schedule_users_data_save(context)
await update.message.reply_text(MESSAGES["ADDED_TO_DRAFT"], reply_markup=drafting_keyboard())
return ENTER_BODY
async def draft_preview(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return ENTER_BODY
parts = get_draft_parts(context)
title = context.user_data.get(K_TITLE, "(no title)")
body = "\n".join(parts)
user_id = update.message.from_user.id
fmt = get_preview_format(user_id)
preview = (
render_create_preview_plain(title, body, None)
if fmt == "plain"
else render_create_preview(title, body, None)
)
parse_mode = "MarkdownV2" if fmt == "markdown" else None
await update.message.reply_text(
preview, parse_mode=parse_mode, reply_markup=drafting_keyboard()
)
return ENTER_BODY
async def draft_preview_cb(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return ENTER_BODY
if not await ensure_private(update, context):
return ENTER_BODY
if not await gate_callback(query, context):
return ENTER_BODY
parts = get_draft_parts(context)
title = context.user_data.get(K_TITLE, "(no title)")
body = "\n".join(parts)
user_id = query.from_user.id
fmt = get_preview_format(user_id)
preview = (
render_create_preview_plain(title, body, None)
if fmt == "plain"
else render_create_preview(title, body, None)
)
parse_mode = "MarkdownV2" if fmt == "markdown" else None
await query.edit_message_text(preview, parse_mode=parse_mode, reply_markup=drafting_keyboard())
return ENTER_BODY
async def draft_clear(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return ENTER_BODY
_clear_active_draft(update.message.from_user.id, context)
await schedule_users_data_save(context)
await update.message.reply_text(
f"{MESSAGES['DRAFT_CLEARED']}\n\n{MESSAGES['ENTER_TITLE_PROMPT']}",
reply_markup=cancel_keyboard(),
)
return ENTER_TITLE
async def draft_clear_cb(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return ENTER_BODY
if not await ensure_private(update, context):
return ENTER_BODY
if not await gate_callback(query, context):
return ENTER_BODY
_clear_active_draft(query.from_user.id, context)
await schedule_users_data_save(context)
await query.edit_message_text(
f"{MESSAGES['DRAFT_CLEARED']}\n\n{MESSAGES['ENTER_TITLE_PROMPT']}",
reply_markup=cancel_keyboard(),
)
return ENTER_TITLE
async def draft_undo_cb(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return ENTER_BODY
if not await ensure_private(update, context):
return ENTER_BODY
if not await gate_callback(query, context):
return ENTER_BODY
undo_stack = context.user_data.get(K_UNDO_STACK, [])
if undo_stack:
last = undo_stack.pop()
parts = get_draft_parts(context)
if parts and parts[-1] == last:
parts.pop()
context.user_data[K_UNDO_STACK] = undo_stack
set_draft_parts(context, parts)
u = users_data[query.from_user.id]
u.draft_parts = list(parts)
u.undo_stack = list(undo_stack)
await schedule_users_data_save(context)
await query.edit_message_text(MESSAGES["REMOVED_LAST_CHUNK"], reply_markup=drafting_keyboard())
else:
await query.edit_message_text(MESSAGES["NOTHING_TO_UNDO"], reply_markup=drafting_keyboard())
return ENTER_BODY
async def _save_current_draft(user_id: int, context: ContextTypes.DEFAULT_TYPE) -> Optional[str]:
title, parts = _active_draft_data(user_id, context)
draft_id = _save_draft_snapshot(user_id, title, parts)
if not draft_id:
return None
await schedule_users_data_save(context)
return _drafts_map(user_id).get(draft_id, {}).get("title") or _draft_title_for_save(title, parts)
async def draft_save_cb(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return ENTER_BODY
if not await ensure_private(update, context):
return ENTER_BODY
if not await gate_callback(query, context):
return ENTER_BODY
user_id = query.from_user.id
title = await _save_current_draft(user_id, context)
if not title:
await query.edit_message_text(MESSAGES["DRAFT_EMPTY"], reply_markup=drafting_keyboard())
return ENTER_BODY
await query.edit_message_text(
MESSAGES["DRAFT_SAVED_AS"].format(title=title), reply_markup=drafting_keyboard()
)
return ENTER_BODY
async def save_draft_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return
if not await ensure_private(update, context):
return
user_id = await ensure_api_key_or_prompt(update, context)
if not user_id:
return
title = await _save_current_draft(user_id, context)
if not title:
await update.message.reply_text(MESSAGES["DRAFT_EMPTY"])
return
await update.message.reply_text(MESSAGES["DRAFT_SAVED_AS"].format(title=title))
async def drafts_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return
if not await ensure_private(update, context):
return
user_id = await ensure_api_key_or_prompt(update, context)
if not user_id:
return
text, markup = build_drafts_message(user_id)
await update.message.reply_text(text, reply_markup=markup)
async def _render_drafts_list(query: Any, user_id: int) -> None:
text, markup = build_drafts_message(user_id)
try:
await query.edit_message_text(text, reply_markup=markup)
except Exception:
pass
async def drafts_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return
if not await ensure_private(update, context):
return
if not await gate_callback(query, context):
return
user_id = await ensure_api_key_or_prompt(update, context)
if not user_id:
return
data = query.data or ""
drafts = _drafts_map(user_id)
if data.startswith(CB_DRAFT_OPEN_PREFIX):
draft_id = data[len(CB_DRAFT_OPEN_PREFIX):]
dval = drafts.get(draft_id)
if not dval:
await query.edit_message_text(stale_action_text())
return
title, parts = _active_draft_data(user_id, context)
if _has_active_draft(title, parts):
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
MESSAGES["BUTTON_DRAFT_REPLACE"],
callback_data=f"{CB_DRAFT_REPLACE_PREFIX}{draft_id}",
),
InlineKeyboardButton(MESSAGES["BUTTON_CANCEL"], callback_data=CB_DRAFT_BACK),
]
]
)
await query.edit_message_text(
MESSAGES["DRAFT_REPLACE_CONFIRM"].format(title=dval.get("title", "")),
reply_markup=keyboard,
)
return
_set_active_draft(user_id, context, dval.get("title", ""), dval.get("parts", []))
await schedule_users_data_save(context)
try:
await query.answer(MESSAGES["DRAFT_LOADED"].split("\n", 1)[0], show_alert=False)
except Exception:
pass
await _render_drafts_list(query, user_id)
elif data.startswith(CB_DRAFT_REPLACE_PREFIX):
draft_id = data[len(CB_DRAFT_REPLACE_PREFIX):]
dval = drafts.get(draft_id)
if not dval:
await query.edit_message_text(stale_action_text())
return
_set_active_draft(user_id, context, dval.get("title", ""), dval.get("parts", []))
await schedule_users_data_save(context)
try:
await query.answer(MESSAGES["DRAFT_LOADED"].split("\n", 1)[0], show_alert=False)
except Exception:
pass
await _render_drafts_list(query, user_id)
elif data.startswith(CB_DRAFT_DELETE_PREFIX):
draft_id = data[len(CB_DRAFT_DELETE_PREFIX):]
dval = drafts.pop(draft_id, None)
await schedule_users_data_save(context)
try:
await query.answer(MESSAGES["DRAFT_DELETED"].format(title=(dval or {}).get("title", "")))
except Exception:
pass
await _render_drafts_list(query, user_id)
elif data == CB_DRAFT_BACK:
await _render_drafts_list(query, user_id)
async def draft_done(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not await ensure_allowed(update, context):
return ConversationHandler.END
parts = get_draft_parts(context)
if not parts:
await update.message.reply_text(MESSAGES["DRAFT_EMPTY"], reply_markup=drafting_keyboard())
return ENTER_BODY
context.user_data[K_BODY] = "\n".join(parts)
user_id = update.message.from_user.id
default_mode = users_data[user_id].settings.get("default_publish_mode", "draft")
await update.message.reply_text(
MESSAGES["CHOOSE_PUBLICATION_OPTION"],
reply_markup=publish_choice_keyboard(default_mode=default_mode),
)
return ENTER_PUBLISH_CHOICE
async def draft_done_cb(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return ConversationHandler.END
if not await ensure_private(update, context):
return ConversationHandler.END
if not await gate_callback(query, context):
return ENTER_BODY
parts = get_draft_parts(context)
if not parts:
await query.edit_message_text(MESSAGES["DRAFT_EMPTY"], reply_markup=drafting_keyboard())
return ENTER_BODY
context.user_data[K_BODY] = "\n".join(parts)
user_id = query.from_user.id
default_mode = users_data[user_id].settings.get("default_publish_mode", "draft")
await query.edit_message_text(
MESSAGES["CHOOSE_PUBLICATION_OPTION"],
reply_markup=publish_choice_keyboard(default_mode=default_mode),
)
return ENTER_PUBLISH_CHOICE
async def template_insert_cb(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if not await ensure_allowed(update, context):
return ENTER_BODY
if not await ensure_private(update, context):
return ENTER_BODY
if not await gate_callback(query, context):
return ENTER_BODY
_, key = query.data.split(":", 1)
tpl = TEMPLATES.get(key)
if tpl:
parts = get_draft_parts(context)
parts.append(tpl)
set_draft_parts(context, parts)
undo_stack = context.user_data.get(K_UNDO_STACK, [])
undo_stack.append(tpl)
context.user_data[K_UNDO_STACK] = undo_stack
# Persist
u = users_data[query.from_user.id]
u.draft_parts = list(parts)
u.undo_stack = list(undo_stack)
await schedule_users_data_save(context)
await query.edit_message_text(
f"📎 Inserted template '{key}'.", reply_markup=drafting_keyboard()
)
else:
await query.edit_message_text("Template not found.", reply_markup=drafting_keyboard())
return ENTER_BODY