Skip to content

Commit 547acb5

Browse files
Neztoresuufi
andauthored
Add onJoinRequest, re-document onJoinRequestHandle, fix uncatchable errors (#504)
* Update shortPoll docs * Add custom timeout function and drop bluebird Timeout function is documented but internal. The Promise file/function was internal - so it is NOT semver major as it should not be used outside of the library. Bluebird is no longer needed. * Replace timeout with better implementation New implementation is shorter (44 -> 24 lines), more beautiful and most importantly: it actually works. Also avoids manually wrapping the original promise, so that's a bonus too. * Fix timer problem * use Finally * Improve error handling across the board * Add onJoinRequest & Re-document onJoinRequestHandle * Regenerate yarn.lock * Fix getRequests error message Co-authored-by: suufi <[email protected]> Co-authored-by: suufi <[email protected]>
1 parent 97dd75f commit 547acb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+182
-68
lines changed

lib/asset/deleteFromInventory.js

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function deleteFromInventory (jar, assetId, xcsrf) {
5555
reject(new Error(error))
5656
}
5757
})
58+
.catch(error => reject(error))
5859
})
5960
}
6061

lib/asset/getProductInfo.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function getProductInfo (asset) {
4444
}
4545
}
4646
})
47+
.catch(error => reject(error))
4748
})
4849
}
4950

lib/asset/uploadItem.js

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function uploadItem (jar, file, name, assetType, groupId) {
7474
}
7575
})
7676
})
77+
.catch(error => reject(error))
7778
})
7879
}
7980

lib/avatar/redrawAvatar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function redrawAvatar (jar, token) {
3535
} else {
3636
reject(new Error('Redraw avatar failed'))
3737
}
38-
})
38+
}).catch(error => reject(error))
3939
})
4040
}
4141

lib/avatar/removeAssetId.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function removeAssetId (assetId, jar, xcsrf) {
4242
} else {
4343
resolve()
4444
}
45-
})
45+
}).catch(error => reject(error))
4646
})
4747
}
4848

lib/avatar/wearAssetId.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function wearAssetId (assetId, jar, xcsrf) {
4242
} else {
4343
resolve()
4444
}
45-
})
45+
}).catch(error => reject(error))
4646
})
4747
}
4848

lib/cache/wrap.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Includes
22
const addIf = require('./addIf.js')
33
const options = require('../options.js')
4-
const promise = require('../internal/promise.js')
54

65
// Define
76
function wrap (type, index, func) {
87
const cache = options.cache
98
const group = cache[type]
109
if (group.expire > 0 || group.permanent) {
11-
return promise(function (resolve, reject) {
10+
return new Promise(function (resolve, reject) {
1211
addIf(cache, type, index, {
1312
done: resolve,
1413
add: function (done) {

lib/chat/addUsersToConversation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function addUsersToConversation (conversationId, userIds, jar, token) {
4848
}
4949
reject(new Error(error))
5050
}
51-
})
51+
}).catch(error => reject(error))
5252
})
5353
}
5454

lib/chat/removeFromGroupConversation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function removeFromGroupConversation (conversationId, userId, jar, token) {
4949
}
5050
reject(new Error(error))
5151
}
52-
})
52+
}).catch(error => reject(error))
5353
})
5454
}
5555

lib/chat/renameGroupConversation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function renameGroupConversation (conversationId, newTitle, jar, token) {
4444
} else {
4545
reject(new Error('Rename group chat failed'))
4646
}
47-
})
47+
}).catch(error => reject(error))
4848
})
4949
}
5050

lib/chat/sendChatMessage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function sendChatMessage (conversationId, messageText, jar, token) {
4444
} else {
4545
throw new Error('Send chat message failed')
4646
}
47-
})
47+
}).catch(error => reject(error))
4848
})
4949
}
5050

lib/chat/setChatUserTyping.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function setChatUserTyping (conversationId, isTyping, jar, token) {
4848
}
4949
reject(new Error(error))
5050
}
51-
})
51+
}).catch(error => reject(error))
5252
})
5353
}
5454

lib/chat/startGroupConversation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function startGroupConversation (userIds, chatTitle, jar, token) {
4949
}
5050
reject(new Error(error))
5151
}
52-
})
52+
}).catch(error => reject(error))
5353
})
5454
}
5555

lib/game/configureGamePass.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function configureGamePass (gamePassId, name, description, price, icon, jar, tok
8181
reject(new Error(`An unexpected error occurred with status code ${res.statusCode}.${priceComment}`))
8282
}
8383
}
84-
})
84+
}).catch(error => reject(error))
8585
})
8686
}
8787

lib/game/getDeveloperProducts.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function getDeveloperProducts (jar, placeId, page) {
4141
}
4242
}
4343
})
44+
.catch(error => reject(error))
4445
})
4546
}
4647

lib/game/getGameInstances.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function getGameInstances (jar, placeId, startIndex) {
4444
}
4545
}
4646
})
47+
.catch(error => reject(error))
4748
})
4849
}
4950

lib/game/updateUniverseAccess.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function updateUniverseAccess (universeId, isPublic, jar, token) {
4343
} else {
4444
reject(new Error(`An unknown error occurred with updateUniverseAccess() | [${statusCode}] universeId: ${universeId}, isPublic: ${isPublic}`))
4545
}
46-
})
46+
}).catch(error => reject(error))
4747
})
4848
}
4949

lib/group/deleteWallPost.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function deleteWallPost (jar, token, group, postId) {
4646
reject(new Error(`${res.statusCode} ${errors.join(', ')}`))
4747
}
4848
}
49-
})
49+
}).catch(error => reject(error))
5050
})
5151
}
5252

lib/group/exile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function exileUser (group, target, jar, xcsrf) {
4545
} else {
4646
resolve()
4747
}
48-
})
48+
}).catch(error => reject(error))
4949
})
5050
}
5151

lib/group/getAuditLog.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Includes
22
const http = require('../util/http.js').func
3-
const Promise = require('bluebird')
43

54
exports.required = ['group']
65
exports.optional = ['actionType', 'userId', 'sortOrder', 'limit', 'cursor', 'jar']
@@ -45,7 +44,7 @@ function getAuditLog (group, actionType, userId, sortOrder, limit, cursor, jar)
4544
} else {
4645
resolve(responseData)
4746
}
48-
})
47+
}).catch(error => reject(error))
4948
})
5049
}
5150

lib/group/getGroup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function getGroup (groupId) {
4141
reject(new Error(`${res.statusCode} ${res.body}`))
4242
}
4343
}
44-
})
44+
}).catch(error => reject(error))
4545
})
4646
}
4747

lib/group/getJoinRequest.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Includes
22
const http = require('../util/http.js').func
3-
const Promise = require('bluebird')
43

54
// Args
65
exports.required = ['group', 'userId']
@@ -43,7 +42,7 @@ function getJoinRequest (jar, group, userId) {
4342
reject(new Error(`${res.statusCode} ${errors.join(', ')}`))
4443
}
4544
}
46-
})
45+
}).catch(error => reject(error))
4746
})
4847
}
4948

lib/group/getJoinRequests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Includes
22
const http = require('../util/http.js').func
3-
const Promise = require('bluebird')
43

54
// Args
65
exports.required = ['group']
@@ -46,6 +45,7 @@ function getJoinRequests (jar, group, sortOrder, limit, cursor) {
4645
}
4746
}
4847
})
48+
.catch(error => reject(error))
4949
})
5050
}
5151

lib/group/getPlayers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function getPlayersInRoleOnPage (jar, group, rolesetId, sortOrder, limit, cursor
4444
reject(new Error(`${res.statusCode} ${errors.join(', ')}`))
4545
}
4646
}
47-
})
47+
}).catch(error => reject(error))
4848
})
4949
}
5050

@@ -71,7 +71,7 @@ function getPlayersInRole (jar, group, rolesetId, sortOrder, limit, cursor, curr
7171
.then(function (newCurrentPlayers) {
7272
return resolve(newCurrentPlayers)
7373
})
74-
})
74+
}).catch(error => reject(error))
7575
})
7676
}
7777

lib/group/getRole.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Dependencies
22
const entities = require('entities')
3-
const Promise = require('bluebird')
43

54
// Includes
65
const getRoles = require('./getRoles.js').func

lib/group/getRolePermissions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getRolePermissions (group, rolesetId, jar) {
4040
} else {
4141
resolve(responseData)
4242
}
43-
})
43+
}).catch(error => reject(error))
4444
})
4545
}
4646

lib/group/getRoles.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function getRoles (group) {
4444
}
4545
resolve(roles)
4646
}
47-
})
47+
}).catch(error => reject(error))
4848
})
4949
}
5050

lib/group/getShout.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Includes
22
const http = require('../util/http.js').func
3-
const Promise = require('bluebird')
43

54
// Args
65
exports.required = ['group']

lib/group/getWall.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const http = require('../util/http.js').func
2-
const Promise = require('bluebird')
32

43
exports.required = ['group']
54
exports.optional = ['sortOrder', 'limit', 'cursor', 'jar']
@@ -42,7 +41,7 @@ function getPosts (group, sortOrder, limit, cursor, jar) {
4241
} else {
4342
resolve(responseData)
4443
}
45-
})
44+
}).catch(error => reject(error))
4645
})
4746
}
4847

lib/group/groupPayout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function groupPayout (jar, token, group, data, recurring, usePercentage) {
5454
reject(new Error(`${res.statusCode} ${errors.join(', ')}`))
5555
}
5656
}
57-
})
57+
}).catch(error => reject(error))
5858
})
5959
}
6060

lib/group/handleJoinRequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function handleJoinRequest (group, userId, accept, jar, xcsrf) {
4646
} else {
4747
resolve()
4848
}
49-
})
49+
}).catch(error => reject(error))
5050
})
5151
}
5252

lib/group/onJoinRequest.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Includes
2+
const settings = require('../../settings.json')
3+
const shortPoll = require('../util/shortPoll.js').func
4+
const getJoinRequests = require('./getJoinRequests.js').func
5+
const promiseTimeout = require('../internal/timeout')
6+
7+
// Args
8+
exports.required = ['group']
9+
exports.optional = ['jar']
10+
11+
// Docs
12+
/**
13+
* 🔐 An event for when someone makes a request to join the group.
14+
* @category Group
15+
* @alias onJoinRequest
16+
* @param {number} group - The id of the group.
17+
* @returns An EventEmitter that emits when someone tries to join.
18+
* @example const noblox = require("noblox.js")
19+
* // Login using your cookie
20+
* const e = noblox.onJoinRequest()
21+
* e.on("data", function(data) {
22+
* console.log("New request!", data)
23+
* })
24+
* e.on("error", function (err) {
25+
* ...
26+
* })
27+
**/
28+
29+
async function getRequests (jar, group, cursor) {
30+
const requests = []
31+
const res = await promiseTimeout(getJoinRequests({ jar, group, cursor, limit: 100 }), settings.event.timeout, 'getRequests onJoinRequests internal')
32+
requests.push.apply(requests, res.data)
33+
if (res.nextPageCursor) {
34+
requests.push.apply(requests, await getRequests(jar, group, res.nextPageCursor))
35+
}
36+
return requests
37+
}
38+
39+
// Define
40+
exports.func = function (args) {
41+
return shortPoll({
42+
getLatest: function (latest) {
43+
return getRequests(args.jar, args.group)
44+
.then(function (requests) {
45+
const given = []
46+
for (const key in requests) {
47+
if (Object.prototype.hasOwnProperty.call(requests, key)) {
48+
const date = new Date(requests[key].created.slice(0, requests[key].created.lastIndexOf('.')))
49+
if (date > latest) {
50+
latest = date
51+
given.push(requests[key])
52+
}
53+
}
54+
}
55+
return {
56+
latest: latest,
57+
data: given
58+
}
59+
})
60+
},
61+
delay: 'onJoinRequest'
62+
})
63+
}

0 commit comments

Comments
 (0)