Forum Discussion

iftomkins's avatar
10 years ago

Get URL of user avatar, but get default image if user's account has been closed

We have a custom component that generates the author block of each topic/reply. The custom component pulls the avatar URL for the message author via the API. However, when the user has closed their account, this API call does not successfully return the default anonymous user image from the settings. Instead, it returns a mysterious default image from deep in the depths of Lithium (see attached).

 

Lithium support told us that to fix this, we need to update the logic in our custom author component. This is the solution I came up with, and it works, but I wanted to post here both to see if it's a good solution, and to potentially assist future seekers. 

 

The solution checks to see if the user id of the message author is -1, since that is the user ID that Lithium seems to be assigning to closed user accounts. Is this a solid solution?

 

<#if messageAuthorId == -1 >
    <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" />
<#else>
    <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
</#if>

weird_default_image.png
  • Hi iftomkins

     

    Looks good, however I'd recommend wrapping in <attempt> tags to catch any errors should something go wrong. It is highly unlikely as this is fairly simple logic however this should cover most scenarios.

     

    <#attempt>
    <#if messageAuthorId == -1 >
        <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" />
    <#else>
        <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
    </#if>
    <#recover>
    <!-- what to do if the above logic fails or there is an error --> <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" /> </#attempt>

    Wrapping in this around your code will catch any exceptions in the code should they occurr and instead of displaying the error it'll default to the anonymous user avatar you have specified.

2 Replies

  • Hi iftomkins

     

    Looks good, however I'd recommend wrapping in <attempt> tags to catch any errors should something go wrong. It is highly unlikely as this is fairly simple logic however this should cover most scenarios.

     

    <#attempt>
    <#if messageAuthorId == -1 >
        <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" />
    <#else>
        <#assign avatarUrl = rest("/users/id/${messageAuthorId}/profiles/avatar/url").value />
    </#if>
    <#recover>
    <!-- what to do if the above logic fails or there is an error --> <#assign avatarUrl = "/html/assets/icon_anonymous_message.png" /> </#attempt>

    Wrapping in this around your code will catch any exceptions in the code should they occurr and instead of displaying the error it'll default to the anonymous user avatar you have specified.