UCL / kapta-mobile

Mobile Progressive Web App enabling users to generate maps from WhatsApp chats
Apache License 2.0
1 stars 1 forks source link

Make contribution ID consistent from one whatsapp chat import to another #129

Open tcouch opened 2 weeks ago

tcouch commented 2 weeks ago

Currently contributionid is a uuid generated at random for each observation each time a whatsapp chat is exported into Kapta Mobile. In order to deal with the problem of duplicates from repeated uploads however, it would be better to generate an id from attributes which are consistent from one export to the next. That's because the uploadAPI uses contributionid as the filename in the S3 bucket so we'd be overwriting/updating observations that have already been uploaded as opposed to creating new files.

tcouch commented 2 weeks ago

To make it unique, it'd have to be a combination of message.sender and message.datetime. I did consider including groupName, but that's too easy to change. I'm not sure what happens if a message sender changes their username if that updates old messages in the chat, in which case even message.sender isn't reliable, but there isn't really an alternative.

There doesn't appear to be a built in way to generate hashes, but this code snippet from blogpost looks like one way to do it:

export const sha256 = async (text: string): Promise<string> => {
  const encoder = new TextEncoder();
  const data = encoder.encode(text);
  const hash = await crypto.subtle.digest('SHA-256', data);

  return Array.from(new Uint8Array(hash))
    .map((byte) => byte.toString(16).padStart(2, '0'))
    .join('');
};