Skip to content

How SketchCollab works

Iyxan edited this page Feb 4, 2021 · 6 revisions

How does SketchCollab works?

Let's use FAQs because I don't really know how to explain everything in one text :P

(If you asked something in the issues page, I'll add that question and the answer here)

How can SketchCollab know which project is a SketchCollab project (or project that has been uploaded to SketchCollab)?

So, at first glance, the solution would be to save project IDs into local space (e.g. SharedPreferences). This is a simple and straightforward idea, but the thing is, that the project IDs can change overtime and the project can be sent to other people making the user need to painstakingly change the reference to the project manually.

But, SketchCollab's approach is different. This approach is resistant of ID change and project removal, So, SketchCollab stores it's project key and informations regarding about the project inside mysc/list/{project_id}/project. If the project ID is changed, these keys remain unchanged, when you send a SketchCollab project, these keys will still remain, (well except if you removed it :l).

Here is the code snippet applying that custom keys.

What are the keys, and what do they do?

Here are the keys (yes, you can check it out right now on your project):

  • sk-collab-key - This key stores the project key of that project. For example, if this project is uploaded to SketchCollab with the key "ybgqwfNUkygfA", then the project key will be that key. This is used to access and compare the project from the database with the local project.
  • sk-collab-owner - This key stores the author of the project itself, This is used so we can check the project is owned by who. Well, at first you might think that the user can just edit the author, yes, we know, But it's not going to be a big of a difference. The real and trusted author key is stored in the database. If they changed the author key to be their own UID, they still can't edit the project (create commits) as we checks if the key is the same as the project author in the database, not if the key is the same as the current user uid.
  • sk-collab-latest-commit - This key stores the latest commit ID the project has got / applied, This is used so we can know how updated the project is. Example, if a project is at commit number (not id) 3, and in the server it's at commit number 4. This project is outdated and will need to be synced with the server.
  • sk-collab-project-visibility - This key can only be "public" or "private", This key indicates if the project is well, private or public, Simple. "Why?" Well, we needed to know where the project is located, public projects are stored in the /projects collection, whilst private projects are stored on userdata/{uid}/projects collection, This key reduces our time to check at where this project is located. Note that this has nothing to do with the project's visibility itself, If the user manually changed it, nothing will happen (except that SketchCollab will fail to locate the project).

How did SketchCollab implement commiting?

Commiting is actually simple. For those who didn't know, commiting is basically submitting a patch to a repository / project.

So, basically, we just made a subcollection of a project document named commits. Everytime you do a commit, A new document (with auto ID) will be added to this collection. These commits should be sorted by timestamp so when we clone a project, it will apply the patch into the right order.

How can SketchCollab generate a patch?

Using the DiffMatchPatch library.

Here is a code snippet of using it.

How did SketchCollab detect if a project is changed?

SketchCollab uses hash algorithm (sha512) to hash a project into a string with a fixed length. When something is changed in the project, the hash will change and that's how we detect the changes. The hash is compared with the latest commit of the project in the database.

Clone this wiki locally