Fix sending empty picture when updating identity without updated picture

Closes #188

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2019-10-08 19:32:48 +02:00
parent 5e110fd042
commit 89c7a3854b
3 changed files with 46 additions and 6 deletions

View File

@ -11,16 +11,19 @@ query($username: String!) {
preferredUsername, preferredUsername,
suspended, suspended,
avatar { avatar {
id,
name, name,
url url
}, },
banner { banner {
id,
url url
}, },
feedTokens { feedTokens {
token token
}, },
organizedEvents { organizedEvents {
id,
uuid, uuid,
title, title,
beginsOn beginsOn
@ -40,6 +43,7 @@ export const GET_PERSON = gql`
preferredUsername, preferredUsername,
suspended, suspended,
avatar { avatar {
id,
name, name,
url url
}, },
@ -50,6 +54,7 @@ export const GET_PERSON = gql`
token token
}, },
organizedEvents { organizedEvents {
id,
uuid, uuid,
title, title,
beginsOn beginsOn
@ -63,6 +68,7 @@ query {
loggedPerson { loggedPerson {
id, id,
avatar { avatar {
id,
url url
}, },
preferredUsername, preferredUsername,
@ -109,6 +115,7 @@ query LoggedUserParticipations($afterDateTime: DateTime, $beforeDateTime: DateTi
name, name,
domain, domain,
avatar { avatar {
id,
url url
} }
}, },
@ -129,6 +136,7 @@ query LoggedUserParticipations($afterDateTime: DateTime, $beforeDateTime: DateTi
name, name,
domain, domain,
avatar { avatar {
id,
url url
} }
} }
@ -156,6 +164,7 @@ export const LOGGED_USER_DRAFTS = gql`
name, name,
domain, domain,
avatar { avatar {
id,
url url
} }
}, },
@ -176,6 +185,7 @@ query {
identities { identities {
id, id,
avatar { avatar {
id,
url url
}, },
preferredUsername, preferredUsername,
@ -196,6 +206,7 @@ mutation CreatePerson($preferredUsername: String!, $name: String!, $summary: Str
name, name,
summary, summary,
avatar { avatar {
id,
url url
} }
} }
@ -215,6 +226,7 @@ export const UPDATE_PERSON = gql`
name, name,
summary, summary,
avatar { avatar {
id,
url url
}, },
} }

View File

@ -22,3 +22,17 @@ export function buildFileVariable<T>(file: File | null, name: string, alt?: stri
}, },
}; };
} }
export function readFileAsync(file: File): Promise<string|ArrayBuffer|null> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsBinaryString(file);
});
}

View File

@ -94,7 +94,7 @@ import PictureUpload from '@/components/PictureUpload.vue';
import { MOBILIZON_INSTANCE_HOST } from '@/api/_entrypoint'; import { MOBILIZON_INSTANCE_HOST } from '@/api/_entrypoint';
import { Dialog } from 'buefy/dist/components/dialog'; import { Dialog } from 'buefy/dist/components/dialog';
import { RouteName } from '@/router'; import { RouteName } from '@/router';
import { buildFileFromIPicture, buildFileVariable } from '@/utils/image'; import { buildFileFromIPicture, buildFileVariable, readFileAsync } from '@/utils/image';
import { changeIdentity } from '@/utils/auth'; import { changeIdentity } from '@/utils/auth';
@Component({ @Component({
@ -198,9 +198,11 @@ export default class EditIdentity extends Vue {
async updateIdentity() { async updateIdentity() {
try { try {
const variables = await this.buildVariables();
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation: UPDATE_PERSON, mutation: UPDATE_PERSON,
variables: this.buildVariables(), variables,
update: (store, { data: { updatePerson } }) => { update: (store, { data: { updatePerson } }) => {
const data = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES }); const data = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES });
@ -225,9 +227,11 @@ export default class EditIdentity extends Vue {
async createIdentity() { async createIdentity() {
try { try {
const variables = await this.buildVariables();
await this.$apollo.mutate({ await this.$apollo.mutate({
mutation: CREATE_PERSON, mutation: CREATE_PERSON,
variables: this.buildVariables(), variables,
update: (store, { data: { createPerson } }) => { update: (store, { data: { createPerson } }) => {
const data = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES }); const data = store.readQuery<{ identities: IPerson[] }>({ query: IDENTITIES });
@ -305,10 +309,20 @@ export default class EditIdentity extends Vue {
.replace(/[^a-z0-9._]/g, ''); .replace(/[^a-z0-9._]/g, '');
} }
private buildVariables() { private async buildVariables() {
const avatarObj = buildFileVariable(this.avatarFile, 'avatar', `${this.identity.preferredUsername}'s avatar`); const oldAvatarFile = await buildFileFromIPicture(this.identity.avatar);
const oldAvatarFileContent = await readFileAsync(oldAvatarFile);
const newAvatarFileContent = await readFileAsync(this.avatarFile);
return Object.assign({}, this.identity, avatarObj); const avatarObj = buildFileVariable(this.avatarFile, 'avatar', `${this.identity.preferredUsername}'s avatar`);
const res = Object.assign({}, this.identity, avatarObj);
/**
* If the avatar didn't change, no need to try reuploading it
*/
if (oldAvatarFileContent === newAvatarFileContent) {
res.avatar = {};
}
return res;
} }
private async redirectIfNoIdentitySelected (identityParam?: string) { private async redirectIfNoIdentitySelected (identityParam?: string) {