Disable editing for other users while someone is editing

Hi!
I’m working on a Phoenix Liveview project and in this project I have a “Post” that contains title and content field.
Now, this post is publically available and can be edited by any authenticated users. However I want to allow only one person to edit the post at a time. So if someone is currently editing the post, other users won’t be able to edit the Post(I’ll show a “Someone is editing” message).
But I’m not exactly sure how to approach this problem. I have thought of two possible solutions:

  1. Disable editing on database level: I’m using ecto and postgres to save the post. I can set an edit flag to true when someone is editing and, on UI side I’ll check for this flag and disable/enable editing. However, it has one shortcoming. I’m not sure when to set this flag back to false. Let’s say someone is currently editing the form, and they just close the tab(without saving or anything) then how will the edit flag be set to false?

  2. Using Phoenix presence: I can use Phoenix presence to track the user when they edit the form. I can then use Presence.list to know if someone is editing the post or not. This will handle user suddenly dropping off without saving anything. However I’m not sure if this solution is viable or not?

I wanted to ask the community on how they would approach this problem? And some possible solutions to this problem?

Thanks and have a nice day!

I have used a presence-feature to accomplish this in a company I worked at. It was enough for the support staff to know that someone else was viewing the page. So that solved the problem and it would be more error prone to lock the form.

2 Likes

Here’s a related thread that may be helpful

1 Like

@andreaseriksson @codeanpeace Thank you! I went ahead with Presence approach.

Basically, when on edit, I track the user through presence. Others get notified through PubSub that a user is editing the Post, so I disable the editing feature for the time being. When user leaves, again through PubSub, I enable edit. Presence.handle_metas is where I broadcast PubSub messages from.

It took less effort than I thought(Phoenix is :heart:).

1 Like