Skip to content

feat(messages): pin and unpin a message with tg messages pin and unpin - #192

Open
ertembiyik wants to merge 12 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-pin
Open

ertembiyik wants to merge 12 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-pin

Conversation

@ertembiyik

@ertembiyik ertembiyik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What and why

tg messages pin --chat <peer> --id <id> [--silent] and tg messages unpin --chat <peer> --id <id> pin and unpin a message, in the shape #23 proposes. Refs #23

The need: pinning meant a raw tg api messages.updatePinnedMessage call with a hand-built input peer, and the cached message kept its old isPinned.

Before (0.14.0)

$ tg messages pin --chat me --id 1
USAGE tg messages list|get|search|wait|watch|unread|read
…
Unknown command pin

After (real output of the e2e fake, whitespace collapsed)

$ tg messages pin --chat @teamchat --id 25 --silent
{ "success": true, "data": { "chatId": -1001234567890, "messageId": 25, "pinned": true,
  "message": { "chatId": -1001234567890, "messageId": 25, "fromId": 999, "text": "Release notes are in the wiki.",
    "messageType": "text", "hasMedia": false, "mediaPath": null, "attachment": null, "action": null,
    "isOutgoing": true, "isEdited": false, "isPinned": true, "isDeleted": false, "replyToId": null,
    "forwardFromId": null, "editDate": null, "date": 1760000000,
    "chat": { "id": -1001234567890, "title": "Team Chat", "username": "teamchat", "type": "supergroup" },
    "sender": { "id": 999, "username": null, "firstName": null, "lastName": null } } } }

tg messages unpin --chat @teamchat --id 25 prints the same with "pinned": false and "isPinned": false.

A refusal says what is wrong:

$ tg messages pin --chat @alice --id 14
{ "success": false, "error": { "code": "TELEGRAM_ERROR", "message": "You do not have permission to pin messages in this chat", "details": { "chatId": 10, "messageId": 14, "telegramError": "CHAT_ADMIN_REQUIRED" }, "retryable": false } }

How it works

  • One call each: messages.updatePinnedMessage, with unpin for tg messages unpin. The two commands share one module and one runner.
  • --silent maps to the method's silent flag. Without it Telegram notifies the chat's members, as its apps do by default; the help text and docs say that a pin is visible to everyone in the chat. unpin never notifies and has no --silent.
  • The cache follows. A new setPinned in messages-cache.ts sets is_pinned on the cached row, and a "pinned a message" service message in the response is cached through the mapper tg send uses (fromUpdates, now exported as newMessageInputs). The output carries the message as tg messages get prints it, or null when the cache doesn't hold it: it is not fetched, and the pin is done either way.
  • Access. Both are writes with chatArgs: ['chat']: read-only mode refuses them and allowedChats applies.

Telegram's refusals are TELEGRAM_ERROR with the name in details.telegramError, as in #187: MESSAGE_ID_INVALID, and PIN_RESTRICTED, CHAT_ADMIN_REQUIRED, CHAT_WRITE_FORBIDDEN, USER_BANNED_IN_CHANNEL as "no permission" (names from the method's docs page).

Found on the way, not changed here: the daemon doesn't apply updatePinnedMessages to the cache, so a pin made in another app shows up only when the message is fetched again. setPinned is ready for that handler; it looks worth its own change.

Decisions you may want to change:

  • Notify by default, --silent to opt out, as tg send --silent does. The opposite default would be the safer one for an agent.
  • The flag comes from the request: after a successful call the row is set to what was asked. raw_json keeps its old pinned until the message is fetched again.
  • The service message is cached but not reported in the output.

Left out: pinning only for yourself in a private chat (pm_oneside), unpinning every message of a chat, forum topics.

This is one of four PRs for #23, one command each: edit (#187), delete, forward, pin. Each is cut from main and stands alone. All four touch src/commands/messages.ts, src/commands/access.ts, the shared fake client (src/__e2e__/helpers/fake-client-cli.ts), the README row for #23 and the CHANGELOG, so each one that lands conflicts with the rest in those spots; I merge main into the others as you land them.

Update: main merged in (5443e73)

