grammers icon indicating copy to clipboard operation
grammers copied to clipboard

Friendly methods

Open Lonami opened this issue 4 years ago • 8 comments

Use of raw API should be the exception, not the norm. Our client should offer an idiomatic interface for the most commonly used methods.

Starting by porting Telethon's client methods would be a good start (not including all, and not necessarily with the same name).

Methods which are in my opinion worth implementing (as of Telethon 1.21.1, on the left Telethon's naming, on the right implemented versions):

Skipped methods and why:

  • Auth
    • start. Too high level. grammers-client is a library, not an application, and the login flow is something applications should decide.
    • qr_login. May be added if there's enough demand for it, but otherwise the implementation is pretty simple.
    • is_connected. If you have a Client, you're connected. If you're not connected, sending requests will fail. I don't think it's worth adding a method to check for connectivity, but I am not against it if there's a solid reason for it.
    • disconnected. It's a future that behaves like run_until_disconnected.
    • loop. grammers does not hold any "event loop" or "executor" inside of it.
    • set_proxy. Is changing proxies live something desirable? Would it complicate the code too much if added (once proxies are supported)?
  • Downloads
    • download_file. Too low level, and download_media already serves its purpose.
  • Dialogs
    • conversation. Way too much to add. The current implementation in Telethon is quite limitting, and in Rust it may even be harder to get a similar feel with little benefit. Finite State Machines are more than enough, and one of the (if not the) most flexible approaches.
  • Users
    • get_input_entity. There is no persistent cache, so this method doesn't serve any use. Furthermore, input entities are abstracted away in grammers.
    • get_peer_id. Not very useful without a persistent cache (for example, for usernames). A PackedChat or Chat already contains the ID.
  • Parse Mode
    • parse_mode. A client-wide parse-mode behaves pretty much like "global state" in Telethon which is not desirable in grammers (it even affects Message attached to the TelegramClient in Telethon). Instead, each Message decides how it should be formatted.
  • Updates
    • on. Decorators are nice and all, but in Telethon they rely on global state, which is harder to achieve in grammers. If it can be made pretty enough and to work well enough, I may consider adding this feature.
    • add_event_handler. Callbacks are not rusty.
    • remove_event_handler. If there are no event handlers, there is nothing to remove.
    • list_event_handlers. If there are no event handlers, there is nothing to list.

For the get_ variants (get_messages, get_dialogs, …), the iter_ methods are more than enough. Users who need a Vec can build it on their own (and decide what happens if there's an error midway through building the list rather than throwing it all away).

Lonami avatar Feb 05 '20 11:02 Lonami

Something that's just as important is reviewing the errors that are exposed through the public interface. For this, it might be a good idea to pretend we're a user of the library, and go through the documentation of the library built offline to see how one would use it.

Lonami avatar Dec 15 '20 19:12 Lonami

The new-type pattern should probably be used for all the iterators, so that they can be exposed from the grammers_client::client module. The methods in the Client struct would then point the user to these to instruct them how to configure them.

Lonami avatar Dec 15 '20 19:12 Lonami

Future proofing should be taken into consideration.

Lonami avatar Feb 07 '21 00:02 Lonami

Perhaps taking AsRef<R: RemoteCall> would enable people to send requests from things like Box<dyn RemoteCall>, needs testing.

Lonami avatar Mar 06 '21 09:03 Lonami

@Lonami Why do we need Box<dyn RemoteCall>? Do we not know all the telegram APIs in advance such that we need boxing to do it?

pickfire avatar Mar 06 '21 17:03 pickfire

I don't need it, but someone might. Just like std::fs APIs take an AsRef<Path>, it might be a good idea to do something similar for more flexibility.

Lonami avatar Mar 06 '21 21:03 Lonami

@Lonami Shouldn't we leave it until someone needs it? I think that is maybe a bit too much as of now.

pickfire avatar Mar 07 '21 17:03 pickfire

It's just changing fn invoke<R: RemoteCall>(&self, request: &R) for fn invoke<R: AsRef<RemoteCall>>(&self, request: R). Why are you so opposed to it?

Lonami avatar Mar 07 '21 19:03 Lonami

is it possible to use telethon's sessions ?

DdoSseRr avatar Nov 25 '23 21:11 DdoSseRr

Not directly, and direct support is not planned. Regardless, that's out of scope for this issue. Which is probably safe to close in any case.

Lonami avatar Nov 26 '23 00:11 Lonami