How to This: Share Feature
User story: A user U can share a resource R owned by user O to a space S.
Description: How can I can reproduce Facebook’s share feature?
- Relational database
Table User_U: id
Table Resource_R: id, fk_user_o_id (foreign key to user O)
Table SharedResource_SR: id, fk_resource_id (foreign key to resource R), fk_user_u_id (foreign key to user U), fk_space_s_id (foreign key to space S), shared_at (datetime)
Table Space_S: id
Observations: We go with a denormalized schema. The data redundancy is quite small (a regular user shares a given resource in a space once or twice) so we will prefer increasing the Read performance.
- SCRUD functions
Share a resource
Requirements:
- A resource can be shared several times in a space. (interactions should be encouraged, spam can be handled by moderators)
Pseudo-algorithm:
- Insert SharedResource_SR row
Unshare a resource
Requirements:
- Can’t unshare someone else’s shared resource.
Pseudo-algorithm:
- delete the SharedResource_SR row by id where the fk_user_u_id field and the requesting user’s id correspond
Get all resources shared in a given space
SQL:
SELECT r.id, IF(sr.fk_user_u_id = ‘{{user_id}}’, true, false) AS can_unshare FROM Resource_R as r JOIN SharedResource_SR AS sr ON sr.fk_resource_id = r.id WHERE sr.fk_space_s_id = ‘{{given space id}}’
Observations:
In this SQL statement we add a condition to tell the view whether or not the current user can Unshare a given text. If that’s the case, we will display an Unshare button when it’s appropriate.