This loop creates an array of post_id that will appear as unread for the user being synchronized (only those which the user is authorised to read). If you look downside, the next query fills unread table with that ids.