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

Added getNotifications to the API and updated documentation #728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [`api.getCurrentUserID`](#getCurrentUserID)
* [`api.getEmojiUrl`](#getEmojiUrl)
* [`api.getFriendsList`](#getFriendsList)
* [`api.getNotifications`](#getNotifications)
* [`api.getThreadHistory`](#getThreadHistory)
* [`api.getThreadInfo`](#getThreadInfo)
* [`api.getThreadList`](#getThreadList)
Expand Down Expand Up @@ -544,6 +545,37 @@ login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, ap

---------------------------------------

<a name="getNotifications"></a>
### api.getNotifications(amount, callback)

Returns an array of objects with up to `amount` most recent notifications.

__Arguments__

* `callback(err, arr)` - A callback called when the query is done (either with an error or with an confirmation object). `arr` is an array of objects with the following fields: `id`, `text`, `html`, `time`, `seen`, `read`. `html` is the same as `text` but with ranges emphasized. `time`, `seen`, and `read` are all millisecond timestamps.

__Example__

```js
const fs = require("fs");
const login = require("facebook-chat-api");

login({appState: JSON.parse(fs.readFileSync('appstate.json', 'utf8'))}, (err, api) => {
if(err) return console.error(err);

api.getNotifications((err, data) => {
if(err) return console.error(err);

data.forEach(notification => {
if(!notification.seen)
console.log(new Date(notification.time), notification.text);
});
});
});
```

---------------------------------------

<a name="getThreadHistory"></a>
### api.getThreadHistory(threadID, amount, timestamp, callback)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Result:
* [`api.getAppState`](DOCS.md#getAppState)
* [`api.getCurrentUserID`](DOCS.md#getCurrentUserID)
* [`api.getFriendsList`](DOCS.md#getFriendsList)
* [`api.getNotifications`](DOCS.md#getNotifications)
* [`api.getThreadHistory`](DOCS.md#getThreadHistory)
* [`api.getThreadInfo`](DOCS.md#getThreadInfo)
* [`api.getThreadList`](DOCS.md#getThreadList)
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ function buildAPI(globalOptions, html, jar) {
'getCurrentUserID',
'getEmojiUrl',
'getFriendsList',
'getNotifications',
'getThreadHistory',
'getThreadInfo',
'getThreadList',
Expand Down
61 changes: 61 additions & 0 deletions src/getNotifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";

var utils = require("../utils");
var log = require("npmlog");

function formatText(notification, boldStart, boldEnd) {
const boldChars = boldStart.length + boldEnd.length
let formattedText = notification.title.text
notification.title.ranges.forEach( (range, i) => {
let start = range.offset + i * boldChars
let end = range.offset + range.length + i * boldChars
formattedText =
formattedText.slice( 0, start ) + boldStart +
formattedText.slice( start, end ) + boldEnd +
formattedText.slice( end )
})
return formattedText;
}

function formatData(obj) {
return obj.nodes.map( notification => {
return {
id: notification.alert_id,
text: notification.title.text,
html: formatText(notification, "<b>", "</b>"),
time: notification.timestamp.time * 1000,
seen: notification.first_seen_time * 1000,
read: notification.first_read_time * 1000
};
});
}

module.exports = function(defaultFuncs, api, ctx) {
return function getNotifications(amount, callback) {
if (!callback) {
throw { error: "getNotifications: need callback" };
}

defaultFuncs
.postFormData(
"https://www.facebook.com/ajax/notifications/client/get.php",
ctx.jar,
{},
{ length: amount }
)
.then(utils.parseAndCheckLogin(ctx, defaultFuncs))
.then(function(resData) {
if (!resData) {
throw { error: "getNotifications returned empty object." };
}
if (resData.error) {
throw resData;
}
callback(null, formatData(resData.payload));
})
.catch(function(err) {
log.error("getNotifications", err);
return callback(err);
});
};
};
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,26 @@ describe('Login:', function() {
});
});

it('should get notifications', function (done) {
api.getNotifications(function(err, data) {
try {
checkErr(done)(err);
assert(getType(data) === "Array");
data.map(v => {
assert(getType(v.id) === "String");
assert(getType(v.text) === "String");
assert(getType(v.html) === "String");
assert(getType(v.time) === "Number");
assert(getType(v.seen) === "Number");
assert(getType(v.read) === "Number");
})
done();
} catch(e){
done(e);
}
});
});

it('should parse share attachment correctly', function () {
var formatted = formatDeltaMessage(shareAttachmentFixture);
assert(formatted.attachments[0].type === "share");
Expand Down