Updating stars in Firebase post by transaction - ios

According to the Firebase documentation, it is recommended to duplicate the store data for faster access like so (Firebase example):
let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
"author": username,
"title": title,
"body": body]
let childUpdates = ["/posts/\(key)": post,
"/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)
And, afterwords if you want to add a rating/stars to the post, you should do it with a runTransactionBlock call to a reference:
ref.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in {}
The reference to this last call is the reference to the post you wanna rate
My question is, How are we suppose to update two different references in the same transaction?
Should we make 2 of them? It doesn't seem really safe to me to do it this way
Thank you for your help

Related

how to updata new item in Firebase

I want to add a new item to my list, I use firebase without user(email,password)
I want to add new items like this:
list
vdjs43ndjufd87
"name" : Jak
"university" : Harvard
list
vdjs43ndjufd87
"name" : Jak
"university" : Harvard
"age" : 23
this code is not working for me
let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
"author": username,
"title": title,
"body": body]
let childUpdates = ["/posts/\(key)": post,
"/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)`
Did you check that you were allowed to add items to you database?
By default, the firebase database has a protection that you need to deactivate while you're in development if you didn't implement any connection module for the users of your app yet.
Don't forget to reactivate this protection before submitting your app though!
You can read more about rules here: https://firebase.google.com/docs/database/security/

How do I connect my database to API.AI?

How do I connect my database to API.AI
Making every sentence into INTENT and creating entities for each doesn't seem to be a good idea? So what is the best possible way to go about?
As far as I know it is not possible yet, but you can switch to row mode and past your entities inCVS or JSON format OR import a JSON/CSV file containing all your entities.
The file should look like below (JSON format):
[
{
"value": "val1",
"synonyms": [
"syn1",
"syn2"
]
},
{
"value": "val2",
"synonyms": [
"syn21",
"syn22"
]
},
]
So you can image of writing a small job that reads entities from you DB and make a JSON/CSV file according the wanted format.
Once the job done, this process may dramatically facilitate the creation of your entities on api.ai.
If you use a webhook for an intent, you can pass params to your endpoint where you can do all the queries to your db
I did a demo where I was querying news (cheating as I was getting it from the web, but I could plug a DB).
The was getting requests such as:
"What are the latest news about France"
latest and France would be params that I send through to the webhook endpoint.
You would get the following JSON sent your endpoint by API.AI
"result": {
"source": "agent",
"resolvedQuery": "latest news about France",
"action": "show.news",
"actionIncomplete": false,
"parameters": {
"adjective": "latest",
"subject": "France"
}
Then you can query all the news for France and order them by latest
In my understanding the idea is to create entities that are "placeholders" for the values you need to query.
Then you teach the AI with few examples by tagging in the request what did the person ask. Let say someone asks:
"what is the oldest news about France?"
The AI may not know what is oldest thus you tell it is is an adjective and from now on you can get oldest as a param

Firebase append data to node

How can I append data to a node in Firebase? The value update would just be a string, not a dictionary.
Firebase should be able to increment each items. I thought update would work, but that only updates a dictionary. When I attempt to set the value, it erases everything.
FIRDatabase().reference().database.childRef(refUrl: path).updateChildValues([:])
Example:
node{
1:itemOne
2:itemTwo
3:itemThree
}
From firebase docs
https://firebase.google.com/docs/database/ios/read-and-write
Update specific fields
To simultaneously write to specific children of a node without overwriting other child nodes, use the updateChildValues method.
try something like
let key = ref.child("node").childByAutoId().key
let post = ["uid": userID,
"author": username,
"title": title,
"body": body]
let childUpdates = ["/posts/\(key)": post,
"/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)

Look a message's text from events api response

I'm using slacks events API and have setup a subscription to the reactions_added event. Now when a reaction is added to a message, slack will send me a post body with all the details of the dispatched event as described here.
The problem I'm having is that I want to get the details, specifically the text of the message that my users have reacted to so I can parse/store etc that specific message. I assumed the message would return with some type of UUID that I could then respond to the callback and get the text, however I'm find it difficult to get the specific message.
The only endpoint I see available is the channels.history, which doesn't seem to give me the granularity I'm looking for.
So the tl;dr is: How do I look up a via slacks API, a messages text sent from the events API? Give the information I have the event_ts, channel and message ts I thought would be enough. I'm using the ruby slack-api gem FWIW.
You can indeed use the method channels.history (https://api.slack.com/methods/channels.history) to retrieve message from a public channel . The reaction_added dispatched event includes the channel ID and timestamp of the original message (in the item) and the combination of channelId + timestamp should be unique.
Be careful that you use the correct timestamp though. You need to use item.ts not event_ts
Full example dispatched event from the docs:
{
"token": "z26uFbvR1xHJEdHE1OQiO6t8",
"team_id": "T061EG9RZ",
"api_app_id": "A0FFV41KK",
"event": {
"type": "reaction_added",
"user": "U061F1EUR",
"item": {
"type": "message",
"channel": "C061EG9SL",
"ts": "1464196127.000002"
},
"reaction": "slightly_smiling_face"
},
"event_ts": "1465244570.336841",
"type": "event_callback",
"authed_users": [
"U061F7AUR"
]}
So calling channels.history with these values set should work:
latest = item.ts value
oldest = item.ts value
inclusive = 1
channel = item.channel value
If you want to get messages from a private channel you need to use groups.history.
https://api.slack.com/methods/channels.history

Is Possible to POST array of dictionaries in Afnetworking POST Method?

I am developing one iOS application that Post data to web service and Get data from the Web service.
I am using Afnetworking for Post the data to web service.Web service accept the data in the form of array of dictionaries like
[{"name":"stephen","age":25},{"name":"john","age":35},{"name":"david","age":45},{"name":"roger","age":15}]
which need to send in the body of the request and set Accept application/json in the header of the request.
I checked some examples already avilable in the stack overflow but all are explain about Post dictionary .My question is possible to Post array to web service using Afnetworking. If it is possible help me to develop the code which completely reach my requirements.
If your are sending all dictionaries in single array. Very first you have to JSONserialize every dictionary and then put it in array and then again JSON serialise the whole array. Then only it will be called as a correct JSON format. Its the only way you can do it.
My suggestion for you is to use AFJSONRequestSerializer.
Swift solution:
let manager = AFHTTPSessionManager(baseURL: URL(string: yourBaseURL))
manager.requestSerializer = AFJSONRequestSerializer()
let parameters: [[String: Any]] = [["name": "stephen", "age": 25], ["name": "john", "age": 35],["name": "david", "age": 45]]
manager.post(requestURL,
parameters: parameters,
progress: nil,
success: { (task, data) in
// process server response
}, failure: { (task, error) in
// process error server response
})
AFJSONRequestSerializer sets Content-type to application/json in request header.

Resources