Query Posts

The social.plus SDK provides powerful post querying functionality that enables flexible content discovery and filtering. Query posts from communities, user feeds, or across the entire platform with customizable search criteria and real-time results.

Overview

Flexible Filtering

Filter by post type, target, deletion status, and custom criteria

Live Results

Query results are returned as live collections with real-time updates

Query Parameters

ParameterTypeDescriptionOptions
targetIdStringCommunity or user IDCommunity ID, User ID
targetTypeStringType of targetcommunity, user
typesArrayPost content typestext, image, video, file, poll, liveStream, custom
includeDeletedBooleanInclude deleted poststrue, false (default)
sortByStringSort orderlastCreated (default), firstCreated
feedTypeStringFeed type filterpublished, reviewing, declined

Basic Post Querying

import AmitySDK

class PostQueryManager {
    private let client: AmityClient
    
    init(client: AmityClient) {
        self.client = client
    }
    
    // Query community posts with basic filters
    func queryCommunityPosts(
        communityId: String,
        postTypes: [AmityPostDataType] = [],
        includeDeleted: Bool = false
    ) -> AmityCollection<AmityPost> {
        
        let repository = AmityPostRepository(client: client)
        let query = AmityPostQuery()
            .targetId(communityId)
            .targetType(.community)
            .sortBy(.lastCreated)
            .includeDeleted(includeDeleted)
        
        if !postTypes.isEmpty {
            query.types(postTypes)
        }
        
        return repository.getPosts(query: query)
    }
    
    // Query user feed posts
    func queryUserPosts(
        userId: String,
        sortBy: AmityPostQuerySortOption = .lastCreated
    ) -> AmityCollection<AmityPost> {
        
        let repository = AmityPostRepository(client: client)
        let query = AmityPostQuery()
            .targetId(userId)
            .targetType(.user)
            .sortBy(sortBy)
            .includeDeleted(false)
        
        return repository.getPosts(query: query)
    }
    
    // Advanced media gallery query
    func queryMediaGallery(
        targetId: String,
        targetType: AmityPostTargetType,
        mediaTypes: [AmityPostDataType] = [.image, .video]
    ) -> AmityCollection<AmityPost> {
        
        let repository = AmityPostRepository(client: client)
        let query = AmityPostQuery()
            .targetId(targetId)
            .targetType(targetType)
            .types(mediaTypes)
            .sortBy(.lastCreated)
            .includeDeleted(false)
        
        return repository.getPosts(query: query)
    }
    
    // Query posts with live observation
    func observePosts(
        communityId: String,
        onUpdate: @escaping ([AmityPost]) -> Void,
        onError: @escaping (Error) -> Void
    ) -> AmityNotificationToken {
        
        let collection = queryCommunityPosts(communityId: communityId)
        
        return collection.observe { result in
            switch result {
            case .success(let postsInfo):
                let posts = postsInfo.object
                print("Posts updated: \(posts.count) total")
                onUpdate(posts)
                
            case .failure(let error):
                print("Query error: \(error)")
                onError(error)
            }
        }
    }
    
    // Query with pagination
    func queryPostsWithPagination(
        communityId: String,
        pageSize: Int = 20
    ) -> AmityCollection<AmityPost> {
        
        let repository = AmityPostRepository(client: client)
        let query = AmityPostQuery()
            .targetId(communityId)
            .targetType(.community)
            .sortBy(.lastCreated)
            .limit(pageSize)
        
        return repository.getPosts(query: query)
    }
    
    // Query posts for moderation
    func queryPostsForModeration(
        communityId: String,
        feedType: AmityPostFeedType = .reviewing
    ) -> AmityCollection<AmityPost> {
        
        let repository = AmityPostRepository(client: client)
        let query = AmityPostQuery()
            .targetId(communityId)
            .targetType(.community)
            .feedType(feedType)
            .sortBy(.lastCreated)
            .includeDeleted(true)
        
        return repository.getPosts(query: query)
    }
}

Advanced Query Features

Common Use Cases

Community Feed

Display all posts in a community with real-time updates and customizable filtering options.

User Profile

Show a user’s post history with privacy controls and content type filtering.

Media Gallery

Create image and video galleries by filtering posts with media attachments.

Content Moderation

Review pending posts and manage community content with moderation-specific queries.

Analytics Dashboard

Gather post data for engagement metrics and community insights.

Search Results

Implement advanced search functionality with multiple filter criteria.

Best Practices

Query Examples

Custom Post Types: For custom post types, use a namespace-like format (e.g., "my.customtype"). This ensures proper categorization and prevents conflicts with built-in types.
Performance Note: Querying large datasets without proper filtering can impact performance. Always use appropriate filters and pagination for optimal user experience.