@rmdes/indiekit-endpoint-lastfm 1.0.5 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/config.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Get effective config: DB-stored settings override env var defaults
3
+ * @param {object} db - MongoDB database instance
4
+ * @param {object} lastfmConfig - Plugin config from env vars
5
+ * @returns {Promise<object>} Effective apiKey, username, and other config
6
+ */
7
+ export async function getEffectiveConfig(db, lastfmConfig) {
8
+ let apiKey = lastfmConfig?.apiKey || "";
9
+ let username = lastfmConfig?.username || "";
10
+
11
+ if (db) {
12
+ try {
13
+ const settings = await db
14
+ .collection("lastfmMeta")
15
+ .findOne({ key: "settings" });
16
+ if (settings) {
17
+ if (settings.apiKey) apiKey = settings.apiKey;
18
+ if (settings.username) username = settings.username;
19
+ }
20
+ } catch {
21
+ // Fall through to defaults
22
+ }
23
+ }
24
+
25
+ return { apiKey, username };
26
+ }
@@ -1,34 +1,8 @@
1
1
  import { LastFmClient } from "../lastfm-client.js";
2
+ import { getEffectiveConfig } from "../config.js";
2
3
  import { runSync, getCachedStats, refreshStatsCache } from "../sync.js";
3
4
  import * as utils from "../utils.js";
4
5
 
5
- /**
6
- * Get effective config: DB-stored settings override env var defaults
7
- * @param {object} db - MongoDB database instance
8
- * @param {object} lastfmConfig - Plugin config from env vars
9
- * @returns {Promise<object>} Effective apiKey and username
10
- */
11
- async function getEffectiveConfig(db, lastfmConfig) {
12
- let apiKey = lastfmConfig?.apiKey || "";
13
- let username = lastfmConfig?.username || "";
14
-
15
- if (db) {
16
- try {
17
- const settings = await db
18
- .collection("lastfmMeta")
19
- .findOne({ key: "settings" });
20
- if (settings) {
21
- if (settings.apiKey) apiKey = settings.apiKey;
22
- if (settings.username) username = settings.username;
23
- }
24
- } catch {
25
- // Fall through to defaults
26
- }
27
- }
28
-
29
- return { apiKey, username };
30
- }
31
-
32
6
  /**
33
7
  * Dashboard controller
34
8
  */
@@ -115,10 +89,8 @@ export const dashboardController = {
115
89
  }
116
90
  const summary = cachedStatsData?.summary?.all || null;
117
91
 
118
- // Determine public frontend URL
119
- const publicUrl = lastfmEndpoint
120
- ? lastfmEndpoint.replace(/api$/, "")
121
- : "/lastfm";
92
+ // Public page is the combined /listening page
93
+ const publicUrl = "/listening";
122
94
 
