It's probably a good idea to extract the generator code into a separate file.
That will make it easier to test and maintain. However it does not solve the fundamental issue which is that the requirements for a database id are not the same as the requirements for a Permalink id. Yet the output is being used for both. Database id: - needs to be unique within a database for all possible messages otherwise some messages cannot be stored (this is intrinsic to ElasticSearch) - it does not need to be unique across databases - it does not need to be preserved if the database has to be reloaded. Permalink id: - once a Permalink has been published, it must always be possible to find the message - if a mail archive has to be recreated, it must be possible to generate the same id - ideally, every id should be distinct - ideally, every message should have an id.
