Mike de GeofroySoftware Engineer

My attempt at building a telegram filesystem

Intro

I've always wanted to win a hackathon by using the most random tech stack possible, just to show that building products really isn't about the technologies you use, but how you leverage them and what you build with them.

An example of things we considered: Sratch, В☦☦, Huecker.io, Yoptascript, MS Paint, using Telegram's saved messages as a database.

Using telegram saved messages as a database... hmm... What if you could mount a telegram chat onto your local filesystem?

Well hey, it's:

But Mike, there are ready solutions like google drive or amazon s3 out there? why not just use them? Well, it's just not that fun.

I conveniently got a similar labwork assignment, the idea is to use FUSE, to mount something to my local filesystem, well, it's been kind of a meme in ITMO for a while to use Saved Messages in telegram as a database.. So well, sounded perfect for me and my @Etozherraf.

How to mount telegram to my filesystem

Ok well, we are going to be using fuse or filesystem in userspace, here's how it works.

So apparently the whenever you call a command like ls or cd from the command line, if that happens to be in a directory where fuse is mounted to, it will redirect the call to your application and you can decide what to do with it.

This let's you do literally anything, check this video out

cool cool cool cool cool... what are we writing it in? c++. why? because I hate myself.

The Plan

My teammate is on a windows computer, this means that we need a shared environment to be able to develop in more or less one environment. So I'm using Docker and wrote this Dockerfile... and this makefile.. you can now use make up make down make shell, to work with the docker container. Neat.

I got fuse working just fine, and now, I'm going to let my teammate figure out how to correctly implement these functions, while I'm going to work on the part that works with telegram.

Telegram integration

Here, this is me trying to figure out how to use telegram database library. What a shitshow. Here, have a look at the examples.

I basically took this example, stripped it off all of the things that I don't need, and then I wrote my own handlers.. See, I wanted to make a request inside of a request, the thing is, the requests are handled by the request handler, this one is running in it's own thread, and it runs the handlers of each request in it's therad, that sucks, why? Well let's say I want to make a request in a request. The reqest_handler will receive the first request, call it's handler, but that handler, will make a new request, that request won't get completed until it's handled, but the handler is waiting for request 1 to complete. That's called a deadlock. So how do we fix this? Well, honestly I don't know, but here's what I made. Each request is now a class, and basically each class has it's own thread, and whenever we call a new request, it get's executed in that thread, this means that the request_handler can just call the handle, and then continue on handling incoming requests. Cool huh? Well maybe there's a solution using thread pools and shit like that.. I just decided to go with this, it looked like the simplest approach.

Let's check out how the fuse part is going.. oh.. 2 functions... alright.

Well now that we got the request's sorted out we have to think on how to actually parse the telegram messages into filesystem entities.

More

Contact me on Telegram. You can see more of my code on GitHub.