Skip to content

Latest commit

 

History

History
186 lines (156 loc) · 15 KB

File metadata and controls

186 lines (156 loc) · 15 KB

How to use RocketChat Library for HarmonyOS: A developer’s Guide

1. Introduction

RocketChat is an application interface for server methods and message stream subscriptions. Using this package, third party apps can control and query a Rocket.Chat server instance, via REST APIs. Designed especially for chat automation, this library makes it easy for application developers to provide the best solutions and experience for their community.

This library facilitates communication among colleagues, students, clients, organizations, and others by offering public and private channels (groups) as well as one-on-one conversations. It allows you to manage users, visitors, public channels, and private groups. The RocketChat Library's features enable us to build a powerful and flexible communication platform.

2. Typical Use Cases

RocketChat library, is quite beneficial in the development of apps that we use on a regular basis. It is highly useful for everyday use because we need chat platforms like this for communication, whether it's within organizations or with friends. Some of the typical use cases are mentioned below:

  • Chat Application use case:
    This use case makes it easy to click on a created / joined channel to see previously posted messages or send new ones. This can be achieved using the postMessageChat API. Here the messages are differentiated into 2 colors, one color for messages sent by you and another color for messages sent by others.

Italian Trulli

3. Capability and Features

This section of the document lists the features of RocketChat which makes the library easy and useful for communication. REST APIs of RocketChat are grouped into the following categories:

  • Channel Endpoints: These are REST APIs related to public channel. It can be used for creating a channel, leaving a channel, retrieving the history of channel , archiving/Unarchiving the channel etc.

  • Group Endpoints: These endpoints include REST APIs of private channel/group. It is Used for creating a private channel or leaving a private channel. It also supports other functionalities like groupCreate, groupinvite, groupLeave and groupList etc

  • Visitor endpoints: These are REST APIs associated with the Livechat visitor. They are used for visitor registration, deleting visitor, retrieving the visitor info and setting the status of the visitor.

  • Users endpoints: These are user related APIs that provide functionality like creating users with admin rights, listing all users, deleting users and retrieving user information etc.

  • Authentication Endpoints: These are the APIs related to login and logout of the users. It generates an authentication token at the time of login and also takes care of invalidating the token at the time of logout.

4. Installation

For using rocketchat module in sample application, add rocketchat library in project root directory and include the below dependency in entry package.json.

{
  "dependencies": {
    "rocketchat_ohos": "file:../rocketchat_ohos"
  }
}

5. Usage

This section will guide us in understanding how to use the library in Harmony app development project.

Step 1: Import rocketchat dependencies into sample app

    import {RestAPI} from 'rocketchat_ohos';

Step 2: Define Internet permissions in config.json of sample app

  "reqPermissions": [
      {
        "name": "ohos.permission.INTERNET",
        "reason": "Api call",
        "usedScene": {
          "ability": [
            "com.openharmony.rocketchat.MainAbility"
          ],
          "when": "always"
        }
      }
    ]

List of APIs supported

Below is the list of APIs which are supported:

Feature   API  Information
Livechat - Page Visited Endpoint     pageVisited(pageData)     pageVisited - To Send Visitor navigation history
Livechat - Visitors Endpoints
  • visitorRegistration(visitor)
  • visitor(token)
  • visitorDelete(token)
  • setVisitorStatus(visitor)
  • visitorRegistration - Registers a new visitor
  • visitor - Retrieves a visitor
  • visitorDelete - Deletes a visitor
  • setVisitorStatus - Sets visitor status
