One of the first things you might need to do in Sportstalk is init a User. Users are shared between chat and commenting in the same application.
To create a user, you can use either the chat or comment clients, or a UserService (advanced).
constcommentClient=sdk.CommentClient.init({...});commentClient.createOrUpdateUser({userid:"definedByYourSystem-MustBeUnique",userToken:'yourUserJWT',handle:"Must-Be-Unique-String"}).then(function(user){// user has been created.}).catch(function(error){// make sure to catch and handle errors.// It is possible to have network or settings errors.// For instance if you do not set a unique handle you will get an error.})
Most users will want to just find and join a conversation created by an admin in the sportstalk dashboard.
To list conversations, use the listConversations() method of the CommentsClient, like so:
constresponse=commentClient.listsConversations();constconversations=response.conversations;// Array of Conversation objectsconstcursor=response.cursor;// used for scrolling through long lists of conversations.
Powering your UI with this data is up to you, but you might do something like so (in pug template format):
h3 Conversations
uleach conversation in conversations
li=conversation.titlespan.id=conversation.id
To join a conversation, you will need a user, please see the section above about creating a user first.
Once you have a user, joining a conversation is simple:
asyncfunctionshowJoinConversation(){constuser=awaitcommentClient.createOrUpdateUser({userid:"definedByYourSystem-MustBeUnique",handle:"Must-Be-Unique-String",userToken:'User_jwt_token'})// this will automatically set the user, but you can also set the user manuallycommentClient.setUser(user);constlist=awaitcommentClient.listConversations();constconversations=list.conversations;// Let's join the first conversation in the listcommentClient.setCurrentConversation(conversations[0]);// you should ensure there are conversations first to avoid a null error// You are now able to get a list of recent commentsletcomments=awaitcommentClient.getComments();// let's make our own comment!constmycomment=awaitcommentClient.comment("This is my comment on this conversation!");// let's see the comment in the listcomments=awaitcommentClient.getComments();// my comment will be included unlesss there was an error}
constsdk=require('sportstalk-sdk');constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});asyncfunctioncreateConversation(){try{constconversation=awaitclient.createConversation({title:"My conversation",property:"Optional-property-string",moderation:"pre",// can also be 'postmaxreports:3,// can be as low as 0.open:true,//set to false if you don't want comments until a future point.});}catch(e){// Network error, permissions error, etc. The error message will tell you what is wrong.}// conversation will be created or an error will be thrown.}
Create a new conversation that others can join and add comments.
Create a new user or update an existing one. You need a user to be set for some operations.
By default, setDefault is TRUE, meaning that if you create or update a user, that will be the user used for commenting.
asyncfunctioncreateOrUpdateUserExampleFunction(){constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});constuser=awaitclient.createOrUpdateUser({userid:"UniqueStringId",handle:"UniqueButReadable",displayname:"A pretty string purely for display.",pictureurl:"A full url to an image to be used by chat applications for an avatar, e.g. https://...."profileurl:"A full url to a user's profile or webpage"});// user will be created. if the userid already exists that user will be updated.}
asyncfunctionsetCurrentConversationExampleFunction(){constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});constconversation=awaitclient.createConversation({conversationid:'my-conversation-id',property:'TEST',moderation:'pre',maxreports:3,title:'Demo conversation',open:true,},false);// can also do it this way.letcurrentConversation=client.getCurrentConversation();// currentConversation is Nullclient.setCurrentConversation(conversation);currentConversation=client.getCurrentConversation();if(currentConversation===conversation){console.log("They are the same!")// this will print.}}
Gets the current conversation. Will be null or undefined if there is no current conversation.
asyncfunctiongetCurrentConversationExampleFunction(){constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});constconversation=awaitclient.createConversation({conversationid:'my-conversation-id',property:'TEST',moderation:'pre',maxreports:3,title:'Demo conversation',conversationisopen:true,},true);// can also do it this way.consttheSameConversation=client.getCurrentConversation();if(theSameConversation===conversation){console.log("They are the same!")// this will print.}}
Retrieves data about a specific conversation from the server.
asyncfunctiongetConversationExampleFunction(){constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});constconversation=awaitclient.createConversation({conversationid:'my-conversation-id',property:'TEST',moderation:'pre',maxreports:3,title:'Demo conversation',conversationisopen:true,},false);// can also do it this way.constconversationFromServer=client.getConversation('my-conversation-id');}
constsdk=require('sportstalk-sdk');asyncfunctiondeleteConversationExampleFunction(){constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});constconversation=awaitclient.createConversation({conversationid:'my-conversation-id',property:'TEST',moderation:'pre',maxreports:3,title:'Demo conversation',conversationisopen:true,},false);// can also do it this way.constdeletionResponse=awaitclient.deleteConversation(conversation);}
Make a comment on the current conversation. Will throw an error if a conversation is not set.
constsdk=require('sportstalk-sdk');asyncfunctioncreateCommentExampleFunction(){constclient=sdk.CommentClient.init({appId:'your-app-id',apiToken:'your-api-token'});constconversation=awaitclient.createConversation({conversationid:'my-conversation-id',property:'TEST',moderation:'pre',maxreports:3,title:'Demo conversation',conversationisopen:true,},true);// second parameter sets this as default// can also do it this way.client.setCurrentConversation(conversation);constuser=awaitclient.createOrUpdateUser({userid:'someuserid',handle:'testuser'});constcomment=client.makeComment('This is a comment');}
Retrieves a specific comment. The param can either be a comment object with an id or just the id.
constsdk=require('sportstalk-sdk');asyncfunctiongetCommentExampleFunction(){constclient=sdk.CommentClient.init({appId:'yourappId',apiToken:'your-api-token'});constconversation=awaitclient.createConversation({conversationid:'my-conversation-id',property:'TEST',moderation:'pre',maxreports:3,title:'Demo conversation',conversationisopen:true,},true);// second parameter sets this as default// can also do it this way.client.setCurrentConversation(conversation);constuser=awaitclient.createOrUpdateUser({userid:'someuserid',handle:'testuser'});constcomment=client.makeComment('This is a comment');}
Comment Client API
Getting Started
If you are looking to build a custom conversation, you will the need the CommentClient, which you can get by:
Typescript imports:
Using Require:
Creating a user
One of the first things you might need to do in Sportstalk is init a User. Users are shared between chat and commenting in the same application. To create a user, you can use either the chat or comment clients, or a UserService (advanced).
Finding and joining a conversation
Most users will want to just find and join a conversation created by an admin in the sportstalk dashboard.
To list conversations, use the listConversations() method of the CommentsClient, like so:
Powering your UI with this data is up to you, but you might do something like so (in pug template format):
To join a conversation, you will need a user, please see the section above about creating a user first. Once you have a user, joining a conversation is simple:
Comment Client
Creating a CommentClient
setConfig()
Updates the client configuration. Usually you should just create a new client.
getConfig(): SportsTalkConfig
Returns the current configuration object
createConversation (conversation: Conversation, setDefault: boolean)
Create a new conversation that others can join and add comments.
createOrUpdateUser (user: User, setDefault?:boolean): Promise(User)
Create a new user or update an existing one. You need a user to be set for some operations. By default, setDefault is TRUE, meaning that if you create or update a user, that will be the user used for commenting.
setCurrentConversation(conversation)
Set the current conversation for commenting. The parameter can either be a conversation object or just a conversation ID.
You can see the Conversation and Comments models in this file: https://gitlab.com/sportstalk247/sdk-javascript/-/blob/master/src/models/CommentsModels.ts
getCurrentConversation(): Conversation | null | undefined
Gets the current conversation. Will be null or undefined if there is no current conversation.
getConversation(conversation: Conversation | string): Promise<Conversation>
Retrieves data about a specific conversation from the server.
deleteConversation(conversation: Conversation | string)
Deletes a conversation
makeComment(comment: string, replyto?: Comment | string)
Make a comment on the current conversation. Will throw an error if a conversation is not set.
getComment(comment: Comment | string): Promise<Comment | null>
Retrieves a specific comment. The param can either be a comment object with an id or just the id.
deleteComment(comment:Comment | string, final: boolean): Promise<CommentDeletionResponse>
Deletes a comment
updateComment(comment:Comment)
Updates a comment
reactToComment(comment:Comment | string, reaction:Reaction)
Reacts to a comment
voteOnComment(comment:Comment | string, vote:Vote)
Vote a comment up or down
reportComment(comment:Comment | string, reportType: ReportType)
Report a comment for violating community rules.
getCommentReplies(comment:Comment, request?: CommentRequest)
Get replies to a comment
getComments(request?: CommentRequest, conversation?: Conversation)
Gets the latest comments for the default conversation.
listConversations(filter?: ConversationRequest)
List conversations that are available to comment.