Skip to content

Commit 0862d14

Browse files
LmTechyTEMohayo
andauthored
Added /tenor support (#80)
* Added /tenor support * Added to config sorry * Move tenor into integrations.js for future compatibility Remove config duplicate requiring -> global.config --------- Co-authored-by: ohayo <9443476+ohayo@users.noreply.github.com>
1 parent 333594e commit 0862d14

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const store = require('./store');
1717
const oauth2 = require('./oauth2/index');
1818
const entitlements = require('./entitlements');
1919
const activities = require('./activities');
20+
const integrations = require('./integrations');
2021

2122
global.config = globalUtils.config;
2223
//just in case
@@ -98,6 +99,7 @@ app.use("/invite", instanceMiddleware("VERIFIED_EMAIL_REQUIRED"), invites);
9899
app.use("/webhooks", instanceMiddleware("VERIFIED_EMAIL_REQUIRED"), webhooks);
99100
app.use("/oauth2", instanceMiddleware("VERIFIED_EMAIL_REQUIRED"), oauth2);
100101
app.use("/store", instanceMiddleware("VERIFIED_EMAIL_REQUIRED"), store);
102+
app.use("/integrations", instanceMiddleware("VERIFIED_EMAIL_REQUIRED"), integrations);
101103

102104
app.use("/track", (_, res) => {
103105
return res.status(204).send();

api/integrations.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const express = require('express');
2+
const globalUtils = require('../helpers/globalutils');
3+
const { logText } = require('../helpers/logger');
4+
const quickcache = require('../helpers/quickcache');
5+
const router = express.Router({ mergeParams: true });
6+
const fetch = require('node-fetch');
7+
8+
router.get("/tenor/search", quickcache.cacheFor(60 * 30, true), async (req, res) => {
9+
try {
10+
const query = req.query.q;
11+
12+
if (!query || !global.config.tenor_api_key) {
13+
return res.json([]);
14+
}
15+
16+
const baseUrl = "https://tenor.googleapis.com/v2/search";
17+
const params = new URLSearchParams({
18+
q: query,
19+
key: global.config.tenor_api_key,
20+
limit: 50,
21+
media_filter: "tinygif"
22+
}).toString();
23+
24+
const url = `${baseUrl}?${params}`;
25+
26+
const response = await fetch(url, {
27+
method: "GET"
28+
});
29+
30+
const data = await response.json();
31+
const results = data.results || [];
32+
33+
const gifs = results
34+
.map(gif => {
35+
const media = gif.media_formats?.tinygif;
36+
return {
37+
type: "gif",
38+
src: media?.url || null,
39+
url: gif.itemurl,
40+
width: 100,
41+
height: 100
42+
};
43+
})
44+
.filter(g => g.src !== null);
45+
46+
return res.json(gifs);
47+
} catch (err) {
48+
logText(err, "error");
49+
50+
return res.status(500).json({
51+
code: 500,
52+
message: "Internal Server Error"
53+
});
54+
}
55+
});
56+
57+
module.exports = router;

config.example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"serve_selector": true,
1414
"require_release_date_cookie": true,
1515
"cache_authenticated_get_requests" : true,
16+
"tenor_api_key": "",
1617
"mr_server": {
1718
"enabled" : false,
1819
"port" : 4444,

0 commit comments

Comments
 (0)