Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added postToTopic() for MessagingService needs #793

Merged
merged 7 commits into from
May 25, 2024

Conversation

commonly-ts
Copy link
Contributor

@commonly-ts commonly-ts commented Apr 16, 2024

Requires user to have set their API key. The API key requires access permission Messaging Service API and its operation Publish.
image

Use case example:

// JavaScript

const noblox = require("noblox.js")

await noblox.setAPIKey(process.env.API_KEY)

const universeId = 4283343407
const topicName = "ModerateUser"
const data = { targetUser: 123456789, staffMember: 1210019099, action: "Kick" }

await noblox.publishToTopic(universeId, topicName, data)
-- Roblox Luau

local MessagingService = game:GetService("MessagingService")
local HttpService = game:GetService("HttpService")

MessagingService:SubscribeAsync("ModerateUser", function(message)
    -- The information passed in the JavaScript is placed into `Data` and needs to be decoded
    -- Types can be used here to parse the decoded data
    local data = HttpService:JSONDecode(message.Data)
	
    print(data)
end)

Roblox documentation:
https://create.roblox.com/docs/reference/cloud/messaging-service
https://create.roblox.com/docs/cloud/open-cloud/usage-messaging

Copy link
Contributor

@nottisa nottisa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing! I currently don't have that much time to test. I will test sooner or later.

Comment on lines 37 to 47
return http(httpOpt)
.then(function ({ statusCode }) {
switch (statusCode) {
case 200: resolve(true)
case 400: reject(new Error(`[${statusCode}] Bad Request, check what data is being sent`))
case 401: reject(new Error(`[${statusCode}] Unauthorized`))
case 403: reject(new Error(`[${statusCode}] Forbidden, Possible insufficient API key access permissions`))
case 500: reject(new Error(`[${statusCode}] Internal server error`))
}
}).catch(reject)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling should probably be handled like in this file: lib/friends/acceptFriendRequest.js. If I'm wrong, you don't actually need the format string and can just manually enter the error codes which might be slightly more performant.

Copy link
Contributor Author

@commonly-ts commonly-ts Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a later commit to replace the switch case with just an if else.
I'm not sure what you mean that I don't need to format the string? - ah I know what you mean, yeah that could be done instead.

Copy link
Contributor

@nottisa nottisa Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just mean errors should probably be reported as they come from Roblox, and not what we currently understand those errors to be. The file I linked has dynamic error handling that should pull the errors from the API instead of being hard coded in. I saw the later commit, I think it would just be in our best interest to pull the errors directly from Roblox.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I completely agree. I'll make a commit for the errors to be handled like they are in acceptFriendRequest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling is now updated to give proper errors. The errors this API returns are very different than others so I had to spent a little more time figuring them out.

Valid API key that has any sort of invalid access permissions/scopes will return:
Error: [403] InsufficientScope Scope not authorized.

Invalid API key will return:
Error: [401] Unauthorized Invalid API Key

@Neztore Neztore added this to the v5 milestone Apr 17, 2024
@nottisa
Copy link
Contributor

nottisa commented Apr 19, 2024

Looks good. I'll try and test it in the next couple days.

Copy link
Member

@Neztore Neztore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nottisa mentioned no longer being able to contribute to the library (I assume due to other commitments), so I am reviewing.. I am happy to merge this once the typings are updated (https://github.com/noblox/noblox.js/tree/master/typings) but the way we handle open cloud may change after tomorrows meeting.

@commonly-ts
Copy link
Contributor Author

I am happy to merge this once the typings are updated (https://github.com/noblox/noblox.js/tree/master/typings)

I have committed the change to the typings. Unsure why it also committed changes to whitespace removal 🤷

@commonly-ts commonly-ts requested a review from Neztore May 10, 2024 23:39
@Neztore Neztore added the enhancement New feature or request label May 25, 2024
@Neztore Neztore merged commit e1c01e1 into noblox:master May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants