Skip to content

Commit 01ee6ab

Browse files
committed
New lens group ID search for creators
1 parent 673dc69 commit 01ee6ab

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/endpoints/explorer/search.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Config } from '../../utils/config.js';
33
import * as Cache from '../../utils/cache.js';
44
import * as Util from '../../utils/helper.js';
55
import * as Web from '../../utils/web.js';
6+
import * as Creator from '../../utils/creator.js';
67

78
const useRelay = Config.app.relay.server;
89
const useWebSource = Config.app.flag.enable_web_source;
@@ -20,6 +21,16 @@ router.post('/', async function (req, res, next) {
2021
return res.json({ "lenses": [] });
2122
}
2223

24+
if (Util.isGroupId(searchTerm)) {
25+
const groupLenses = await Creator.getLensGroup(searchTerm);
26+
if (Array.isArray(groupLenses) && groupLenses.length) {
27+
for (const lens of groupLenses) {
28+
Cache.Search.set(lens.lens_id, lens);
29+
}
30+
return res.json({ "lenses": groupLenses });
31+
}
32+
}
33+
2334
let searchResults = await Util.advancedSearch(searchTerm);
2435
if (searchResults && searchResults.length) {
2536
searchResults = Util.modifyResponseURLs(searchResults);

src/utils/creator.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { BridgeError, CameraKitClient, DataMessage, ErrorMessage } from "@ptrumpis/snap-camerakit-bridge";
2+
import * as dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const bridgeAddr = process.env.BRIDGE_ADDR;
7+
const apiToken = process.env.BRIDGE_API_TOKEN;
8+
let isInitialized = false;
9+
10+
const client = (bridgeAddr) ? new CameraKitClient(bridgeAddr) : null;
11+
12+
async function getLensGroup(groupId) {
13+
try {
14+
if (!client) {
15+
throw new Error('You need to edit your .env file and set BRIDGE_ADDR');
16+
}
17+
18+
if (!apiToken) {
19+
throw new Error('You need to edit your .env file and set BRIDGE_API_TOKEN');
20+
}
21+
22+
let message = null;
23+
if (!isInitialized) {
24+
message = await client.init(apiToken);
25+
if (message instanceof ErrorMessage) {
26+
throw BridgeError.fromJSON(message.error)
27+
} else if (message instanceof DataMessage) {
28+
isInitialized = (message.data) ? true : false;
29+
}
30+
}
31+
32+
message = await client.getLensGroup(groupId);
33+
if (message instanceof ErrorMessage) {
34+
throw BridgeError.fromJSON(message.error)
35+
} else if (message instanceof DataMessage) {
36+
return message.data;
37+
}
38+
} catch (e) {
39+
console.error(`[Error] Failed to get lens group: ${e.message}`);
40+
}
41+
42+
return [];
43+
}
44+
45+
export { getLensGroup };

src/utils/helper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ function isLensId(str) {
182182
return id.test(str);
183183
}
184184

185+
function isGroupId(str) {
186+
var regex = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i;
187+
return regex.test(str);
188+
}
189+
185190
function isUrl(url) {
186191
try {
187192
new URL(url);
@@ -205,4 +210,4 @@ function sleep(ms) {
205210
});
206211
}
207212

208-
export { advancedSearch, relayRequest, getUnlockFromRelay, mirrorSearchResults, downloadLens, downloadUnlock, mergeLensesUnique, parseLensUuid, parseLensUuidFromShareUrl, isLensUuid, isLensId, isUrl, modifyResponseURLs, sleep };
213+
export { advancedSearch, relayRequest, getUnlockFromRelay, mirrorSearchResults, downloadLens, downloadUnlock, mergeLensesUnique, parseLensUuid, parseLensUuidFromShareUrl, isLensUuid, isLensId, isGroupId, isUrl, modifyResponseURLs, sleep };

0 commit comments

Comments
 (0)