Skip to content

Commit

Permalink
feat: archive chats (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliyss committed Feb 26, 2020
1 parent 5c9e76e commit 132424e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 3 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ client.on('message', async msg => {
const attachmentData = await quotedMsg.downloadMedia();
client.sendMessage(msg.from, attachmentData, { caption: 'Here\'s your requested media.' });
}

} else if (msg.body == '!location') {
msg.reply(new Location(37.422, -122.084, 'Googleplex\nGoogle Headquarters'));
} else if (msg.location) {
Expand All @@ -168,6 +167,9 @@ client.on('message', async msg => {
} else {
msg.reply('I can only delete my own messages');
}
} else if (msg.body === '!archive') {
const chat = await msg.getChat();
chat.archive();
}
});

Expand Down
25 changes: 24 additions & 1 deletion src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ class Client extends EventEmitter {
return new Message(this, newMessage);
}


/**
* Get all current chat instances
* @returns {Promise<Array<Chat>>}
Expand Down Expand Up @@ -392,6 +391,30 @@ class Client extends EventEmitter {
});
}

/**
* Enables and returns the archive state of the Chat
* @returns {boolean}
*/
async archiveChat(chatId) {
return await this.pupPage.evaluate(async chatId => {
let chat = await window.Store.Chat.get(chatId);
await window.Store.Cmd.archiveChat(chat, true);
return chat.archive;
}, chatId);
}

/**
* Changes and returns the archive state of the Chat
* @returns {boolean}
*/
async unarchiveChat(chatId) {
return await this.pupPage.evaluate(async chatId => {
let chat = await window.Store.Chat.get(chatId);
await window.Store.Cmd.archiveChat(chat, false);
return chat.archive;
}, chatId);
}

}

module.exports = Client;
21 changes: 21 additions & 0 deletions src/structures/Chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Chat extends Base {
*/
this.timestamp = data.t;

/**
* Indicates if the Chat is archived
* @type {boolean}
*/
this.archived = data.archive;

return super._patch(data);
}

Expand All @@ -62,6 +68,21 @@ class Chat extends Base {
async sendMessage(content, options) {
return this.client.sendMessage(this.id._serialized, content, options);
}

/**
* archives this chat
*/
async archive() {
return this.client.archiveChat(this.id._serialized);
}

/**
* un-archives this chat
*/
async unarchive() {
return this.client.unarchiveChat(this.id._serialized);
}

}

module.exports = Chat;

0 comments on commit 132424e

Please sign in to comment.