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

get Contact avatarUrl rather than a image file #1918

Closed
VEADoc opened this issue Feb 24, 2020 · 5 comments
Closed

get Contact avatarUrl rather than a image file #1918

VEADoc opened this issue Feb 24, 2020 · 5 comments

Comments

@VEADoc
Copy link

VEADoc commented Feb 24, 2020

Is your feature request related to a problem? Please describe.
I'd like to save contact's avatar url into my db directly
By this way ,I can display avatar flexibly

egg.

My avatar

[ 132px * 132*px ] http://wx.qlogo.cn/mmhead/ver_1/ib8fq9LMlcm17X8IILM3hNgGtcKENm7fYibPozFSDfp7x7oLBEiaia6TdAdNILEkSORbW8umkU31MGy9EgAd8QesJA/132

If change last part , It will return different size.

[ 1024px* 1024px ]
http://wx.qlogo.cn/mmhead/ver_1/ib8fq9LMlcm17X8IILM3hNgGtcKENm7fYibPozFSDfp7x7oLBEiaia6TdAdNILEkSORbW8umkU31MGy9EgAd8QesJA/0

Describe the solution you'd like
add some code to src/user/contact.ts

 /**
   * Get the avatarUrl 'avatar' from a contact
   * // Like `http://wx.qlogo.cn/mmhead/ver_1/ib8fq9LMlcm17X8IILM3hNgGtcKENm7fYibPozFSDfp7x7oLBEiaia6TdAdNIEEkSORbW8umkU31MGy9EgAd8QesJA/132`
   * 
   * @returns {string | null}
   * @example
   * const avatarUrl = contact.avatarUrl()
  */
  public avatarUrl(): null | string {
    return (this.payload && this.payload.avatar) || null
  }

[enhancement]

@VEADoc
Copy link
Author

VEADoc commented Feb 24, 2020

valid size suffix :

  • 0 1024px*1024px
  • 46 46px*46px
  • 64 64px*64px
  • 96 96px*96px
  • 132 132px*132px

@huan
Copy link
Member

huan commented Feb 24, 2020

Thanks for the issue and the pull request, appreciate it.

Currently, we are using FileBox to encapsulate all files inside the Wechaty framework, including the avatar image.

The reason we use FileBox is that in some conditions we can use NOT the URL directly, for example, the Web WeChat protocol requires you send the cookie to the HTTP server when you are getting your image from the URL.

However, if you are using the wechaty-puppet-padplus, I believe there's a chance to do a workaround for archiving your goal by getting the url from the FileBox instance:

import { FileBoxType } from 'file-box'
const fileBox = await contact.avatar()
const type = (fileBox as any).boxType
if (type === FileBoxType.Url) {
  console.info('url:', (fileBox as any).remoteUrl)
} else {
  console.info('not a url type for FileBox')
}

According to the above reason (Web Protocol limitation), we can not accept the design in your Pull Request because we need to make sure the abstract of wechaty-puppet can support all protocols, which should including the current ones, and the future ones (a puppet implementation might use Base64 encoding for your avatar, which will be no url at all).

So please let me know if the workaround works for you (if you are using the pad protocol), and let's keep this issue open and to see if we can get any better ideas to solve it.

BTW: We can consider using the new Image class to support 3 sizes of the avatar, which I believe it should be enough for most cases. See: #1871

@huan huan added the question label Feb 24, 2020
@VEADoc
Copy link
Author

VEADoc commented Feb 24, 2020

I'm using pad protocol( padplus ) now. By the way Web Protocol is out-of-date

If get avatar by new Image class ,I must get avatar files first ,then put them to my server or cloud object server like AWS S3 and Alibaba Cloud OSS , finally save my link into db

It's seems will take some times for files download complete one by one.

If Web Protocol limitation is the only reason , I would be grateful if you reconsider this issue.

@huan
Copy link
Member

huan commented Feb 24, 2020

No, you needn't download anything to your server when you are using FileBox with the new Image class.

Please read the code before you continue designing, you will get to know the right direction for this problem.

@VEADoc
Copy link
Author

VEADoc commented Feb 24, 2020

I am sorry for my carelessness

I found that in #1871

class Image {
  url(): string // does not grantee to be visited directly (?)
  thumbnail(): FileBox
  hd(): FileBox
  artwork(): FileBox
}

Looking forward to Wechaty@0.31.17 available 😃

@VEADoc VEADoc closed this as completed Feb 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants