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

Implement Realm.User.push() #2995

Merged
merged 2 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ NOTE: This version bumps the Realm file format to version 11. It is not possible

### Enhancements
* Added RemoteMongoClient functionality to `Realm.User`
* Added Push functionality to `Realm.User`
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we can elaborate a bit more what this is (in end user terms). (I can see we also missed to do that with RemoteMongoClient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, I don't actually know what "Push" does in end user terms. This sounds like a good thing to have our Docs team work on so that they can ensure that release notes use consistent terminology with the rest of the documentation.


### Fixed
* Added missing `SyncConfiguration.error` field in the typescript definitions.
Expand Down
27 changes: 27 additions & 0 deletions docs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,33 @@ class User {
* @returns {Realm~RemoteMongoDB}
*/
remoteMongoClient(serviceName) { }

/**
* @class Realm.User~Push Access to the operations of the push service.
*/

/**
* Registers the provided token with this User's device.
*
* @function Realm.User~Push#register
* @param {string} token
* @returns {Promise<void>} completed when the user is registered, or the operation fails.
*/

/**
* Deregisters this User's device.
*
* @function Realm.User~Push#deregister
* @returns {Promise<void>} completed when the user is deregistered, or the operation fails.
*/

/**
* Access the operations of the push service.
*
* @param {string} serviceName
* @returns {Realm.User~Push}
*/
push(serviceName) { }
}

/**
Expand Down
22 changes: 4 additions & 18 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,15 @@

"use strict";

const {promisify} = require("./utils.js");

const instanceMethods = {
logIn(credentials) {
return new Promise((resolve, reject) => {
this._login(credentials, (user, error) => {
if (error) {
reject(error);
} else {
resolve(user);
}
});
});
return promisify(cb => this._login(credentials, cb));
},

removeUser() {
return new Promise((resolve, reject) => {
this._removeUser((error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
return promisify(cb => this._removeUser(cb));
},

get auth() {
Expand Down
2 changes: 2 additions & 0 deletions lib/browser/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ createMethods(User.prototype, objectTypes.USER, [
"_deleteUser",
"_linkCredentials",
"_callFunction",
"_pushRegister",
"_pushDeregister",
]);

Object.defineProperties(User.prototype, {
Expand Down
52 changes: 7 additions & 45 deletions lib/email_password_provider_client_methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,27 @@

'use strict';

const {promisify} = require("./utils.js");

const instanceMethods = {
registerEmail(email, password) {
return new Promise((resolve, reject) => {
this._registerEmail(email, password, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._registerEmail(email, password, cb));
},

confirmUser(token, token_id) {
return new Promise((resolve, reject) => {
this._confirmUser(token, token_id, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._confirmUser(token, token_id, cb));
},

resendConfirmationEmail(email) {
return new Promise((resolve, reject) => {
this._resendConfirmationEmail(email, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._resendConfirmationEmail(email, cb));
},

sendResetPasswordEmail(email) {
return new Promise((resolve, reject) => {
this._sendResetPasswordEmail(email, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._sendResetPasswordEmail(email, cb));
},

resetPassword(password, token, token_id) {
return new Promise((resolve, reject) => {
this._sendResetPasswordEmail(password, token, token_id, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._sendResetPasswordEmail(password, token_id, token_id, cb));
}
};

Expand Down
46 changes: 18 additions & 28 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@
"use strict";

const {RemoteMongoDBCollection} = require("./mongo_client.js");
const {promisify} = require("./utils.js");

const instanceMethods = {
linkCredentials(credentials) {
return new Promise((resolve, reject) => {
this._linkCredentials(credentials, (user, error) => {
if (error) {
reject(error);
} else {
resolve(user);
}
});
});
return promisify(cb => this._linkCredentials(credentials, cb));
},

callFunction(name, args, service = undefined) {
Expand All @@ -48,27 +41,12 @@ const instanceMethods = {
}
args = cleanArgs(args);

return new Promise((resolve, reject) => {
this._callFunction(name, args, service, (result, error) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
});
return promisify(cb => this._callFunction(name, args, service, cb));
},

refreshCustomData() {
return new Promise((resolve, reject) => {
this._refreshCustomData((error) => {
if (error) {
reject(error);
} else {
resolve(this.customData);
}
});
});
async refreshCustomData() {
await promisify(cb => this._refreshCustomData(cb));
return this.customData;
},

remoteMongoClient(serviceName) {
Expand All @@ -87,6 +65,18 @@ const instanceMethods = {
};
},

push(serviceName) {
const user = this;
return {
register(token) {
return promisify(cb => user._pushRegister(serviceName, token, cb));
},
deregister() {
return promisify(cb => user._pushDeregister(serviceName, cb));
},
};
},

_functionsOnService(service) {
const user = this;
return new Proxy({}, {
Expand Down
64 changes: 9 additions & 55 deletions lib/user_apikey_provider_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,31 @@

"use strict";

const {promisify} = require("./utils.js");

const instanceMethods = {
createAPIKey(name) {
return new Promise((resolve, reject) => {
this._createAPIKey(name, (apiKey, err) => {
if (err) {
reject(err);
} else {
resolve(apiKey);
}
});
});
return promisify(cb => this._createAPIKey(name, cb));
},

fetchAPIKey(id) {
return new Promise((resolve, reject) => {
this._fetchAPIKey(id, (apiKey, err) => {
if (err) {
reject(err);
} else {
resolve(apiKey);
}
});
});
return promisify(cb => this._fetchAPIKey(id, cb));
},

fetchAPIKeys() {
return new Promise((resolve, reject) => {
this._fetchAPIKeys((apiKeys, err)=> {
if (err) {
reject(err);
} else {
resolve(apiKeys);
}
});
});
return promisify(cb => this._fetchAPIKeys(cb));
},

deleteAPIKey(apiKeyId) {
return new Promise((resolve, reject) => {
this._deleteAPIKey(apiKeyId, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._deleteAPIKey(apiKeyId, cb));
},

enableAPIKey(apiKeyId) {
return new Promise((resolve, reject) => {
this._enableAPIKey(apiKeyId, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._enableAPIKey(apiKeyId, cb));
},

disableAPIKey(apiKeyId) {
return new Promise((resolve, reject) => {
this._disableAPIKey(apiKeyId, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
return promisify(cb => this._disableAPIKey(apiKeyId, cb));
},
};

Expand All @@ -99,4 +53,4 @@ const staticMethods = {
module.exports = {
static: staticMethods,
instance: instanceMethods,
};
};
47 changes: 47 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

"use strict";


module.exports = {
/**
* Helper to wrap callback-taking C++ function into a Promise-returning JS function
* @example
* // floop() is a wrapper method on a type with a _floop C++ method.
* function floop(how, why) {
* return promisify(cb => this._floop(how, why, cb));
* }
*/
promisify(func) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice simplification!!

return new Promise((resolve, reject) => {
func((...cbargs) => {
if (cbargs.length < 1 || cbargs.length > 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

So only 1 or 2 arguments? Maybe [1, 2].includes(cbargs.length)?

throw Error(`invalid cbargs length ${cbargs.length}`)
let error = cbargs[cbargs.length-1];
if (error) {
reject(error);
} else if (cbargs.length == 2) {
resolve(cbargs[0]);
} else {
resolve();
}
});
});
},
}
1 change: 1 addition & 0 deletions realm.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"src/object-store/src/sync/remote_mongo_collection.cpp",
"src/object-store/src/sync/remote_mongo_database.cpp",
"src/object-store/src/sync/generic_network_transport.cpp",
"src/object-store/src/sync/push_client.cpp",
"src/object-store/src/util/bson/bson.cpp",
"src/object-store/src/util/bson/regular_expression.cpp",
],
Expand Down
5 changes: 4 additions & 1 deletion src/js_app_credentials.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ template<typename T>
void CredentialsClass<T>::function(ContextType ctx, ObjectType this_object, Arguments& arguments, ReturnValue& return_value) {
arguments.validate_count(1);
const std::string payload_json = Value::validated_to_string(ctx, arguments[0], "payload");
const auto payload_bson = bson::parse(payload_json);
if (payload_bson.type() != bson::Bson::Type::Document)
throw std::invalid_argument("payload must be a json object");

auto credentials = realm::app::AppCredentials::function(payload_json);
auto credentials = realm::app::AppCredentials::function(payload_bson.operator const bson::BsonDocument&());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upstream object-store changed the parameter type.

return_value.set(create_object<T, CredentialsClass<T>>(ctx, new app::AppCredentials(credentials)));
}

Expand Down
Loading