Add light weight webp avatars (#457)

* Add generate webp avatars, also small size

* Use new avatars in frontend
This commit is contained in:
Reckless_Satoshi 2023-04-25 18:40:46 +00:00 committed by GitHub
parent b1e9081ff8
commit e878299ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 5 deletions

View File

@ -687,9 +687,19 @@ class UserView(APIView):
rh.assemble(roboset="set1", bgset="any") # for backgrounds ON rh.assemble(roboset="set1", bgset="any") # for backgrounds ON
# Does not replace image if existing (avoid re-avatar in case of nick collusion) # Does not replace image if existing (avoid re-avatar in case of nick collusion)
image_path = avatar_path.joinpath(nickname + ".png") # Deprecate "png" and keep "webp" only after v0.4.4
image_path = avatar_path.joinpath(nickname + ".webp")
if not image_path.exists(): if not image_path.exists():
with open(image_path, "wb") as f: with open(image_path, "wb") as f:
rh.img.save(f, format="WEBP", quality=80)
image_small_path = avatar_path.joinpath(nickname + ".small.webp")
with open(image_small_path, "wb") as f:
resized_img = rh.img.resize((80, 80))
resized_img.save(f, format="WEBP", quality=80)
png_path = avatar_path.joinpath(nickname + ".png")
with open(png_path, "wb") as f:
rh.img.save(f, format="png", optimize=True) rh.img.save(f, format="png", optimize=True)
# Create new credentials and login if nickname is new # Create new credentials and login if nickname is new
@ -716,7 +726,7 @@ class UserView(APIView):
context["referral_code"] = token_urlsafe(8) context["referral_code"] = token_urlsafe(8)
user.profile.referral_code = context["referral_code"] user.profile.referral_code = context["referral_code"]
user.profile.avatar = "static/assets/avatars/" + nickname + ".png" user.profile.avatar = "static/assets/avatars/" + nickname + ".webp"
# Noticed some PGP keys replaced at re-login. Should not happen. # Noticed some PGP keys replaced at re-login. Should not happen.
# Let's implement this sanity check "If profile has not keys..." # Let's implement this sanity check "If profile has not keys..."

View File

@ -16,7 +16,6 @@ import {
} from '@mui/icons-material'; } from '@mui/icons-material';
import RobotAvatar from '../../components/RobotAvatar'; import RobotAvatar from '../../components/RobotAvatar';
import { AppContext, UseAppStoreType, closeAll } from '../../contexts/AppContext'; import { AppContext, UseAppStoreType, closeAll } from '../../contexts/AppContext';
import styled from '@emotion/styled';
interface NavBarProps { interface NavBarProps {
width: number; width: number;

View File

@ -285,6 +285,7 @@ const RobotProfile = ({
style={{ width: '2.6em', height: '2.6em' }} style={{ width: '2.6em', height: '2.6em' }}
placeholderType='loading' placeholderType='loading'
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
</Grid> </Grid>
<Grid item> <Grid item>

View File

@ -180,6 +180,7 @@ const BookTable = ({
statusColor={statusBadgeColor(params.row.maker_status)} statusColor={statusBadgeColor(params.row.maker_status)}
tooltip={t(params.row.maker_status)} tooltip={t(params.row.maker_status)}
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
</ListItemAvatar> </ListItemAvatar>
<ListItemText primary={params.row.maker_nick} /> <ListItemText primary={params.row.maker_nick} />
@ -207,6 +208,7 @@ const BookTable = ({
statusColor={statusBadgeColor(params.row.maker_status)} statusColor={statusBadgeColor(params.row.maker_status)}
tooltip={t(params.row.maker_status)} tooltip={t(params.row.maker_status)}
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
</ListItemButton> </ListItemButton>
</div> </div>

View File

@ -226,6 +226,7 @@ const DepthChart: React.FC<DepthChartProps> = ({
statusColor={statusBadgeColor(order.maker_status)} statusColor={statusBadgeColor(order.maker_status)}
tooltip={t(order.maker_status)} tooltip={t(order.maker_status)}
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
</Grid> </Grid>
</Grid> </Grid>

View File

@ -226,6 +226,7 @@ const OrderDetails = ({
tooltip={t(order.maker_status)} tooltip={t(order.maker_status)}
orderType={order.type} orderType={order.type}
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
</ListItemAvatar> </ListItemAvatar>
<ListItemText <ListItemText
@ -257,6 +258,7 @@ const OrderDetails = ({
tooltip={t(order.taker_status)} tooltip={t(order.taker_status)}
orderType={order.type === 0 ? 1 : 0} orderType={order.type === 0 ? 1 : 0}
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
</ListItemAvatar> </ListItemAvatar>
</ListItem> </ListItem>

View File

@ -9,6 +9,7 @@ import placeholder from './placeholder.json';
interface Props { interface Props {
nickname: string | undefined; nickname: string | undefined;
smooth?: boolean; smooth?: boolean;
small?: boolean;
flipHorizontally?: boolean; flipHorizontally?: boolean;
style?: object; style?: object;
imageStyle?: object; imageStyle?: object;
@ -29,6 +30,7 @@ const RobotAvatar: React.FC<Props> = ({
tooltip, tooltip,
tooltipPosition = 'right', tooltipPosition = 'right',
smooth = false, smooth = false,
small = false,
flipHorizontally = false, flipHorizontally = false,
placeholderType = 'loading', placeholderType = 'loading',
style = {}, style = {},
@ -57,12 +59,12 @@ const RobotAvatar: React.FC<Props> = ({
useEffect(() => { useEffect(() => {
if (nickname != undefined) { if (nickname != undefined) {
if (window.NativeRobosats === undefined) { if (window.NativeRobosats === undefined) {
setAvatarSrc(baseUrl + '/static/assets/avatars/' + nickname + '.png'); setAvatarSrc(`${baseUrl}/static/assets/avatars/${nickname}${small ? '.small' : ''}.webp`);
setNicknameReady(true); setNicknameReady(true);
} else { } else {
setNicknameReady(true); setNicknameReady(true);
apiClient apiClient
.fileImageUrl(baseUrl, '/static/assets/avatars/' + nickname + '.png') .fileImageUrl(baseUrl, `/static/assets/avatars/${nickname}${small ? '.small' : ''}.webp`)
.then(setAvatarSrc); .then(setAvatarSrc);
} }
} else { } else {

View File

@ -36,6 +36,7 @@ const MessageCard: React.FC<Props> = ({ message, isTaker, userConnected, baseUrl
statusColor={userConnected ? 'success' : 'error'} statusColor={userConnected ? 'success' : 'error'}
nickname={message.userNick} nickname={message.userNick}
baseUrl={baseUrl} baseUrl={baseUrl}
small={true}
/> />
} }
style={{ backgroundColor: cardColor }} style={{ backgroundColor: cardColor }}

View File

@ -116,6 +116,7 @@ const TradeSummary = ({
baseUrl={baseUrl} baseUrl={baseUrl}
style={{ height: '1.5em', width: '1.5em' }} style={{ height: '1.5em', width: '1.5em' }}
nickname={makerNick} nickname={makerNick}
small={true}
/> />
&nbsp; &nbsp;
{t('Maker')} {t('Maker')}
@ -131,6 +132,7 @@ const TradeSummary = ({
avatarClass='smallAvatar' avatarClass='smallAvatar'
style={{ height: '1.5em', width: '1.5em' }} style={{ height: '1.5em', width: '1.5em' }}
nickname={takerNick} nickname={takerNick}
small={true}
/> />
</ToggleButton> </ToggleButton>
</ToggleButtonGroup> </ToggleButtonGroup>