SELECT COUNT(DISTINCT f.id) FROM files f 
                 INNER JOIN file_shares fs ON f.id = fs.file_id 
                 WHERE (fs.shared_with_user_id = {user_id} OR fs.shared_with_role_id = {role_id})
                 AND (fs.expires_at IS NULL OR fs.expires_at > NOW())
                 AND f.uploaded_by != {user_id}