Skip to content

Commit 38084ca

Browse files
authored
Generate correct fake imcs for voice and video note drafts (#597)
A draft carries a note's file, duration, waveform and length flat, while the input message content it stands for nests them in `inputVoiceNote' and `inputVideoNote'. `telega-ins--draft-content-one-line' relabelled the draft's `:@type' and passed the flat fields on, so `telega-ins--input-content-one-line' had to accept either shape to serve both callers. Moved the fields into the wrapper where the fake imc is built, so only one shape reaches the renderer and its fallback to the content itself is no longer needed. A note's `file_path' becomes the `inputFileLocal' the wrapper expects, which the relabelling never provided.
1 parent 97406c7 commit 38084ca

2 files changed

Lines changed: 41 additions & 22 deletions

File tree

telega-ins.el

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,11 +4211,7 @@ If SHORT-P is non-nil then use short version."
42114211
(telega-ins--self-destruct-type tl-ttl 'short)))
42124212
))))
42134213
(inputMessageVoiceNote
4214-
;; The duration and waveform sit inside the `inputVoiceNote' this is
4215-
;; built with, but a draft keeps them flat -- `draftMessageContentVoiceNote'
4216-
;; has no wrapper -- and drafts reach this same renderer as a fake imc.
4217-
;; Falling back to IMC itself is what serves that caller.
4218-
(let* ((note (or (plist-get imc :voice_note) imc))
4214+
(let* ((note (plist-get imc :voice_note))
42194215
(duration (or (plist-get note :duration) 0))
42204216
(waveform (plist-get note :waveform)))
42214217
(telega-ins "VoiceNote ")
@@ -4227,8 +4223,7 @@ If SHORT-P is non-nil then use short version."
42274223
(telega-ins " (" (telega-duration-human-readable duration) ")")))
42284224
(inputMessageVideoNote
42294225
(telega-ins "VideoNote")
4230-
;; Flat for a draft, wrapped for an attachment; see the voice note above.
4231-
(let* ((note (or (plist-get imc :video_note) imc))
4226+
(let* ((note (plist-get imc :video_note))
42324227
(duration (or (plist-get note :duration) 0))
42334228
(thumb-filename (telega--tl-get note :thumbnail :thumbnail :path)))
42344229
(when (and telega-use-images thumb-filename)
@@ -4359,14 +4354,32 @@ If SHORT-P is non-nil then use short version."
43594354
(defun telega-ins--draft-content-one-line (draft-content)
43604355
"Insert DRAFT-CONTENT for one line usage."
43614356
(let* ((content-type (telega--tl-type draft-content))
4362-
(imc-type (cl-case content-type
4363-
(draftMessageContentText "inputMessageText")
4364-
(draftMessageContentVoiceNote "inputMessageVoiceNote")
4365-
(draftMessageContentVideoNote "inputMessageVideoNote"))))
4366-
(cond (imc-type
4367-
(let ((fake-imc (copy-sequence draft-content)))
4368-
(plist-put fake-imc :@type imc-type)
4369-
(telega-ins--input-content-one-line fake-imc)))
4357+
(fake-imc
4358+
(cl-case content-type
4359+
(draftMessageContentText
4360+
(let ((imc (copy-sequence draft-content)))
4361+
(plist-put imc :@type "inputMessageText")))
4362+
;; A note's file, duration, waveform and length are flat in the
4363+
;; draft and nested in the input message content it stands for, so
4364+
;; these are moved into the wrapper rather than relabelled.
4365+
(draftMessageContentVoiceNote
4366+
(list :@type "inputMessageVoiceNote"
4367+
:voice_note
4368+
(list :@type "inputVoiceNote"
4369+
:voice_note (list :@type "inputFileLocal"
4370+
:path (plist-get draft-content :file_path))
4371+
:duration (plist-get draft-content :duration)
4372+
:waveform (plist-get draft-content :waveform))))
4373+
(draftMessageContentVideoNote
4374+
(list :@type "inputMessageVideoNote"
4375+
:video_note
4376+
(list :@type "inputVideoNote"
4377+
:video_note (list :@type "inputFileLocal"
4378+
:path (plist-get draft-content :file_path))
4379+
:duration (plist-get draft-content :duration)
4380+
:length (plist-get draft-content :length)))))))
4381+
(cond (fake-imc
4382+
(telega-ins--input-content-one-line fake-imc))
43704383
((eq content-type 'draftMessageContentRichMessage)
43714384
(telega-ins--one-lined
43724385
(telega-ins--rich-message (plist-get draft-content :message))))

test.el

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,15 @@ Have Stoploss 690 Satoshi." :entities []))))
301301
:length 240)))))))
302302

303303
(ert-deftest telega-vvnote-tdlib-1.8.66-one-line ()
304-
"Input line reads a wrapped attachment, and a draft, which has no wrapper."
304+
"Input line reads a wrapped attachment, and a draft converted into one."
305305
(let ((telega-use-images nil))
306306
(cl-flet ((one-line (imc)
307307
(with-temp-buffer
308308
(telega-ins--input-content-one-line imc)
309+
(buffer-substring-no-properties (point-min) (point-max))))
310+
(draft-line (draft-content)
311+
(with-temp-buffer
312+
(telega-ins--draft-content-one-line draft-content)
309313
(buffer-substring-no-properties (point-min) (point-max)))))
310314
(should (string-match-p
311315
"VoiceNote.*(3s)"
@@ -323,16 +327,18 @@ Have Stoploss 690 Satoshi." :entities []))))
323327
:path "/tmp/note.mp4")
324328
:duration 9
325329
:length 240)))))
326-
;; `draftMessageContentVoiceNote' and `draftMessageContentVideoNote' have
327-
;; no wrapper, and reach this same function as a fake imc.
330+
;; A draft keeps these fields flat, and is rendered by the same function
331+
;; after `telega-ins--draft-content-one-line' has nested them.
328332
(should (string-match-p
329333
"VoiceNote.*(7s)"
330-
(one-line '(:@type "inputMessageVoiceNote"
331-
:duration 7 :waveform "AAAA"))))
334+
(draft-line '(:@type "draftMessageContentVoiceNote"
335+
:file_path "/tmp/note.mp4"
336+
:duration 7 :waveform "AAAA"))))
332337
(should (string-match-p
333338
"VideoNote.*(11s)"
334-
(one-line '(:@type "inputMessageVideoNote"
335-
:duration 11 :length 240)))))))
339+
(draft-line '(:@type "draftMessageContentVideoNote"
340+
:file_path "/tmp/note.mp4"
341+
:duration 11 :length 240)))))))
336342

337343
(ert-deftest telega-org-formatting ()
338344
"Test org mode text formatting."

0 commit comments

Comments
 (0)