Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Created November 28, 2024 00:08
Show Gist options
  • Save brennanMKE/72e643c5b313c20699b79d100df8169e to your computer and use it in GitHub Desktop.
Save brennanMKE/72e643c5b313c20699b79d100df8169e to your computer and use it in GitHub Desktop.
AT Protocol in Swift

To get started with implementing your custom feed and moderation services using the ATProtoKit Swift package, follow these steps:

  1. Clone the Repository

First, clone the ATProtoKit repository to review the examples and understand the package:

git clone https://github.com/MasterJ93/ATProtoKit.git
cd ATProtoKit
  1. Integrate ATProtoKit into Your Project

Add the package to your Swift project using Swift Package Manager:

  • Open your Xcode project or workspace.
  • Go to File > Add Packages.
  • Enter the repository URL:

https://github.com/MasterJ93/ATProtoKit

  • Choose the latest version and add it to your project.
  1. Set Up Your Environment

Before creating custom feeds or moderation services, you'll need:

  • An AT Protocol server: Either use Bluesky's API endpoints or set up a custom AT Protocol server.
  • Authentication: Obtain an authentication token for interacting with the AT Protocol API.
  1. Using ATProtoKit

ATProtoKit simplifies working with the AT Protocol. Below are examples to help you start with custom feed and moderation features.

A. Authenticate the User

You need a session to make authenticated requests.

import ATProtoKit

// Initialize the client
let client = ATProtocolClient()

// Log in with username and password
Task {
    do {
        let session = try await client.login(username: "your_username", password: "your_password")
        print("Logged in! Session:", session)
    } catch {
        print("Login failed:", error)
    }
}

After logging in, store the session to authenticate future API requests.

B. Fetch and Modify Feeds

To implement a custom feed, you'll interact with the feed endpoints. Here's an example to fetch and display posts:

Task {
    do {
        let posts = try await client.fetchFeedPosts(for: "your_feed_url")
        for post in posts {
            print("Post by \(post.author.handle): \(post.content.text)")
        }
    } catch {
        print("Error fetching posts:", error)
    }
}

For a custom feed:

  • Define the logic for curating posts (e.g., based on hashtags, follows, or other criteria).
  • Use the post endpoints to publish curated content back to the feed.

C. Implement Moderation

Moderation involves analyzing content and taking appropriate actions (e.g., hiding, flagging, or removing posts).

Task {
    do {
        // Fetch moderation reports
        let reports = try await client.fetchModerationReports()
        for report in reports {
            print("Reported post: \(report.postId), Reason: \(report.reason)")
        }
        
        // Example: Hide a post
        let result = try await client.hidePost(postId: "post_id_to_hide")
        print("Post hidden:", result)
    } catch {
        print("Moderation error:", error)
    }
}

Custom Logic:

  • Integrate automated moderation using AI/ML tools (e.g., Core ML or third-party APIs) to analyze text and images.
  • Build moderation queues for human review using custom APIs or local databases.
  1. Extend Functionality

The ATProtoKit package is extensible. Here's how you can extend it:

  • Custom Endpoints: Add new functions to call additional AT Protocol APIs.
  • Custom Feed Logic: Use Swift's Codable and Combine to manage feed curation workflows.
  • Local Storage: Cache data locally using Core Data or SQLite for offline capabilities.
  1. Testing and Deployment
  • Use a test environment to simulate API interactions.
  • Deploy your services or feeds on a server to allow others to interact with them.
  • Host your own AT Protocol server if necessary for full control.

Example Project

Create a sample Xcode project integrating ATProtoKit:

  1. Set up a basic SwiftUI interface to display feeds.
  2. Use a background Task to fetch posts and apply moderation.
  3. Add controls to enable users to interact with custom feeds.

Let me know if you'd like a specific implementation walkthrough for any of these steps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment