There must be a clear statement of the injustice which must be corrected. There should be a hearing where all involved can state their view of the situation. The accused should be allowed representation. Judgements should be made by persons who are at some level uninvolved with the conflict and committed to balanced interpretation of evidence. Any punishments should be related to the nature and scale of the injustice. And so on--of course someone with more direct experience could summarize this and make it happen better than I.
The point is that mob justice is not justice and all you have to do is give it some basic rules and constraints and it either gets deflected or gathers up into something that has actual meaning.