[Feature Enhancement]: UserSessionRegistry as a Protocol

オープン
#190 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
swift

調査の方向性

Read Sources/ATProtoKit/APIReference/SessionManager/UserSessionRegistry.swift and the linked working branch to understand the existing registry and proposed protocol. Check how session persistence and token storage are handled, then determine what documentation and tests are needed for a supported custom implementation; done means the protocol, persistence guidance, and behavior are validated in a real app.

索引モデルが issue の本文から書いたものです。

説明

enhancement
Summary

As a developer building an ios app, it would be nice to have UserSessionRegistry as a protocol, so that users can pass in their own implmentation to persist the sessions

Pain points

I've been building out an app using this package and found that the UserSessionRegistry seems to be only in memory. I propose changing this to be a protocol so each system can provide a way to save these, for me that was with SwiftData. and also saving the accessToken in the keychain, like the refreshToken is

Considered Alternatives

There is a chance that I may have misunderstood how to use UserSessionRegistry, but I could not find a solution that would allow that to persist besides reloading it manually every reboot of the app

Is this a breaking change?

No

Additional Context

I acutally already have a working branch found here fatfingers23/ATProtoKit/tree/feature/UserSessionRegistryProtocol. So far it seems to be working, but I would like to clean it up, add proper formating, add documentation, and test more in a real life scenario before making a PR. But feel free to use it or propose a different approach, and I don't mind putting in the time to make it a proper PR to give back to the project

I have a SwiftData implementation of the UserSessionRegistry protocol here that is working in my app

import ATProtoKit
import Foundation
import SwiftData

@MainActor
public struct PersistentUserSessionRegistry: UserSessionRegistry {

  private var modelContext: ModelContext

  init(modelContext: ModelContext) {
    self.modelContext = modelContext
  }

  public func register(_ id: UUID, session: UserSession) async {
    let model = UserSessionToModel(id, session: session)
    self.modelContext.insert(model)
    do {
      try self.modelContext.save()
    } catch {
      print("Error saving user session: \(error)")
    }

  }

  public func getSession(for id: UUID) async -> UserSession? {
    let descriptor = FetchDescriptor<UserSessionModel>(
      predicate: #Predicate { $0.id == id }
    )

    do {
      let models = try self.modelContext.fetch(descriptor)
      guard let model = models.first else { return nil }
      return ModelToUserSession(model)
    } catch {
      return nil
    }
  }

  public func containsSession(for id: UUID) async -> Bool {
    let descriptor = FetchDescriptor<UserSessionModel>(
      predicate: #Predicate { $0.id == id }
    )

    do {
      let count = try self.modelContext.fetchCount(descriptor)
      return count > 0
    } catch {
      return false
    }
  }

  public func removeSession(for id: UUID) async {
    let descriptor = FetchDescriptor<UserSessionModel>(
      predicate: #Predicate { $0.id == id }
    )

    do {
      let models = try self.modelContext.fetch(descriptor)
      for model in models {
        self.modelContext.delete(model)
      }
      try self.modelContext.save()
    } catch {
      // Handle error silently or log as needed
    }
  }

  public func removeAllSessions() async {
    let descriptor = FetchDescriptor<UserSessionModel>()

    do {
      let models = try self.modelContext.fetch(descriptor)
      for model in models {
        self.modelContext.delete(model)
      }
      try self.modelContext.save()
    } catch {
      // Handle error silently or log as needed
    }
  }

  public func getAllSessions() async -> [UUID: UserSession] {
    let descriptor = FetchDescriptor<UserSessionModel>()

    do {
      let models = try self.modelContext.fetch(descriptor)
      var sessions: [UUID: UserSession] = [:]

      for model in models {
        let session = ModelToUserSession(model)
        sessions[model.id] = session
      }

      return sessions
    } catch {
      return [:]
    }
  }

}

private func ModelToUserSession(_ model: UserSessionModel) -> UserSession {
  return UserSession(
    handle: model.handle,
    sessionDID: model.sessionDID,
    email: model.email,
    isEmailConfirmed: model.isEmailConfirmed,
    isEmailAuthenticationFactorEnabled: model.isEmailAuthenticationFactorEnabled,
    didDocument: model.didDocument,
    isActive: model.isActive,
    status: model.status,
    serviceEndpoint: model.serviceEndpoint,
    pdsURL: model.pdsURL
  )
}

private func UserSessionToModel(_ id: UUID, session: UserSession) -> UserSessionModel {
  return UserSessionModel(
    sessionId: id,
    handle: session.handle,
    sessionDID: session.sessionDID,
    email: session.email,
    isEmailConfirmed: session.isEmailConfirmed,
    isEmailAuthenticationFactorEnabled: session.isEmailAuthenticationFactorEnabled,
    didDocument: session.didDocument,
    isActive: session.isActive,
    status: session.status,
    serviceEndpoint: session.serviceEndpoint,
    pdsURL: session.pdsURL)
}
主要言語
Swift
スター
226
フォーク
40
平均マージ
5時間 8分
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MasterJ93/ATProtoKit のほかの issue

MasterJ93/ATProtoKit の issue をすべて見る

似ている issue

Swift の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。