How do I get user entitlements to persist across conversations - actions-on-google

I am trying to see if a user has purchased a product by checking conv.user.entitlements but except for immediately after purchase, this is empty.
This value seemed to be persisting between conversations previously, now it is always empty. I am able to see the entitlement using conv.user.entitlements immediately after purchase, but then it is gone.
if (arg.purchaseStatus === 'PURCHASE_STATUS_OK') {
console.log('purchase2 ', conv.user.entitlements);
//this works as expected showing the entitlement
In the logs I see: purchase2 [ { entitlements: [ [Object] ],
packageName: 'com.chad.myfirstapp' } ]
But when I try to log the same in the next conversation, I get:
purchase2 [ ]

While the conv.user object is capable of storing data across conversations, there are both technical and legal limitations to keep in mind, documented here.
When the Assistant can match an identity to the user, the contents of user storage never expires, and only the user or the Action itself can clear it.
When the Assistant can't match an identity to the user, the content of user storage is cleared at the end of the conversation. Examples of cases where the Assistant can't match an identity to the user are:
Voice match is set up and there is no match.
The user disabled personal data.
In addition to the conv.user object, you can also store data in your preferred database. This Github sample demonstrates how to connect Dialogflow to a Firebase Firestore database.
You can find the write function here:
function writeToDb (agent) {
// Get parameter from Dialogflow with the string to add to the database
const databaseEntry = agent.parameters.databaseEntry;
// Get the database collection 'dialogflow' and document 'agent' and store
// the document {entry: "<value of database entry>"} in the 'agent' document
const dialogflowAgentRef = db.collection('dialogflow').doc('agent');
return db.runTransaction(t => {
t.set(dialogflowAgentRef, {entry: databaseEntry});
return Promise.resolve('Write complete');
}).then(doc => {
agent.add(`Wrote "${databaseEntry}" to the Firestore database.`);
}).catch(err => {
console.log(`Error writing to Firestore: ${err}`);
agent.add(`Failed to write "${databaseEntry}" to the Firestore database.`);
});
}

If you just want to see if a user has purchased an item in the past, use the conv.user.storage to store either a boolean whether the user has purchased something in the past:
conv.user.storage.hasPurchasedItem = true
Or perhaps you can make an array of purchases in conv.user.storage and check its length:
conv.user.storage.purchases = conv.user.storage.purchases || []
conv.user.storage.purchases.push({item: bananas, cost: 0.99})

Related

Update presence member status causing presence member list to display updated user as only member

In Ably, Im using
Ably React hook
In the documentation, there is a way to update member status when monitoring what users have entered a room.
const [presenceData, updateStatus] = usePresence("your-channel-name", "initial state");
// The `updateStatus` function can be used to update the presence data for the current client
updateStatus("new status");
This is where I'm having an issue. I take the presence Data and generate a list of users like this.
const [presenceData, updateStatus] = usePresence(
channel,
{
id: user.id,
name: user.name,
isModerator: false,
},
(presenceUpdate) => {
presenceUpdate.action === "update" && console.log({ presenceUpdate });
}
So my list generates correctly. In the callback, I see that the data(presenceUpdate) is updated correctly. When a user enters the room they are displayed correctly. When I start updating the status (ie: change the isModerator flag to true), the presenceData list shows the incorrect user data. It shows the updated user twice (assuming two users are in the room).
updateStatus({...user, isModerator: true})
When using channeling with presenceMember data together, updateStatus has weird side effects.
const [channel] = useChannel("your-channel-name", (message) => {
console.log(message);
});
When you use updateStatus, are you just supposed to pass the updated member data or is there something else you need to do so your presenceMember data shows the correct information?
What Im expecting:
presenceData = [{id: 1, name: 'John', isModerator:false},{id:2, name: 'Steve', isModerator: true}]
What Im getting when I try updating Steve by setting isModerator to true:
presenceData = [{id:2, name: 'Steve', isModerator: true},{id:2, name: 'Steve', isModerator: true}]
Any ideas on what I'm doing wrong?
I figured it out. You cant update anyone else's member data besides your own. I assumed I can have one user update the status of other users but what ends up happening is everyone's status is updated with the object in updateStatus(object). To make sure you dont update other users' data property, have some check that lets you decide if you are the user that is updating your status. This way, the updateStatus function is only called for that user and not for everyone else.

How do I retrieve a key from a firestore document (using swift 4/ios)

In the firebase docs for firebase database there is info on flattening a database and a message app is used as an example.
https://firebase.google.com/docs/database/web/structure-data
// Conversation members are easily accessible
// and stored by chat conversation ID
"members": {
// we'll talk about indices like this below
"one": {
"ghopper": true,
"alovelace": true,
"eclarke": true
},
"two": { ... },
"three": { ... }
},
Under "members"/"one" each key is the name of a participating member in the chat. I'm pretty sure in firebase database there is a getKey method to get each key.
I have set up my database (using firestore instead of firebase) in a similar way but where each chat is a unique identifier and the document contains the members of the chat where the key is their firebase auth id eg.
var docsref: DocumentReference?
docsref = self.defaultStore?.colleection("chats").document(chatID)
docsref?.getDocument { (document, error) in
if let _ = document {
print("document!.data()")
} else {
print("Document does not exist")
}
}
And when I output:
print(document!.data())
I get ["2mHuccccxxxxxxxxxxk4wkIAt33": 1] which is the firebase auth code of one of the participating chat members and a boolean.
I would like to get that key but wasn't sure how.
Thanks.
I'm not sure if it's the exact Syntax but self.defaultStore?.colleection might be a typo with the double E. It could also possibly be that you're missing a step. You're getting inside the chatID and you're expecting to output the keys inside members without actually accessing it. I'd access members after .document(chatID)
You can get it as follows.
to get value document.data()
to get key document.documentID

Meteor Subscriptions Selecting the Entire Set?

I've defined a publication:
Meteor.publish('uninvited', function (courseId: string) {
return Users.find({
'profile.courses': {
$ne: courseId
}
});
});
So, in when a subscriber subscribes to this, I expect Users.find() to return only users that are not enrolled in that particular course. So, on my client, when I write:
this.uninvitedSub = MeteorObservable.subscribe("uninvited", this.courseId).subscribe(() => {
this.uninvited = Users.find().zone()});
I expect uninvited to contain only a subset of users, however, I'm getting the entire set of users regardless of whether or not they are enrolled in a particular course. I've made sure that my data is correct and that there are users enrolled in the course that I'm concerned with. I've also verified that this.courseId is working as expected. Is there an error with my code, or should I further look into my data to see if there's anything wrong with it?
**Note:
When I write this:
this.uninvitedSub = MeteorObservable.subscribe("uninvited", this.courseId).subscribe(() => {
this.uninvited = Users.find({
'profile.courses': {}
}).zone();
});
With this, it works as expected! Why? The difference is that my query now contains 'profile.courses': {}.

Mapping Facebook friend id to Firebase uid in realtime database

How do I map a Facebook friend id to a Firebase uid in the realtime database? It's my understanding that the Firebase uid is not the same as a Facebook id.
My current user flow is logging into Facebook through the Facebook sdk, and then passing the facebook access token to the Firebase sdk to login with Firebase.
My end goal is to store game scores so that a user can see the scores of their friends as well as their own. I do not want to request every score for every player and filter for this information on the client. I would rather send X queries for X amount of friends and only request the scores desired.
Not sure what the downvote was for, but here is the solution I ended up using.
Storing the scores by facebookId instead of firebaseId allows the lookup of scores of friends, since a facebookId can't be mapped to a firebaseId without storing it in the database anyway. It looks like this:
facebookUsers {
facebookId {
"firebaseId" : firebaseId,
"scores" : {
level : score,
}
}
}
This structure allows you to easily lookup scores by facebookId, and also map facebookIds to firebaseIds if desired for other operations.
To save scores:
FirebaseDatabase.DefaultInstance
.GetReference("facebookUsers")
.Child(facebookId)
.Child("scores")
.Child(level)
.SetValueAsync(score);
To lookup scores:
FirebaseDatabase.DefaultInstance
.GetReference("facebookUsers")
.Child(facebookId)
.Child("scores")
.Child(level)
.GetValueAsync();
To restrict writes based on facebookId, use the following rules:
{
"rules": {
".read": "auth != null",
"facebookUsers": {
"$facebookId" : {
".write": "data.child('firebaseId').val() == auth.uid || (!data.exists() && newData.child('firebaseId').val() == auth.uid)",
}
}
}
}
Hopefully this helps someone more than a downvote.

Add array object to minimongo

I have a chat app, that is using Ionic 2 and Meteor with MongoDB. It works perfectly.
However, everything is stored in the MongoDB on the server, so each time a user wants to view their messages, they need to be connected to the Meteor/Mongo Server running in the cloud. Also, if one user deletes their chat, it will delete the chat on the MongoDB, and the corresponding other user will also have their chat deleted.
I would like similar functionality as WhatsApp where the messages are held locally on the device (I am using SQLite), and only new messages are held in the cloud until both users download them.
Currently my app iterates over a Mongo.Cursor<Chat> object. It also observes this object (this.chats.observe({changed: (newChat, oldChat) => this.disposeChat(oldChat), removed: (chat) => this.disposeChat(chat)});).
I get chat data from SQLlite that I have stored locally (Array<Chat>).
Question
Is it possible to add the SQLite data (Array<Chat>) to the Mongo.Cursor<Chat>? When I do so, I want to just add to minimongo and not MongoDB on the server.
Thanks
UPDATE
Asp per advise below, I do the following:
let promise: Promise<Mongo.Cursor<Chat>> = new Promise<Mongo.Cursor<Chat>>(resolve => {
this.subscribe('chats', this.senderId, registeredIds, () => {
let chats: Mongo.Cursor<Chat> = Chats.find(
{ memberIds: { $in: registeredIds } },
{
sort: { lastMessageCreatedAt: -1 },
transform: this.transformChat.bind(this),
fields: { memberIds: 1, lastMessageCreatedAt: 1 }
}
);
this.localChatCollection = new Mongo.Collection<Chat>(null);
console.log(this.localChatCollection);
chats.forEach(function (chat: Chat) {
console.log('findChats(): add chat to collection: ' + chat);
this.localChatCollection.insert(chat);
});
Will update if it works.
UPDATE
When I do the following, it inserts the chat object:
let promise: Promise<Mongo.Collection<Chat>> = this.findChats();
promise.then((data: Mongo.Collection<Chat>) => {
let localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null);
data.find().forEach(function (chat: Chat) {
console.log('==> ' + chat);
localChatCollection.insert(chat);
});
However, if I define the localChatCollection globally, it does not insert the chat object. There are no errors but the process just stops on the insert line.
private localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null);
....
this.localChatCollection.insert(chat);
Any ideas how I can get this to insert into a globally defined collection?
Is it possible to add the SQLite data (Array) to the Mongo.Cursor? When I do so, I want to just add to minimongo and not MongoDB on the server.
Meteor itself knows nothing about SQLite, but it sounds like you have that part of it working.
To just add to minimongo and not the mongodb server, you're looking for a client-side collection. Just pass null in as the first parameter to the call to create your collection i.e.
var localChatCollection = new Mongo.Collection(null)
You can then insert to localChatCollection the same way you would with a synchronized collection.
Source: Meteor docs