Facebook: Is it possible to show only the user OWN posts - facebook-graph-api

I ask for the wall posts of a user and I also get the posts of other people who posted there.
How do I post ONLY the posts of the user?

In FQL:
SELECT post_id, message FROM stream WHERE source_id = <uid> AND actor_id = <uid>
If both source_id and actor_id are the same you will get only that user's posts to his own wall. (See http://developers.facebook.com/docs/reference/fql/stream)
Is that what you are trying to do? What have you got so far?

Related

how to get only results in many to many relationship in django

I have the following relationships:
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
class Post(models.Model):
customer = models.ForeignKey('common.Customer',
mentions = models.ManyToManyField('common.Customer',related_name='mentions')
I want to get all of the users that are mentioned in a post. I'm thinking something like this:
customer = Customer.objects.get(user=request.user)
posts = Post.objects.filter(mentions__in=customer).order_by('-created_at')
Is this close to what I'm trying to accomplish?
Try this line
users = User.objects.filter(mentions__isnull=False)
I saw this in the django many to many documentation and it worked:
posts = Post.objects.filter(mentions__pk=customer.id)
It absolutely isn't, I'm afraid.
customer = Customer.objects.get(user=request.user)
posts = Post.objects.filter(mentions__in=customer).order_by('-created_at')
Would fail at mentions__in = customer because the __in lookup expects an iterable (which a single customer is not).
Aside from that, that query would give you all posts in which customer was mentioned, which could also be achieved in two more straight forward ways:
posts = Post.objects.filter(mentions=customer).order_by('-created_at')
posts = customer.mentions.order_by('-created_at') # using the 'related_name' from the customer's side
You want to get all the users that are mentioned in a post. But what post? You forgot to mention that in your question. You only gave us the current user (request.user), who can have multiple posts.
I'm going to guess and show how you could get all other users mentioned in posts made by the current user.
To make things clearer in respect to the related_name of that relation, I will change it to related_name = 'mentionend'.
posts = Post.objects.filter(mentions=customer) # all posts of the current user
# all other users mentioned in those posts
users = Customer.objects.exclude(user=customer).filter(mentionend__in=posts) # using 'related_name'
# or
users = posts.mentions.exclude(user=customer)

Createing Album using Graph API

I am trying to create an album in some Facebook test users using V2.2. I tried couple of APIs (as below)
POST http://graph.facebook.com/{user-id}/albums (passed "Name" field with value)
POST http://graph.facebook.com/v2.2/me/albums (passed "Name" field with value)
As a response I get some "id". I believe that's the album id. But when I cross check using client by login in to user account -> Photos, I don't see any album created there.
Can somebody assist with the same?

With django, how can I retrieve if the user "likes" the article of other items?

I have 3 tables in my database, the first one is the "user" table with users' basic information, the second one is the "article" table, and the last one is the "user_like_article" table which store a record for a user and a article if the user likes the article.
For instance, if a user A like an article X, the the record in the "user_like_article" table will be like this: (A,X)
So now, I want to indicate whether the user, who has logged in, like a certain article in the article list I diplay.
I have figure out a way. I first get the article list. After that, I get the list of article's id which the specific user likes. The other things work like the code below
for article in articles_to_diplay:
if article in article_list_user_likes:
article['like'] = True
This method works, but somehow I think it is too expensive. Displaying "like" button is just a little part of my project, but it is indeed very common. Facebook, Twitter, blogs, "like" is in widespread use.
Is there any way less expensive to accomplish this feature?
Thank you very much!
It's not quite simple to use some nice looking API, since we check query against logged in user object, so we need to inject is_liked method for each article object.
Also you can use ManyToManyField field for Article model, instead creating additional table (unless you're using some extra data, then let me know, so I can modify the answer accordingly.
models.py
from django.contrib.auth.models import User
class Article(models.Model):
title = models.CharField(max_length=50)
likes = models.ManyToManyField(User)
def __unicode__(self):
return self.title
views.py
def home(request):
articles = Article.objects.all().prefetch_related('likes')
for a in articles:
a.is_liked = (request.user in a.likes.all())
return render(request, 'home.html', {'articles': articles})

FQL accessing Friends Likes

How do I access friends likes with FQL? I can see it in the Graph API:
https://graph.facebook.com/1111111/likes
... but I can't seem to figure out how to do it in FQL.
fql?q=SELECT uid, page_id from page_fan where uid IN(SELECT uid2 from friend where uid1=me())
You can try this in your api explorer to get user friends likes.
You can do this by querying url_like table:
SELECT url FROM url_like WHERE user_id IN (
SELECT uid2 FROM friend WHERE uid1 = me()
)
You cannot however get list of likes by friends for: photo, album, event, group, note, link, video, application, status, check-in, review, comment, post.
This info is located in like table but to query it based on user_id you need read_stream permissions from that user...
EDIT: Added Graph API Explorer Example to easily preview this:
http://developers.facebook.com/tools/explorer?method=GET&path=fql%3Fq%3DSELECT%20url%20FROM%20url_like%20WHERE%20user_id%20IN%20(SELECT%20uid2%20FROM%20friend%20WHERE%20uid1%20%3D%20me())

Single fql query for "friend id" to display latest photos

I am looking for optimized FQL way getting latest photos per user_id, the only one I can think now is to get aid's (album id's) and query each aid for photos ... (this is lots of querying)
You can use subquery, should be like so:
SELECT object_id, src_small
FROM photo
WHERE aid IN (SELECT aid FROM album WHERE owner = me())
ORDER BY created DESC LIMIT 1
hope this helps