How I checked it

  • E2E with the fake client (10 tests): pin and unpin in a private chat, a supergroup and Saved Messages, with isPinned read back offline each time; the supergroup's service message cached; --silent reaching the request; a message the cache doesn't hold; a refusal with the cache untouched; a bad --id refused offline; no credentials. The access suite adds read-only and allowedChats refusals from the table.

  • Unit tests (9): the request for pin, silent pin and unpin, each refusal in both wordings, other errors passed on.

  • Live, by hand, on a personal account in Saved Messages: pinned a message with --silent and read pinned: true back through messages.getMessages and isPinned: true from the cache without --fresh; unpinned it and read both back as false. Pinning it a second time and unpinning it a second time both succeeded, and the docs now say so. No service message appeared in Saved Messages.

  • A step in the live smoke suite (src/__integration__/). Written, not run: I have no test session.

  • Not verified live: a group or supergroup, a pin without --silent, the service message, and the refusals.

  • bun run check: clean. bun run test: 2356 pass, 0 fail. bun run test:e2e: 468 pass, 0 fail.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/cli-commands.md, docs/configuration.md, docs/api-design.md, README, CHANGELOG)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

Update: second code review (e1599ad)

A second, independent review of this branch. Each point below was first shown by a failing test, then fixed.

  • CHAT_NOT_MODIFIED, which Telegram lists for this method, is counted as done, as the docs promise for a repeated pin or unpin; it was passed on as a failure. This answer was not seen live.
  • Docs: Telegram notifies the members of a group of a pin; the text said every chat.
  • Shared code on main that the review also pointed at, unchanged here: the access guard and the command resolve the account and the chat separately; a cached username can outlive its owner; flags a command does not declare are ignored; the activity log write after a successful request can turn it into a failure.

After these commits: bun run check clean, bun run test 1206 pass, bun run test:e2e 352 pass, 0 fail.

Update: main merged in (649eef9)

main up to #220 merged in, no conflicts. One follow-up commit (b08963e), for pin and unpin: the command acts on the chat the allowedChats check resolved (checkedChat), as tg send and tg messages read do since #220. No test of its own: the handoff is tested on main for tg send; here the command's e2e file was run once by hand with allowedChats set, and every case whose chat was in the list passed.

After the merge: bun run check clean, bun run test 1217 pass, bun run test:e2e 353 pass, 0 fail.

Update: main merged in (853a651)

main up to 0.15.1 and #232 merged in. Conflicts: CHANGELOG.md (the entry of this PR now sits under the new ## [Unreleased]) and the fake client of the e2e tests.

After the merge: bun run check clean, bun run test 1229 pass, bun run test:e2e 379 pass, 0 fail.

Update: main merged in (ac85803)

main up to 0.15.2 (#234) merged in. One conflict: CHANGELOG.md (the entry of this PR stays under ## [Unreleased], above the new 0.15.2 section). No code change in this PR.

After the merge: bun run check clean, bun run test 1229 pass, bun run test:e2e 379 pass, 0 fail.

Update: main merged in (392c8d8)

No changes beyond the conflicts in CHANGELOG.md and README.md. Checks: bun run check clean, unit 1349 pass, e2e 452 pass, 0 fail.

🤖 Generated with Claude Code

ertembiyik and others added 5 commits November 18, 2026 15:09
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. main dropped MessagesHistory.getMany (neoromantic#174): the item is read with get. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main now has neoromantic#195: Knip in bun run check, and long as a runtime dependency. No conflict; the check is clean.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main now has neoromantic#196, a test time budget only. No conflict; the check is clean.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
README, access.ts, messages.ts, send/sent-message-cache.ts and db/messages-cache.ts conflicted only with main's oxfmt reformat and inferred return types and were resolved by keeping main's code with this branch's pin lines (newMessageInputs export, MessagesCache.setPinned); the pin's e2e, unit and integration tests and its fake-client and access additions moved to main's colocated layout (src/test/*, *.test-utils.ts, pin-message.test.ts beside its module). The merge also brought the code under main's oxlint/landmines rules (no prose comments, inferred return types), reused createAccountCacheDb and raised the integration file's allowed raised-timeout count to 8.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik and others added 7 commits November 19, 2026 19:53
The docs promise that pinning a pinned message, or unpinning one that isn't,
succeeds. CHAT_NOT_MODIFIED, which Telegram lists for this method, was passed
on as a failure. The docs also said every chat is notified of a pin; that is
true of groups.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ssages-pin

# Conflicts:
#	CHANGELOG.md
#	src/test/fake-client-cli.test-utils.ts
…ssages-pin

# Conflicts:
#	CHANGELOG.md
#	README.md

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant