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?

1) 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.

2) SCRUD functions

Share a resource


  1. A resource can be shared several times in a space. (interactions should be encouraged, spam can be handled by moderators)


  1. Insert SharedResource_SR row

Unshare a resource


  1. Can't unshare someone else's shared resource.


  1. 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


SELECT, 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 = WHERE sr.fk_space_s_id = '{{given space id}}'


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.