twilio / twilio-chat-demo-android

Chat API Demo Application for Android
MIT License
62 stars 51 forks source link

Error 0:50107 User not authorized for command. When I try to change message attributes #99

Closed nikdevpub closed 4 years ago

nikdevpub commented 5 years ago

When changing attributes on IOS, everything works well, but on Android I get an error message: "0:50107 User not authorized for command" com.twilio:chat-android:4.2.0 ` private fun initChatClient(chatData: ChatData) { viewState.setChatData(chatData) Timber.d(chatData.identity) val props = ChatClient.Properties.Builder() .createProperties() Timber.d(chatData.toString()) ChatClient.create( context, chatData.access_token, props, object : CallbackListener() { override fun onSuccess(chatClient: ChatClient) { this@ChatPresenter.chatClient = chatClient loadChannels(chatData.identity) Timber.d("Success creating Twilio Chat Client") }

            override fun onError(errorInfo: ErrorInfo) {
                Timber.d("Error creating Twilio Chat Client: " + errorInfo.message)
                viewState.showError(errorInfo.message)
            }
        }
    )
}

private fun loadChannels(identity: String) {

    chatClient?.channels?.getChannel(identity, object : CallbackListener<Channel>() {
        override fun onSuccess(channel: Channel?) {
            if (channel != null) {
                Timber.d("Joining Channel")
                joinChannel(channel)
            } else {
                Timber.d("Creating Channel")

                chatClient?.channels?.createChannel(
                    identity,
                    Channel.ChannelType.PRIVATE, object : CallbackListener<Channel>() {
                        override fun onSuccess(channel: Channel?) {
                            if (channel != null) {
                                Timber.d("Joining Channel")
                                joinChannel(channel)
                            }
                        }

                        override fun onError(errorInfo: ErrorInfo?) {
                            Timber.d("Error creating channel: " + errorInfo!!.message)
                            viewState.showError(errorInfo.message)
                        }
                    })
            }
        }

        override fun onError(errorInfo: ErrorInfo?) {
            Timber.d("Error retrieving channel: " + errorInfo!!.message)
            loadChannels(identity)
        }

    })
}

private fun joinChannel(channel: Channel) {
    Timber.d("Joining Channel: " + channel.uniqueName + " - " + channel.status + "-- " + channel.messages)
    this@ChatPresenter.generalChannel = channel
    listenGeneralChanel()

    if (channel.status != Channel.ChannelStatus.JOINED) {
        channel.join(object : StatusListener() {
            override fun onSuccess() {
                this@ChatPresenter.generalChannel = channel
                Timber.d("Joined default channel")
                listenGeneralChanel()
            }

            override fun onError(errorInfo: ErrorInfo) {
                Timber.d("Error joining channel: " + errorInfo.message)
            }
        })
    }
}

private fun loadMessages() { viewState.setMessageListLoading(true) isNextPageLoading = true

    generalChannel?.messages?.getLastMessages(ITEMS_ON_PAGE, object : CallbackListener<List<Message>>() {
        override fun onSuccess(messages: List<Message>?) {
            if (messages != null && !messages.isEmpty()) viewState.setMessageList(messages)
            viewState.setMessageListLoading(false)
            currentShownMessagesCount = ITEMS_ON_PAGE.toLong()
            isNextPageLoading = false
            Timber.d("getLastMessages")
        }

        override fun onError(errorInfo: ErrorInfo?) {
            super.onError(errorInfo)
            viewState.showError(errorInfo?.message)
            Timber.d(errorInfo!!.message)
            isNextPageLoading = false
            viewState.setMessageListLoading(false)
        }
    })
}

`

nikdevpub commented 5 years ago

`fun deleteMessage(message: Message, type: ChatMessageAdapter.DeleteType) { if (generalChannel != null) {

        val obj = JSONObject()
        obj.put("status", "deleted")
        if (type == ChatMessageAdapter.DeleteType.Global) {
            obj.put("for", "everyone")
        } else {
            obj.put("for", "patient")
        }

        viewState.setFileLoading(true)
        message.setAttributes(obj, object : StatusListener() {
            override fun onSuccess() {
                viewState.setFileLoading(false)
                viewState.updateMessages()
            }

            override fun onError(errorInfo: ErrorInfo?) {
                super.onError(errorInfo)
                viewState.setFileLoading(false)
                viewState.showError(context.getString(R.string.chat_deleting_error) + errorInfo.toString())
            }
        })
    }
}`
berkus commented 5 years ago

Hi

Does your user have editAnyMessageAttributes or editOwnMessageAttributes permissions enabled?

See this doc for explanation of roles. And use https://www.twilio.com/console/chat/services/<YOUR ISxxx service instance id here>/roles/new/channel to see full list of permissions.

Your default channel user role should have one of the listed above permissions. Open https://www.twilio.com/console/chat/services/<YOUR ISxxx service instance id here>/roles to see a list of the roles and click on channel user role - by default it has editOwnMessageAttributes permission but not editAnyMessageAttributes - this is for security so users don't edit other user's messages.

If that's the case - you are probably trying to modify other user's message's attributes.

berkus commented 4 years ago

Closing as apparently resolved.