2020-08-26 15:44:50 -07:00
|
|
|
import { DataModelName } from '@datahub/data-models/constants/entity';
|
|
|
|
import { getApiRoot, ApiVersion } from '@datahub/utils/api/shared';
|
|
|
|
import { getJSON, postJSON } from '@datahub/utils/api/fetcher';
|
|
|
|
import { encodeUrn } from '@datahub/utils/validators/urn';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a url for a specific entity by urn and the social action we are constructing
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we are making this request
|
|
|
|
* @param {string} urn - identifier for the specific entity for which we want to construct the url
|
|
|
|
* @param {string} action - the kind of action we are creating
|
|
|
|
*/
|
|
|
|
const getSocialActionsUrl = (entityType: DataModelName, urn: string, action: string): string =>
|
|
|
|
`${getApiRoot(ApiVersion.v2)}/${entityType}/${encodeUrn(urn)}/${action}`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Using the socialActionsUrl as a base, create a function that constructs one specifically for
|
|
|
|
* like actions
|
|
|
|
* @param {DataModelName} entityType - type of entity for which we want to construct this url
|
|
|
|
* @param {string} urn - urn identifier for the specific entity for which we are constructing the url
|
|
|
|
*/
|
|
|
|
const getLikeActionsUrl = (entityType: DataModelName, urn: string): string =>
|
|
|
|
getSocialActionsUrl(entityType, urn, 'likes');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an entity type and urn, construct a getter for which to retrieve likes information
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to read like information
|
|
|
|
* @param {string} urn - the identifier for the entity for which we want to read like information
|
|
|
|
*/
|
2020-10-13 11:40:01 -07:00
|
|
|
export const readLikesForEntity = (entityType: DataModelName, urn: string): Promise<Com.Linkedin.Common.Likes> =>
|
|
|
|
getJSON({ url: getLikeActionsUrl(entityType, urn) });
|
2020-08-26 15:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an entity type and urn, post an update request that adds the user to the list of those who
|
|
|
|
* like the specified entity
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to add a like
|
|
|
|
* @param {string} urn - the identifier for the entity to which to add the user's like action
|
|
|
|
* @return an updated likes aspect for the entity
|
|
|
|
*/
|
2020-10-13 11:40:01 -07:00
|
|
|
export const addLikeForEntity = (entityType: DataModelName, urn: string): Promise<Com.Linkedin.Common.Likes> =>
|
|
|
|
postJSON({ url: `${getLikeActionsUrl(entityType, urn)}/add`, data: {} });
|
2020-08-26 15:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an entity type and urn, post an update request that removes the user to the list of those
|
|
|
|
* who like the specified entity
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to add a like
|
|
|
|
* @param {string} urn - the identifier for teh entity to which to add the user's like action
|
|
|
|
* @return an updated likes aspect for the entity
|
|
|
|
*/
|
2020-10-13 11:40:01 -07:00
|
|
|
export const removeLikeForEntity = (entityType: DataModelName, urn: string): Promise<Com.Linkedin.Common.Likes> =>
|
|
|
|
postJSON({ url: `${getLikeActionsUrl(entityType, urn)}/remove`, data: {} });
|
2020-08-26 15:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Using the socialActionsUrl as a base, create a function that constructs one specifically for
|
|
|
|
* follow actions
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to construct this urn
|
|
|
|
* @param {string} urn - urn identifier for the specific entity instance for which we are
|
|
|
|
* constructing the url
|
|
|
|
*/
|
|
|
|
const getFollowActionsUrl = (entityType: DataModelName, urn: string): string =>
|
|
|
|
getSocialActionsUrl(entityType, urn, 'follows');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an entity type and urn, construct a getter for which to retrieve follows information
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to read follow information
|
|
|
|
* @param {string} urn - the identifier for the entity for which we want to read follow information
|
|
|
|
*/
|
2020-10-13 11:40:01 -07:00
|
|
|
export const readFollowsForEntity = (entityType: DataModelName, urn: string): Promise<Com.Linkedin.Common.Follow> =>
|
|
|
|
getJSON({ url: getFollowActionsUrl(entityType, urn) });
|
2020-08-26 15:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an entity type and urn, construct a getter for which to add the user as a follower
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to add the user as a follower
|
|
|
|
* @param {string} urn - the identifier for the entity for which we want to update follow information
|
|
|
|
* @return an updated follow aspect for the entity, if successful
|
|
|
|
*/
|
2020-10-13 11:40:01 -07:00
|
|
|
export const addFollowForEntity = (entityType: DataModelName, urn: string): Promise<Com.Linkedin.Common.Follow> =>
|
|
|
|
postJSON({ url: `${getFollowActionsUrl(entityType, urn)}/add`, data: {} });
|
2020-08-26 15:44:50 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an entity type and urn, construct a getter for which to remove the user as a follower
|
|
|
|
* @param {DataModelName} entityType - the type of entity for which we want to remove the user as a follower
|
|
|
|
* @param {string} urn - the identifier for the entity for which we want to update follow information
|
|
|
|
* @return an updated follow aspect for the entity, if successful
|
|
|
|
*/
|
2020-10-13 11:40:01 -07:00
|
|
|
export const removeFollowForEntity = (entityType: DataModelName, urn: string): Promise<Com.Linkedin.Common.Follow> =>
|
|
|
|
postJSON({ url: `${getFollowActionsUrl(entityType, urn)}/remove`, data: {} });
|