123
95
  response.render("lastfm", {
124
96
  title: response.__("lastfm.title"),
@@ -1,4 +1,5 @@
1
1
  import { LastFmClient } from "../lastfm-client.js";
2
+ import { getEffectiveConfig } from "../config.js";
2
3
  import * as utils from "../utils.js";
3
4
 
4
5
  /**
@@ -11,13 +12,16 @@ export const lovedController = {
11
12
  */
12
13
  async api(request, response, next) {
13
14
  try {
14
- const { lastfmConfig } = request.app.locals.application;
15
+ const { application } = request.app.locals;
16
+ const { lastfmConfig } = application;
15
17
 
16
18
  if (!lastfmConfig) {
17
19
  return response.status(500).json({ error: "Not configured" });
18
20
  }
19
21
 
20
- const { apiKey, username, cacheTtl, limits } = lastfmConfig;
22
+ const db = application.getLastfmDb?.();
23
+ const { apiKey, username } = await getEffectiveConfig(db, lastfmConfig);
24
+ const limits = lastfmConfig.limits || {};
21
25
  const page = parseInt(request.query.page) || 1;
22
26
  const limit = Math.min(
23
27
  parseInt(request.query.limit) || limits.loved || 20,
@@ -27,7 +31,7 @@ export const lovedController = {
27
31
  const client = new LastFmClient({
28
32
  apiKey,
29
33
  username,
30
- cacheTtl,
34
+ cacheTtl: lastfmConfig.cacheTtl,
31
35
  });
32
36
 
33
37
  const lovedRes = await client.getLovedTracks(page, limit);
@@ -1,4 +1,5 @@
1
1
  import { LastFmClient } from "../lastfm-client.js";
2
+ import { getEffectiveConfig } from "../config.js";
2
3
  import * as utils from "../utils.js";
3
4
 
4
5
  /**
@@ -11,18 +12,20 @@ export const nowPlayingController = {
11
12
  */
12
13
  async api(request, response, next) {
13
14
  try {
14
- const { lastfmConfig } = request.app.locals.application;
15
+ const { application } = request.app.locals;
16
+ const { lastfmConfig } = application;
15
17
 
16
18
  if (!lastfmConfig) {
17
19
  return response.status(500).json({ error: "Not configured" });
18
20
  }
19
21
 
20
- const { apiKey, username, cacheTtl } = lastfmConfig;
22
+ const db = application.getLastfmDb?.();
23
+ const { apiKey, username } = await getEffectiveConfig(db, lastfmConfig);
21
24
 
22
25
  const client = new LastFmClient({
23
26
  apiKey,
24
27
  username,
25
- cacheTtl: Math.min(cacheTtl, 60_000), // Max 1 minute cache for now playing
28
+ cacheTtl: Math.min(lastfmConfig.cacheTtl, 60_000),
26
29
  });
27
30
 
28
31
  const track = await client.getLatestScrobble();
@@ -1,4 +1,5 @@
1
1
  import { LastFmClient } from "../lastfm-client.js";
2
+ import { getEffectiveConfig } from "../config.js";
2
3
  import * as utils from "../utils.js";
3
4
 
4
5
  /**
@@ -11,13 +12,16 @@ export const scrobblesController = {
11
12
  */
12
13
  async api(request, response, next) {
13
14
  try {
14
- const { lastfmConfig } = request.app.locals.application;
15
+ const { application } = request.app.locals;
16
+ const { lastfmConfig } = application;
15
17
 
16
18
  if (!lastfmConfig) {
17
19
  return response.status(500).json({ error: "Not configured" });
18
20
  }
19
21
 
20
- const { apiKey, username, cacheTtl, limits } = lastfmConfig;
22
+ const db = application.getLastfmDb?.();
23
+ const { apiKey, username } = await getEffectiveConfig(db, lastfmConfig);
24
+ const limits = lastfmConfig.limits || {};
21
25
  const page = parseInt(request.query.page) || 1;
22
26
  const limit = Math.min(
23
27
  parseInt(request.query.limit) || limits.scrobbles || 20,
@@ -27,7 +31,7 @@ export const scrobblesController = {
27
31
  const client = new LastFmClient({
28
32
  apiKey,
29
33
  username,
30
- cacheTtl,
34
+ cacheTtl: lastfmConfig.cacheTtl,
31
35
  });
32
36
 
33
37
  const scrobblesRes = await client.getRecentTracks(page, limit);
@@ -1,4 +1,5 @@
1
1
  import { LastFmClient } from "../lastfm-client.js";
2
+ import { getEffectiveConfig } from "../config.js";
2
3
  import { getAllStats, getScrobbleTrends } from "../stats.js";
3
4
  import { getCachedStats } from "../sync.js";
4
5
 
@@ -12,21 +13,22 @@ export const statsController = {
12
13
  */
13
14
  async api(request, response, next) {
14
15
  try {
15
- const { lastfmConfig } = request.app.locals.application;
16
+ const { application } = request.app.locals;
17
+ const { lastfmConfig } = application;
16
18
 
17
19
  if (!lastfmConfig) {
18
20
  return response.status(500).json({ error: "Not configured" });
19
21
  }
20
22
 
21
23
  // Try database first, fall back to cache for public routes
22
- const db = request.app.locals.database;
24
+ const db = application.getLastfmDb?.();
23
25
  let stats;
24
26
 
25
27
  if (db) {
26
- // Create client for API-based top artists/albums
28
+ const { apiKey, username } = await getEffectiveConfig(db, lastfmConfig);
27
29
  const client = new LastFmClient({
28
- apiKey: lastfmConfig.apiKey,
29
- username: lastfmConfig.username,
30
+ apiKey,
31
+ username,
30
32
  cacheTtl: lastfmConfig.cacheTtl,
31
33
  });
32
34
 
@@ -64,13 +66,14 @@ export const statsController = {
64
66
  */
65
67
  async apiTrends(request, response, next) {
66
68
  try {
67
- const { lastfmConfig } = request.app.locals.application;
69
+ const { application } = request.app.locals;
70
+ const { lastfmConfig } = application;
68
71
 
69
72
  if (!lastfmConfig) {
70
73
  return response.status(500).json({ error: "Not configured" });
71
74
  }
72
75
 
73
- const db = request.app.locals.database;
76
+ const db = application.getLastfmDb?.();
74
77
  const days = Math.min(parseInt(request.query.days) || 30, 90);
75
78
 
76
79
  if (db) {
package/locales/en.json CHANGED
@@ -40,8 +40,8 @@
40
40
  "noConfig": "Last.fm API key and username are required. Configure them below."
41
41
  },
42
42
  "widget": {
43
- "description": "View full scrobble activity on the public page",
44
- "view": "View Public Page"
43
+ "description": "View combined listening activity on the public page",
44
+ "view": "View Listening Page"
45
45
  }
46
46
  }
47
47
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rmdes/indiekit-endpoint-lastfm",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Last.fm scrobble and listening activity endpoint for Indiekit. Display listening history, loved tracks, and statistics.",
5
5
  "keywords": [
6
6
  "indiekit",