Use atproto actions with ease in iOS shortcuts
1//
2// UserAccountParameter.swift
3// shortcut
4//
5// Created by Bailey Townsend on 6/29/25.
6//
7
8import AppIntents
9import Foundation
10
11struct AtIdentifierAppEntity: AppEntity {
12 //The users handle
13 var id: UUID
14
15 @Property
16 var handle: String
17
18 @Property
19 var did: String
20
21 static var typeDisplayName: LocalizedStringResource = "Saved AT Identifier"
22
23 static var typeDescription: LocalizedStringResource? =
24 "This is your saved user session that is created inside of the app"
25
26 var displayRepresentation: AppIntents.DisplayRepresentation {
27 DisplayRepresentation(title: LocalizedStringResource(stringLiteral: handle))
28 }
29
30 static var typeDisplayRepresentation: TypeDisplayRepresentation = TypeDisplayRepresentation(
31 name: LocalizedStringResource("AT Identifier"))
32
33 static var defaultQuery = AtIdentifierAppEntityQuery()
34}
35
36struct AtIdentifierAppEntityQuery: EntityQuery {
37
38 func entities(for identifiers: [UUID]) async throws -> [AtIdentifierAppEntity] {
39 let atProtocolManager = AtProtocolManager()
40 do {
41 let sessions = try await atProtocolManager.getSessions(sessionIDs: identifiers)
42 if sessions.isEmpty {
43 return []
44
45 }
46 return sessions.map { session in
47 let userAccount: AtIdentifierAppEntity = AtIdentifierAppEntity(id: session.id)
48 userAccount.handle = session.handle
49 userAccount.did = session.sessionDID
50 return userAccount
51 }
52 } // return identifiers.compactMap { SessionManager.session(for: $0) }
53
54 catch {
55 print(error)
56 throw GenericIntentError.message(
57 "Could not retrieve saved AT Identifiers. Please make sure you have logged in via the app"
58 )
59 }
60 }
61
62 func suggestedEntities() async throws -> [AtIdentifierAppEntity] {
63
64 let atProtocolManager = AtProtocolManager()
65 do {
66 let sessions = try await atProtocolManager.getAllSessions()
67 if sessions.isEmpty {
68 return []
69 }
70 return sessions.map { session in
71 let userAccount: AtIdentifierAppEntity = AtIdentifierAppEntity(id: session.id)
72 userAccount.handle = session.handle
73 userAccount.did = session.sessionDID
74 return userAccount
75 }
76 }
77
78 catch {
79 throw GenericIntentError.message(
80 "Could not retrieve saved AT Identifiers. Please make sure you have logged in via the app"
81 )
82 }
83 }
84}