mirror of
https://github.com/maunium/stickerpicker.git
synced 2025-08-28 19:01:44 +02:00
Converted gif-search code to use matrix-widget-api
This commit is contained in:
167
web/src/index.js
167
web/src/index.js
@@ -18,6 +18,7 @@ import { Spinner } from "./spinner.js"
|
||||
import { SearchBox } from "./search-box.js"
|
||||
import * as widgetAPI from "./widget-api.js"
|
||||
import * as frequent from "./frequently-used.js"
|
||||
// import GiphyAPI from "./GiphySearch.js"
|
||||
|
||||
// The base URL for fetching packs. The app will first fetch ${PACK_BASE_URL}/index.json,
|
||||
// then ${PACK_BASE_URL}/${packFile} for each packFile in the packs object of the index.json file.
|
||||
@@ -47,11 +48,107 @@ const defaultState = {
|
||||
},
|
||||
}
|
||||
|
||||
class GiphySearchTab extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
searchTerm: "",
|
||||
gifs: [],
|
||||
loading: false,
|
||||
GIFById: {},
|
||||
};
|
||||
this.handleSearchChange = this.handleSearchChange.bind(this);
|
||||
this.searchGifs = this.searchGifs.bind(this);
|
||||
this.handleGifClick = this.handleGifClick.bind(this);
|
||||
}
|
||||
|
||||
async searchGifs() {
|
||||
this.setState({ loading: true });
|
||||
try {
|
||||
const apiKey = "Gc7131jiJuvI7IdN0HZ1D7nh0ow5BU6g";
|
||||
const url = `https://api.giphy.com/v1/gifs/search?q=${this.state.searchTerm}&api_key=${apiKey}`;
|
||||
this.setState({ loading: true });
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
this.setState({ gifs: data.data, loading: false });
|
||||
data.data.forEach((jsonElement) => {
|
||||
const id = jsonElement.id;
|
||||
const updatedItem = {
|
||||
"body": jsonElement.title,
|
||||
"info": {
|
||||
"h": jsonElement.images.original.height,
|
||||
"w": jsonElement.images.original.width,
|
||||
"size": jsonElement.images.original.size,
|
||||
"mimetype": "image/gif",
|
||||
"thumbnail_info": {
|
||||
"h": jsonElement.images.fixed_width_still.height,
|
||||
"mimetype": "image/jpg",
|
||||
"size": jsonElement.images.fixed_width_still.size,
|
||||
"w": jsonElement.images.fixed_width_still.width
|
||||
},
|
||||
"thumbnail_url": jsonElement.images.fixed_width_still.url
|
||||
},
|
||||
"msgtype": "m.image",
|
||||
"url": jsonElement.images.original.url
|
||||
};
|
||||
this.setState((prevState) => ({
|
||||
GIFById: {...prevState.GIFById, [id]: updatedItem}}));
|
||||
});
|
||||
} catch (error) {
|
||||
this.setState({ error: "Error fetching GIFs", loading: false });
|
||||
this.setState({ loading: false });
|
||||
}
|
||||
}
|
||||
|
||||
handleSearchChange(event) {
|
||||
this.setState({ searchTerm: event.target.value });
|
||||
}
|
||||
|
||||
handleGifClick(gif) {
|
||||
console.log(this.state.GIFById[gif.id]);
|
||||
widgetAPI.sendGIF(this.state.GIFById[gif.id]);
|
||||
}
|
||||
async searchGiphy(searchTerm) {
|
||||
if (!searchTerm) return;
|
||||
|
||||
};
|
||||
|
||||
render() {
|
||||
const { searchTerm, gifs, loading } = this.state;
|
||||
|
||||
return html`
|
||||
<div class="search-box">
|
||||
<input
|
||||
type="text"
|
||||
value=${searchTerm}
|
||||
onInput=${this.handleSearchChange}
|
||||
placeholder="Search GIFs..."
|
||||
/>
|
||||
<button onClick=${this.searchGifs} disabled=${loading}>Search</button>
|
||||
</div>
|
||||
<!-- <div class="gifs-list" style="display: grid"> -->
|
||||
<div class="pack-list">
|
||||
<section class="stickerpack">
|
||||
<div class="sticker-list">
|
||||
${gifs.map((gif) => html`
|
||||
<div class="sticker" onClick=${() => this.handleGifClick(gif)} data-gif-id=${gif.id}>
|
||||
<img src=${gif.images.fixed_height.url} alt=${gif.title} class="visible" data=/>
|
||||
</div>
|
||||
`)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class App extends Component {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.defaultTheme = params.get("theme")
|
||||
this.state = {
|
||||
activeTab: "stickers",
|
||||
packs: defaultState.packs,
|
||||
loading: true,
|
||||
error: null,
|
||||
@@ -266,35 +363,51 @@ class App extends Component {
|
||||
}
|
||||
|
||||
render() {
|
||||
const theme = `theme-${this.state.theme}`
|
||||
const filterActive = !!this.state.filtering.searchTerm
|
||||
const packs = filterActive ? this.state.filtering.packs : [this.state.frequentlyUsed, ...this.state.packs]
|
||||
const theme = `theme-${this.state.theme}`;
|
||||
const filterActive = !!this.state.filtering.searchTerm;
|
||||
const packs = filterActive
|
||||
? this.state.filtering.packs
|
||||
: [this.state.frequentlyUsed, ...this.state.packs];
|
||||
|
||||
if (this.state.loading) {
|
||||
return html`<main class="spinner ${theme}"><${Spinner} size=${80} green /></main>`
|
||||
} else if (this.state.error) {
|
||||
return html`<main class="error ${theme}">
|
||||
<h1>Failed to load packs</h1>
|
||||
<p>${this.state.error}</p>
|
||||
</main>`
|
||||
} else if (this.state.packs.length === 0) {
|
||||
return html`<main class="empty ${theme}"><h1>No packs found 😿</h1></main>`
|
||||
}
|
||||
if (this.state.loading) {
|
||||
return html`<main class="spinner ${theme}"><${Spinner} size=${80} green /></main>`;
|
||||
} else if (this.state.error) {
|
||||
return html`<main class="error ${theme}">
|
||||
<h1>Failed to load packs</h1>
|
||||
<p>${this.state.error}</p>
|
||||
</main>`;
|
||||
} else if (this.state.packs.length === 0) {
|
||||
return html`<main class="empty ${theme}"><h1>No packs found 😿</h1></main>`;
|
||||
}
|
||||
|
||||
return html`<main class="has-content ${theme}">
|
||||
<nav onWheel=${this.navScroll} ref=${elem => this.navRef = elem}>
|
||||
<${NavBarItem} pack=${this.state.frequentlyUsed} iconOverride="recent" />
|
||||
${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)}
|
||||
<${NavBarItem} pack=${{ id: "settings", title: "Settings" }} iconOverride="settings" />
|
||||
</nav>
|
||||
<${SearchBox} onKeyUp=${this.searchStickers} />
|
||||
<div class="pack-list ${isMobileSafari ? "ios-safari-hack" : ""}" ref=${elem => this.packListRef = elem}>
|
||||
${filterActive && packs.length === 0 ? html`<div class="search-empty"><h1>No stickers match your search</h1></div>` : null}
|
||||
${packs.map(pack => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)}
|
||||
<${Settings} app=${this}/>
|
||||
</div>
|
||||
</main>`
|
||||
}
|
||||
return html`<main class="has-content ${theme}">
|
||||
<div class="tab-container">
|
||||
<a href="#stickers" class="tab" onClick=${() => this.setState({ activeTab: "stickers" })}>Stickers</a>
|
||||
<a href="#gifs" class="tab" onClick=${() => this.setState({ activeTab: "gifs" })}>GIFs</a>
|
||||
</div>
|
||||
|
||||
${this.state.activeTab === "stickers" && html`
|
||||
<nav onWheel=${this.navScroll} ref=${elem => this.navRef = elem}>
|
||||
<${NavBarItem} pack=${this.state.frequentlyUsed} iconOverride="recent" />
|
||||
${this.state.packs.map(pack => html`<${NavBarItem} id=${pack.id} pack=${pack}/>`)}
|
||||
<${NavBarItem} pack=${{ id: "settings", title: "Settings" }} iconOverride="settings" />
|
||||
</nav>
|
||||
|
||||
<${SearchBox} onKeyUp=${this.searchStickers} />
|
||||
<div class="pack-list ${isMobileSafari ? "ios-safari-hack" : ""}" ref=${(elem) => (this.packListRef = elem)}>
|
||||
${filterActive && packs.length === 0
|
||||
? html`<div class="search-empty"><h1>No stickers match your search</h1></div>`
|
||||
: null}
|
||||
${packs.map((pack) => html`<${Pack} id=${pack.id} pack=${pack} send=${this.sendSticker} />`)}
|
||||
<${Settings} app=${this} />
|
||||
</div>
|
||||
|
||||
`}
|
||||
${this.state.activeTab === "gifs" && html`
|
||||
<${GiphySearchTab} send=${this.sendGIF} />
|
||||
`}
|
||||
</main>`;
|
||||
}
|
||||
}
|
||||
|
||||
const Settings = ({ app }) => html`
|
||||
|
@@ -13,64 +13,131 @@
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
let widgetId = null
|
||||
|
||||
window.onmessage = event => {
|
||||
if (!window.parent || !event.data) {
|
||||
return
|
||||
}
|
||||
const widgetId = null; // if you know the widget ID, supply it.
|
||||
const api = new mxwidgets.WidgetApi(widgetId);
|
||||
|
||||
const request = event.data
|
||||
if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") {
|
||||
return
|
||||
}
|
||||
// Before doing anything else, request capabilities:
|
||||
api.requestCapabilities(mxwidgets.StickerpickerCapabilities);
|
||||
api.requestCapability(mxwidgets.MatrixCapabilities.MSC4039UploadFile);
|
||||
|
||||
if (widgetId) {
|
||||
if (widgetId !== request.widgetId) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
widgetId = request.widgetId
|
||||
}
|
||||
|
||||
let response
|
||||
|
||||
if (request.action === "visibility") {
|
||||
response = {}
|
||||
} else if (request.action === "capabilities") {
|
||||
response = { capabilities: ["m.sticker"] }
|
||||
} else {
|
||||
response = { error: { message: "Action not supported" } }
|
||||
}
|
||||
|
||||
window.parent.postMessage({ ...request, response }, event.origin)
|
||||
export function sendSticker(content){
|
||||
api.sendSticker(content);
|
||||
}
|
||||
|
||||
export function sendSticker(content) {
|
||||
const data = {
|
||||
content: { ...content },
|
||||
// `name` is for Element Web (and also the spec)
|
||||
// Element Android uses content -> body as the name
|
||||
name: content.body,
|
||||
}
|
||||
// Custom field that stores the ID even for non-telegram stickers
|
||||
delete data.content.id
|
||||
|
||||
// This is for Element iOS
|
||||
const widgetData = {
|
||||
...data,
|
||||
description: content.body,
|
||||
file: `${content.id}.png`,
|
||||
}
|
||||
// Element iOS explodes if there are extra fields present
|
||||
delete widgetData.content["net.maunium.telegram.sticker"]
|
||||
|
||||
window.parent.postMessage({
|
||||
api: "fromWidget",
|
||||
action: "m.sticker",
|
||||
requestId: `sticker-${Date.now()}`,
|
||||
widgetId,
|
||||
data,
|
||||
widgetData,
|
||||
}, "*")
|
||||
export function sendGIF(url){
|
||||
// just print out content, should be URL
|
||||
console.log("Content:"+url.url);
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', url.url, true);
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
if (xhr.status === 200) {
|
||||
const responseData = xhr.responseText;
|
||||
// Call function A with the response data
|
||||
api.uploadFile(responseData)
|
||||
.then(result => {
|
||||
console.log("Here's the result:"+result);
|
||||
resolve(result); // Resolve the outer promise with the result from A
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error); // Reject the outer promise if function A fails
|
||||
});
|
||||
} else {
|
||||
reject(new Error('Failed to fetch data')); // Reject the outer promise if fetching data fails
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
*let widgetId = null
|
||||
*
|
||||
*window.onmessage = event => {
|
||||
* if (!window.parent || !event.data) {
|
||||
* return
|
||||
* }
|
||||
*
|
||||
* const request = event.data
|
||||
* if (!request.requestId || !request.widgetId || !request.action || request.api !== "toWidget") {
|
||||
* return
|
||||
* }
|
||||
*
|
||||
* if (widgetId) {
|
||||
* if (widgetId !== request.widgetId) {
|
||||
* return
|
||||
* }
|
||||
* } else {
|
||||
* widgetId = request.widgetId
|
||||
* }
|
||||
*
|
||||
* let response
|
||||
*
|
||||
* if (request.action === "visibility") {
|
||||
* response = {}
|
||||
* } else if (request.action === "capabilities") {
|
||||
* response = { capabilities: ["m.sticker", "org.matrix.msc4039.upload_file"] }
|
||||
* } else {
|
||||
* response = { error: { message: "Action not supported" } }
|
||||
* }
|
||||
*
|
||||
* window.parent.postMessage({ ...request, response }, event.origin)
|
||||
*}
|
||||
*
|
||||
*export function sendSticker(content) {
|
||||
* const data = {
|
||||
* content: { ...content },
|
||||
* // `name` is for Element Web (and also the spec)
|
||||
* // Element Android uses content -> body as the name
|
||||
* name: content.body,
|
||||
* }
|
||||
* // Custom field that stores the ID even for non-telegram stickers
|
||||
* delete data.content.id
|
||||
*
|
||||
* // This is for Element iOS
|
||||
* const widgetData = {
|
||||
* ...data,
|
||||
* description: content.body,
|
||||
* file: `${content.id}.png`,
|
||||
* }
|
||||
* // Element iOS explodes if there are extra fields present
|
||||
* delete widgetData.content["net.maunium.telegram.sticker"]
|
||||
*
|
||||
* window.parent.postMessage({
|
||||
* api: "fromWidget",
|
||||
* action: "m.sticker",
|
||||
* requestId: `sticker-${Date.now()}`,
|
||||
* widgetId,
|
||||
* data,
|
||||
* widgetData,
|
||||
* }, "*")
|
||||
*}
|
||||
*
|
||||
*export function sendGIF(content) {
|
||||
* const data = {
|
||||
* content: { ...content },
|
||||
* name: content.body,
|
||||
* msgtype: "m.image"
|
||||
* }
|
||||
*
|
||||
* delete data.content.id
|
||||
* // This is for Element iOS
|
||||
* const widgetData = {
|
||||
* ...data,
|
||||
* description: content.body,
|
||||
* file: `${content.id}.png`,
|
||||
* }
|
||||
*
|
||||
* window.parent.postMessage({
|
||||
* api: "fromWidget",
|
||||
* action: "m.room.message",
|
||||
* requestId: `gif-${Date.now()}`,
|
||||
* widgetId,
|
||||
* data,
|
||||
* widgetData,
|
||||
* }, "*")
|
||||
*}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user