Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Added Mentions #460

Merged
merged 3 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ function loadNextThreadHistory(api){
if(err) return console.error(err);

/*
Since the timestamp is from a previous loaded message,
Since the timestamp is from a previous loaded message,
that message will be included in this history so we can discard it unless it is the first load.
*/
if(timestamp != undefined) history.pop();
Expand Down Expand Up @@ -875,7 +875,9 @@ Various types of message can be sent:
* *File or image:* Set field `attachment` to a readable stream or an array of readable streams.
* *URL:* set a field `url` to the desired URL.
* *Emoji:* set field `emoji` to the desired emoji as a string and set field `emojiSize` with size of the emoji (`small`, `medium`, `large`)
* *Mentions:* set field `mentions` to an array of objects. Objects should have the `tag` field set to the text that should be highlighted in the mention (NOTE: the first instance of `tag` is highlighted in the text). Additionally, the object should have an `id` field, where the `id` is the user id of the person being mentioned.
* *Mentions:* set field `mentions` to an array of objects. Objects should have the `tag` field set to the text that should be highlighted in the mention. The object should have an `id` field, where the `id` is the user id of the person being mentioned. The instance of `tag` that is highlighted is determined through indexOf, an optional `fromIndex`
can be passed in to specify the start index to start searching for the `tag` text
in `body` (default=0). (See below for an example.)

Note that a message can only be a regular message (which can be empty) and optionally one of the following: a sticker, an attachment or a url.

Expand Down Expand Up @@ -925,10 +927,11 @@ login({email: "EMAIL", password: "PASSWORD"}, (err, api) => {
// Getting the actual sender name from ID involves calling
// `api.getThreadInfo` and `api.getUserInfo`
api.sendMessage({
body: 'Hello @Sender!',
body: 'Hello @Sender! @Sender!',
mentions: [{
tag: '@Sender',
id: message.senderID,
fromIndex: 9, // Highlight the second occurrence of @Sender
}],
}, message.threadID);
}
Expand Down
31 changes: 22 additions & 9 deletions src/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ module.exports = function(defaultFuncs, api, ctx) {
}
cb();
}

function handleEmoji(msg, form, callback, cb) {
if (msg.emojiSize != null && msg.emoji == null) {
return callback({error: "emoji property is empty"});
Expand Down Expand Up @@ -239,14 +239,27 @@ module.exports = function(defaultFuncs, api, ctx) {

function handleMention(msg, form, callback, cb) {
if (msg.mentions) {
for (let i=0; i < msg.mentions.length; i++) {
let mention = msg.mentions[i];
let tag = mention.tag || '';
let id = mention.id || 0;
form['profile_xmd[' + i + '][offset]'] = msg.body.indexOf(tag);
form['profile_xmd[' + i + '][length]'] = tag.length;
form['profile_xmd[' + i + '][id]'] = id;
}
for (let i=0; i < msg.mentions.length; i++) {
const mention = msg.mentions[i];

const tag = mention.tag;
if (typeof tag !== "string")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add curly braces :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup c:

return callback({error: "Mention tags must be strings."});

const offset = msg.body.indexOf(tag, mention.fromIndex || 0);

if (offset < 0)
log.warn("handleMention", "Mention for \"" + tag +
"\" not found in message string.");

if (mention.id == null)
log.warn("handleMention", "Mention id should be non-null.");

const id = mention.id || 0;
form['profile_xmd[' + i + '][offset]'] = offset;
form['profile_xmd[' + i + '][length]'] = tag.length;
form['profile_xmd[' + i + '][id]'] = id;
}
}
cb();
}
Expand Down