Pending Messages

Pending Messages features lets you introduce asynchronous moderation on messages being sent on channel. To use this feature please get in touch with support so that we can enable it for your organisation.

Sending Pending Messages

Messages can be made pending by default by setting the channel config property mark_messages_pending to true.

ChannelType
  .update("messaging")
  .markMessagesPending(true)
  .request()

You can also set the pending property on a message to mark it as pending on server side (this will override the channel configuration). Please note that this is only server-side feature.

MessageRequestObject messageRequest =
 MessageRequestObject.builder()
  .text(text)
  .userId(testUserRequestObject.getId())
  .build();
Map<String, Object> metadata = new HashMap<String, Object>();
metadata.put("custom_field", "true");

Message.send(testChannel.getType(), testChannel.getId())
 .message(messageRequest)
 .pending(true)
 .pendingMessageMetadata(metadata)
 .request()

Pending messages will only be visible to the user that sent them. They will not be query-able by other users.

Callbacks

When a pending message is sent or deleted, the message along with its pending message metadata will be forwarded to a callback over HTTP(s). You can configure this callback address by updating the application configuration.

App
 .update()
 .asyncModerationConfig(
   App.AsyncModerationConfigRequestObject.builder().callback(
    App.AsyncModerationCallback.builder()
     .mode("CALLBACK_MODE_REST")
     .serverUrl("http://localhost.com")
     .build()
   ).timeoutMs(30000).build()
 )
 .request()

For example, if your callback server url is https://example.com, we would send callbacks:

  • When pending message is sent

POST https://example.com/PassOnPendingMessage

  • When a pending message is deleted

POST https://https://example.com/DeletedPendingMessage

In both callbacks, the body of the POST request will be of the form:

{
	"message": {
		// the message object
	},
	"metadata": {
		// keys and values that you passed as pending_message_metadata
	}
}

Deleting pending messages

Pending messages can be deleted using the normal delete message endpoint. Users are only able to delete their own pending messages. The messages must be hard deleted. Soft deleting a pending message will return an error.

Updating pending messages

Pending messages cannot be updated.

Querying pending messages

A user can retrieve their own pending messages using the following endpoints:

// To query single pending message
Message.get(pendingMessageId).request();

// To query multiple pending messages
Message.getMany(
 "channel_type",
 "channel_id",
 Arrays.asList("pendingMessageId1", "pendingMessageId2")
).request()

Query channels

Each channel that is returned from query channels will also have an array of pending_messages. These are pending messages that were sent to this channel, and belong to the user who made the query channels call. This array will contain a maximum of 100 messages and these will be the 100 most recently sent messages.

List<ChannelGetResponse> channels = Assertions.assertDoesNotThrow(
    () ->
      Channel.list()
        .user(testUserRequestObject)
        .sort(Sort.builder().field("id").direction(Direction.DESC).build())
        .request()).getChannels();

  System.out.println(channels.get(0).getPendingMessages());

Committing pending messages

Calling the commit message endpoint will promote a pending message into a normal message. This message will then be visible to other users and any events/push notifications associated with the message will be sent.

The commit message endpoint is server-side only.

Message.commit("pending_message_id").request()

If a message has been in the pending state longer than the timeout_ms defined for your app, then the pending message will be deleted. The default timeout for a pending message is 3 days.

© Getstream.io, Inc. All Rights Reserved.