diff --git a/javascripts/collection.js b/javascripts/collection.js index 946ed7a..8397c3e 100644 --- a/javascripts/collection.js +++ b/javascripts/collection.js @@ -167,10 +167,11 @@ Vue.createApp({ this.toggleModal(); }, deleteItem() { - if ( vueType === 'private' ) { + // eslint-disable-next-line no-undef + if (vueType !== "private") { return false; } - axios + return axios .delete(`/api/v1/albums/${this.itemId}`) .then(() => { this.fetch(); @@ -186,10 +187,11 @@ Vue.createApp({ }); }, shareCollection() { - if ( vueType === 'private' ) { + // eslint-disable-next-line no-undef + if (vueType !== "private") { return false; } - axios + return axios .patch(`/api/v1/me`, { isPublicCollection: !this.isPublicCollection, }) @@ -219,19 +221,16 @@ Vue.createApp({ }); }, renderAlbumTitle(item) { - let render = ''; + let render = ""; - for ( let i = 0 ; i < item.artists.length ; i += 1 ) { - const { - name, - join, - } = item.artists[i]; - render += `${name} ${join ? `${join} ` : ''}`; + for (let i = 0; i < item.artists.length; i += 1) { + const { name, join } = item.artists[i]; + render += `${name} ${join ? `${join} ` : ""}`; } render += `- ${item.title}`; return render; - } + }, }, -}).mount("#collection"); \ No newline at end of file +}).mount("#collection"); diff --git a/javascripts/mon-compte/ma-collection/details.js b/javascripts/mon-compte/ma-collection/details.js index 5486244..af0414f 100644 --- a/javascripts/mon-compte/ma-collection/details.js +++ b/javascripts/mon-compte/ma-collection/details.js @@ -4,6 +4,8 @@ if (typeof item !== "undefined") { return { // eslint-disable-next-line no-undef item, + // eslint-disable-next-line no-undef + canShareItem, tracklist: [], identifiers: [], modalIsVisible: false, @@ -12,6 +14,11 @@ if (typeof item !== "undefined") { preview: null, index: null, showModalDelete: false, + showModalShare: false, + shareMessage: "", + shareMessageTransformed: "", + shareMessageLength: 0, + shareSubmiting: false, }; }, created() { @@ -23,6 +30,26 @@ if (typeof item !== "undefined") { destroyed() { window.removeEventListener("keydown", this.changeImage); }, + watch: { + shareMessage(message) { + const video = + this.item.videos && this.item.videos.length > 0 + ? this.item.videos[0].uri + : ""; + + this.shareMessageTransformed = message + .replaceAll("{artist}", this.item.artists[0].name) + .replaceAll("{format}", this.item.formats[0].name) + .replaceAll("{year}", this.item.year) + .replaceAll("{video}", video) + .replaceAll("{album}", this.item.title); + + this.shareMessageLength = this.shareMessageTransformed.replace( + video, + new Array(36).join("#") + ).length; + }, + }, methods: { setIdentifiers() { this.identifiers = []; @@ -189,6 +216,33 @@ if (typeof item !== "undefined") { goToArtist() { return ""; }, + shareAlbum() { + if (this.shareSubmiting) { + return false; + } + this.shareSubmiting = true; + axios + .post(`/api/v1/albums/${this.item._id}/share`, { + message: this.shareMessageTransformed, + }) + .then(() => { + showToastr("Album partagé", true); + this.shareMessage = ""; + this.showModalShare = false; + }) + .catch((err) => { + showToastr( + err.response?.data?.message || + "Impossible de partager cet album", + false + ); + }) + .finally(() => { + this.shareSubmiting = false; + }); + + return true; + }, }, }).mount("#ma-collection-details"); } diff --git a/sass/forms.scss b/sass/forms.scss index a780145..125527a 100644 --- a/sass/forms.scss +++ b/sass/forms.scss @@ -31,13 +31,12 @@ max-width: 100%; width: 100%; background-color: var(--input-color); - border: 1px solid transparent !important; + border: 1px solid var(--input-active-color) !important; color: var(--input-font-color); @include transition() {} &:focus-visible { outline: unset; - border-color: var(--input-active-color) !important; } } diff --git a/src/middleware/Albums.js b/src/middleware/Albums.js index b234097..dc481ec 100644 --- a/src/middleware/Albums.js +++ b/src/middleware/Albums.js @@ -297,7 +297,7 @@ Publié automatiquement via #musictopus`; */ async deleteOne() { const res = await AlbumsModel.findOneAndDelete({ - user: this.req.user._id, + User: this.req.user._id, _id: this.req.params.itemId, }); @@ -312,6 +312,83 @@ Publié automatiquement via #musictopus`; ); } + async shareOne() { + const { message: status } = this.req.body; + const { itemId: _id } = this.req.params; + const { _id: User } = this.req.user; + const query = { + _id, + User, + }; + + const album = await AlbumsModel.findOne(query); + + if (!album) { + throw new ErrorEvent( + 404, + "Mise à jour", + "Impossible de trouver cet album" + ); + } + + const { mastodon: mastodonConfig } = this.req.user; + const { publish, token, url } = mastodonConfig; + + if (publish && url && token) { + const M = new Mastodon({ + access_token: token, + api_url: url, + }); + + const media_ids = []; + + if (album.images.length > 0) { + for (let i = 0; i < album.images.length; i += 1) { + if (media_ids.length === 4) { + break; + } + + const filename = `${v4()}.jpg`; + const file = `/tmp/${filename}`; + + // eslint-disable-next-line no-await-in-loop + const { data: buff } = await axios.get( + album.images[i].uri, + { + headers: { + "User-Agent": + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/117.0", + }, + responseType: "arraybuffer", + } + ); + + fs.writeFileSync(file, buff); + + // eslint-disable-next-line no-await-in-loop + const { data: media } = await M.post("media", { + file: fs.createReadStream(file), + }); + + const { id } = media; + + media_ids.push(id); + + fs.unlinkSync(file); + } + } + + await M.post("statuses", { status, media_ids }); + } else { + throw new ErrorEvent( + 406, + `Vous n'avez pas configuré vos options de partage sur votre compte` + ); + } + + return true; + } + /** * Méthode permettant de créer la page "ma-collection" */ diff --git a/src/routes/api/v1/albums.js b/src/routes/api/v1/albums.js index 596b502..7586ad1 100644 --- a/src/routes/api/v1/albums.js +++ b/src/routes/api/v1/albums.js @@ -68,4 +68,17 @@ router } }); +router + .route("/:itemId/share") + .post(ensureLoggedIn("/connexion"), async (req, res, next) => { + try { + const albums = new Albums(req); + const data = await albums.shareOne(); + + sendResponse(req, res, data); + } catch (err) { + next(err); + } + }); + export default router; diff --git a/views/pages/mon-compte/ma-collection/details.ejs b/views/pages/mon-compte/ma-collection/details.ejs index 126cea4..37b1ef1 100644 --- a/views/pages/mon-compte/ma-collection/details.ejs +++ b/views/pages/mon-compte/ma-collection/details.ejs @@ -8,6 +8,7 @@ - {{item.title}} +
@@ -182,8 +183,46 @@
+ + \ No newline at end of file