The Wayback Machine - https://web.archive.org/web/20201210054225/https://github.com/pyrogram/pyrogram/pull/335
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add full_name property in User class #335

Open
wants to merge 3 commits into
base: develop
from
Open

Conversation

@tasi788
Copy link

@tasi788 tasi788 commented Oct 29, 2019

No description provided.

Copy link
Member

@ColinTheShark ColinTheShark left a comment

Some thoughts on this PR. Overall good idea.

As from the first comment I'd suggest a different syntax to prevent potential errors.

full_name=(user.first_name + " " +
user.last_name if user.last_name else None),
Comment on lines 187 to 188

This comment has been minimized.

@ColinTheShark

ColinTheShark Nov 6, 2019
Member

That handling of full_name would likely result in a TypeError because of invalid operand + between NoneType and str as well as Python not being able to concatenate NoneType and str.

This might be an ugly workaround, but it's a working one.

"{} {}".format(user.first_name, user.last_name) if user.last_name else user.first_name

That above line would always only return a string without trailing whitespace. Alternatively we could do the following to avoid the use of .format():

user.first_name + " " + user.last_name if user.last_name else user.first_name

This comment has been minimized.

@JosXa

JosXa Dec 22, 2019
Contributor

I don't see a reason why .format() should be avoided (not sure about f-strings and pyro compatibility, would have to check...)
1st option LGTM.

This comment has been minimized.

@tasi788

tasi788 Mar 16, 2020
Author

I've change the code, please have review.

photo=ChatPhoto._parse(client, user.photo, user.id, user.access_hash),
restrictions=pyrogram.List([Restriction._parse(r) for r in user.restriction_reason]) or None,
photo=ChatPhoto._parse(
client, user.photo, user.id, user.access_hash),
restrictions=pyrogram.List(
[Restriction._parse(r) for r in user.restriction_reason]) or None,
Comment on lines 190 to 197

This comment has been minimized.

@ColinTheShark

ColinTheShark Nov 6, 2019
Member

These changes don't change the code itself, but only the layout/format of it. I guess you've used a formatter and I suggest changing the maximum amount of characters per line on your formatter, as Pyrogram doesn't adhere to PEP8's 80 char limit, but rather it's own 120 (if I remember correctly). Your formatter creates unnecessary diffs.

This comment has been minimized.

@tasi788

tasi788 Mar 16, 2020
Author

I didn't notice that my IDE do autopep8 automatically, now should be good.

tasi788 added 2 commits Mar 16, 2020
using format instead
@delivrance
Copy link
Member

@delivrance delivrance commented May 16, 2020

This should be a property, by the way.

@delivrance
Copy link
Member

@delivrance delivrance commented May 16, 2020

I also experimented with custom formats in the past, which would allow things like f"{user:mention}" to be expanded into an HTML mention (this does actually exist in Pyrogram already, but is nowhere documented yet). Maybe we can have more, like {u:full_name} and such.

However, this only works for string interpolations and having user.mention, user.full_name and such properties is probably a better idea.

@delivrance delivrance changed the title Add full_user in user class Add full_name property in User class May 16, 2020
@JosXa
Copy link
Contributor

@JosXa JosXa commented Aug 26, 2020

I have a constant need for display_name (something that reliably gives you the most concrete, the most "clickable" version of a name):

def displayname(entity: Any) -> str:
    if hasattr(entity, "title"):
        return entity.title
    elif hasattr(entity, "first_name"):
        if entity.last_name:
            return f"{entity.first_name} {entity.last_name}"
        return entity.first_name
    raise ValueError("This entity does not seem to have a display name.")


def user_or_displayname(entity: Any) -> str:
    if entity.username:
        return f"@{entity.username}"
    return displayname(entity)
@JosXa
Copy link
Contributor

@JosXa JosXa commented Aug 26, 2020

Maybe also related?

from enum import IntEnum

from pyrogram import Client
from pyrogram.raw.types import Channel
from pyrogram.types import Message, User, Chat
from typing import Optional, Union, cast, Dict

_links_cache: Dict[int, str] = {}


class Platform(IntEnum):
    android = 1
    ios = 2
    web = 3
    desktop = 4


async def direct_link_to_message(
    reference: Message, platform: Optional[Platform] = Platform.android
) -> str:
    entity_link = await direct_link(reference._client, reference.chat, platform)
    return f"{entity_link}/{reference.message_id}"


async def direct_link(
    client: Client,
    peer: Union[User, Chat, Channel],
    platform: Optional[Platform] = Platform.android,
) -> str:
    if peer.username:
        return f"https://t.me/{peer.username}"
    else:
        if not isinstance(peer, User):
            peer_id = peer.id
            if isinstance(peer, Channel):
                return f"https://t.me/c/{peer_id}"
            invite_link = _links_cache.get(peer_id, None)
            if not invite_link:
                invite_link = (await client.get_chat(peer_id)).invite_link
                _links_cache[peer_id] = invite_link
            if invite_link:
                return cast(str, invite_link)

        if platform == Platform.android:
            return f"tg://openmessage?user_id={peer.id}"
        elif platform == Platform.ios:
            return f"t.me/@{peer.id}"
        elif platform == Platform.web:
            # TODO: maybe incorrect, test!
            return f"https://web.Telegram.org/#/im?p=u{peer.id}"
        else:
            raise ValueError(
                f"User has no username, creating direct link on platform {platform} not possible."
            )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.