forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import { RockskyClient } from "client";
2
3export async function search(
4 query: string,
5 { limit = 20, albums = false, artists = false, tracks = false, users = false }
6): Promise<string> {
7 const client = new RockskyClient();
8 const results = await client.search(query, { size: limit });
9 if (results.records.length === 0) {
10 return `No results found for ${query}.`;
11 }
12
13 // merge all results into one array with type and sort by xata_scrore
14 let mergedResults = results.records.map((record) => ({
15 ...record,
16 type: record.table,
17 }));
18
19 if (albums) {
20 mergedResults = mergedResults.filter((record) => record.table === "albums");
21 }
22
23 if (artists) {
24 mergedResults = mergedResults.filter(
25 (record) => record.table === "artists"
26 );
27 }
28
29 if (tracks) {
30 mergedResults = mergedResults.filter(({ table }) => table === "tracks");
31 }
32
33 if (users) {
34 mergedResults = mergedResults.filter(({ table }) => table === "users");
35 }
36
37 mergedResults.sort((a, b) => b.xata_score - a.xata_score);
38
39 const responses = [];
40 for (const { table, record } of mergedResults) {
41 if (table === "users") {
42 responses.push({
43 handle: record.handle,
44 display_name: record.display_name,
45 did: record.did,
46 link: `https://rocksky.app/profile/${record.did}`,
47 type: "account",
48 });
49 }
50
51 if (table === "albums") {
52 const link = record.uri
53 ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
54 : "";
55 responses.push({
56 title: record.title,
57 artist: record.artist,
58 link: link,
59 type: "album",
60 });
61 }
62
63 if (table === "tracks") {
64 const link = record.uri
65 ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
66 : "";
67 responses.push({
68 title: record.title,
69 artist: record.artist,
70 link: link,
71 type: "track",
72 });
73 }
74
75 if (table === "artists") {
76 const link = record.uri
77 ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
78 : "";
79 responses.push({
80 name: record.name,
81 link: link,
82 type: "artist",
83 });
84 }
85 }
86
87 return JSON.stringify(responses, null, 2);
88}