Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/definitions/rest/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ export type UsersEndpoints = {
'users.deleteOwnAccount': {
POST: (params: { password: string; confirmRelinquish: boolean }) => { success: boolean };
};
'users.setAvatar': {
POST: (params: { avatarUrl: string }) => { success: boolean };
};
};
5 changes: 4 additions & 1 deletion app/lib/methods/helpers/fileUpload/Upload.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class Upload {
type: string | undefined;
name: string | undefined;
} | null;
private fieldName: string;
private headers: { [key: string]: string };
private formData: any;
private uploadTask: FileSystem.UploadTask | null;
Expand All @@ -19,6 +20,7 @@ export class Upload {
constructor() {
this.uploadUrl = '';
this.file = null;
this.fieldName = 'file';
this.headers = {};
this.formData = {};
this.uploadTask = null;
Expand All @@ -37,6 +39,7 @@ export class Upload {

public appendFile(item: IFormData): void {
if (item.uri) {
this.fieldName = item.name;
this.file = { uri: item.uri, type: item.type, name: item.filename };
} else {
this.formData[item.name] = item.data;
Expand All @@ -56,7 +59,7 @@ export class Upload {
headers: this.headers,
httpMethod: 'POST',
uploadType: FileSystem.FileSystemUploadType.MULTIPART,
fieldName: 'file',
fieldName: this.fieldName,
mimeType: this.file.type,
parameters: this.formData
},
Expand Down
45 changes: 41 additions & 4 deletions app/lib/services/restApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { settings as RocketChatSettings } from '@rocket.chat/sdk';

import {
type IAvatarSuggestion,
type IMessage,
Expand Down Expand Up @@ -25,7 +27,7 @@ import { compareServerVersion, getBundleId, isIOS } from '../methods/helpers';
import { getDeviceToken } from '../notifications';
import { store as reduxStore } from '../store/auxStore';
import sdk from './sdk';
import fetch from '../methods/helpers/fetch';
import FileUpload from '../methods/helpers/fileUpload';

export const createChannel = ({
name,
Expand Down Expand Up @@ -700,14 +702,49 @@ export const resetAvatar = (userId: string) =>
export const setAvatarFromService = ({
data,
contentType = '',
service = null
service = null,
url,
server,
user
}: {
data: any;
contentType?: string;
service?: string | null;
}): Promise<void> =>
url?: string;
server: string;
user: { id?: string; token?: string };
}): Promise<any> => {
// RC 0.51.0
sdk.methodCallWrapper('setAvatarFromService', data, contentType, service);
if (service === 'url') {
return sdk.post('users.setAvatar', { avatarUrl: data });
}
if (service === 'upload' && url && server && user?.id && user?.token) {
const rawExtension = url.split('.').pop() || 'png';
const extension = rawExtension.split('?')[0].toLowerCase();

const formData = [
{
name: 'image',
type: `image/${extension}`,
filename: `image.${extension}`,
uri: url
}
];

const headers = {
...RocketChatSettings.customHeaders,
'Content-Type': 'multipart/form-data',
'X-Auth-Token': user.token,
'X-User-Id': user.id
};

const upload = new FileUpload(`${server}/api/v1/users.setAvatar`, headers, formData);

return upload.send();
}

return sdk.methodCallWrapper('setAvatarFromService', data, contentType, service);
};

export const getUsernameSuggestion = () =>
// RC 0.65.0
Expand Down
11 changes: 6 additions & 5 deletions app/views/ChangeAvatarView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ const ChangeAvatarView = () => {
const [state, dispatch] = useReducer(reducer, initialState);
const [saving, setSaving] = useState(false);
const { colors } = useTheme();
const { userId, username, server } = useAppSelector(
const { userId, username, server, user } = useAppSelector(
state => ({
userId: getUserSelector(state).id,
username: getUserSelector(state).username,
server: state.server.server
server: state.server.server,
user: state.login.user
}),
shallowEqual
);
Expand Down Expand Up @@ -162,7 +163,7 @@ const ChangeAvatarView = () => {
await changeRoomsAvatar(room.rid, state?.data);
} else if (state?.url) {
// Change User's Avatar
await changeUserAvatar(state);
await changeUserAvatar(state, server, { id: user.id, token: user.token });
} else if (state.resetUserAvatar) {
// Change User's Avatar
await resetUserAvatar(userId);
Expand All @@ -185,7 +186,7 @@ const ChangeAvatarView = () => {
cropperAvoidEmptySpaceAroundImage: false,
cropperChooseText: I18n.t('Choose'),
cropperCancelText: I18n.t('Cancel'),
includeBase64: true
includeBase64: false
};
try {
const response: Image =
Expand All @@ -194,7 +195,7 @@ const ChangeAvatarView = () => {
: await ImagePicker.openPicker(options);
dispatchAvatar({
type: AvatarStateActions.CHANGE_AVATAR,
payload: { url: response.path, data: `data:image/jpeg;base64,${response.data}`, service: 'upload' }
payload: { url: response.path, service: 'upload' }
});
} catch (error: any) {
if (error?.code !== 'E_PICKER_CANCELLED') {
Expand Down
4 changes: 2 additions & 2 deletions app/views/ChangeAvatarView/submitServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const changeRoomsAvatar = async (rid: string, roomAvatar: string | null)
}
};

export const changeUserAvatar = async (avatarUpload: IAvatar) => {
export const changeUserAvatar = async (avatarUpload: IAvatar, server: string, user: { id?: string; token?: string }) => {
try {
await setAvatarFromService(avatarUpload);
await setAvatarFromService({ ...avatarUpload, server, user });
} catch (e) {
return handleError(e, 'changing_avatar');
}
Expand Down