Query to fetch sent items in a conversation - office365api

Currently i am using a below query which returns all mail items in a conversation which i feel pretty inefficient where i only require sent folder items. How to modify filter query to return items sent by current user.
Query :
let query = "ConversationId eq 'AAQkAGUzZmY0NThiLTUxMjAtNDBhNC05NGFkLTQ3NmRlYjlkMzRiNgAQAOO9BXAJn/VDvZTkMjcb52s='";
https://outlook.office.com/api/beta/me/Messages?$filter=encodeURIComponent(query)

You need to scope your path to only the Sent Items folder:
/me/mailFolders('SentItems')/messages$filter=encodeURIComponent(query)

Related

How to get nil object when there is no record for a specific value in where clause in rails

Currently, i am fetching the user records by using phone number field in Where query in rails like below.
users = User.where(phone: ["123421341234", "123423144", "123423144","444633333",,,,,,,,,,,,])
i have the user records, for the first three mobile numbers. But for the 4'th mobile number(444633333), there is no user record in the table, so for this user i want to get the "nil" object.(if user exists then the user object should be returned)
How can i change the above query. the resultant array should contain the objects in the sequence(in the search array sequence)
I think the only way to do this without making so many queries to the DB is to load all users by the phone_number you already have first then map your phone_numbers to the users found
phones_numbers = [7799569, 7818111, 7820442, 78343033, 78347700, 7836863, 7837873, 7837898, 7838025, 7838442]
users = User.where(phone_number: phones_numbers)
users = phones_numbers.map { |number| users.find_by(phones_number: number) }
the users loaded will be cached in the memory so even if there are queries they run time will be insignificant
you can also use #detect if you wanna do this on ruby/rails level
#same stuff as above
users = phones_numbers.map { |number| users.detect { |user| user.phones_number == number } }

Convert a hash of objects to json in Ruby

I have a hash H which should contain various users as json. "users" list contains all users and every user object contains various user's details like name, age etc. I don't want to iterate over every user in the list and do user.as_json and then merge into the hash.
Is there a single line query which does this?
You can do this, in your action convert it to JSON by using to_json
#users = users.to_json
In the Javascript code, parse it using jQuery's $.parseJSON()
$.parseJSON(#users)

Count current users on the page

I'm trying to count current viewers on the particular page. I need this count to be stored in the DB. The main trouble is to clean up after user leaves the page.
Users are anonymous. Every active user sends AJAX-request every 5 seconds.
What's the best algorithm to do that? Any suggestions?
UPD: I'm trying to reduce amount of queries to the DB, so, I think, I don't really need to store that count in the DB while I can access it other way from the code.
Don't even think about storing this in database, your app will be incredibly slowed down.
So use Cache for this kind of operation.
To count the number of people, I'd say:
assign a random ID to each anonymous user and store it in his session
send the ID within your ajax call
store an Array of Hashes in cache with [{ :user_id, :latest_ping }, {} ] (create a cache var for each page)
delete the elements of the array which appear to be too old
you've your solution: number of users = nb of elements in the array
If you store the users in the database somehow, you could store a last_seen_at field in the users table, and update that with Time.now for every AJAX request that user sends.
To display how many users you currently have, you can just perform a query such as:
#user_count = User.where("last_seen_at < ?", 5.seconds.ago).count
If you want to clean up old users, I suggest that you run some kind of cron job, or use the whenever gem, or something like that, to periodically delete all users that haven't been seen for some time.
I would suggest you create a model that contains a unique key (cookie-id or something) that you save or update with every AJAX heartbeat request.
You then have a session controller that could look like this:
def create
ActiveUser.where(:cookie => params[:id]) || ActiveUser.new
ActiveUser.cookie = prams[:id]
ActiveUser.timestamp = Time.now
ActiveUser.save
end
Your number of active users is then simply a SELECT COUNT(*) FROM ActiveUsers WHERE timestamp > NOW() - 5 or something like that.
Martin Frost is on the right track. There's the #touch method to update last_seen_at: user.touch(:last_seen_at)
But it would be even more efficient to just update the user without having to fetch the model from the database:
> User.update_all({:last_seen_at => Time.now}, {:id => params[:user_id})
SQL (3.1ms) UPDATE "users" SET "last_seen_at" = '2011-11-17 12:37:46.863660' WHERE "users"."id" = 27
=> 1

How to select all of a unique attribute

I would like to find all the Records that have unique email addresses. I am trying to perform this :
#uniq_referrals = []
CardReferral.all.select{|a| #uniq_referrals << a.email}
#referral_count = #uniq_referrals.uniq.each{|a|CardReferral.find_by_email(a)}
But I would like to do it in a single call. Is that possible?
You can use:
CardReferral.select("distinct(email), other_field_you_need")
where other_field_you_need it's a list of field name you need to use from the objects you get from ActiveRecord.
To get the count of unique email record you can use:
CardReferral.count("distinct(email)")
I would like to find all the Records that have unique email addresses. I am trying to perform this :
#uniq_referrals = []
CardReferral.all.select{|a| #uniq_referrals << a.email}
#referral_count = #uniq_referrals.uniq.each{|a|CardReferral.find_by_email(a)}
But I would like to do it in a single call. Is that possible?
I think that it would help you:
Model.group("email")
when we group all the record by email id then you will find all the record that have uniq email. if some record have same id then you will get first record.
please prefer to image for more understanding.
If you could not get yet, then i will explain elaborate more.

Order two #variables together by date

I have a messaging system with two tables: Message and MessageCopy, where each time a message is sent it generates a message_copy for each recipient (with info like read, saved, deleted, etc.).
I would like to show a 'conversation' with sent and received messages for my user, and I would like to splice together sent and received messages, ordered by created_at.
Here are my two currently separated relations.
#sentmessages = Message.where("author_id = ? AND project_id = ?", current_user.id, #project.id).order("created_at DESC")
#receivedmessages = MessageCopy.where("recipient_id = ? AND project_id = ?", current_user.id, #project.id).order("created_at DESC")
I'd like to put these together into one relation using Rails if possible rather than MySQL. Thanks!
I hope I understand correctly - you want a single variable that stores these objects ordered by date.
(#sentmessages + #receivedmessages).sort_by(&:created_at).reverse

Resources