From 857707fc61892e5956e085e6c343b36d97484fdb Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Fri, 30 Oct 2020 12:54:02 -0300 Subject: [PATCH] [FIX] App always sends auth for Avatar requests (#2517) * [FIX] Sending auth for Avatar requests when not necessary * fix storybook * Fix ShareListView not updating avatars Co-authored-by: Diego Mello --- app/actions/actionsTypes.js | 1 + app/actions/share.js | 7 ++++++ app/constants/settings.js | 3 +++ app/containers/Avatar/Avatar.js | 9 +++++--- app/containers/Avatar/index.js | 9 ++++++-- app/lib/database/index.js | 1 + app/lib/rocketchat.js | 22 +++++++++++++++---- app/reducers/share.js | 8 ++++++- app/utils/avatar.js | 4 ++-- .../CreateDiscussionView/SelectChannel.js | 6 +++-- app/views/CreateDiscussionView/SelectUsers.js | 10 +++++++-- app/views/CreateDiscussionView/index.js | 8 +++++-- app/views/ShareListView/index.js | 5 ++++- storybook/stories/index.js | 2 ++ 14 files changed, 76 insertions(+), 19 deletions(-) diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 4e5f9443e..e27a3d11e 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -18,6 +18,7 @@ export const LOGIN = createRequestTypes('LOGIN', [ export const SHARE = createRequestTypes('SHARE', [ 'SELECT_SERVER', 'SET_USER', + 'SET_SETTINGS', 'SET_SERVER_INFO' ]); export const USER = createRequestTypes('USER', ['SET']); diff --git a/app/actions/share.js b/app/actions/share.js index 56e8ced9b..ff4d5a592 100644 --- a/app/actions/share.js +++ b/app/actions/share.js @@ -7,6 +7,13 @@ export function shareSelectServer(server) { }; } +export function shareSetSettings(settings) { + return { + type: SHARE.SET_SETTINGS, + settings + }; +} + export function shareSetUser(user) { return { type: SHARE.SET_USER, diff --git a/app/constants/settings.js b/app/constants/settings.js index ccb1803e2..359dff092 100644 --- a/app/constants/settings.js +++ b/app/constants/settings.js @@ -20,6 +20,9 @@ export default { Accounts_AllowUsernameChange: { type: 'valueAsBoolean' }, + Accounts_AvatarBlockUnauthenticatedAccess: { + type: 'valueAsBoolean' + }, Accounts_CustomFields: { type: 'valueAsString' }, diff --git a/app/containers/Avatar/Avatar.js b/app/containers/Avatar/Avatar.js index 1afc2d660..978711156 100644 --- a/app/containers/Avatar/Avatar.js +++ b/app/containers/Avatar/Avatar.js @@ -24,7 +24,8 @@ const Avatar = React.memo(({ getCustomEmoji, avatarETag, isStatic, - rid + rid, + blockUnauthenticatedAccess }) => { if ((!text && !avatar && !emoji && !rid) || !server) { return null; @@ -59,7 +60,8 @@ const Avatar = React.memo(({ avatar, server, avatarETag, - rid + rid, + blockUnauthenticatedAccess }); } @@ -111,7 +113,8 @@ Avatar.propTypes = { getCustomEmoji: PropTypes.func, avatarETag: PropTypes.string, isStatic: PropTypes.bool, - rid: PropTypes.string + rid: PropTypes.string, + blockUnauthenticatedAccess: PropTypes.bool }; Avatar.defaultProps = { diff --git a/app/containers/Avatar/index.js b/app/containers/Avatar/index.js index c5769dfd6..3218ec7ae 100644 --- a/app/containers/Avatar/index.js +++ b/app/containers/Avatar/index.js @@ -12,7 +12,8 @@ class AvatarContainer extends React.Component { static propTypes = { rid: PropTypes.string, text: PropTypes.string, - type: PropTypes.string + type: PropTypes.string, + blockUnauthenticatedAccess: PropTypes.bool }; static defaultProps = { @@ -93,6 +94,10 @@ class AvatarContainer extends React.Component { const mapStateToProps = state => ({ user: getUserSelector(state), - server: state.share.server || state.server.server + server: state.share.server || state.server.server, + blockUnauthenticatedAccess: + state.share.settings?.Accounts_AvatarBlockUnauthenticatedAccess + ?? state.settings.Accounts_AvatarBlockUnauthenticatedAccess + ?? true }); export default connect(mapStateToProps)(AvatarContainer); diff --git a/app/lib/database/index.js b/app/lib/database/index.js index f254e6709..41f135551 100644 --- a/app/lib/database/index.js +++ b/app/lib/database/index.js @@ -116,6 +116,7 @@ class DB { Permission, CustomEmoji, FrequentlyUsedEmoji, + Setting, User ], actionsEnabled: true diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 76bd5e1dc..32e51921f 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -19,9 +19,7 @@ import fetch from '../utils/fetch'; import { encryptionInit } from '../actions/encryption'; import { setUser, setLoginServices, loginRequest } from '../actions/login'; import { disconnect, connectSuccess, connectRequest } from '../actions/connect'; -import { - shareSelectServer, shareSetUser -} from '../actions/share'; +import { shareSelectServer, shareSetUser, shareSetSettings } from '../actions/share'; import subscribeRooms from './methods/subscriptions/rooms'; import getUsersPresence, { getUserPresence, subscribeUsersPresence } from './methods/getUsersPresence'; @@ -324,8 +322,23 @@ const RocketChat = { RocketChat.setCustomEmojis(); - // set User info try { + // set Settings + const settings = ['Accounts_AvatarBlockUnauthenticatedAccess']; + const db = database.active; + const settingsCollection = db.collections.get('settings'); + const settingsRecords = await settingsCollection.query(Q.where('id', Q.oneOf(settings))).fetch(); + const parsed = Object.values(settingsRecords).map(item => ({ + _id: item.id, + valueAsString: item.valueAsString, + valueAsBoolean: item.valueAsBoolean, + valueAsNumber: item.valueAsNumber, + valueAsArray: item.valueAsArray, + _updatedAt: item._updatedAt + })); + reduxStore.dispatch(shareSetSettings(this.parseSettings(parsed))); + + // set User info const userId = await UserPreferences.getStringAsync(`${ RocketChat.TOKEN_KEY }-${ server }`); const userCollections = serversDB.collections.get('users'); let user = null; @@ -353,6 +366,7 @@ const RocketChat = { database.share = null; reduxStore.dispatch(shareSetUser({})); + reduxStore.dispatch(shareSetSettings({})); }, async e2eFetchMyKeys() { diff --git a/app/reducers/share.js b/app/reducers/share.js index 9c3a7b38c..069d36d7a 100644 --- a/app/reducers/share.js +++ b/app/reducers/share.js @@ -2,7 +2,8 @@ import { SHARE } from '../actions/actionsTypes'; const initialState = { user: {}, - server: '' + server: '', + settings: {} }; export default function share(state = initialState, action) { @@ -17,6 +18,11 @@ export default function share(state = initialState, action) { ...state, user: action.user }; + case SHARE.SET_SETTINGS: + return { + ...state, + settings: action.settings + }; default: return state; } diff --git a/app/utils/avatar.js b/app/utils/avatar.js index 0cdc8e577..b3ef7e416 100644 --- a/app/utils/avatar.js +++ b/app/utils/avatar.js @@ -1,7 +1,7 @@ const formatUrl = (url, size, query) => `${ url }?format=png&size=${ size }${ query }`; export const avatarURL = ({ - type, text, size, user = {}, avatar, server, avatarETag, rid + type, text, size, user = {}, avatar, server, avatarETag, rid, blockUnauthenticatedAccess }) => { let room; if (type === 'd') { @@ -16,7 +16,7 @@ export const avatarURL = ({ const { id, token } = user; let query = ''; - if (id && token) { + if (id && token && blockUnauthenticatedAccess) { query += `&rc_token=${ token }&rc_uid=${ id }`; } if (avatarETag) { diff --git a/app/views/CreateDiscussionView/SelectChannel.js b/app/views/CreateDiscussionView/SelectChannel.js index fa7110b94..806e90090 100644 --- a/app/views/CreateDiscussionView/SelectChannel.js +++ b/app/views/CreateDiscussionView/SelectChannel.js @@ -12,7 +12,7 @@ import { themes } from '../../constants/colors'; import styles from './styles'; const SelectChannel = ({ - server, token, userId, onChannelSelect, initial, theme + server, token, userId, onChannelSelect, initial, blockUnauthenticatedAccess, theme }) => { const [channels, setChannels] = useState([]); @@ -31,7 +31,8 @@ const SelectChannel = ({ user: { id: userId, token }, server, avatarETag: item.avatarETag, - rid: item.rid + rid: item.rid, + blockUnauthenticatedAccess }); return ( @@ -61,6 +62,7 @@ SelectChannel.propTypes = { userId: PropTypes.string, initial: PropTypes.object, onChannelSelect: PropTypes.func, + blockUnauthenticatedAccess: PropTypes.bool, theme: PropTypes.string }; diff --git a/app/views/CreateDiscussionView/SelectUsers.js b/app/views/CreateDiscussionView/SelectUsers.js index b9c8ecf9f..e3a404365 100644 --- a/app/views/CreateDiscussionView/SelectUsers.js +++ b/app/views/CreateDiscussionView/SelectUsers.js @@ -15,7 +15,7 @@ import styles from './styles'; import { themes } from '../../constants/colors'; const SelectUsers = ({ - server, token, userId, selected, onUserSelect, theme + server, token, userId, selected, onUserSelect, blockUnauthenticatedAccess, theme }) => { const [users, setUsers] = useState([]); @@ -48,7 +48,12 @@ const SelectUsers = ({ }, 300); const getAvatar = item => avatarURL({ - text: RocketChat.getRoomAvatar(item), type: 'd', user: { id: userId, token }, server, avatarETag: item.avatarETag + text: RocketChat.getRoomAvatar(item), + type: 'd', + user: { id: userId, token }, + server, + avatarETag: item.avatarETag, + blockUnauthenticatedAccess }); return ( @@ -78,6 +83,7 @@ SelectUsers.propTypes = { userId: PropTypes.string, selected: PropTypes.array, onUserSelect: PropTypes.func, + blockUnauthenticatedAccess: PropTypes.bool, theme: PropTypes.string }; diff --git a/app/views/CreateDiscussionView/index.js b/app/views/CreateDiscussionView/index.js index 32d8a6c59..c489b7481 100644 --- a/app/views/CreateDiscussionView/index.js +++ b/app/views/CreateDiscussionView/index.js @@ -39,7 +39,8 @@ class CreateChannelView extends React.Component { failure: PropTypes.bool, error: PropTypes.object, theme: PropTypes.string, - isMasterDetail: PropTypes.bool + isMasterDetail: PropTypes.bool, + blockUnauthenticatedAccess: PropTypes.bool } constructor(props) { @@ -143,7 +144,7 @@ class CreateChannelView extends React.Component { render() { const { name, users } = this.state; const { - server, user, loading, theme + server, user, loading, blockUnauthenticatedAccess, theme } = this.props; return ( ({ failure: state.createDiscussion.failure, loading: state.createDiscussion.isFetching, result: state.createDiscussion.result, + blockUnauthenticatedAccess: state.settings.Accounts_AvatarBlockUnauthenticatedAccess ?? true, isMasterDetail: state.app.isMasterDetail }); diff --git a/app/views/ShareListView/index.js b/app/views/ShareListView/index.js index 1f3a3b7dd..0e43042f8 100644 --- a/app/views/ShareListView/index.js +++ b/app/views/ShareListView/index.js @@ -108,10 +108,13 @@ class ShareListView extends React.Component { return true; } - const { server, theme } = this.props; + const { server, theme, userId } = this.props; if (server !== nextProps.server) { return true; } + if (userId !== nextProps.userId) { + return true; + } if (theme !== nextProps.theme) { return true; } diff --git a/storybook/stories/index.js b/storybook/stories/index.js index e857db496..915944f88 100644 --- a/storybook/stories/index.js +++ b/storybook/stories/index.js @@ -33,6 +33,7 @@ const reducers = combineReducers({ username: 'diego.mello' } }), + share: () => ({ settings: {} }), meteor: () => ({ connected: true }), activeUsers: () => ({ abc: { status: 'online', statusText: 'dog' } }) }); @@ -61,6 +62,7 @@ storiesOf('RoomItem', module) .addDecorator(story => {story()}) .add('list roomitem', () => ); storiesOf('Message', module) + .addDecorator(story => {story()}) .addDecorator(messageDecorator) .add('list message', () => );