Channels Endpoints
  • addAllChannel(roomId,activeUsersOnly})
  • addLeaderChannel(leaderData)
  • addModeratorChannel(moderatorData)
  • addOwnerChannel(ownerData)
  • archiveChannel(roomId)
  • closeChannel(roomId)
  • createChannel(name, members: Array,readOnly:boolean)
  • deleteChannel(roomId,roomName)
  • getUserAllMentionByChannel(roomId)
  • filesChannel(roomId,roomName)
  • getIntegrationChannel(roomId)
  • historyChannel(roomId,optionalParams)
  • infoChannel(roomId,roomName)
  • inviteChannel(data)
  • joinChannel(roomId,JoinCode)
  • kickChannel(roomId,userId)
  • leaveChannel(roomId)
  • listChannel()
  • joinListChannel()
  • membersChannel(roomId,roomName)
  • messageChannel(roomId,roomName)
  • onlineChannel(roomId)
  • openChannel(roomId)
  • removeLeaderChannel(roomId, userId)
  • removeModerator(roomID,userID)
  • removeOwnerChannel(roomId,userId)
  • renameChannel(roomId,name)
  • rolesChannel(roomId,roomName)
  • setCustomFieldsChannel(roomId,roomName,
    customField:Object)
  • setDefaultChannel(roomId,default)
  • setJoinCodeChannel(roomId, joinCode)
  • setPurposeChannel(roomId,Purpose)
  • setReadOnlyChannel(roomId,readOnly)
  • setTopicChannel(roomId,topic)
  • setTypeChannel(roomId,roomName, channelType)
  • unArchiveChannel(roomId)
  • postMessageChat(postMessageData)
  • addAllChannel - Adds all of the users on the server to a channel
  • addLeaderChannel - Gives the role of Leader for a user in the current channel
  • addModeratorChannel - Gives the role of moderator to a user in a channel
  • addOwnerChannel - Gives the role of owner to a user in a channel
  • archiveChannel - Archives a channel
  • closeChannel - Removes a channel from a user's list of channels
  • createChannel - Creates a new channel
  • deleteChannel - Removes a channel
  • getUserAllMentionByChannel - Retrieves all the mentions of a channel
  • filesChannel - Retrieves a list of files from a channel
  • getIntegrationChannel - Retrieves the channel's integration
  • historyChannel - Retrieves the messages from a channel
  • infoChannel - Gets a channel's information
  • inviteChannel - Adds a user to a channel
  • joinChannel - Joins yourself to a channel
  • kickChannel - Removes a user from a channel
  • leaveChannel - Removes the calling user from a channel
  • listChannel - Retrieves all of the channels from the serve
  • joinListChannel - Retrieves only the channels the calling user has joined
  • membersChannel - Retrieves all channel users
  • messageChannel - Retrieves all channel messages
  • onlineChannel - Lists all online users of a channe
  • openChannel - Adds the channel back to the user's list of channel
  • removeLeaderChannel - Removes the role of Leader for a user in the current channel
  • removeModerator - Removes the role of moderator from a user in the current channel
  • removeOwnerChannel - Removes the role of moderator from a user in a channel
  • renameChannel - Changes a channel's name
  • rolesChannel - Gets the user's roles in the channel
  • setCustomFieldsChannel - Sets a channel's custom fields
  • setDefaultChannel - Sets whether the channel is a default channel or not
  • setJoinCodeChannel - Sets the channel's code required to join it.
  • setPurposeChannel - Sets the description for the channel (the same as channels.setDescription, obsolete)
  • setReadOnlyChannel - Sets whether a channel is read-only or not
  • setTopicChannel - Sets a channel's topic
  • setTypeChannel - Sets the type of room the channel should be
  • unArchiveChannel - Unarchives a channel
  • postMessageChat - Posts a new chat message
