Skip to content

Commit

Permalink
Added MediaShare to Message types
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0arash committed Mar 11, 2021
1 parent 547c075 commit 0505933
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arashgh/insta.js",
"version": "1.6.3",
"version": "1.7.0",
"description": "A fork of @androz2091/insta.js with some new features. Object-oriented library for sending and receiving messages via Instagram",
"main": "src/index.js",
"types": "./typings/index.d.ts",
Expand Down
33 changes: 22 additions & 11 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const MessageCollector = require('./MessageCollector')
const Util = require('../utils/Util')

/**
* Represents a Message
Expand All @@ -9,7 +10,7 @@ class Message {
* @param {string} threadID The ID of the thread
* @param {object} data The data for the message
*/
constructor (client, threadID, data) {
constructor(client, threadID, data) {
/**
* @type {Client}
* The client that instantiated this
Expand Down Expand Up @@ -99,6 +100,7 @@ class Message {
* The data concerning the media
*/
this.mediaData = undefined
this.mediaShareData = undefined;
if (data.item_type === 'animated_media') {
this.mediaData = {
isLike: false,
Expand All @@ -120,6 +122,15 @@ class Message {
isSticker: false,
url: data.media.image_versions2.candidates[0].url
}
} else if (data.item_type === 'media_share') {
this.mediaShareData = {
messageSender: message.author.username,
creatorIgHandle: Util.extractCreator(message.data),
images: Util.extractImages(message.data),
mediaShareUrl: Util.extractMediaShareUrl(message.data),
timestamp: Util.extractPostTimestamp(message.data),
location: Util.extractLocation(message.data),
}
}
/**
* @typedef {object} MessageVoiceData
Expand Down Expand Up @@ -148,19 +159,19 @@ class Message {
* @type {Chat}
* The chat the message was sent in
*/
get chat () {
get chat() {
return this.client.cache.chats.get(this.chatID)
}

/**
* @type {User}
* The author of the message
*/
get author () {
get author() {
return this.client.cache.users.get(this.authorID)
}

_patch (data) {
_patch(data) {
/**
* @typedef {object} MessageLike
*
Expand All @@ -184,7 +195,7 @@ class Message {
* @param {MessageCollectorOptions} options The options for the collector
* @returns {MessageCollector}
*/
createMessageCollector (options) {
createMessageCollector(options) {
const collector = new MessageCollector(this.chat, options)
return collector
}
Expand All @@ -193,15 +204,15 @@ class Message {
* Mark the message as seen.
* @returns {Promise<void>}
*/
markSeen () {
markSeen() {
return this.chat.markMessageSeen(this.id)
}

/**
* Delete the message
* @returns {Promise<void>}
*/
delete () {
delete() {
return this.chat.deleteMessage(this.id)
}

Expand All @@ -210,15 +221,15 @@ class Message {
* @param {string} content The content of the message
* @returns {Promise<Message>}
*/
reply (content) {
reply(content) {
return this.chat.sendMessage(`${this.client.options.disableReplyPrefix ? '' : `${this.author.username}, `}${content}`)
}

toString () {
toString() {
return this.content
}

toJSON () {
toJSON() {
return {
client: this.client.toJSON(),
chatID: this.chatID,
Expand All @@ -234,4 +245,4 @@ class Message {
}
}

module.exports = Message
module.exports = Message
120 changes: 120 additions & 0 deletions src/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,126 @@ class Util {
encoding: "utf8"
});
}

static extractCreator(messageData) {
try {
return messageData.media_share.user.username
} catch (err) {
console.log(err)
return undefined
}
}

static extractImages(messageData) {
let images = []
const postImage = Util.extracteImageFromSinglePost(messageData)
if (postImage) {
images.push(postImage)
}
const carouselImages = Util.extractImagesFromCarousel(messageData)
if (carouselImages) {
images = images.concat(carouselImages)
}
return images
}

static extractMediaShareUrl(messageData) {
try {
return `https://www.instagram.com/p/${messageData.media_share.code}`
} catch (err) {
// console.log(err)
return undefined
}
}

static extracteImageFromSinglePost(messageData) {
try {
return messageData.media_share.image_versions2.candidates[0].url
} catch (err) {
// console.log(err)
return undefined
}
}

static extractImagesFromCarousel(messageData) {
try {
return messageData.media_share.carousel_media.map(mediaObj => mediaObj.image_versions2.candidates[0].url)
} catch (err) {
// console.log(err)
return undefined
}
}

static extractPostTimestamp(messageData) {
try {
return messageData.media_share.taken_at
} catch (err) {
// console.log(err)
return undefined
}
}

static extractLocation(messageData) {
const location = {
coordinates: Util.extractLocationCoordinates(messageData),
address: Util.extractLocationAddress(messageData),
city: Util.extractLocationCity(messageData),
name: Util.extractLocationName(messageData),
shortName: Util.extractLocationShortName(messageData),
}
if (!location.coordinates && !location.address && !location.city && !location.name && !location.shortName) {
return undefined
}
return location
}

static extractLocationCoordinates(messageData) {
try {
return {
lat: messageData.media_share.lat || messageData.media_share.location.lat,
lng: messageData.media_share.lng || messageData.media_share.location.lng,
}
} catch (err) {
// console.log(err)
return undefined
}
}

static extractLocationAddress(messageData) {
try {
return messageData.media_share.location.address
} catch (err) {
// console.log(err)
return undefined
}
}

static extractLocationCity(messageData) {
try {
return messageData.media_share.location.city
} catch (err) {
// console.log(err)
return undefined
}
}

static extractLocationName(messageData) {
try {
return messageData.media_share.location.name
} catch (err) {
// console.log(err)
return undefined
}
}

static extractLocationShortName(messageData) {
try {
return messageData.media_share.location.short_name
} catch (err) {
// console.log(err)
return undefined
}
}
}

module.exports = Util

0 comments on commit 0505933

Please sign in to comment.