Subscription
Posting Feeds
Skapi database provides a subscription feature.
The subscription feature is useful when you want users to subscribe to certain users and fetch feeds from their subscriptions.
With the subscription feature, the uploader can also restrict certain users from accessing specific posts uploaded to the subscription table.
You can let users upload records to the subscription table by setting table.subscription.is_subscription_record to true in postRecord() parameters.
When table.subscription.upload_to_feed is set to true, subscribed users can later fetch all the feeds from all the users they are subscribed to at once using the getFeed() method.
The subscription feature is useful when building a social media platform where users can follow each other's content and fetch their feeds.
With the subscription feature, users can also block certain users from accessing their subscription group records.
The subscription feature can track subscriber counts, manage feeds, and send mass notifications, etc.
Let's assume user 'A' uploads a record in table 'Posts' with subscription group 1.
// User 'A' uploads record in subscription table.
skapi.postRecord(null, {
table: {
name: "Posts",
access_group: "authorized",
subscription: {
is_subscription_record: true
},
},
});To allow other users to access records that require a subscription, they must first subscribe to the uploader using the subscribe() method:
WARNING
Subscribers will not get feeds that are posted prior to the subscription.
WARNING
Anonymous (unsigned) users cannot create subscription records.
Subscribing
WARNING
User must be logged in to call this method.
Let's assume user 'B' wants to access user 'A''s subscription records. User 'B' will need to subscribe to user 'A'.
// User 'B' subscribes to user 'A'.
skapi.subscribe({
user_id: "user_id_of_user_A",
get_feed: true, // Required to enable the get_feed method
});TIP
To use the getFeed() method later, be sure to include the parameter get_feed: true shown in subscribe(option): Promise<string>
Once the user 'B' has subscribed to user 'A', user 'B' can now have access to the records in that subscription table.
// User 'B' now can get records that requires subscription of user 'A'
skapi
.getRecords({
table: {
name: "Posts",
access_group: "authorized",
subscription: "user_id_of_user_A",
},
})
.then((response) => {
console.log(response.list); // All posts user 'A' uploaded to table 'Posts' in subscription group 1.
});TIP
The number of subscribers for a user is tracked in the UserPublic object, which can be retrieved using the getUsers() method.
DANGER
table.subscription.upload_to_feed should be set to true for record to show up in subscription table.
subscribe(option): Promise<Subscription>
Unsubscribing
WARNING
User must be logged in to call this method.
Users can unsubscribe from subscription group 1 using the unsubscribe() method.
// User 'B'
skapi.unsubscribe({
user_id: "user_id_of_user_A",
});For more detailed information on all the parameters and options available with the unsubscribe() method, please refer to the API Reference below:
unsubscribe(option): Promise<string>
WARNING
After unsubscribing, subscription information may need some time to update (usually almost immediate).
Blocking and Unblocking Subscribers
WARNING
User must be logged in to call this method.
One of the benefits of the subscription feature is that users can block certain users from accessing their subscription-level records. But, as mentioned above, subscriptions are not meant to be used as a security restriction.
Even when a user has blocked certain users, those users still have access to files attached to the records, since file access is not restricted by subscription unless the file is private or uses a higher access group than the accessing user.
Other than files, blocked users will not have access to any record data at the subscription access level.
To block a subscriber, user can call the blockSubscriber() method:
Blocking a Subscriber
// User 'A' blocks user 'B' from accessing all subscription group 1.
skapi
.blockSubscriber({
user_id: "user_id_of_user_B",
})
.then((res) => {
// User 'B' no longer have access to user A's subscription group 1.
});For more detailed information on all the parameters and options available with the blockSubscriber() method, please refer to the API Reference below:
blockSubscriber(option): Promise<string>
Unblocking a Subscriber
// User 'A' unblocks user 'B' from subscription group 1.
skapi
.unblockSubscriber({
user_id: "user_id_of_user_B",
})
.then((res) => {
// User 'B' now has access to user A's subscription group 1.
});For more detailed information on all the parameters and options available with the unblockSubscriber() method, please refer to the API Reference below:
unblockSubscriber(option): Promise<string>
Listing subscriptions
The getSubscriptions() method retrieves subscription information from the database.
params:
subscriber: The user ID of the subscriber.subscription: The user ID of the uploader and the subscription group.blocked: Set totrueto only retrieve blocked subscriptions.
Either the params.subscriber or params.subscription value must be provided.
Examples
/**
* Retrieve all subscription information where user 'B' is the subscriber
*/
skapi
.getSubscriptions({
subscriber: "userB_user_id",
})
.then((response) => {
console.log(response.list);
});
/**
* Retrieve all subscription information where user A is being subscribed to
*/
skapi
.getSubscriptions({
subscription: "userA_user_id",
})
.then((response) => {
console.log(response.list);
});
/**
* Check if user 'B' is subscribed to user 'A'
*/
skapi
.getSubscriptions({
subscriber: "userB_user_id",
subscription: "userA_user_id",
})
.then((response) => {
console.log(response.list?.[0]);
});For more detailed information on all the parameters and options available with the getSubscriptions() method, please refer to the API Reference below:
getSubscriptions(params, fetchOptions?): Promise<DatabaseResponse<Subscription>>
Getting Feed
The getFeed() method retrieves the user's feed.
This method retrieves feed records from users the current user subscribes to.
You can use this method to build a feed page for the user.
Examples
First, the user 'A' must upload a record as a feed.
// User 'A' uploads record as a feed.
skapi.postRecord(null, {
table: {
name:'Posts',
access_group: 'authorized',
subscription: {
upload_to_feed: true
}
}})Then user 'B', who is subscribed to user 'A', can fetch the uploaded feeds from users they subscribe to.
/**
* User 'B' Retrieves all feed of access_group: "authorized"
*/
skapi.getFeed({access_group: 'authorized'}).then((response) => {
console.log(response.list); // Fetch all feed records in the 'authorized' access group from users userB subscribes to.
});DANGER
If the record was NOT uploaded with table.subscription.upload_to_feed set to true, it will not show up in the feed.
DANGER
When a user unsubscribes from another user, all past records from that user will no longer show in their feed.
WARNING
Users only see feed records from the time they subscribed onward.