Groups Endpoints
  • groupAddLeader(groupID: string, userId: string)
  • groupAddModerator(roomId,userId)
  • groupAddOwner(roomId,userId)
  • groupArchive(groupID)
  • groupsClose(roomId)
  • groupCreate(name: string, members: Array,readOnly: boolean)
  • groupHistory(roomId,optionalParams)
  • groupsInfo(roomId, roomName)
  • groupInvite(groupID: string, userId: string)
  • groupKick(roomId,userId)
  • groupLeave(groupID: string)
  • groupList()
  • groupOpen(roomId)
  • groupRemoveModerator(roomId,userId)
  • groupRemoveOwner(roomId,userId)
  • groupRename(roomId,name)
  • groupSetDescription(roomId,description)
  • groupSetPurpose(roomId,purpose)
  • groupSetReadOnly(roomId,readOnly)
  • groupSetTopic(groupID,groupTopic)
  • groupSetType(roomId,type)
  • groupAddLeader - Gives the role of Leader for a user in the current group
  • groupAddModerator - Gives the role of moderator to a user in a group
  • groupAddOwner - Gives the role of owner to a user in a group
  • groupArchive - Archives a private group
  • groupsClose - Removes a private group from the list of groups
  • groupCreate - Creates a new private group
  • groupHistory - Retrieves the messages from a private group
  • groupsInfo - Retrieves the information about a private group
  • groupInvite - Adds a user to the private group
  • groupKick - Removes a user from a private group
  • groupLeave - Removes the calling user from the private group
  • groupList - Lists the private groups the caller is part of
  • groupOpen - Adds the private group back to the list of groups
  • groupRemoveModerator - Removes the role of moderator from a user in a group
  • groupRemoveOwner - Removes the role of owner from a user in a group
  • groupRename - Changes the name of the private group
  • groupSetDescription - Sets a private group's description
  • groupSetPurpose - Sets a private group's purpose
  • groupSetReadOnly - Sets whether the room is read-only or not
  • groupSetTopic - Sets a private group's topic
  • groupSetType - Sets the type of room this group will be
Settings Endpoints     publicSettings(query)     publicSettings - Lists all public settings
Users Endpoints
  • userCreate(data)
  • userDelete(data)
  • usersInfo(userId,userName)
  • usersList(data)
  • setAvatar(avatarData)
  • usersPresence(data)
  • userCreate - Creates a new user
  • userDelete - Deletes an existing user
  • usersInfo - Retrieves information about a user
  • usersList - Retrieves all of the users in the system and their information
  • setAvatar - Sets avatar
  • usersPresence - Gets a user's presence if the query string userId or username is provided, otherwise it gets the callee's
Authentication Endpoints
  • login()
  • logout()
  • login - Authenticate using username and password with the REST API
  • logout - Invalidate your REST API authentication token

6. API usage examples

In this section, we can have a look at some of the examples where the APIs of this library is put to use and the results which we can achieve.

Example 1
In this example, we can see login functionality.

        
TextInput({ placeholder: 'Username/email' })
   ...
   .onChange((value: string) => {
        this.userName = value
})

TextInput({ placeholder: 'Password' })
   ...
   .onChange((value: string) => {
        this.userPassword = value
})

restAPI = new RestAPI("https://open.rocket.chat/api/v1/",
this.userName,this.userPassword);
restAPI.login().then(function (data) {
    console.info("onClickLogin data:" + JSON.stringify(data));
    authToken = data.data.authToken;
    userId = data.data.userId;
    that.showToast("Login Successful");
}).catch(function (error) {
    that.showToast("Error :" + error);
    console.info("onClickLogin error:" + error);
});
        

Italian Trulli

Example 2
In this example, we can see groupCreate functionality.

        
let groupName = "Squad"
var readOnly = false;
let filtered_Ulist = ["Vicky", "Mathew", "Rachel"]
restAPI.groupCreate (groupName,filtered_Ulist,readOnly).
then(function (data) {
    console.info("restApiSample groupCreate data:" + JSON.stringify(data));
    groupID = data.group._id;
    console.info('groupCreate data:' + JSON.stringify(data));
    console.info('groupCreate groupID:' + groupID);
    that.showToast("Success - group Created" + JSON.stringify(data));
}).catch(function (error) {
    that.showToast("Error :" + error);
    console.info("groupCreate error:" + error);
});
        

Italian Trulli

Example 3
In this example, we can see listChannel functionality.

        
restAPI.listChannel().then(function (data) {
    console.info("listChannel data:" + JSON.stringify(data));
    that.showToast("Success");
}).catch(function (error) {
    that.showToast("Error :" + error);
    console.info("listChannel error:" + error);
});    
        

Italian Trulli

7. Conclusion


RocketChat is a simple-to-use yet very powerful communication platform. The performance of the library is very good even when it works on one of the latest operating systems in the world, which is HarmonyOS!

  • For more exciting libraries to develop your app, peep into third-party-components at
    OpenHarmony-TPC

  • To know more about the development work happening on harmony application layer, and even be part of the exciting stuff, watch this space of Application Library Engineering Group