Merge pull request #2051 from ecency/nt/reputation

Nt/reputation
This commit is contained in:
Feruz M 2021-08-25 14:18:56 +03:00 committed by GitHub
commit 84fccaed24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 107 deletions

View File

@ -9,6 +9,7 @@ import HeaderView from '../view/headerView';
import { AccountContainer, ThemeContainer } from '../../../containers';
import { hidePostsThumbnails } from '../../../redux/actions/uiAction';
import { parseReputation } from '../../../utils/user';
const HeaderContainer = ({
selectedUser,
@ -48,6 +49,7 @@ const HeaderContainer = ({
{({ currentAccount, isLoggedIn, isLoginDone }) => {
const _user = isReverse && selectedUser ? selectedUser : currentAccount;
const reputation = parseReputation(get(_user, 'reputation'));
return (
<HeaderView
displayName={get(_user, 'display_name')}
@ -58,7 +60,7 @@ const HeaderContainer = ({
isLoggedIn={isLoggedIn}
isLoginDone={isLoginDone}
isReverse={isReverse}
reputation={get(_user, 'reputation')}
reputation={reputation}
username={get(_user, 'name')}
hideUser={hideUser}
enableViewModeToggle={enableViewModeToggle}

View File

@ -20,7 +20,7 @@ class PostHeaderDescription extends PureComponent {
// Component Functions
_handleOnUserPress = (username) => {
const { navigation, profileOnPress, reputation, currentAccountUsername } = this.props;
const { navigation, profileOnPress, reputation } = this.props;
if (profileOnPress) {
profileOnPress(username);
@ -78,7 +78,6 @@ class PostHeaderDescription extends PureComponent {
date,
isHideImage,
name,
reputation,
size,
tag,
content,
@ -98,18 +97,13 @@ class PostHeaderDescription extends PureComponent {
onPress={() => this._handleOnUserPress(name)}
>
{!isHideImage && (
<>
<UserAvatar
style={[styles.avatar, { width: size, height: size, borderRadius: size / 2 }]}
disableSize
username={name}
defaultSource={DEFAULT_IMAGE}
noAction
/>
<View style={styles.reputationWrapper}>
<Text style={styles.reputation}>{reputation}</Text>
</View>
</>
<UserAvatar
style={[styles.avatar, { width: size, height: size, borderRadius: size / 2 }]}
disableSize
username={name}
defaultSource={DEFAULT_IMAGE}
noAction
/>
)}
</TouchableOpacity>
<View style={styles.leftContainer}>

View File

@ -24,8 +24,7 @@ import { userActivity } from '../ecency/ePoint';
// Utils
import { decryptKey } from '../../utils/crypto';
import { parsePosts, parsePost, parseComments } from '../../utils/postParser';
import { getName, getAvatar } from '../../utils/user';
import { getReputation } from '../../utils/reputation';
import { getName, getAvatar, parseReputation } from '../../utils/user';
import parseToken from '../../utils/parseToken';
import parseAsset from '../../utils/parseAsset';
import filterNsfwPost from '../../utils/filterNsfwPost';
@ -266,7 +265,7 @@ export const getUser = async (user, loggedIn = true) => {
getCache('rcPower');
await setCache('rcPower', rcPower);
_account.reputation = getReputation(_account.reputation);
_account.reputation = parseReputation(_account.reputation);
_account.username = _account.name;
_account.unread_activity_count = unreadActivityCount;
_account.vp_manabar = client.rc.calculateVPMana(_account);

View File

@ -1,34 +0,0 @@
export default (input) => {
if (!input) {
return 0;
}
if (input === 0) {
return 25;
}
if (!input) {
return input;
}
let neg = false;
if (input < 0) {
neg = true;
}
let reputationLevel = Math.log10(Math.abs(input));
reputationLevel = Math.max(reputationLevel - 9, 0);
if (reputationLevel < 0) {
reputationLevel = 0;
}
if (neg) {
reputationLevel *= -1;
}
reputationLevel = reputationLevel * 9 + 25;
return Math.floor(reputationLevel);
};

View File

@ -7,8 +7,8 @@ import FastImage from 'react-native-fast-image';
// Utils
import parseAsset from './parseAsset';
import { getReputation } from './reputation';
import { getResizedAvatar, getResizedImage } from './image';
import { getResizedAvatar } from './image';
import { parseReputation } from './user';
const webp = Platform.OS === 'ios' ? false : true;
@ -43,7 +43,7 @@ export const parsePost = (post, currentUserName, isPromoted, isList = false, isC
post.image = catchPostImage(post, 600, 500, webp ? 'webp' : 'match');
post.thumbnail = catchPostImage(post, 10, 7, webp ? 'webp' : 'match');
post.author_reputation = getReputation(post.author_reputation);
post.author_reputation = parseReputation(post.author_reputation);
post.avatar = getResizedAvatar(get(post, 'author'));
if (!isList) {
post.body = renderPostBody(post, true, webp);
@ -77,7 +77,7 @@ export const parsePost = (post, currentUserName, isPromoted, isList = false, isC
export const parseComments = async (comments) => {
return comments.map((comment) => {
comment.pending_payout_value = parseFloat(get(comment, 'pending_payout_value', 0)).toFixed(3);
comment.author_reputation = getReputation(get(comment, 'author_reputation'));
comment.author_reputation = parseReputation(get(comment, 'author_reputation'));
comment.avatar = getResizedAvatar(get(comment, 'author'));
comment.markdownBody = get(comment, 'body');
comment.body = renderPostBody(comment, true, webp);
@ -144,7 +144,6 @@ export const parseActiveVotes = (post) => {
if (!isEmpty(post.active_votes)) {
forEach(post.active_votes, (value) => {
value.reward = (value.rshares * ratio).toFixed(3);
//value.reputation = getReputation(get(value, 'reputation'));
value.percent /= 100;
value.is_down_vote = Math.sign(value.percent) < 0;
value.avatar = getResizedAvatar(get(value, 'voter'));

View File

@ -1,36 +0,0 @@
export const getReputation = (reputation) => {
if (reputation === null) {
return reputation;
}
if (isFloat(reputation)) {
return Math.floor(reputation);
}
let _reputation = String(parseInt(reputation, 10));
const neg = _reputation.charAt(0) === '-';
_reputation = neg ? _reputation.substring(1) : _reputation;
const str = _reputation;
const leadingDigits = parseInt(str.substring(0, 4), 10);
const log = Math.log(leadingDigits) / Math.log(10);
const n = str.length - 1;
let out = n + (log - parseInt(log, 10));
// eslint-disable-next-line no-restricted-globals
if (isNaN(out)) {
out = 0;
}
out = Math.max(out - 9, 0);
out *= neg ? -1 : 1;
out = out * 9 + 25;
out = parseInt(out, 10);
return out;
};
function isFloat(n) {
return Number(n) === n && n % 1 !== 0;
}

View File

@ -1,28 +1,35 @@
export const getReputation = (input) => {
if (input === 0) {
return 25;
const isHumanReadable = (input: number): boolean => {
return Math.abs(input) > 0 && Math.abs(input) <= 100;
}
export const parseReputation = (input: string | number): number => {
if (typeof input === 'number' && isHumanReadable(input)) {
return Math.floor(input);
}
if (!input) {
return input;
if (typeof input === 'string') {
input = Number(input);
if (isHumanReadable(input)) {
return Math.floor(input);
}
}
if (input === 0) {
return 25;
}
let neg = false;
if (input < 0) {
neg = true;
}
if (input < 0) neg = true;
let reputationLevel = Math.log10(Math.abs(input));
reputationLevel = Math.max(reputationLevel - 9, 0);
if (reputationLevel < 0) {
reputationLevel = 0;
}
if (reputationLevel < 0) reputationLevel = 0;
if (neg) {
reputationLevel *= -1;
}
if (neg) reputationLevel *= -1;
reputationLevel = reputationLevel * 9 + 25;