feat(mobile): add Siri and Shortcuts music intents #40

Merged
joshuafhiggins merged 2 commits from t3/expo-app-intents-siri-music-shortcuts into t3/upgrade-next-expo-remove-pod-workarounds 2026-10-22 19:44:30 -04:00
32 changed files with 3833 additions and 54 deletions

No files matched your search

@@ -0,0 +1,42 @@
import Foundation
import AppIntents
internal import ExpoAppIntents
internal import ExpoModulesCore
/// Registers the app-owned intent types and their system integrations.
enum MusicAppIntents {
static func register() {
Task {
await AppIntentDispatcher.shared.setShortcutsRefreshHandler {
AppShortcuts.updateAppShortcutParameters()
}
AppShortcuts.updateAppShortcutParameters()
}
}
}
/// Registered Expo inline module that wires app-target App Intents code to expo-app-intents.
/// Do not change the name of this class.
final class AppIntentsSetup: Module {
public func definition() -> ExpoModulesCore.ModuleDefinition {
Name("AppIntentsSetup")
AsyncFunction("replaceCatalog") { (kind: String, json: String) in
try await MusicCatalogStorage.shared.replace(kind: kind, json: json)
}
AsyncFunction("catalogJSON") { (kind: String) in
let records = try await MusicCatalogStorage.shared.records(kind: kind)
return String(decoding: try JSONEncoder().encode(records), as: UTF8.self)
}
AsyncFunction("completeInvocation") { (token: String, error: String?) in
await MusicIntentExecution.shared.complete(token: token, error: error)
}
OnCreate {
MusicAppIntents.register()
MusicEntityRegistration.register()
}
}
}
@@ -0,0 +1,96 @@
import AppIntents
/// Phrases compiled into the app. Each one includes the app name, and each
/// phrase fills at most one parameter. Siri asks for anything a phrase leaves out.
struct AppShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: PlayMusicIntent(),
phrases: [
"Play music in \(.applicationName)",
"Play \(\.$song) in \(.applicationName)",
"Play the album \(\.$album) in \(.applicationName)",
"Play \(\.$artist) in \(.applicationName)",
"Play the playlist \(\.$playlist) in \(.applicationName)",
],
shortTitle: "Play Music",
systemImageName: "play.fill"
)
AppShortcut(
intent: AddToPlaylistIntent(),
phrases: [
"Add to a playlist in \(.applicationName)",
"Add \(\.$song) to a playlist in \(.applicationName)",
],
shortTitle: "Add to Playlist",
systemImageName: "text.badge.plus"
)
AppShortcut(
intent: AddToUpNextIntent(),
phrases: [
"Play \(\.$song) next in \(.applicationName)",
"Add to Up Next in \(.applicationName)",
],
shortTitle: "Add to Up Next",
systemImageName: "text.line.first.and.arrowtriangle.forward"
)
AppShortcut(
intent: ClearUpNextIntent(),
phrases: [
"Clear Up Next in \(.applicationName)",
],
shortTitle: "Clear Up Next",
systemImageName: "clear"
)
AppShortcut(
intent: CreatePlaylistIntent(),
phrases: [
"Create a playlist in \(.applicationName)",
],
shortTitle: "Create Playlist",
systemImageName: "music.note.list"
)
AppShortcut(
intent: GetCurrentSongIntent(),
phrases: [
"What's playing in \(.applicationName)",
],
shortTitle: "Get Current Song",
systemImageName: "music.note"
)
AppShortcut(
intent: LoveSongIntent(),
phrases: [
"Love this song in \(.applicationName)",
"Favorite this song in \(.applicationName)",
"Favorite song in \(.applicationName)",
],
shortTitle: "Love Song",
systemImageName: "heart.fill"
)
AppShortcut(
intent: PauseMusicIntent(),
phrases: [
"Pause \(.applicationName)",
],
shortTitle: "Pause",
systemImageName: "pause.fill"
)
AppShortcut(
intent: ResumeMusicIntent(),
phrases: [
"Resume \(.applicationName)",
],
shortTitle: "Resume",
systemImageName: "play.fill"
)
AppShortcut(
intent: SkipMusicIntent(),
phrases: [
"Skip this song in \(.applicationName)",
],
shortTitle: "Skip",
systemImageName: "forward.fill"
)
}
}
@@ -0,0 +1,625 @@
#if compiler(>=6.4)
import AppIntents
internal import ExpoAppIntents
import Foundation
import MediaIntents
/// Apple's audio schemas are what Siri and Shortcuts use for Music.app actions
/// such as play, add to library, add to playlist, love, and create station.
/// They require the iOS 27 SDK. Older compilers keep the custom intents in
/// `MusicIntents.swift`.
@available(iOS 27.0, *)
@AppEnum(schema: .audio.playbackAttributes)
enum PlaybackAttributes: String {
case shuffle
case `repeat`
static let caseDisplayRepresentations: [PlaybackAttributes: DisplayRepresentation] = [
.shuffle: "Shuffle",
.`repeat`: "Repeat",
]
}
@available(iOS 27.0, *)
@AppEnum(schema: .audio.queueInsertionLocation)
enum QueueInsertionLocation: String {
case next
case tail
static let caseDisplayRepresentations: [QueueInsertionLocation: DisplayRepresentation] = [
.next: "Play Next",
.tail: "Play Last",
]
}
@available(iOS 27.0, *)
@AppEnum(schema: .audio.affinityState)
enum AffinityState: String {
case like
case dislike
case unset
static let caseDisplayRepresentations: [AffinityState: DisplayRepresentation] = [
.like: "Like",
.dislike: "Dislike",
.unset: "Unset",
]
}
@available(iOS 27.0, *)
@UnionValue
enum PlaylistOwner {
case name(String)
}
@available(iOS 27.0, *)
@AppEntity(schema: .audio.artist)
struct AudioArtistEntity {
static let defaultQuery = AudioArtistQuery()
var id: String
var name: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(name)")
}
init(record: AppIntentEntityRecord) {
id = record.id
name = record.title
}
}
@available(iOS 27.0, *)
struct AudioArtistQuery: EntityStringQuery {
func entities(for identifiers: [AudioArtistEntity.ID]) async throws -> [AudioArtistEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.artistKind, matching: identifiers)
.map(AudioArtistEntity.init(record:))
}
func suggestedEntities() async throws -> [AudioArtistEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(AudioArtistEntity.init(record:))
}
func entities(matching string: String) async throws -> [AudioArtistEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
if string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(AudioArtistEntity.init(record:))
}
return MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.nameScore)
.map(AudioArtistEntity.init(record:))
}
}
@available(iOS 27.0, *)
extension AudioArtistQuery: IntentValueQuery {
func values(for audioSearch: AudioSearch) async throws -> [AudioArtistEntity] {
switch audioSearch.criteria {
case .searchQuery(let query):
return try await entities(matching: query)
case .unspecified:
return try await suggestedEntities()
case .url(let urls):
return try await artists(for: urls)
@unknown default:
return []
}
}
private func artists(for urls: [URL]) async throws -> [AudioArtistEntity] {
let identifiers = urls.compactMap { url -> String? in
guard case .artist(let id) = MusicCatalog.linkTarget(from: url) else {
return nil
}
return id
}
return try await entities(for: identifiers)
}
}
@available(iOS 27.0, *)
@AppEntity(schema: .audio.album)
struct AudioAlbumEntity {
static let defaultQuery = AudioAlbumQuery()
var id: String
var title: String
var artistName: String
var artists: [AudioArtistEntity]
var universalProductCode: String?
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
}
init(record: AppIntentEntityRecord, artistsById: [String: AudioArtistEntity]) {
id = record.id
title = record.title
artistName = record.metadata["artistName"] ?? ""
universalProductCode = nil
if let artist = artistsById[record.metadata["artistId"] ?? ""] {
artists = [artist]
} else {
artists = []
}
}
}
@available(iOS 27.0, *)
struct AudioAlbumQuery: EntityStringQuery {
func entities(for identifiers: [AudioAlbumEntity.ID]) async throws -> [AudioAlbumEntity] {
let artistsById = try await Self.artistsById()
return try await MusicCatalog.records(ofKind: MusicCatalog.albumKind, matching: identifiers)
.map { AudioAlbumEntity(record: $0, artistsById: artistsById) }
}
func suggestedEntities() async throws -> [AudioAlbumEntity] {
let artistsById = try await Self.artistsById()
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map { AudioAlbumEntity(record: $0, artistsById: artistsById) }
}
func entities(matching string: String) async throws -> [AudioAlbumEntity] {
let artistsById = try await Self.artistsById()
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
let matches = string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
? MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
: MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.albumScore)
return matches.map { AudioAlbumEntity(record: $0, artistsById: artistsById) }
}
fileprivate static func artistsById() async throws -> [String: AudioArtistEntity] {
let artists = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
.map(AudioArtistEntity.init(record:))
return Dictionary(uniqueKeysWithValues: artists.map { ($0.id, $0) })
}
}
@available(iOS 27.0, *)
extension AudioAlbumQuery: IntentValueQuery {
func values(for audioSearch: AudioSearch) async throws -> [AudioAlbumEntity] {
switch audioSearch.criteria {
case .searchQuery(let query):
return try await entities(matching: query)
case .unspecified:
return try await suggestedEntities()
case .url(let urls):
let identifiers = urls.compactMap { url -> String? in
guard case .album(let id) = MusicCatalog.linkTarget(from: url) else {
return nil
}
return id
}
return try await entities(for: identifiers)
@unknown default:
return []
}
}
}
@available(iOS 27.0, *)
@AppEntity(schema: .audio.song)
struct AudioSongEntity {
static let defaultQuery = AudioSongQuery()
var id: String
var title: String
var artistName: String
var composerName: String?
var albumTitle: String?
var artists: [AudioArtistEntity]
var album: AudioAlbumEntity?
var composers: [AudioArtistEntity]
var internationalStandardRecordingCode: String?
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
}
init(
record: AppIntentEntityRecord,
artistsById: [String: AudioArtistEntity],
albumsById: [String: AudioAlbumEntity]
) {
id = record.id
title = record.title
artistName = record.metadata["artistName"] ?? ""
composerName = nil
albumTitle = record.metadata["albumTitle"]
composers = []
internationalStandardRecordingCode = nil
if let artist = artistsById[record.metadata["artistId"] ?? ""] {
artists = [artist]
} else {
artists = []
}
album = albumsById[record.metadata["albumId"] ?? ""]
}
}
@available(iOS 27.0, *)
struct AudioSongQuery: EntityStringQuery {
func entities(for identifiers: [AudioSongEntity.ID]) async throws -> [AudioSongEntity] {
let context = try await AudioLibraryContext.load()
return try await MusicCatalog.records(ofKind: MusicCatalog.songKind, matching: identifiers)
.map { context.song(from: $0) }
}
func suggestedEntities() async throws -> [AudioSongEntity] {
let context = try await AudioLibraryContext.load()
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true).map {
context.song(from: $0)
}
}
func entities(matching string: String) async throws -> [AudioSongEntity] {
let context = try await AudioLibraryContext.load()
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
let matches = string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
? MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
: MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.songScore)
return matches.map { context.song(from: $0) }
}
}
@available(iOS 27.0, *)
extension AudioSongQuery: IntentValueQuery {
func values(for audioSearch: AudioSearch) async throws -> [AudioSongEntity] {
switch audioSearch.criteria {
case .searchQuery(let query):
return try await entities(matching: query)
case .unspecified:
return try await suggestedEntities()
case .url(let urls):
let context = try await AudioLibraryContext.load()
var matches: [AppIntentEntityRecord] = []
for url in urls {
guard let target = MusicCatalog.linkTarget(from: url) else {
continue
}
matches.append(contentsOf: try await MusicCatalog.songs(for: target))
}
return matches.map { context.song(from: $0) }
@unknown default:
return []
}
}
}
@available(iOS 27.0, *)
@AppEntity(schema: .audio.playlist)
struct AudioPlaylistEntity {
static let defaultQuery = AudioPlaylistQuery()
var id: String
var title: String
var owner: PlaylistOwner?
var createdByMe: Bool?
var curatedForMe: Bool?
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)")
}
init(record: AppIntentEntityRecord) {
id = record.id
title = record.title
let ownerName = record.metadata["ownerName"] ?? ""
owner = ownerName.isEmpty ? nil : .name(ownerName)
createdByMe = record.metadata["createdByMe"] == "1"
curatedForMe = nil
}
}
@available(iOS 27.0, *)
struct AudioPlaylistQuery: EntityStringQuery {
func entities(for identifiers: [AudioPlaylistEntity.ID]) async throws -> [AudioPlaylistEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind, matching: identifiers)
.map(AudioPlaylistEntity.init(record:))
}
func suggestedEntities() async throws -> [AudioPlaylistEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
.map(AudioPlaylistEntity.init(record:))
}
func entities(matching string: String) async throws -> [AudioPlaylistEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
if string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return records.map(AudioPlaylistEntity.init(record:))
}
return MusicCatalog.rank(records, query: string, limit: 50, score: MusicCatalog.nameScore)
.map(AudioPlaylistEntity.init(record:))
}
}
@available(iOS 27.0, *)
extension AudioPlaylistQuery: IntentValueQuery {
func values(for audioSearch: AudioSearch) async throws -> [AudioPlaylistEntity] {
switch audioSearch.criteria {
case .searchQuery(let query):
return try await entities(matching: query)
case .unspecified:
return try await suggestedEntities()
case .url(let urls):
let identifiers = urls.compactMap { url -> String? in
guard case .playlist(let id) = MusicCatalog.linkTarget(from: url) else {
return nil
}
return id
}
return try await entities(for: identifiers)
@unknown default:
return []
}
}
}
@available(iOS 27.0, *)
@AppEntity(schema: .audio.warmupAudioQueueResult)
struct WarmupAudioQueueResultEntity {
static let defaultQuery = WarmupAudioQueueResultQuery()
var id: String
var title: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)")
}
}
@available(iOS 27.0, *)
struct WarmupAudioQueueResultQuery: EntityStringQuery {
func entities(for identifiers: [WarmupAudioQueueResultEntity.ID]) async throws -> [WarmupAudioQueueResultEntity] {
identifiers.compactMap { id in
guard let entry = WarmupAudioStore.entry(id: id) else {
return nil
}
return WarmupAudioQueueResultEntity(id: entry.id, title: entry.title)
}
}
func suggestedEntities() async throws -> [WarmupAudioQueueResultEntity] {
WarmupAudioStore.all().map { WarmupAudioQueueResultEntity(id: $0.id, title: $0.title) }
}
func entities(matching string: String) async throws -> [WarmupAudioQueueResultEntity] {
let query = MusicCatalog.normalized(string)
return WarmupAudioStore.all().compactMap { entry in
if query.isEmpty || MusicCatalog.normalized(entry.title).contains(query) {
return WarmupAudioQueueResultEntity(id: entry.id, title: entry.title)
}
return nil
}
}
}
@available(iOS 27.0, *)
enum WarmupAudioStore {
private static let defaultsKey = "otunes.warmupAudioQueue"
struct Entry: Codable {
var id: String
var title: String
}
static func remember(id: String, title: String) {
var entries = all()
entries.removeAll { $0.id == id }
entries.append(Entry(id: id, title: title))
if entries.count > 20 {
entries = Array(entries.suffix(20))
}
if let data = try? JSONEncoder().encode(entries) {
UserDefaults.standard.set(data, forKey: defaultsKey)
}
}
static func entry(id: String) -> Entry? {
all().first { $0.id == id }
}
static func all() -> [Entry] {
guard let data = UserDefaults.standard.data(forKey: defaultsKey),
let entries = try? JSONDecoder().decode([Entry].self, from: data)
else {
return []
}
return entries
}
}
@available(iOS 27.0, *)
@UnionValue
enum AudioItem {
case song(AudioSongEntity)
case album(AudioAlbumEntity)
case artist(AudioArtistEntity)
case playlist(AudioPlaylistEntity)
var kind: String {
switch self {
case .song:
return "song"
case .album:
return "album"
case .artist:
return "artist"
case .playlist:
return "playlist"
}
}
var itemId: String {
switch self {
case .song(let song):
return song.id
case .album(let album):
return album.id
case .artist(let artist):
return artist.id
case .playlist(let playlist):
return playlist.id
}
}
var itemTitle: String {
switch self {
case .song(let song):
return song.title
case .album(let album):
return album.title
case .artist(let artist):
return artist.name
case .playlist(let playlist):
return playlist.title
}
}
}
@available(iOS 27.0, *)
private struct AudioLibraryContext {
var artistsById: [String: AudioArtistEntity]
var albumsById: [String: AudioAlbumEntity]
static func load() async throws -> AudioLibraryContext {
let artistsById = try await AudioAlbumQuery.artistsById()
let albums = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind).map {
AudioAlbumEntity(record: $0, artistsById: artistsById)
}
return AudioLibraryContext(
artistsById: artistsById,
albumsById: Dictionary(uniqueKeysWithValues: albums.map { ($0.id, $0) })
)
}
func song(from record: AppIntentEntityRecord) -> AudioSongEntity {
AudioSongEntity(record: record, artistsById: artistsById, albumsById: albumsById)
}
}
@available(iOS 27.0, *)
@AppIntent(schema: .audio.playAudio)
struct PlayAudioIntent: AudioStartingIntent {
static let openAppWhenRun = true
var audioEntity: AudioItem
var playbackAttributes: Set<PlaybackAttributes>
var warmupAudioQueueResult: WarmupAudioQueueResultEntity?
var queueLocation: QueueInsertionLocation?
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let queue: String
switch queueLocation {
case .next:
queue = "next"
case .tail:
queue = "tail"
case nil:
queue = "now"
}
try await MusicCommands.play(
kind: audioEntity.kind,
id: audioEntity.itemId,
title: audioEntity.itemTitle,
shuffle: playbackAttributes.contains(.shuffle),
repeatMode: playbackAttributes.contains(.`repeat`) ? "all" : "none",
queue: queue,
warmupId: warmupAudioQueueResult?.id
)
return .result(dialog: "Playing \(audioEntity.itemTitle).")
}
}
@available(iOS 27.0, *)
@AppIntent(schema: .audio.warmupAudioQueue)
struct WarmupAudioQueueIntent {
static let openAppWhenRun = true
var audioEntity: AudioItem
var playbackAttributes: Set<PlaybackAttributes>
@MainActor
func perform() async throws -> some IntentResult & ReturnsValue<WarmupAudioQueueResultEntity> {
let id = try await MusicCommands.warmup(
kind: audioEntity.kind,
id: audioEntity.itemId,
title: audioEntity.itemTitle,
shuffle: playbackAttributes.contains(.shuffle)
)
WarmupAudioStore.remember(id: id, title: audioEntity.itemTitle)
let result = WarmupAudioQueueResultEntity(id: id, title: audioEntity.itemTitle)
return .result(value: result)
}
}
@available(iOS 27.0, *)
@AppIntent(schema: .audio.addToPlaylist)
struct AddAudioToPlaylistIntent {
static let openAppWhenRun = true
var audioEntity: AudioItem
var playlist: AudioPlaylistEntity
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
if audioEntity.kind == "playlist" && audioEntity.itemId == playlist.id {
throw MusicIntentFailure("Choose a different playlist.")
}
try await MusicCommands.addToPlaylist(
playlistId: playlist.id,
sources: [(kind: audioEntity.kind, id: audioEntity.itemId)]
)
return .result(dialog: "Adding to \(playlist.title).")
}
}
@available(iOS 27.0, *)
@AppIntent(schema: .audio.updateAudioAffinity)
struct UpdateAudioAffinityIntent {
static let openAppWhenRun = true
var affinityState: AffinityState
var target: AudioItem
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
if affinityState == .dislike {
throw MusicIntentFailure("oTunes doesn't keep dislikes. You can unlike it instead.")
}
if target.kind == "playlist" {
throw MusicIntentFailure("oTunes can love songs, albums, and artists.")
}
let state = affinityState == .like ? "like" : "unset"
try await MusicCommands.updateAffinity(kind: target.kind, id: target.itemId, state: state)
let verb = affinityState == .like ? "Loving" : "Unliking"
return .result(dialog: "\(verb) \(target.itemTitle).")
}
}
@available(iOS 27.0, *)
@AppIntent(schema: .audio.createStation)
struct CreateAudioStationIntent {
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
if song.artistId.isEmpty {
throw MusicIntentFailure("oTunes can't find an artist for this song.")
}
try await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
let artist = song.artistName.isEmpty ? "this artist" : song.artistName
return .result(dialog: "Playing more by \(artist).")
}
}
#endif
@@ -0,0 +1,332 @@
import AppIntents
internal import ExpoAppIntents
import Foundation
struct MusicIntentFailure: Error, CustomLocalizedStringResourceConvertible {
let localizedStringResource: LocalizedStringResource
init(_ message: String) {
localizedStringResource = LocalizedStringResource(stringLiteral: message)
}
}
enum MusicLinkTarget {
case song(String)
case album(String)
case artist(String)
case playlist(String)
}
/// Reads the full disk-backed catalogs JavaScript publishes and turns
/// spoken requests into those records. The same kinds back Shortcuts pickers and,
/// on iOS 27, the audio schemas Siri uses for Apple Music-style requests.
enum MusicCatalog {
static let songKind = "song"
static let albumKind = "album"
static let artistKind = "artist"
static let playlistKind = "playlist"
static let nowPlayingKind = "nowPlaying"
/// Kept in sync with the catalog query and the JavaScript bridge.
static let favoritesPlaylistId = "otunes:favorites"
static func records(ofKind kind: String) async throws -> [AppIntentEntityRecord] {
try await MusicCatalogStorage.shared.records(kind: kind)
}
static func records(ofKind kind: String, matching identifiers: [String]) async throws -> [AppIntentEntityRecord] {
try await records(ofKind: kind).filter { identifiers.contains($0.id) }
}
static func currentSong() async throws -> SongEntity? {
guard let current = try await records(ofKind: nowPlayingKind).first else {
return nil
}
if let song = try await SongQuery().entities(for: [current.id]).first {
return song
}
return SongEntity(record: current)
}
static func normalized(_ value: String) -> String {
value
.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)
.trimmingCharacters(in: .whitespacesAndNewlines)
}
static func cleanedQuery(_ query: String) -> String {
var text = normalized(query)
let prefixes = [
"the song ", "the album ", "the artist ", "the playlist ",
"songs by ", "music by ", "song ", "album ", "artist ", "playlist ", "some ",
]
var didStrip = true
while didStrip {
didStrip = false
for prefix in prefixes where text.hasPrefix(prefix) {
text = String(text.dropFirst(prefix.count))
.trimmingCharacters(in: .whitespacesAndNewlines)
didStrip = true
}
}
return text
}
static func rank(
_ records: [AppIntentEntityRecord],
query: String,
limit: Int,
score: (AppIntentEntityRecord, String) -> Int
) -> [AppIntentEntityRecord] {
let cleaned = cleanedQuery(query)
guard !cleaned.isEmpty else {
return []
}
return records
.compactMap { record -> (AppIntentEntityRecord, Int)? in
let value = score(record, cleaned)
return value > 0 ? (record, value) : nil
}
.sorted { lhs, rhs in
if lhs.1 != rhs.1 {
return lhs.1 > rhs.1
}
return lhs.0.title.localizedCaseInsensitiveCompare(rhs.0.title) == .orderedAscending
}
.prefix(limit)
.map(\.0)
}
static func songScore(_ record: AppIntentEntityRecord, _ query: String) -> Int {
let title = normalized(record.title)
let artist = normalized(record.metadata["artistName"] ?? "")
if title == query {
return 100
}
if let range = query.range(of: " by ") {
let titlePart = normalized(String(query[..<range.lowerBound]))
let artistPart = normalized(String(query[range.upperBound...]))
var score = 0
if !titlePart.isEmpty && (title == titlePart || title.contains(titlePart)) {
score += title == titlePart ? 70 : 50
}
if !artistPart.isEmpty && (artist == artistPart || artist.contains(artistPart)) {
score += artist == artistPart ? 30 : 20
}
return score >= 50 ? score : 0
}
if title.contains(query) || (query.contains(title) && title.count > 2) {
return title.contains(query) ? 80 : 60
}
return 0
}
static func albumScore(_ record: AppIntentEntityRecord, _ query: String) -> Int {
let title = normalized(record.title)
let artist = normalized(record.metadata["artistName"] ?? "")
if title == query {
return 100
}
if title.contains(query) || (query.contains(title) && title.count > 2) {
return 80
}
if artist == query {
return 45
}
if artist.contains(query) && query.count > 2 {
return 30
}
return 0
}
static func nameScore(_ record: AppIntentEntityRecord, _ query: String) -> Int {
let name = normalized(record.title)
let synonyms = record.synonyms.map(normalized)
if name == query || synonyms.contains(query) {
return 100
}
if name.contains(query) || synonyms.contains(where: { $0.contains(query) }) {
return 80
}
if query.contains(name) && name.count > 2 {
return 60
}
return 0
}
static func suggested(
_ records: [AppIntentEntityRecord],
limit: Int,
favoritesFirst: Bool
) -> [AppIntentEntityRecord] {
let pool: [AppIntentEntityRecord]
if favoritesFirst {
let favorites = records.filter { $0.metadata["favorite"] == "1" }
pool = favorites.isEmpty ? records : favorites
} else {
pool = records
}
return Array(pool.prefix(limit))
}
static func linkTarget(from url: URL) -> MusicLinkTarget? {
let parts = url.path.split(separator: "/").map(String.init).filter { !$0.isEmpty }
guard let kind = parts.first, parts.count >= 2 else {
return nil
}
let id = parts[1]
switch kind {
case "album":
if let songId = URLComponents(url: url, resolvingAgainstBaseURL: false)?
.queryItems?
.first(where: { $0.name == "s" })?
.value,
!songId.isEmpty
{
return .song(songId)
}
return .album(id)
case "artist":
return .artist(id)
case "playlist":
return .playlist(id == "favorites" ? favoritesPlaylistId : id)
default:
return nil
}
}
static func songs(for target: MusicLinkTarget) async throws -> [AppIntentEntityRecord] {
let songs = try await records(ofKind: songKind)
switch target {
case .song(let id):
return songs.filter { $0.id == id }
case .album(let id):
return Array(songs.filter { $0.metadata["albumId"] == id }.prefix(25))
case .artist(let id):
return Array(songs.filter { $0.metadata["artistId"] == id }.prefix(25))
case .playlist:
return []
}
}
static func formatDuration(_ seconds: Int) -> String {
guard seconds > 0 else {
return ""
}
return String(format: "%d:%02d", seconds / 60, seconds % 60)
}
}
enum MusicCommands {
static func play(
kind: String,
id: String,
title: String,
shuffle: Bool?,
repeatMode: String?,
queue: String,
warmupId: String?
) async throws -> String {
let params: AppIntentParams = [
"kind": .string(kind),
"id": .string(id),
"title": .string(title),
"queue": .string(queue),
"shuffle": shuffle.map { .string($0 ? "songs" : "off") } ?? .null,
"repeat": repeatMode.map(AppIntentValue.string) ?? .null,
"warmupId": warmupId.map(AppIntentValue.string) ?? .null,
]
return try await MusicIntentExecution.shared.run(name: "playMusic", params: params)
}
static func warmup(kind: String, id: String, title: String, shuffle: Bool) async throws -> String {
try await MusicIntentExecution.shared.run(
name: "warmupAudio",
params: [
"kind": .string(kind),
"id": .string(id),
"title": .string(title),
"shuffle": .string(shuffle ? "songs" : "off"),
]
)
}
static func addToPlaylist(playlistId: String, sources: [(kind: String, id: String)]) async throws {
try await MusicIntentExecution.shared.run(
name: "addToPlaylist",
params: [
"playlistId": .string(playlistId),
"sources": .array(sources.map { source in
.object([
"kind": .string(source.kind),
"id": .string(source.id),
])
}),
]
)
}
static func addToUpNext(
position: String,
sources: [(kind: String, id: String)]
) async throws {
try await MusicIntentExecution.shared.run(
name: "addToUpNext",
params: [
"position": .string(position),
"sources": .array(sources.map { source in
.object([
"kind": .string(source.kind),
"id": .string(source.id),
])
}),
]
)
}
static func createPlaylist(name: String, sources: [(kind: String, id: String)]) async throws {
try await MusicIntentExecution.shared.run(
name: "createPlaylist",
params: [
"name": .string(name),
"sources": .array(sources.map { source in
.object([
"kind": .string(source.kind),
"id": .string(source.id),
])
}),
]
)
}
static func clearUpNext() async throws {
try await MusicIntentExecution.shared.run(name: "clearUpNext")
}
static func createStation(artistId: String, artistName: String) async throws {
try await MusicIntentExecution.shared.run(
name: "createStation",
params: [
"artistId": .string(artistId),
"title": .string(artistName),
]
)
}
static func updateAffinity(kind: String, id: String, state: String) async throws {
try await MusicIntentExecution.shared.run(
name: "updateAffinity",
params: [
"kind": .string(kind),
"id": .string(id),
"state": .string(state),
]
)
}
static func transport(_ command: String) async throws {
try await MusicIntentExecution.shared.run(
name: "transport",
params: ["command": .string(command)]
)
}
}
@@ -0,0 +1,245 @@
import AppIntents
@preconcurrency import CoreSpotlight
internal import ExpoAppIntents
internal import ExpoModulesCore
import UniformTypeIdentifiers
enum MusicEntityRegistration {
static func register() {
AppIntentDonationRegistry.shared.register("playMusic", as: PlayMusicIntent.self)
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("song", as: SongEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("song", as: SongEntity.self)
}
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("album", as: AlbumEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("album", as: AlbumEntity.self)
}
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("artist", as: ArtistEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("artist", as: ArtistEntity.self)
}
if #available(iOS 18.0, *) {
AppEntityIdentifierRegistry.shared.registerIndexed("playlist", as: PlaylistEntity.self)
} else {
AppEntityIdentifierRegistry.shared.register("playlist", as: PlaylistEntity.self)
}
}
}
extension SongEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension SongEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = title
attributes.displayName = title
attributes.contentDescription = "\(artistName) · \(albumTitle)"
attributes.keywords = [title, artistName, albumTitle, "music", "song"]
return attributes
}
}
struct OpenSongIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Song"
static let openAppWhenRun = true
@Parameter(title: "Song")
var target: SongEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("song"), "id": .string(target.id)]
)
return .result()
}
}
extension AlbumEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension AlbumEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = title
attributes.displayName = title
attributes.contentDescription = artistName
attributes.keywords = [title, artistName, "music", "album"]
return attributes
}
}
struct OpenAlbumIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Album"
static let openAppWhenRun = true
@Parameter(title: "Album")
var target: AlbumEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("album"), "id": .string(target.id)]
)
return .result()
}
}
extension ArtistEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension ArtistEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = name
attributes.displayName = name
attributes.contentDescription = "oTunes artist"
attributes.keywords = [name, "music", "artist"]
return attributes
}
}
struct OpenArtistIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Artist"
static let openAppWhenRun = true
@Parameter(title: "Artist")
var target: ArtistEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("artist"), "id": .string(target.id)]
)
return .result()
}
}
extension PlaylistEntity: AppIntentEntityRecordConvertible {}
@available(iOS 18.0, *)
extension PlaylistEntity: IndexedEntity {
var attributeSet: CSSearchableItemAttributeSet {
let attributes = CSSearchableItemAttributeSet(contentType: .text)
attributes.title = title
attributes.displayName = title
attributes.contentDescription = "oTunes playlist"
attributes.keywords = [title, "music", "playlist"]
return attributes
}
}
struct OpenPlaylistIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Playlist"
static let openAppWhenRun = true
@Parameter(title: "Playlist")
var target: PlaylistEntity
@MainActor
func perform() async throws -> some IntentResult {
_ = try await MusicIntentExecution.shared.run(
name: "openMusic", params: ["kind": .string("playlist"), "id": .string(target.id)]
)
return .result()
}
}
extension PlayMusicIntent: DonatableAppIntent {
struct DonationParams: Record {
@Field(.required) var kind: String = ""
@Field(.required) var id: String = ""
@Field var shuffle: Bool = false
}
init(donationParams: DonationParams) async throws {
self.init()
shuffle = donationParams.shuffle ? .songs : .off
switch donationParams.kind {
case "song":
guard let value = try await SongQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This song is no longer available.")
}
song = value
case "album":
guard let value = try await AlbumQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This album is no longer available.")
}
album = value
case "artist":
guard let value = try await ArtistQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This artist is no longer available.")
}
artist = value
case "playlist":
guard let value = try await PlaylistQuery().entities(for: [donationParams.id]).first else {
throw MusicIntentFailure("This playlist is no longer available.")
}
playlist = value
default:
throw MusicIntentFailure("Unknown music type.")
}
}
}
// The system can recover an evicted Spotlight index without waiting for the next
// catalog refresh. Rebuild only the compact catalog published to expo-app-intents.
#if compiler(>=6.4)
@available(iOS 27.0, *)
extension SongQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [SongEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "song", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "song")
}
}
@available(iOS 27.0, *)
extension AlbumQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [AlbumEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "album", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "album")
}
}
@available(iOS 27.0, *)
extension ArtistQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [ArtistEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "artist", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "artist")
}
}
@available(iOS 27.0, *)
extension PlaylistQuery: IndexedEntityQuery {
func reindexEntities(
for identifiers: [PlaylistEntity.ID], indexDescription: CSSearchableIndexDescription
) async throws {
try await AppEntityIdentifierRegistry.shared.updateIndexFromCatalog(kind: "playlist", matching: identifiers)
}
func reindexAllEntities(indexDescription: CSSearchableIndexDescription) async throws {
try await AppEntityIdentifierRegistry.shared.replaceIndexFromCatalog(kind: "playlist")
}
}
#endif
@@ -0,0 +1,301 @@
import AppIntents
internal import ExpoAppIntents
import Foundation
struct SongEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Song")
static let defaultQuery = SongQuery()
var id: String
var title: String
var artistName: String
var artistId: String
var albumTitle: String
var albumId: String
var durationSeconds: Int
var genres: String
var year: String
var trackNumber: String
var discNumber: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
}
init(record: AppIntentEntityRecord) {
id = record.id
title = record.title
artistName = record.metadata["artistName"] ?? record.subtitle ?? ""
artistId = record.metadata["artistId"] ?? ""
albumTitle = record.metadata["albumTitle"] ?? ""
albumId = record.metadata["albumId"] ?? ""
durationSeconds = Int(record.metadata["duration"] ?? "") ?? 0
genres = record.metadata["genres"] ?? ""
year = record.metadata["year"] ?? ""
trackNumber = record.metadata["track"] ?? ""
discNumber = record.metadata["disk"] ?? ""
}
}
struct SongQuery: EntityStringQuery {
func entities(for identifiers: [String]) async throws -> [SongEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.songKind, matching: identifiers)
.map(SongEntity.init(record:))
}
func suggestedEntities() async throws -> [SongEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(SongEntity.init(record:))
}
func entities(matching string: String) async throws -> [SongEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.songKind)
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
if query.isEmpty {
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(SongEntity.init(record:))
}
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.songScore)
.map(SongEntity.init(record:))
}
}
struct AlbumEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Album")
static let defaultQuery = AlbumQuery()
var id: String
var title: String
var artistName: String
var artistId: String
var year: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(title)", subtitle: "\(artistName)")
}
init(record: AppIntentEntityRecord) {
id = record.id
title = record.title
artistName = record.metadata["artistName"] ?? record.subtitle ?? ""
artistId = record.metadata["artistId"] ?? ""
year = record.metadata["year"] ?? ""
}
}
struct AlbumQuery: EntityStringQuery {
func entities(for identifiers: [String]) async throws -> [AlbumEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.albumKind, matching: identifiers)
.map(AlbumEntity.init(record:))
}
func suggestedEntities() async throws -> [AlbumEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(AlbumEntity.init(record:))
}
func entities(matching string: String) async throws -> [AlbumEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.albumKind)
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
if query.isEmpty {
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(AlbumEntity.init(record:))
}
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.albumScore)
.map(AlbumEntity.init(record:))
}
}
struct ArtistEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Artist")
static let defaultQuery = ArtistQuery()
var id: String
var name: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(name)")
}
init(record: AppIntentEntityRecord) {
id = record.id
name = record.title
}
}
struct ArtistQuery: EntityStringQuery {
func entities(for identifiers: [String]) async throws -> [ArtistEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.artistKind, matching: identifiers)
.map(ArtistEntity.init(record:))
}
func suggestedEntities() async throws -> [ArtistEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(ArtistEntity.init(record:))
}
func entities(matching string: String) async throws -> [ArtistEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.artistKind)
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
if query.isEmpty {
return MusicCatalog.suggested(records, limit: 25, favoritesFirst: true)
.map(ArtistEntity.init(record:))
}
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.nameScore)
.map(ArtistEntity.init(record:))
}
}
struct PlaylistEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Playlist")
static let defaultQuery = PlaylistQuery()
var id: String
var title: String
var ownerName: String
var createdByMe: Bool
var displayRepresentation: DisplayRepresentation {
if ownerName.isEmpty {
return DisplayRepresentation(title: "\(title)")
}
return DisplayRepresentation(title: "\(title)", subtitle: "\(ownerName)")
}
init(record: AppIntentEntityRecord) {
id = record.id
title = record.title
ownerName = record.metadata["ownerName"] ?? record.subtitle ?? ""
createdByMe = record.metadata["createdByMe"] == "1"
}
}
struct PlaylistQuery: EntityStringQuery {
func entities(for identifiers: [String]) async throws -> [PlaylistEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind, matching: identifiers)
.map(PlaylistEntity.init(record:))
}
func suggestedEntities() async throws -> [PlaylistEntity] {
try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
.map(PlaylistEntity.init(record:))
}
func entities(matching string: String) async throws -> [PlaylistEntity] {
let records = try await MusicCatalog.records(ofKind: MusicCatalog.playlistKind)
let query = string.trimmingCharacters(in: .whitespacesAndNewlines)
if query.isEmpty {
return records.map(PlaylistEntity.init(record:))
}
return MusicCatalog.rank(records, query: query, limit: 50, score: MusicCatalog.nameScore)
.map(PlaylistEntity.init(record:))
}
}
enum MusicShuffle: String, AppEnum {
case off
case songs
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Shuffle")
static let caseDisplayRepresentations: [MusicShuffle: DisplayRepresentation] = [
.off: "Off",
.songs: "Songs",
]
}
enum MusicRepeat: String, AppEnum {
case none
case one
case all
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Repeat")
static let caseDisplayRepresentations: [MusicRepeat: DisplayRepresentation] = [
.none: "None",
.one: "One",
.all: "All",
]
}
enum UpNextPosition: String, AppEnum {
case next
case last
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Position")
static let caseDisplayRepresentations: [UpNextPosition: DisplayRepresentation] = [
.next: "Play Next",
.last: "Play Last",
]
}
enum MusicDetail: String, AppEnum {
case title
case artist
case album
case albumArtist
case genre
case duration
case year
case trackNumber
case discNumber
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Detail")
static let caseDisplayRepresentations: [MusicDetail: DisplayRepresentation] = [
.title: "Title",
.artist: "Artist",
.album: "Album",
.albumArtist: "Album Artist",
.genre: "Genre",
.duration: "Duration",
.year: "Year",
.trackNumber: "Track Number",
.discNumber: "Disc Number",
]
}
enum MusicSelection {
static func sources(
song: SongEntity?,
album: AlbumEntity?,
artist: ArtistEntity?,
playlist: PlaylistEntity?
) throws -> [(kind: String, id: String, title: String)] {
var sources: [(kind: String, id: String, title: String)] = []
if let song {
sources.append(("song", song.id, song.title))
}
if let album {
sources.append(("album", album.id, album.title))
}
if let artist {
sources.append(("artist", artist.id, artist.name))
}
if let playlist {
sources.append(("playlist", playlist.id, playlist.title))
}
if sources.isEmpty {
throw MusicIntentFailure("Choose a song, album, artist, or playlist.")
}
return sources
}
static func singleTarget(
song: SongEntity?,
album: AlbumEntity?,
artist: ArtistEntity?,
playlist: PlaylistEntity?
) throws -> (kind: String, id: String, title: String)? {
let sources = [
song.map { (kind: "song", id: $0.id, title: $0.title) },
album.map { (kind: "album", id: $0.id, title: $0.title) },
artist.map { (kind: "artist", id: $0.id, title: $0.name) },
playlist.map { (kind: "playlist", id: $0.id, title: $0.title) },
].compactMap { $0 }
if sources.count > 1 {
throw MusicIntentFailure("Choose one song, album, artist, or playlist.")
}
return sources.first
}
}
@@ -0,0 +1,425 @@
import AppIntents
import Foundation
// Actions that use the React player or authenticated mutations open the app and
// wait for its acknowledgment. Native catalog reads can run without opening it.
struct PlayMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Play Music"
static let description = IntentDescription("Plays music using oTunes.")
static let openAppWhenRun = true
@Parameter(title: "Song")
var song: SongEntity?
@Parameter(title: "Album")
var album: AlbumEntity?
@Parameter(title: "Artist")
var artist: ArtistEntity?
@Parameter(title: "Playlist")
var playlist: PlaylistEntity?
@Parameter(title: "Shuffle")
var shuffle: MusicShuffle?
@Parameter(title: "Repeat")
var repeatMode: MusicRepeat?
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let target = try MusicSelection.singleTarget(
song: song,
album: album,
artist: artist,
playlist: playlist
)
let kind = target?.0 ?? "resume"
let id = target?.1 ?? ""
let name = target?.2 ?? "your music"
try await MusicCommands.play(
kind: kind,
id: id,
title: name,
shuffle: shuffle.map { $0 == .songs },
repeatMode: repeatMode?.rawValue,
queue: "now",
warmupId: nil
)
return .result(dialog: "Playing \(name).")
}
}
struct AddToPlaylistIntent: AppIntent {
static let title: LocalizedStringResource = "Add to Playlist"
static let description = IntentDescription(
"Adds the items passed as input to the specified playlist."
)
static let openAppWhenRun = true
@Parameter(title: "Song")
var song: SongEntity?
@Parameter(title: "Album")
var album: AlbumEntity?
@Parameter(title: "Artist")
var artist: ArtistEntity?
@Parameter(title: "Music")
var sourcePlaylist: PlaylistEntity?
@Parameter(title: "Playlist")
var playlist: PlaylistEntity
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let sources = try MusicSelection.sources(
song: song,
album: album,
artist: artist,
playlist: sourcePlaylist
)
if sources.contains(where: { $0.id == playlist.id && $0.kind == "playlist" }) {
throw MusicIntentFailure("Choose a different playlist.")
}
try await MusicCommands.addToPlaylist(
playlistId: playlist.id,
sources: sources.map { (kind: $0.kind, id: $0.id) }
)
return .result(dialog: "Adding to \(playlist.title).")
}
}
struct AddToUpNextIntent: AppIntent {
static let title: LocalizedStringResource = "Add to Up Next"
static let description = IntentDescription(
"Adds the music passed as input to your Up Next queue."
)
static let openAppWhenRun = true
@Parameter(title: "Song")
var song: SongEntity?
@Parameter(title: "Album")
var album: AlbumEntity?
@Parameter(title: "Artist")
var artist: ArtistEntity?
@Parameter(title: "Playlist")
var playlist: PlaylistEntity?
@Parameter(title: "Position", default: .next)
var position: UpNextPosition
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let sources = try MusicSelection.sources(
song: song,
album: album,
artist: artist,
playlist: playlist
)
try await MusicCommands.addToUpNext(
position: position.rawValue,
sources: sources.map { (kind: $0.kind, id: $0.id) }
)
let placement = position == .next ? "Playing this next." : "Adding this to the end of Up Next."
return .result(dialog: "\(placement)")
}
}
struct ClearUpNextIntent: AppIntent {
static let title: LocalizedStringResource = "Clear Up Next"
static let description = IntentDescription("Clears the Up Next queue.")
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
try await MusicCommands.clearUpNext()
return .result(dialog: "Clearing Up Next.")
}
}
struct CreatePlaylistIntent: AppIntent {
static let title: LocalizedStringResource = "Create Playlist"
static let description = IntentDescription(
"Creates a new playlist, adding any items passed as input."
)
static let openAppWhenRun = true
@Parameter(title: "Name")
var name: String
@Parameter(title: "Song")
var song: SongEntity?
@Parameter(title: "Album")
var album: AlbumEntity?
@Parameter(title: "Artist")
var artist: ArtistEntity?
@Parameter(title: "Playlist")
var playlist: PlaylistEntity?
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
let trimmed = name.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.isEmpty {
throw MusicIntentFailure("A playlist needs a name.")
}
let sources = [
song.map { (kind: "song", id: $0.id) },
album.map { (kind: "album", id: $0.id) },
artist.map { (kind: "artist", id: $0.id) },
playlist.map { (kind: "playlist", id: $0.id) },
].compactMap { $0 }
try await MusicCommands.createPlaylist(name: trimmed, sources: sources)
return .result(dialog: "Creating \(trimmed).")
}
}
struct GetCurrentSongIntent: AppIntent {
static let title: LocalizedStringResource = "Get Current Song"
static let description = IntentDescription(
"Returns the song that is currently playing in oTunes, if any."
)
static let openAppWhenRun = false
@MainActor
func perform() async throws -> some IntentResult & ReturnsValue<SongEntity> & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
return .result(value: song, dialog: "\(song.title)")
}
}
struct GetDetailsOfMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Get Details of Music"
static let description = IntentDescription(
"Gets a specific piece of information from the music passed into the action."
)
static let openAppWhenRun = false
@Parameter(title: "Song")
var song: SongEntity?
@Parameter(title: "Album")
var album: AlbumEntity?
@Parameter(title: "Artist")
var artist: ArtistEntity?
@Parameter(title: "Playlist")
var playlist: PlaylistEntity?
@Parameter(title: "Detail")
var detail: MusicDetail
@MainActor
func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog {
let value = try await detailValue()
return .result(value: value, dialog: value.isEmpty ? "Not available." : "\(value)")
}
private func detailValue() async throws -> String {
if let target = try MusicSelection.singleTarget(
song: song,
album: album,
artist: artist,
playlist: playlist
) {
return try value(for: target)
}
guard let current = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Choose music, or play a song in oTunes first.")
}
return Self.value(detail, song: current)
}
private func value(for target: (kind: String, id: String, title: String)) throws -> String {
switch target.kind {
case "song":
guard let song else {
throw MusicIntentFailure("That song is no longer in your library.")
}
return Self.value(detail, song: song)
case "album":
guard let album else {
throw MusicIntentFailure("That album is no longer in your library.")
}
return Self.value(detail, album: album)
case "artist":
guard let artist else {
throw MusicIntentFailure("That artist is no longer in your library.")
}
return Self.value(detail, artist: artist)
case "playlist":
guard let playlist else {
throw MusicIntentFailure("That playlist is no longer in your library.")
}
return Self.value(detail, playlist: playlist)
default:
throw MusicIntentFailure("oTunes can't read that.")
}
}
private static func value(_ detail: MusicDetail, song: SongEntity) -> String {
switch detail {
case .title:
return song.title
case .artist, .albumArtist:
return song.artistName
case .album:
return song.albumTitle
case .genre:
return song.genres
case .duration:
return MusicCatalog.formatDuration(song.durationSeconds)
case .year:
return song.year == "0" ? "" : song.year
case .trackNumber:
return song.trackNumber
case .discNumber:
return song.discNumber
}
}
private static func value(_ detail: MusicDetail, album: AlbumEntity) -> String {
switch detail {
case .title, .album:
return album.title
case .artist, .albumArtist:
return album.artistName
case .year:
return album.year == "0" ? "" : album.year
case .genre, .duration, .trackNumber, .discNumber:
return ""
}
}
private static func value(_ detail: MusicDetail, artist: ArtistEntity) -> String {
switch detail {
case .title, .artist, .albumArtist:
return artist.name
case .album, .genre, .duration, .year, .trackNumber, .discNumber:
return ""
}
}
private static func value(_ detail: MusicDetail, playlist: PlaylistEntity) -> String {
switch detail {
case .title:
return playlist.title
case .artist, .albumArtist:
return playlist.ownerName
case .album, .genre, .duration, .year, .trackNumber, .discNumber:
return ""
}
}
}
struct LoveSongIntent: AppIntent {
static let title: LocalizedStringResource = "Love Song"
static let description = IntentDescription(
"Loves the song that is playing in oTunes. Also used for Favorite Song."
)
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
try await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "like")
return .result(dialog: "Loving \(song.title).")
}
}
struct UnlikeSongIntent: AppIntent {
static let title: LocalizedStringResource = "Unlike Song"
static let description = IntentDescription("Removes the love from the song that is playing.")
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
try await MusicCommands.updateAffinity(kind: "song", id: song.id, state: "unset")
return .result(dialog: "Unliking \(song.title).")
}
}
struct CreateStationIntent: AppIntent {
static let title: LocalizedStringResource = "Create Station"
static let description = IntentDescription(
"Plays more music by the artist of the song that is playing."
)
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let song = try await MusicCatalog.currentSong() else {
throw MusicIntentFailure("Nothing is playing in oTunes.")
}
if song.artistId.isEmpty {
throw MusicIntentFailure("oTunes can't find an artist for this song.")
}
try await MusicCommands.createStation(artistId: song.artistId, artistName: song.artistName)
let artist = song.artistName.isEmpty ? "this artist" : song.artistName
return .result(dialog: "Playing more by \(artist).")
}
}
struct PauseMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Pause Music"
static let description = IntentDescription("Pauses playback in oTunes.")
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
try await MusicCommands.transport("pause")
return .result(dialog: "Pausing.")
}
}
struct ResumeMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Resume Music"
static let description = IntentDescription("Resumes playback in oTunes.")
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
try await MusicCommands.transport("resume")
return .result(dialog: "Resuming.")
}
}
struct SkipMusicIntent: AppIntent {
static let title: LocalizedStringResource = "Skip Song"
static let description = IntentDescription("Skips to the next song in oTunes.")
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
try await MusicCommands.transport("next")
return .result(dialog: "Skipping.")
}
}
struct PreviousTrackIntent: AppIntent {
static let title: LocalizedStringResource = "Previous Song"
static let description = IntentDescription("Goes back to the previous song in oTunes.")
static let openAppWhenRun = true
@MainActor
func perform() async throws -> some IntentResult & ProvidesDialog {
try await MusicCommands.transport("previous")
return .result(dialog: "Going back.")
}
}
@@ -0,0 +1,70 @@
import Foundation
internal import ExpoAppIntents
/// Full catalogs live in Application Support, never in UserDefaults. The package
/// catalog contains only a bounded selection for shortcut suggestions and Spotlight.
actor MusicCatalogStorage {
static let shared = MusicCatalogStorage()
private func location(_ kind: String) throws -> URL {
guard ["song", "album", "artist", "playlist", "nowPlaying"].contains(kind) else {
throw MusicIntentFailure("Unknown music catalog.")
}
let directory = try FileManager.default.url(
for: .applicationSupportDirectory, in: .userDomainMask,
appropriateFor: nil, create: true
).appendingPathComponent("MusicIntents", isDirectory: true)
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
return directory.appendingPathComponent("\(kind).json")
}
func replace(kind: String, json: String) throws {
let data = Data(json.utf8)
let records = try JSONDecoder().decode([AppIntentEntityRecord].self, from: data)
guard Set(records.map(\.id)).count == records.count,
records.allSatisfy({ !$0.id.isEmpty && !$0.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty })
else { throw MusicIntentFailure("Invalid music catalog.") }
try data.write(to: location(kind), options: [.atomic, .completeFileProtectionUntilFirstUserAuthentication])
}
func records(kind: String) throws -> [AppIntentEntityRecord] {
let url = try location(kind)
guard FileManager.default.fileExists(atPath: url.path) else { return [] }
return try JSONDecoder().decode([AppIntentEntityRecord].self, from: Data(contentsOf: url))
}
}
/// App-owned completion channel. Expo's dispatcher itself remains one-way.
actor MusicIntentExecution {
static let shared = MusicIntentExecution()
private var waiting = Set<String>()
private var results: [String: String] = [:]
func complete(token: String, error: String?) {
guard waiting.contains(token) else { return }
results[token] = error ?? ""
}
func run(name: String, params: AppIntentParams = [:]) async throws -> String {
let token = UUID().uuidString
let deadline = Date().addingTimeInterval(25)
waiting.insert(token)
defer {
waiting.remove(token)
results.removeValue(forKey: token)
}
var payload = params
payload["completionToken"] = .string(token)
payload["deadline"] = .double(deadline.timeIntervalSince1970 * 1000)
let id = await AppIntentDispatcher.shared.dispatch(name: name, params: payload)
while Date() < deadline {
try Task.checkCancellation()
if let error = results.removeValue(forKey: token) {
if !error.isEmpty { throw MusicIntentFailure(error) }
return id
}
try await Task.sleep(for: .milliseconds(100))
}
throw MusicIntentFailure("oTunes did not finish in time. Open the app, check your connection, and try again.")
}
}
+33
View File
@@ -0,0 +1,33 @@
# Siri and Shortcuts
This integration uses Expo 58 inline modules and `expo-app-intents`. Rebuild the native app after changing Swift declarations or registrations. An OTA JavaScript update cannot add these capabilities.
Playback and library changes open oTunes because they need its authenticated React runtime. Swift dispatches a request and waits for an app-owned completion acknowledgment. Playback waits for the native audio player to load the requested track; warmup waits for it to load while paused. Errors reach Siri, and requests that have expired are discarded before starting a side effect. Native catalog reads do not open the app.
The full entity catalog lives in Application Support. The backend supplies pages of 200 records. Expo's UserDefaults catalog holds at most 100 suggestions per kind, with favorites first, and mirrors those suggestions into Spotlight. Siri's native queries search the full disk catalog. Catalogs refresh on launch, foreground entry, favorite changes, and every five minutes while foregrounded.
Songs, albums, artists, and playlists have Spotlight open intents and on-screen entity associations. Associations require iOS 18.4 and Xcode 27; Spotlight indexing requires iOS 18. Playback donations are made after the player starts, preserve shuffle, and resolve real catalog entities. Removed records trigger donation cleanup. Sign-out clears catalogs, Spotlight entries, donations, and pending invocations. On iOS 27, the query types also handle system requests to rebuild Spotlight indexes.
Audio schema intents require Xcode 27 and iOS 27. Custom intents remain available on earlier supported versions and remain visible on iOS 27 because intent discovery metadata must be a compile-time constant.
## Backend rollout
Deploy the companion Convex changes before installing the new mobile build:
- `library.intentCatalogPage` supplies authenticated, paginated entity records.
- `appIntentPlaylists` records playlist-creation request IDs in the same transaction as creation. Replaying a request returns the original playlist ID.
- `playlists.createPlaylist` accepts an optional `invocationId`; existing callers can omit it.
## Verification
Run from the repository root:
```sh
pnpm --filter @otunes/mobile typecheck
pnpm --filter @otunes/convex typecheck
node --test apps/mobile/lib/app-intents.test.cjs packages/convex/convex/app-intent-playlists.test.cjs
```
Build iOS with Xcode and verify the app target's `ExtractAppIntentsMetadata` phase. TypeScript checks alone do not validate Swift declarations or Apple's schema contracts.
On a suitable device, verify cold and warm playback, paused warmup followed by playback, offline errors, request expiry, playlist replay, and a saved shortcut after upgrading. Check entity resolution after rename/delete and sign-out. Verify Spotlight opening, on-screen requests, and donations separately; metadata extraction does not establish system discovery or successful runtime execution.
+6 -2
View File
@@ -65,10 +65,14 @@
}
],
"expo-image",
"expo-status-bar"
"expo-status-bar",
"expo-app-intents"
],
"experiments": {
"typedRoutes": true
"typedRoutes": true,
"inlineModules": {
"watchedDirectories": ["app-intents"]
}
},
"extra": {
"router": {},
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { Stack, useIsFocused, useLocalSearchParams } from "expo-router"
import { StatusBar } from "expo-status-bar"
import { memo, useCallback, useEffect, useMemo } from "react"
@@ -180,7 +181,7 @@ export default function AlbumScreen() {
indicatorStyle={palette.isDark ? "white" : "black"}
ListHeaderComponent={
<View>
<View style={styles.hero}>
<MusicEntityView kind="album" id={album.id} style={styles.hero}>
<CoverImage
uri={coverUri}
hasCover={album.cover != null}
@@ -244,7 +245,7 @@ export default function AlbumScreen() {
/>
</Pressable>
</View>
</View>
</MusicEntityView>
<CollectionDivider color={palette.divider} />
</View>
}
@@ -332,39 +333,44 @@ const AlbumTrackRow = memo(function AlbumTrackRow({
}}
onAction={(action) => void onAction(track, album, action)}
>
<Pressable
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
<Text
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
<MusicEntityView kind="song" id={track.id}>
<Pressable
style={styles.trackRow}
onPress={() => void onPlay(track.id as Id<"songs">, songIds)}
>
{number}
</Text>
<View
style={[styles.trackContent, { borderBottomColor: palette.divider }]}
>
<View style={styles.trackMeta}>
<Text
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.title}
</Text>
{track.artists ? (
<Text
style={[styles.trackNumber, { color: palette.secondaryForeground }]}
>
{number}
</Text>
<View
style={[
styles.trackContent,
{ borderBottomColor: palette.divider },
]}
>
<View style={styles.trackMeta}>
<Text
style={{ color: palette.secondaryForeground }}
style={[styles.trackTitle, { color: palette.foreground }]}
numberOfLines={1}
>
{track.artists.join(", ")}
{track.title}
</Text>
) : null}
{track.artists ? (
<Text
style={{ color: palette.secondaryForeground }}
numberOfLines={1}
>
{track.artists.join(", ")}
</Text>
) : null}
</View>
<Text style={{ color: palette.secondaryForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</View>
<Text style={{ color: palette.secondaryForeground }}>
{formatDuration(track.durationSeconds)}
</Text>
</View>
</Pressable>
</Pressable>
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { Stack, useLocalSearchParams } from "expo-router"
import { Pressable, StyleSheet, Text, View } from "react-native"
import { Ionicons } from "@expo/vector-icons"
@@ -95,7 +96,11 @@ export default function ArtistScreen() {
paddingBottom={contentBottomInset}
contentInset={false}
ListHeaderComponent={
<View style={styles.hero}>
<MusicEntityView
kind="artist"
id={data.artist.id}
style={styles.hero}
>
<CoverImage
uri={artistCoverUrl(config.serverUrl, data.artist.id)}
hasCover={data.artist.photo != null}
@@ -138,7 +143,7 @@ export default function ArtistScreen() {
<Ionicons name="shuffle" size={24} color={colors.foreground} />
</Pressable>
</View>
</View>
</MusicEntityView>
}
/>
</>
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { useMutation } from "convex/react"
import { memo, useCallback, useEffect, useMemo } from "react"
import {
@@ -343,6 +344,7 @@ function PlaylistContent({
onScrollBeginDrag={swipeGroup.closeOpenRow}
ListHeaderComponent={
<PlaylistHero
entityId={playlistId ?? "otunes:favorites"}
coverUri={coverUri}
hasCover={playlist.cover != null}
title={playlist.title}
@@ -450,6 +452,7 @@ const PlaylistSongRow = memo(function PlaylistSongRow({
})
const PlaylistHero = memo(function PlaylistHero({
entityId,
coverUri,
hasCover,
title,
@@ -461,6 +464,7 @@ const PlaylistHero = memo(function PlaylistHero({
onPlay,
onShuffle,
}: {
entityId: string
coverUri: string
hasCover: boolean
title: string
@@ -473,7 +477,7 @@ const PlaylistHero = memo(function PlaylistHero({
onShuffle: () => void
}) {
return (
<View>
<MusicEntityView kind="playlist" id={entityId}>
<View style={styles.hero}>
<CoverImage uri={coverUri} hasCover={hasCover} style={styles.cover} />
<Text style={[styles.title, { color: palette.foreground }]}>
@@ -528,7 +532,7 @@ const PlaylistHero = memo(function PlaylistHero({
</View>
</View>
<CollectionDivider color={palette.divider} />
</View>
</MusicEntityView>
)
})
+4 -1
View File
@@ -7,6 +7,7 @@ import { Redirect, Stack } from "expo-router"
import { Platform } from "react-native"
import { useEffect } from "react"
import { AppIntentBridge } from "@/components/app-intent-bridge"
import { ConvexAuthRecovery } from "@/components/convex-auth-recovery"
import { LoadingView } from "@/components/ui/primitives"
import { AddToPlaylistProvider } from "@/contexts/add-to-playlist"
@@ -68,6 +69,7 @@ export default function AppLayout() {
return (
<ConvexAuthRecovery onInvalidate={invalidateSession}>
<PlaybackProvider>
<AppIntentBridge />
<AppNavigationSync />
<AddToPlaylistProvider>
<FavoritesProvider>
@@ -77,7 +79,8 @@ export default function AppLayout() {
name="settings"
options={{
headerShown: false,
presentation: Platform.OS === "ios" ? "modal" : "fullScreenModal",
presentation:
Platform.OS === "ios" ? "modal" : "fullScreenModal",
animation: "slide_from_bottom",
gestureEnabled: true,
gestureDirection: "vertical",
+7 -2
View File
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { useRouter } from "expo-router"
import * as Haptics from "expo-haptics"
import { StatusBar } from "expo-status-bar"
@@ -216,7 +217,11 @@ export default function PlayerScreen() {
<Animated.View style={[styles.root, backgroundStyle, dismissStyle]}>
<StatusBar style={colors.isDark ? "light" : "dark"} />
<View style={[styles.safe, { paddingTop: insets.top }]}>
<MusicEntityView
kind="song"
id={currentSong.id}
style={[styles.safe, { paddingTop: insets.top }]}
>
<GestureDetector gesture={dismissGesture()}>
<Pressable
onPress={() => router.back()}
@@ -472,7 +477,7 @@ export default function PlayerScreen() {
</View>
</View>
</GestureDetector>
</View>
</MusicEntityView>
</Animated.View>
)
}
@@ -0,0 +1,822 @@
import { useConvex, useQuery } from "convex/react"
import * as AppIntents from "expo-app-intents"
import type { AppIntentEntity, AppIntentInvocation } from "expo-app-intents"
import { useEffect, useMemo, useRef, useSyncExternalStore } from "react"
import { AppState } from "react-native"
import {
completeMusicInvocation,
publishMusicCatalog,
trackMusicCatalogSync,
} from "@/lib/app-intents"
import { useAppNavigation } from "@/lib/app-navigation"
import {
ANONYMOUS_CACHE_SCOPE,
useCacheScope,
} from "@/contexts/persistent-cache"
import { usePlayback } from "@/contexts/playback"
import {
FAVORITES_PLAYLIST_ID,
loadHandledIntentIds,
rememberHandledIntentId,
} from "@/lib/app-intent-handled"
import { showError } from "@/lib/toast"
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
import { getPlaybackStore } from "@otunes/convex/client/playback-store"
import type { RepeatMode } from "@otunes/convex/client/playback-queue"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
const warmedAudioIds = new Set<string>()
class IntentRejected extends Error {}
const KNOWN_INTENTS = new Set([
"playMusic",
"warmupAudio",
"addToPlaylist",
"addToUpNext",
"clearUpNext",
"createPlaylist",
"createStation",
"updateAffinity",
"transport",
"openMusic",
])
type MusicSource = {
kind: string
id: string
}
function readString(value: unknown) {
return typeof value === "string" && value.trim() ? value : null
}
function readSources(value: unknown): MusicSource[] {
if (!Array.isArray(value)) {
return []
}
return value.flatMap((entry) => {
if (!entry || typeof entry !== "object") {
return []
}
const source = entry as { kind?: unknown; id?: unknown }
if (typeof source.kind !== "string" || typeof source.id !== "string") {
return []
}
if (!source.kind || !source.id) {
return []
}
return [{ kind: source.kind, id: source.id }]
})
}
function shuffleChoice(value: unknown, fallback: boolean) {
if (value === "songs") return true
if (value === "off") return false
if (value == null) return fallback
throw new IntentRejected("Shuffle must be Off or Songs.")
}
function repeatChoice(value: unknown): RepeatMode | null {
if (value == null) return null
if (value === "none") return "none"
if (value === "one") return "once"
if (value === "all") return "always"
throw new IntentRejected("Repeat must be None, One, or All.")
}
function ensureActive(invocation: AppIntentInvocation) {
const deadline = invocation.params.deadline
if (typeof deadline === "number" && Date.now() >= deadline) {
throw new IntentRejected("This request expired. Please try again.")
}
}
function isTransient(error: unknown) {
const message = (
error instanceof Error ? error.message : String(error)
).toLowerCase()
return (
message.includes("not authenticated") ||
message.includes("network") ||
message.includes("fetch") ||
message.includes("connection") ||
message.includes("timed out") ||
message.includes("timeout")
)
}
function songEntity(song: {
id: string
title: string
artistName: string
artistId: string
albumTitle: string
albumId: string
durationSeconds: number
genres?: string
year?: number
track?: number
diskNumber?: number | null
favorite?: boolean
}): AppIntentEntity {
return {
id: song.id,
title: song.title,
subtitle: song.artistName,
synonyms: song.artistName ? [song.artistName] : undefined,
metadata: {
artistName: song.artistName,
artistId: song.artistId,
albumTitle: song.albumTitle,
albumId: song.albumId,
duration: String(song.durationSeconds),
genres: song.genres ?? "",
year: song.year == null ? "" : String(song.year),
track: song.track == null ? "" : String(song.track),
disk: song.diskNumber == null ? "" : String(song.diskNumber),
favorite: song.favorite ? "1" : "0",
},
}
}
export function AppIntentBridge() {
const scope = useCacheScope()
const store = useMemo(
() => getPlaybackStore(scope === ANONYMOUS_CACHE_SCOPE ? null : scope),
[scope]
)
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot)
const user = useQuery(api.auth.currentUser)
return user && snapshot.hydrated ? <ReadyAppIntentBridge key={scope} /> : null
}
function ReadyAppIntentBridge() {
const convex = useConvex()
const cacheScope = useCacheScope()
const playback = usePlayback()
const user = useQuery(api.auth.currentUser)
const navigation = useAppNavigation()
const favorites = useQuery(api.favorites.getUserFavorites)
const catalogUserKey = user ? JSON.stringify([user._id, favorites]) : null
const store = useMemo(
() =>
getPlaybackStore(
cacheScope === ANONYMOUS_CACHE_SCOPE ? null : cacheScope
),
[cacheScope]
)
const snapshot = useSyncExternalStore(store.subscribe, store.getSnapshot)
const handledIds = useRef<Set<string> | null>(null)
const publishChain = useRef(Promise.resolve())
const publishGeneration = useRef(0)
const playbackRef = useRef(playback)
playbackRef.current = playback
useEffect(() => {
if (!AppIntents.isAvailable() || !user) return
const generation = ++publishGeneration.current
let stopped = false
let refreshing = false
const refresh = () => {
if (refreshing || stopped) return
refreshing = true
publishChain.current = publishChain.current
.catch(() => undefined)
.then(async () => {
for (const kind of ["artist", "album", "song", "playlist"] as const) {
const entities: AppIntentEntity[] = []
let cursor: string | null = null
do {
const result: {
page: AppIntentEntity[]
isDone: boolean
continueCursor: string
} = await convex.query(api.library.intentCatalogPage, {
kind,
paginationOpts: { cursor, numItems: 200 },
})
if (stopped || generation !== publishGeneration.current) return
entities.push(...result.page)
cursor = result.isDone ? null : result.continueCursor
} while (cursor !== null)
await publishMusicCatalog(kind, entities)
}
})
.catch((error) =>
console.warn("Could not publish music catalog.", error)
)
.finally(() => {
refreshing = false
})
trackMusicCatalogSync(publishChain.current)
}
refresh()
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") refresh()
})
const timer = setInterval(() => {
if (AppState.currentState === "active") refresh()
}, 5 * 60_000)
return () => {
stopped = true
++publishGeneration.current
subscription.remove()
clearInterval(timer)
}
}, [convex, cacheScope, catalogUserKey])
const currentSongId = playback.queueSongIds[0] ?? null
const currentSong = playback.currentSong
useEffect(() => {
if (!snapshot.hydrated || !AppIntents.isAvailable()) {
return
}
if (!currentSongId) {
void publishMusicCatalog("nowPlaying", []).catch((error) => {
console.warn("Could not clear the current song.", error)
})
return
}
if (!currentSong || currentSong.id !== currentSongId) {
return
}
void publishMusicCatalog("nowPlaying", [
songEntity({
id: currentSong.id,
title: currentSong.title,
artistName: currentSong.artist.name,
artistId: currentSong.artist.id,
albumTitle: currentSong.album.title,
albumId: currentSong.album.id,
durationSeconds: currentSong.durationSeconds,
}),
]).catch((error) => {
console.warn("Could not publish the current song.", error)
})
}, [currentSong, currentSongId, snapshot.hydrated])
AppIntents.useAppIntents(async (pendingIntents) => {
if (!handledIds.current) {
handledIds.current = await loadHandledIntentIds()
}
const handled = handledIds.current
for (const invocation of pendingIntents) {
if (handled.has(invocation.id)) {
const token = readString(invocation.params.completionToken)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
if (!KNOWN_INTENTS.has(invocation.name)) {
await AppIntents.removePendingInvocationAsync(invocation.id)
continue
}
const token = readString(invocation.params.completionToken)
try {
ensureActive(invocation)
await performInvocation(invocation)
if (
["playMusic", "warmupAudio", "transport", "createStation"].includes(
invocation.name
)
) {
const id = store.getState().queue[0]
const song = playbackRef.current.currentSong
if (!id) await publishMusicCatalog("nowPlaying", [])
else if (song?.id === id)
await publishMusicCatalog("nowPlaying", [
songEntity({
id: song.id,
title: song.title,
artistName: song.artist.name,
artistId: song.artist.id,
albumTitle: song.album.title,
albumId: song.album.id,
durationSeconds: song.durationSeconds,
}),
])
}
await rememberHandledIntentId(handled, invocation.id)
if (token) await completeMusicInvocation(token, null)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (error) {
if (token) {
await completeMusicInvocation(
token,
error instanceof Error ? error.message : "oTunes couldn't do that."
)
}
if (token || error instanceof IntentRejected || !isTransient(error)) {
const message =
error instanceof Error ? error.message : "oTunes couldn't do that."
if (!(error instanceof IntentRejected) || message) {
showError(message)
}
try {
await rememberHandledIntentId(handled, invocation.id)
await AppIntents.removePendingInvocationAsync(invocation.id)
} catch (removeError) {
console.warn(
"App Intent remains pending",
invocation.id,
removeError
)
}
continue
}
console.warn("App Intent remains pending", invocation.id, error)
}
}
async function songIdsFor(kind: string, id: string) {
if (kind === "song") {
return [id as Id<"songs">]
}
if (kind === "album") {
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{
albumId: id as Id<"albums">,
}
)
}
if (kind === "artist") {
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getOrderedArtistSongIds,
{ artistId: id as Id<"artists"> }
)
}
if (kind === "playlist") {
if (id === FAVORITES_PLAYLIST_ID) {
const favorites = await fetchCachedQuery(
convex,
cacheScope,
api.favorites.getUserFavorites,
{}
)
return favorites.favoriteSongs
}
return fetchCachedQuery(
convex,
cacheScope,
api.playlists.getPlaylistSongIds,
{ playlistId: id as Id<"playlists"> }
)
}
throw new IntentRejected("Choose a song, album, artist, or playlist.")
}
async function uniqueSongIds(sources: MusicSource[]) {
const ids: Id<"songs">[] = []
for (const source of sources) {
ids.push(...(await songIdsFor(source.kind, source.id)))
}
return [...new Set(ids)]
}
async function performInvocation(invocation: AppIntentInvocation) {
const actions = playbackRef.current
switch (invocation.name) {
case "openMusic": {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!id || !kind) throw new IntentRejected("Choose music to open.")
if (kind === "song") {
const song = await convex.query(api.library.getSong, {
id: id as Id<"songs">,
})
if (!song)
throw new IntentRejected("This song is no longer available.")
navigation.push(`/album/${song.album}`)
} else if (kind === "playlist" && id === FAVORITES_PLAYLIST_ID) {
navigation.push("/playlist/favorites")
} else if (["album", "artist", "playlist"].includes(kind)) {
navigation.push(`/${kind}/${id}`)
} else throw new IntentRejected("Unknown music type.")
return
}
case "playMusic":
await playMusic(invocation)
if (!["next", "tail"].includes(String(invocation.params.queue))) {
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
}
return
case "warmupAudio":
await warmup(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(false)
warmedAudioIds.add(invocation.id)
return
case "addToPlaylist":
await addToPlaylist(invocation)
return
case "addToUpNext":
await addToUpNext(invocation)
return
case "clearUpNext":
ensureActive(invocation)
await actions.clearUpNext()
return
case "createPlaylist":
await createPlaylist(invocation)
return
case "createStation":
await createStation(invocation)
ensureActive(invocation)
await playbackRef.current.waitForPlaybackReady(true)
return
case "updateAffinity":
await updateAffinity(invocation)
return
case "transport":
await transport(invocation)
if (store.getState().queue.length > 0) {
const command = invocation.params.command
await playbackRef.current.waitForPlaybackReady(
command === "resume"
? true
: command === "pause"
? false
: undefined
)
}
return
default:
return
}
}
async function playMusic(invocation: AppIntentInvocation) {
const actions = playbackRef.current
const queue = readString(invocation.params.queue) ?? "now"
const warmupId = readString(invocation.params.warmupId)
const repeat = repeatChoice(invocation.params.repeat)
if (repeat) {
ensureActive(invocation)
await actions.setRepeatMode(repeat)
}
if (queue === "next" || queue === "tail") {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!kind || !id) {
throw new IntentRejected("Choose music to add to Up Next.")
}
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to add to Up Next.")
}
if (queue === "next") {
ensureActive(invocation)
await actions.addToQueueNext(ids)
} else {
ensureActive(invocation)
await actions.addToQueueLast(ids)
}
return
}
if (warmupId && warmedAudioIds.has(warmupId)) {
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
const kind = readString(invocation.params.kind) ?? "resume"
if (kind === "resume") {
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (shuffled !== actions.shuffleEnabled) {
ensureActive(invocation)
await actions.setShuffleEnabled(shuffled)
}
if (actions.queueSongIds.length === 0) {
const favorites = await fetchCachedQuery(
convex,
cacheScope,
api.favorites.getUserFavorites,
{}
)
if (favorites.favoriteSongs.length === 0) {
throw new IntentRejected(
"Nothing is queued, and your favorites are empty."
)
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(favorites.favoriteSongs)
} else {
ensureActive(invocation)
await actions.playInOrder(favorites.favoriteSongs)
}
return
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
const id = readString(invocation.params.id)
if (!id) {
throw new IntentRejected("Choose music to play.")
}
const shuffled = shuffleChoice(
invocation.params.shuffle,
actions.shuffleEnabled
)
if (kind === "song") {
const song = await fetchCachedQuery(
convex,
cacheScope,
api.library.getSong,
{
id: id as Id<"songs">,
}
)
if (!song) {
throw new IntentRejected("That song is no longer in your library.")
}
const albumSongIds = await fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await actions.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
shuffled,
true
)
return
}
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to play.")
}
if (shuffled) {
ensureActive(invocation)
await actions.playShuffled(ids)
} else {
ensureActive(invocation)
await actions.playInOrder(ids)
}
}
async function warmup(invocation: AppIntentInvocation) {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
if (!kind || !id) {
throw new IntentRejected("Choose music to get ready.")
}
const shuffled = shuffleChoice(invocation.params.shuffle, false)
if (kind === "song") {
const song = await fetchCachedQuery(
convex,
cacheScope,
api.library.getSong,
{
id: id as Id<"songs">,
}
)
if (!song) {
throw new IntentRejected("That song is no longer in your library.")
}
const albumSongIds = await fetchCachedQuery(
convex,
cacheScope,
api.playlists.getAlbumSongIds,
{ albumId: song.album }
)
ensureActive(invocation)
await playbackRef.current.startFromSong(
song._id,
albumSongIds.length > 0 ? albumSongIds : [song._id],
shuffled,
false
)
} else {
const ids = await songIdsFor(kind, id)
if (ids.length === 0) {
throw new IntentRejected("Nothing to get ready.")
}
ensureActive(invocation)
await playbackRef.current.prepareQueue(ids, shuffled)
}
}
async function addToPlaylist(invocation: AppIntentInvocation) {
const playlistId = readString(invocation.params.playlistId)
const sources = readSources(invocation.params.sources)
if (!playlistId || sources.length === 0) {
throw new IntentRejected("Choose music and a playlist.")
}
const songIds = await uniqueSongIds(sources)
if (songIds.length === 0) {
throw new IntentRejected("Those items have no songs to add.")
}
if (playlistId === FAVORITES_PLAYLIST_ID) {
for (const source of sources) {
if (source.kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSong, {
songId: source.id as Id<"songs">,
})
} else if (source.kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteAlbum, {
albumId: source.id as Id<"albums">,
})
} else if (source.kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteArtist, {
artistId: source.id as Id<"artists">,
})
} else if (source.kind === "playlist") {
const ids = await songIdsFor(source.kind, source.id)
if (ids.length > 0) {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSongs, {
songIds: ids,
})
}
} else {
throw new IntentRejected(
"Choose a song, album, artist, or playlist."
)
}
}
return
}
ensureActive(invocation)
await convex.mutation(api.playlists.addSongsToPlaylist, {
playlistId: playlistId as Id<"playlists">,
songIds,
})
}
async function addToUpNext(invocation: AppIntentInvocation) {
const sources = readSources(invocation.params.sources)
if (sources.length === 0) {
throw new IntentRejected("Choose music to add to Up Next.")
}
const ids = await uniqueSongIds(sources)
if (ids.length === 0) {
throw new IntentRejected("Nothing to add to Up Next.")
}
const position =
readString(invocation.params.position) === "last" ? "last" : "next"
if (position === "last") {
ensureActive(invocation)
await playbackRef.current.addToQueueLast(ids)
} else {
ensureActive(invocation)
await playbackRef.current.addToQueueNext(ids)
}
}
async function createPlaylist(invocation: AppIntentInvocation) {
const name = readString(invocation.params.name)
if (!name) {
throw new IntentRejected("A playlist needs a name.")
}
const songIds = await uniqueSongIds(
readSources(invocation.params.sources)
)
ensureActive(invocation)
await convex.mutation(api.playlists.createPlaylist, {
name,
songIds,
invocationId: invocation.id,
})
}
async function createStation(invocation: AppIntentInvocation) {
const artistId = readString(invocation.params.artistId)
if (!artistId) {
throw new IntentRejected("Nothing is playing in oTunes.")
}
const ids = await songIdsFor("artist", artistId)
if (ids.length === 0)
throw new IntentRejected("This artist has no songs to play.")
ensureActive(invocation)
await playbackRef.current.playShuffled(ids)
}
async function updateAffinity(invocation: AppIntentInvocation) {
const kind = readString(invocation.params.kind)
const id = readString(invocation.params.id)
const state = readString(invocation.params.state)
if (!kind || !id || (state !== "like" && state !== "unset")) {
throw new IntentRejected(
"oTunes can love or unlike a song, album, or artist."
)
}
if (kind === "playlist") {
throw new IntentRejected("oTunes can love songs, albums, and artists.")
}
if (state === "like") {
if (kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteSong, {
songId: id as Id<"songs">,
})
} else if (kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteAlbum, {
albumId: id as Id<"albums">,
})
} else if (kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.favoriteArtist, {
artistId: id as Id<"artists">,
})
} else {
throw new IntentRejected(
"oTunes can love songs, albums, and artists."
)
}
return
}
if (kind === "song") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteSong, {
songId: id as Id<"songs">,
})
} else if (kind === "album") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteAlbum, {
albumId: id as Id<"albums">,
})
} else if (kind === "artist") {
ensureActive(invocation)
await convex.mutation(api.favorites.unfavoriteArtist, {
artistId: id as Id<"artists">,
})
} else {
throw new IntentRejected("oTunes can love songs, albums, and artists.")
}
}
async function transport(invocation: AppIntentInvocation) {
const actions = playbackRef.current
const command = readString(invocation.params.command)
if (command === "pause") {
ensureActive(invocation)
actions.setIsPlaying(false)
return
}
if (command === "resume") {
if (actions.queueSongIds.length === 0) {
throw new IntentRejected("Nothing is queued in oTunes.")
}
ensureActive(invocation)
actions.setIsPlaying(true)
return
}
if (command === "next") {
ensureActive(invocation)
await actions.skipNext()
return
}
if (command === "previous") {
ensureActive(invocation)
await actions.skipPrevious()
return
}
throw new IntentRejected("oTunes doesn't know that playback control.")
}
})
return null
}
+10 -3
View File
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { Ionicons } from "@expo/vector-icons"
import { memo, useCallback, type ReactElement } from "react"
import {
@@ -78,7 +79,9 @@ export const AlbumCard = memo(function AlbumCard({
share={{ type: "album", albumId: album.id }}
onAction={(action) => void handleAlbumAction(album, action)}
>
{card}
<MusicEntityView kind="album" id={album.id}>
{card}
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -121,7 +124,9 @@ export const ArtistCard = memo(function ArtistCard({
share={{ type: "artist", artistId: artist.id }}
onAction={(action) => void handleArtistAction(artist, action)}
>
{card}
<MusicEntityView kind="artist" id={artist.id}>
{card}
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -170,7 +175,9 @@ export const PlaylistCard = memo(function PlaylistCard({
share={{ type: "playlist", playlistId: playlist.id }}
onAction={(action) => void handlePlaylistAction(playlist, action)}
>
{card}
<MusicEntityView kind="playlist" id={playlist.id}>
{card}
</MusicEntityView>
</MediaContextMenu>
)
})
@@ -0,0 +1,27 @@
import { useIsFocused } from "expo-router"
import { AppEntityView } from "expo-app-intents"
import type { PropsWithChildren } from "react"
import { View, type ViewProps } from "react-native"
import type { MusicEntityKind } from "@/lib/app-intents"
/** Only the focused screen contributes entities to Siri's on-screen context. */
export function MusicEntityView({
kind,
id,
children,
...props
}: PropsWithChildren<
ViewProps & {
kind: MusicEntityKind
id: string
}
>) {
const focused = useIsFocused()
return focused ? (
<AppEntityView entity={kind} entityId={id} {...props}>
{children}
</AppEntityView>
) : (
<View {...props}>{children}</View>
)
}
+17 -4
View File
@@ -1,3 +1,4 @@
import { MusicEntityView } from "@/components/music-entity-view"
import { collectionInset } from "@/components/collection-list"
import { CoverImage } from "@/components/cover-image"
import { useServerConfig } from "@/contexts/server-config"
@@ -45,10 +46,16 @@ export const SongRow = memo(function SongRow({
const content = (
<>
<View style={styles.rowMeta}>
<Text style={[styles.rowTitle, { color: foreground }]} numberOfLines={1}>
<Text
style={[styles.rowTitle, { color: foreground }]}
numberOfLines={1}
>
{song.title}
</Text>
<Text style={[styles.rowSubtitle, { color: secondaryForeground }]} numberOfLines={1}>
<Text
style={[styles.rowSubtitle, { color: secondaryForeground }]}
numberOfLines={1}
>
{song.artist.name} · {song.album.title}
</Text>
</View>
@@ -82,12 +89,18 @@ export const SongRow = memo(function SongRow({
)
if (!contextMenu) {
return row
return (
<MusicEntityView kind="song" id={song.id}>
{row}
</MusicEntityView>
)
}
return (
<MediaContextMenu {...contextMenu}>
{row}
<MusicEntityView kind="song" id={song.id}>
{row}
</MusicEntityView>
</MediaContextMenu>
)
})
+6 -1
View File
@@ -5,6 +5,7 @@ import { useCallback, useMemo, useState, type ReactNode } from "react"
import { ConvexAuthSessionProvider } from "@/contexts/convex-auth-session"
import { PersistentCacheProvider } from "@/contexts/persistent-cache"
import { useServerConfig } from "@/contexts/server-config"
import { clearAppIntentCatalogs } from "@/lib/app-intent-handled"
import { clearStoredAuthTokens, secureStorage } from "@/lib/auth-storage"
export function ConvexAppProvider({ children }: { children: ReactNode }) {
@@ -21,7 +22,11 @@ export function ConvexAppProvider({ children }: { children: ReactNode }) {
return
}
await clearStoredAuthTokens(config.convexUrl)
setAuthEpoch((epoch) => epoch + 1)
try {
await clearAppIntentCatalogs()
} finally {
setAuthEpoch((epoch) => epoch + 1)
}
}, [config])
if (!client || !config) {
+149 -6
View File
@@ -39,6 +39,7 @@ import { getAuthCookieHeader } from "@/lib/auth-cookies"
import { flushPersistentCache } from "@/lib/persistent-cache"
import { loadPlaybackSongs } from "@/lib/playback-files"
import { rememberSongs, useSong, useSongs } from "@/lib/song-cache"
import { donateMusicPlay, type MusicEntityKind } from "@/lib/app-intents"
import { showError } from "@/lib/toast"
import { albumCoverUrl, songAudioSource } from "@/lib/urls"
import {
@@ -82,6 +83,7 @@ type PlaybackContextValue = {
currentTime: number
duration: number
seekTo: (seconds: number) => void
waitForPlaybackReady: (playing?: boolean) => Promise<void>
setIsPlaying: (playing: boolean) => void
setVolume: (volume: number) => void
playInOrder: (songIds: Id<"songs">[]) => Promise<void>
@@ -112,8 +114,18 @@ type PlaybackContextValue = {
songIds?: Id<"songs">[]
) => Promise<void>
toggleShuffle: () => Promise<void>
setShuffleEnabled: (enabled: boolean) => Promise<void>
cycleRepeat: () => Promise<void>
setRepeatMode: (mode: RepeatMode) => Promise<void>
prepareQueue: (songIds: Id<"songs">[], shuffled: boolean) => Promise<void>
startFromSong: (
songId: Id<"songs">,
songIds: Id<"songs">[],
shuffled: boolean,
play: boolean
) => Promise<void>
clearQueue: () => Promise<void>
clearUpNext: () => Promise<void>
clearHistory: () => Promise<void>
reorderQueue: (songIds: Id<"songs">[]) => Promise<void>
skipToQueueIndex: (index: number) => Promise<void>
@@ -138,8 +150,13 @@ type PlaybackActionsValue = Pick<
| "playPlaylist"
| "playPlaylistShuffled"
| "toggleShuffle"
| "setShuffleEnabled"
| "cycleRepeat"
| "setRepeatMode"
| "prepareQueue"
| "startFromSong"
| "clearQueue"
| "clearUpNext"
| "clearHistory"
| "reorderQueue"
| "skipToQueueIndex"
@@ -857,7 +874,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
recipe: (
state: playbackQueue.PlaybackQueueState
) => playbackQueue.PlaybackQueueState,
recorded: { playlistId?: Id<"playlists"> } = {}
recorded: {
playlistId?: Id<"playlists">
entity?: { kind: MusicEntityKind; id: string }
} = {}
) => {
playRequestRef.current += 1
const previousSongId = store.getState().queue[0]
@@ -869,6 +889,29 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
void seekTo(0)
}
setIsPlaying(true)
void (async () => {
const deadline = Date.now() + 20_000
while (
Date.now() < deadline &&
store.getState().queue[0] === firstSongId
) {
const player = playlistRef.current
if (
player?.isLoaded &&
player.playing &&
loadedSongIdsRef.current[player.currentIndex] === firstSongId
) {
await donateMusicPlay(
recorded.entity?.kind ??
(recorded.playlistId ? "playlist" : "song"),
recorded.entity?.id ?? recorded.playlistId ?? firstSongId,
store.getState().shuffleEnabled
)
return
}
await new Promise((resolve) => setTimeout(resolve, 250))
}
})()
recordPlay(
recorded.playlistId
? { playlistId: recorded.playlistId }
@@ -882,7 +925,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
async (
songIds: Id<"songs">[] | Promise<Id<"songs">[]>,
shuffled: boolean,
recorded: { playlistId?: Id<"playlists"> } = {}
recorded: {
playlistId?: Id<"playlists">
entity?: { kind: MusicEntityKind; id: string }
} = {}
) => {
const request = ++playRequestRef.current
let resolvedSongIds: Id<"songs">[]
@@ -1018,25 +1064,33 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const playAlbum = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
playCollection(loadAlbumSongIds(albumId, songIds), false),
playCollection(loadAlbumSongIds(albumId, songIds), false, {
entity: { kind: "album", id: albumId },
}),
[playCollection, loadAlbumSongIds]
)
const playAlbumShuffled = useCallback(
(albumId: Id<"albums">, songIds?: Id<"songs">[]) =>
playCollection(loadAlbumSongIds(albumId, songIds), true),
playCollection(loadAlbumSongIds(albumId, songIds), true, {
entity: { kind: "album", id: albumId },
}),
[playCollection, loadAlbumSongIds]
)
const playArtist = useCallback(
(artistId: Id<"artists">) =>
playCollection(loadArtistSongIds(artistId), false),
playCollection(loadArtistSongIds(artistId), false, {
entity: { kind: "artist", id: artistId },
}),
[playCollection, loadArtistSongIds]
)
const playArtistShuffled = useCallback(
(artistId: Id<"artists">) =>
playCollection(loadArtistSongIds(artistId), true),
playCollection(loadArtistSongIds(artistId), true, {
entity: { kind: "artist", id: artistId },
}),
[playCollection, loadArtistSongIds]
)
@@ -1060,16 +1114,63 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
store.update(playbackQueue.toggleShuffle)
}, [store])
const setShuffleEnabled = useCallback(
async (enabled: boolean) => {
store.update((state) => playbackQueue.setShuffleEnabled(state, enabled))
},
[store]
)
const cycleRepeat = useCallback(async () => {
store.update(playbackQueue.cycleRepeat)
}, [store])
const setRepeatMode = useCallback(
async (mode: RepeatMode) => {
store.update((state) => playbackQueue.setRepeatMode(state, mode))
},
[store]
)
const prepareQueue = useCallback(
async (songIds: Id<"songs">[], shuffled: boolean) => {
if (songIds.length === 0) return
playRequestRef.current += 1
store.update((state) =>
playbackQueue.startQueue(state, songIds, shuffled)
)
setIsPlaying(false)
},
[store, setIsPlaying]
)
const startFromSong = useCallback(
async (
songId: Id<"songs">,
songIds: Id<"songs">[],
shuffled: boolean,
play: boolean
) => {
playRequestRef.current += 1
store.update((state) => playbackQueue.setShuffleEnabled(state, shuffled))
store.update((state) =>
playbackQueue.startQueueFromSong(state, songId, songIds)
)
setIsPlaying(play)
},
[store, setIsPlaying]
)
const clearQueue = useCallback(async () => {
playRequestRef.current += 1
store.update(playbackQueue.clearQueue)
setIsPlaying(false)
}, [store, setIsPlaying])
const clearUpNext = useCallback(async () => {
store.update(playbackQueue.clearUpNext)
}, [store])
const clearHistory = useCallback(async () => {
store.update(playbackQueue.clearHistory)
}, [store])
@@ -1131,6 +1232,36 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
store.update(playbackQueue.skipPrevious)
}, [store, seekTo])
// Queue changes return before React has loaded the native audio source. Siri
// must observe the native player, rather than treating a state update as success.
const waitForPlaybackReady = useCallback(
async (playing?: boolean) => {
const expectedId = store.getState().queue[0]
if (!expectedId) throw new Error("Nothing is queued to play.")
const deadline = Date.now() + 20_000
while (Date.now() < deadline) {
if (store.getState().queue[0] !== expectedId) {
throw new Error(
"The playback queue changed before the request finished."
)
}
const player = playlistRef.current
if (
player &&
loadedSongIdsRef.current[player.currentIndex] === expectedId &&
player.isLoaded &&
(playing === undefined || player.playing === playing)
)
return
await new Promise((resolve) => setTimeout(resolve, 100))
}
throw new Error(
"The audio did not become ready. Check your connection and try again."
)
},
[store]
)
const actionsValue = useMemo<PlaybackActionsValue>(
() => ({
setIsPlaying,
@@ -1147,8 +1278,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
playPlaylist,
playPlaylistShuffled,
toggleShuffle,
setShuffleEnabled,
cycleRepeat,
setRepeatMode,
prepareQueue,
startFromSong,
clearQueue,
clearUpNext,
clearHistory,
reorderQueue,
skipToQueueIndex,
@@ -1161,7 +1297,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
addToQueueLast,
clearHistory,
clearQueue,
clearUpNext,
cycleRepeat,
prepareQueue,
startFromSong,
setRepeatMode,
setShuffleEnabled,
playAlbum,
playAlbumShuffled,
playArtist,
@@ -1197,6 +1338,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
const value = useMemo<PlaybackContextValue>(
() => ({
...actionsValue,
waitForPlaybackReady,
queueSongIds,
queueHistorySongIds,
currentSong,
@@ -1211,6 +1353,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
}),
[
actionsValue,
waitForPlaybackReady,
currentSong,
currentTime,
duration,
+59
View File
@@ -0,0 +1,59 @@
import AsyncStorage from "@react-native-async-storage/async-storage"
import * as AppIntents from "expo-app-intents"
import { publishMusicCatalog } from "@/lib/app-intents"
export const FAVORITES_PLAYLIST_ID = "otunes:favorites"
const HANDLED_KEY = "otunes:handled-app-intents"
const MAX_HANDLED = 200
const CATALOG_KINDS = [
"song",
"album",
"artist",
"playlist",
"nowPlaying",
] as const
export async function clearAppIntentCatalogs() {
if (!AppIntents.isAvailable()) {
return
}
const results = await Promise.allSettled([
AppIntents.deleteDonationsAsync({ intent: "playMusic" }),
AppIntents.clearPendingInvocationsAsync(),
...CATALOG_KINDS.map((kind) => publishMusicCatalog(kind, [])),
])
const failed = results.find((result) => result.status === "rejected")
if (failed?.status === "rejected") throw failed.reason
}
export async function loadHandledIntentIds() {
const raw = await AsyncStorage.getItem(HANDLED_KEY)
if (!raw) {
return new Set<string>()
}
try {
const parsed = JSON.parse(raw) as unknown
if (!Array.isArray(parsed)) {
return new Set<string>()
}
return new Set(parsed.filter((id) => typeof id === "string"))
} catch {
return new Set<string>()
}
}
export async function rememberHandledIntentId(ids: Set<string>, id: string) {
ids.add(id)
const list = [...ids]
const trimmed =
list.length > MAX_HANDLED ? list.slice(list.length - MAX_HANDLED) : list
if (trimmed.length !== list.length) {
ids.clear()
for (const item of trimmed) {
ids.add(item)
}
}
await AsyncStorage.setItem(HANDLED_KEY, JSON.stringify(trimmed))
}
+147
View File
@@ -0,0 +1,147 @@
const { test } = require("node:test")
const assert = require("node:assert/strict")
const fs = require("node:fs")
const vm = require("node:vm")
const ts = require("typescript")
// Exercise the app's catalog lifecycle without requiring a React Native runtime.
function runtime() {
const disk = new Map()
const catalogs = new Map()
const deleted = []
const donated = []
const native = {
catalogJSON: async (kind) => JSON.stringify(disk.get(kind) ?? []),
replaceCatalog: async (kind, json) => disk.set(kind, JSON.parse(json)),
completeInvocation: async () => {},
}
const expo = {
isAvailable: () => true,
getEntityCatalogAsync: async (kind) => catalogs.get(kind) ?? [],
setEntityCatalogAsync: async (kind, entities) =>
catalogs.set(kind, entities),
deleteDonationsAsync: async (filter) => deleted.push(filter),
donateIntentAsync: async (name, params) => donated.push({ name, params }),
}
const module = { exports: {} }
const source = ts.transpileModule(
fs.readFileSync(`${__dirname}/app-intents.ts`, "utf8"),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}
).outputText
vm.runInNewContext(source, {
module,
exports: module.exports,
console: { warn() {} },
require: (name) => {
if (name === "expo") return { requireOptionalNativeModule: () => native }
if (name === "expo-app-intents") return expo
throw new Error(`Unexpected import ${name}`)
},
})
return { api: module.exports, native, expo, disk, catalogs, deleted, donated }
}
function song(id, favorite = false) {
return {
id: String(id),
title: `Song ${id}`,
metadata: { favorite: favorite ? "1" : "0" },
}
}
function deferred() {
let resolve
const promise = new Promise((done) => {
resolve = done
})
return { promise, resolve }
}
test("large libraries remain fully searchable on disk while suggestions stay bounded", async () => {
const { api, disk, catalogs } = runtime()
const songs = Array.from({ length: 10_000 }, (_, id) => song(id, id === 9999))
await api.publishMusicCatalog("song", songs)
assert.equal(disk.get("song").length, 10_000)
assert.equal(catalogs.get("song").length, 100)
assert.equal(catalogs.get("song")[0].id, "9999")
})
test("deleting a song outside the suggestion subset removes its donations", async () => {
const { api, deleted } = runtime()
const songs = Array.from({ length: 200 }, (_, id) => song(id))
await api.publishMusicCatalog("song", songs)
await api.publishMusicCatalog("song", songs.slice(0, -1))
assert.equal(deleted.length, 1)
assert.equal(deleted[0].entity, "song")
assert.equal(deleted[0].id, "199")
})
test("a queued clear finishes after an in-flight publication and leaves no stale index", async () => {
const { api, native, disk, catalogs } = runtime()
const gate = deferred()
const original = native.replaceCatalog
native.replaceCatalog = async (...args) => {
await gate.promise
return original(...args)
}
const publish = api.publishMusicCatalog("song", [song(1)])
const clear = api.publishMusicCatalog("song", [])
gate.resolve()
await Promise.all([publish, clear])
assert.equal(disk.get("song").length, 0)
assert.equal(catalogs.get("song").length, 0)
})
test("a storage failure does not poison subsequent catalog cleanup", async () => {
const { api, native, catalogs } = runtime()
const original = native.replaceCatalog
native.replaceCatalog = async () => {
throw new Error("Disk unavailable")
}
await assert.rejects(
api.publishMusicCatalog("song", [song(1)]),
/Disk unavailable/
)
native.replaceCatalog = original
await api.publishMusicCatalog("song", [])
assert.equal(catalogs.get("song").length, 0)
})
test("donations wait for catalog synchronization and failures do not reject playback", async () => {
const { api, donated, expo } = runtime()
const sync = deferred()
api.trackMusicCatalogSync(sync.promise)
const donation = api.donateMusicPlay("album", "album-1")
await new Promise((resolve) => setImmediate(resolve))
assert.equal(donated.length, 0)
sync.resolve()
await donation
assert.equal(donated[0].params.id, "album-1")
expo.donateIntentAsync = async () => {
throw new Error("Suggestions unavailable")
}
await api.donateMusicPlay("album", "album-1")
})
test("failed donation cleanup retains deleted records so the next sync can retry", async () => {
const { api, disk, expo, deleted } = runtime()
await api.publishMusicCatalog("song", [song(1), song(2)])
const cleanup = expo.deleteDonationsAsync
expo.deleteDonationsAsync = async () => {
throw new Error("Cleanup failed")
}
await assert.rejects(
api.publishMusicCatalog("song", [song(1)]),
/Cleanup failed/
)
assert.equal(disk.get("song").length, 2)
expo.deleteDonationsAsync = cleanup
await api.publishMusicCatalog("song", [song(1)])
assert.equal(deleted[0].id, "2")
assert.equal(disk.get("song").length, 1)
})
+74
View File
@@ -0,0 +1,74 @@
import { requireOptionalNativeModule } from "expo"
import * as AppIntents from "expo-app-intents"
import type { AppIntentEntity } from "expo-app-intents"
type MusicIntentModule = {
replaceCatalog: (kind: string, json: string) => Promise<void>
catalogJSON: (kind: string) => Promise<string>
completeInvocation: (token: string, error: string | null) => Promise<void>
}
const native = requireOptionalNativeModule<MusicIntentModule>("AppIntentsSetup")
export type MusicEntityKind = "song" | "album" | "artist" | "playlist"
const SPOTLIGHT_LIMIT = 100
let publishing: Promise<void> = Promise.resolve()
let catalogSync: Promise<void> = Promise.resolve()
export function trackMusicCatalogSync(sync: Promise<void>) {
catalogSync = sync
}
/** Serialize writes so sign-out cleanup cannot be overtaken by an older publish. */
export function publishMusicCatalog(kind: string, entities: AppIntentEntity[]) {
const write = publishing
.catch(() => undefined)
.then(async () => {
if (!native || !AppIntents.isAvailable()) return
const oldDiskCatalog = JSON.parse(
await native.catalogJSON(kind)
) as AppIntentEntity[]
const suggestions = [...entities]
.sort(
(a, b) =>
Number(b.metadata?.favorite === "1") -
Number(a.metadata?.favorite === "1")
)
.slice(0, SPOTLIGHT_LIMIT)
const previous = await AppIntents.getEntityCatalogAsync(kind)
const remaining = new Set(entities.map((entity) => entity.id))
for (const entity of new Map(
[...oldDiskCatalog, ...previous].map((item) => [item.id, item])
).values()) {
if (!remaining.has(entity.id) && kind !== "nowPlaying") {
await AppIntents.deleteDonationsAsync({ entity: kind, id: entity.id })
}
}
await native.replaceCatalog(kind, JSON.stringify(entities))
await AppIntents.setEntityCatalogAsync(kind, suggestions)
})
publishing = write
return write
}
export async function completeMusicInvocation(
token: string,
error: string | null
) {
await native?.completeInvocation(token, error)
}
/** Donation errors never turn a completed user action into a playback failure. */
export async function donateMusicPlay(
kind: MusicEntityKind,
id: string,
shuffle = false
) {
if (!native || !AppIntents.isAvailable()) return
try {
await catalogSync
await publishing
await AppIntents.donateIntentAsync("playMusic", { kind, id, shuffle })
} catch (error) {
console.warn("Could not donate music playback.", error)
}
}
+2 -1
View File
@@ -1,5 +1,6 @@
import type { useRouter } from "expo-router"
import { clearAppIntentCatalogs } from "@/lib/app-intent-handled"
import { resetPlaybackStores } from "@otunes/convex/client/playback-store"
import { clearPersistentCache } from "@/lib/persistent-cache"
@@ -8,7 +9,7 @@ type AppRouter = ReturnType<typeof useRouter>
/** Forgets this device's cached library and playback state. */
export async function clearLocalData() {
resetPlaybackStores()
await clearPersistentCache()
await Promise.all([clearPersistentCache(), clearAppIntentCatalogs()])
}
export async function signOutToSignIn(
+1
View File
@@ -23,6 +23,7 @@
"@react-native/metro-config": "0.88.0",
"convex": "^1.40.0",
"expo": "~58.0.16",
"expo-app-intents": "0.5.10",
"expo-asset": "~58.0.17",
"expo-audio": "^58.0.9",
"expo-blur": "~58.0.5",
+28
View File
@@ -132,6 +132,34 @@ export function cycleRepeat(state: PlaybackQueueState): PlaybackQueueState {
return { ...state, repeatEnabled }
}
export function setShuffleEnabled(
state: PlaybackQueueState,
enabled: boolean
): PlaybackQueueState {
if (state.shuffleEnabled === enabled) {
return state
}
return toggleShuffle(state)
}
export function setRepeatMode(
state: PlaybackQueueState,
repeatEnabled: RepeatMode
): PlaybackQueueState {
if (state.repeatEnabled === repeatEnabled) {
return state
}
return { ...state, repeatEnabled }
}
/** Drops everything after the current song. The song that is playing stays. */
export function clearUpNext(state: PlaybackQueueState): PlaybackQueueState {
if (state.queue.length <= 1) {
return state
}
return { ...state, queue: [state.queue[0]] }
}
export function clearQueue(state: PlaybackQueueState): PlaybackQueueState {
return { ...state, queue: [], history: [], progressSeconds: 0 }
}
@@ -0,0 +1,85 @@
const { test } = require("node:test")
const assert = require("node:assert/strict")
const fs = require("node:fs")
const vm = require("node:vm")
const ts = require("typescript")
const values = require("convex/values")
function playlistMutation() {
const module = { exports: {} }
const source = ts.transpileModule(
fs.readFileSync(`${__dirname}/playlists.ts`, "utf8"),
{
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}
).outputText
vm.runInNewContext(source, {
module,
exports: module.exports,
require: (name) => {
if (name === "convex/server") return { paginationOptsValidator: {} }
if (name === "convex/values") return values
if (name === "@convex-dev/auth/server")
return { getAuthUserId: async (ctx) => ctx.userId }
if (name === "./_generated/server")
return {
mutation: (definition) => definition,
query: (definition) => definition,
}
if (name === "./library") return {}
throw new Error(`Unexpected import ${name}`)
},
})
return module.exports.createPlaylist.handler
}
test("a delivered playlist request is committed once per account", async () => {
const createPlaylist = playlistMutation()
const tables = { playlists: [], appIntentPlaylists: [] }
const db = {
insert: async (table, document) => {
const id = `${table}-${tables[table].length + 1}`
tables[table].push({ ...document, _id: id })
return id
},
query: (table) => ({
withIndex: (_name, configure) => {
const filters = []
const index = {
eq: (key, value) => {
filters.push([key, value])
return index
},
}
configure(index)
return {
unique: async () =>
tables[table].find((row) =>
filters.every(([key, value]) => row[key] === value)
) ?? null,
}
},
}),
}
const request = {
name: "Test playlist",
songIds: ["song-1"],
invocationId: "request-1",
}
const first = await createPlaylist({ userId: "user-1", db }, request)
const replay = await createPlaylist({ userId: "user-1", db }, request)
assert.equal(first, replay)
assert.equal(tables.playlists.length, 1)
const otherAccount = await createPlaylist({ userId: "user-2", db }, request)
assert.notEqual(otherAccount, first)
assert.equal(tables.playlists.length, 2)
const nextRequest = await createPlaylist(
{ userId: "user-1", db },
{ ...request, invocationId: "request-2" }
)
assert.notEqual(nextRequest, first)
assert.equal(tables.playlists.length, 3)
})
+124
View File
@@ -1047,6 +1047,130 @@ export const getSongsForPlayback = query({
},
})
const FAVORITES_PLAYLIST_ID = "otunes:favorites"
/** Bounded pages for the device's disk-backed Siri catalog. */
export const intentCatalogPage = query({
args: {
kind: v.union(
v.literal("song"),
v.literal("album"),
v.literal("artist"),
v.literal("playlist")
),
paginationOpts: paginationOptsValidator,
},
handler: async (ctx, { kind, paginationOpts }) => {
const userId = await getAuthUserId(ctx)
if (!userId) throw new ConvexError("Not authenticated")
const user = await ctx.db.get("users", userId)
if (!user) throw new ConvexError("Not authenticated")
type Entity = {
id: string
title: string
subtitle?: string
synonyms?: string[]
metadata: Record<string, string>
}
const page: Entity[] = []
if (kind === "song") {
const result = await ctx.db.query("songs").paginate(paginationOpts)
for (const song of result.page) {
const album = await ctx.db.get("albums", song.album)
const artist = await ctx.db.get("artists", song.albumArtist)
if (!album || !artist || !song.name.trim()) continue
const credits = getSongArtistNames(song.artist)
const artistName = songArtistsMatchAlbum(credits, artist.name)
? artist.name
: credits.join(", ") || artist.name
page.push({
id: song._id,
title: song.name,
subtitle: artistName,
metadata: {
artistName,
artistId: artist._id,
albumTitle: album.name,
albumId: album._id,
duration: String(Math.max(0, Math.round(song.length))),
genres: song.genres.join(", "),
year: String(song.year),
track: String(song.track),
disk: String(song.diskNumber ?? ""),
favorite: user.favoriteSongs?.includes(song._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
if (kind === "album") {
const result = await ctx.db.query("albums").paginate(paginationOpts)
for (const album of result.page) {
const artist = await ctx.db.get("artists", album.artist)
if (!artist || !album.name.trim()) continue
page.push({
id: album._id,
title: album.name,
subtitle: artist.name,
metadata: {
artistName: artist.name,
artistId: artist._id,
year: String(album.year),
favorite: user.favoriteAlbums?.includes(album._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
if (kind === "artist") {
const result = await ctx.db.query("artists").paginate(paginationOpts)
for (const artist of result.page) {
if (artist.name.trim())
page.push({
id: artist._id,
title: artist.name,
metadata: {
favorite: user.favoriteArtists?.includes(artist._id) ? "1" : "0",
},
})
}
return { ...result, page }
}
const result = await ctx.db.query("playlists").paginate(paginationOpts)
for (const playlist of result.page) {
if (
playlist.owner !== userId &&
!user.subscribedPlaylists?.includes(playlist._id)
)
continue
if (!playlist.name.trim()) continue
const owner = await ctx.db.get("users", playlist.owner)
page.push({
id: playlist._id,
title: playlist.name,
subtitle: owner?.name ?? "",
metadata: {
ownerName: owner?.name ?? "",
createdByMe: playlist.owner === userId ? "1" : "0",
},
})
}
if (paginationOpts.cursor === null)
page.unshift({
id: FAVORITES_PLAYLIST_ID,
title: "Favorites",
synonyms: [
"favorites",
"favourite songs",
"liked songs",
"loved songs",
],
metadata: { ownerName: user.name, createdByMe: "1" },
})
return { ...result, page }
},
})
export const getSongByName = internalQuery({
args: { name: v.string() },
handler: async (ctx, args) => {
+19 -1
View File
@@ -358,10 +358,21 @@ export const getPlaylistSongIds = query({
export const createPlaylist = mutation({
args: {
name: v.string(),
invocationId: v.optional(v.string()),
songIds: v.optional(v.array(v.id("songs"))),
},
handler: async (ctx, args) => {
const userId = await requireAuth(ctx)
const invocationId = args.invocationId
if (invocationId) {
const previous = await ctx.db
.query("appIntentPlaylists")
.withIndex("by_user_invocation", (q) =>
q.eq("userId", userId).eq("invocationId", invocationId)
)
.unique()
if (previous) return previous.playlistId
}
const trimmed = args.name.trim()
if (!trimmed) {
throw new ConvexError("Playlist name cannot be empty")
@@ -369,12 +380,19 @@ export const createPlaylist = mutation({
const uniqueSongIds = [...new Set(args.songIds ?? [])]
return await ctx.db.insert("playlists", {
const playlistId = await ctx.db.insert("playlists", {
name: trimmed,
owner: userId,
cover: null,
songs: uniqueSongIds,
})
if (args.invocationId)
await ctx.db.insert("appIntentPlaylists", {
userId,
invocationId: args.invocationId,
playlistId,
})
return playlistId
},
})
+5
View File
@@ -40,6 +40,11 @@ export default defineSchema({
})
.index("signature", ["signature"])
.index("sessionId", ["sessionId"]),
appIntentPlaylists: defineTable({
userId: v.id("users"),
invocationId: v.string(),
playlistId: v.id("playlists"),
}).index("by_user_invocation", ["userId", "invocationId"]),
appSettings: defineTable({
disableAccountCreation: v.boolean(),
}),
+24
View File
@@ -55,6 +55,9 @@ importers:
expo:
specifier: ~58.0.16
version: 58.0.16(@babel/core@7.29.7)(@expo/dom-webview@58.0.2)(@expo/metro-runtime@58.0.14)(expo-router@58.0.24)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3)
expo-app-intents:
specifier: 0.5.10
version: 0.5.10(@babel/core@7.29.7)(expo@58.0.16)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)
expo-asset:
specifier: ~58.0.17
version: 58.0.17(expo@58.0.16)(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3)
@@ -3950,6 +3953,14 @@ packages:
exif-parser@0.1.12:
resolution: {integrity: sha512-c2bQfLNbMzLPmzQuOr8fy0csy84WmwnER81W88DzTp9CYNPJ6yzOj2EZAh9pywYpqHnshVLHQJ8WzldAyfY+Iw==}
expo-app-intents@0.5.10:
resolution: {integrity: sha512-PNxAbIaNjLDeajnVJGClDXnB1CFkGScuAYcEORFsmaLmABMjlxq/jNC708cgWVyDgX2XUvDBXifq2rqvQAsPmQ==}
hasBin: true
peerDependencies:
expo: '*'
react: '*'
react-native: '*'
expo-asset@58.0.17:
resolution: {integrity: sha512-CDYXjwNoTQh4s00pR99mkbwh6A5/eUDMqKXPoLnuTW6kYKzuQEVxLpOPTj5zg0uYpCA6KAHI294li0roYGMqHQ==}
peerDependencies:
@@ -10953,6 +10964,19 @@ snapshots:
exif-parser@0.1.12: {}
expo-app-intents@0.5.10(@babel/core@7.29.7)(expo@58.0.16)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0):
dependencies:
'@expo/ui': 58.0.21(@babel/core@7.29.7)(expo@58.0.16)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)
commander: 12.1.0
expo: 58.0.16(@babel/core@7.29.7)(@expo/dom-webview@58.0.2)(@expo/metro-runtime@58.0.14)(expo-router@58.0.24)(react-dom@19.3.0(react@19.3.0))(react-native-worklets@0.13.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0))(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3)
prompts: 2.4.2
react: 19.3.0
react-native: 0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0)
transitivePeerDependencies:
- '@babel/core'
- react-dom
- react-native-worklets
expo-asset@58.0.17(expo@58.0.16)(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7))(@types/react@19.3.0)(react@19.3.0))(react@19.3.0)(typescript@6.0.3):
dependencies:
'@expo/image-utils': 0.12.5(typescript@6.0.3)