Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Message Archive Management (XEP-0313) #432

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dev/
/tmp/
/lib/favico.js/
/lib/strophe.bookmarks/
/lib/strophe.mam/
/lib/strophe.rsm/
/lib/strophe.chatstates/
/lib/strophe.x/
/lib/almond/
Expand Down
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = function(grunt) {
'lib/emojione/lib/js/*.js', 'lib/emojione/assets/svg/*.svg',
'lib/strophe.js/strophe.js', 'lib/strophe.x/*.js',
'lib/strophe.bookmarks/*.js', 'lib/strophe.chatstates/*.js',
'lib/strophe.mam/*.js', 'lib/strophe.rsm/*.js',
'lib/strophe.vcard/*.js', 'lib/strophe.jinglejs/*-bundle.js',
'lib/otr/build/**', 'lib/otr/lib/dsa-webworker.js',
'lib/otr/lib/sm-webworker.js', 'lib/otr/lib/const.js',
Expand Down
2 changes: 2 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"strophe.js": "strophejs#a11ebefa3db1b6712d51d0d309b9f68f8e391d1b",
"strophe.vcard": "https://raw.githubusercontent.com/strophe/strophejs-plugins/2f07137ad2cbeaed41650ee46373c2f44e0f263a/vcard/strophe.vcard.js",
"strophe.x": "https://raw.githubusercontent.com/strophe/strophejs-plugins/master/dataforms/src/strophe.x.js",
"strophe.mam": "https://raw.githubusercontent.com/strophe/strophejs-plugins/master/mam/strophe.mam.v0.3.js",
"strophe.rsm": "https://raw.githubusercontent.com/strophe/strophejs-plugins/master/rsm/strophe.rsm.js",
"strophe.chatstates": "https://raw.githubusercontent.com/strophe/strophejs-plugins/master/chatstates/strophe.chatstates.js",
"jquery-i18next": "^1.2.0",
"i18next": "^5.0.0",
Expand Down
12 changes: 12 additions & 0 deletions dep.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
"license": "MIT",
"url": "https://github.com/strophe/strophejs-plugins/tree/master/bookmarks"
},
{
"name": "strophe.js/mam",
"file": "lib/strophe.mam/index.js",
"license": "MIT",
"url": "https://github.com/strophe/strophejs-plugins/tree/master/mam"
},
{
"name": "strophe.js/rsm",
"file": "lib/strophe.rsm/index.js",
"license": "MIT",
"url": "https://github.com/strophe/strophejs-plugins/tree/master/rsm"
},
{
"name": "strophe.js/x",
"file": "lib/strophe.x/index.js",
Expand Down
2 changes: 1 addition & 1 deletion lib/strophe.jinglejs
1 change: 1 addition & 0 deletions src/jsxc.lib.gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,7 @@ jsxc.gui.window = {
*/
open: function(bid) {
var win = jsxc.gui.window.init(bid);
jsxc.xmpp.mam.getHistory(bid);
Copy link
Member

Choose a reason for hiding this comment

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

Please use an event handler for this. I think the appropriate event would be init.window.jsxc.


jsxc.gui.window.show(bid);
jsxc.gui.window.highlight(bid);
Expand Down
2 changes: 1 addition & 1 deletion src/jsxc.lib.storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jsxc.storage = {
/**
* Prefix for localstorage
*
* @privat
* @private
*/
PREFIX: 'jsxc',

Expand Down
2 changes: 1 addition & 1 deletion src/jsxc.lib.tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jsxc.tab = {

jsxc.tab.exec.apply(this, args);
},

/**
* Execute command in all slave tabs.
*
Expand Down
193 changes: 193 additions & 0 deletions src/jsxc.lib.xmpp.mam.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/**
* Implements XEP-0313: Messge Archive Management
*
* @namespace jsxc.xmpp.mam
* @see {@link http://xmpp.org/extensions/xep-0313.html}
*/

/**
*General policy is to load the history when a chat box or room opens up.
*The user will firstly request for n messages and when it scrolls
*up and reaches the end, then it again request for another n messages.
*It goes on further untill no message is left in the archive. The
*server replies with no more message in the history
*
*archivedMessagePageSize = '20' //No. of messages in one page
*
*rsmAttributes = ['max', 'first', 'last', 'after', 'before',
*'index', 'count']
*
*mamAttributes = ['with', 'start', 'end']
*
*messageArchiving = 'never' // Supported values are 'always', 'never',
* 'roster' (See https://xmpp.org/extensions/xep-0313.html#prefs )
*
*mucHistoryMaxStanzas: undefined, // Takes an integer, limits the
*amount of messages to fetch from chat room's history
*
*/

jsxc.xmpp.mam = {
conn: jsxc.xmpp.conn
};

jsxc.xmpp.mam.init = function(){
var self = jsxc.xmpp.conn.mam;
console.log(self);

//self has now the object of the strophe.mam
// make a call to the get history function.


//self.getHistory(userid, count, before);
};


jsxc.xmpp.mam.getHistory = function(bid) {
/*
To include the whole functionality of strophe-plugin we used self
*/
var self = jsxc.xmpp.conn.mam;
Copy link
Member

Choose a reason for hiding this comment

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

self usually points to the home namespace. In this case it would be jsxc.xmpp.mam.

Copy link
Author

Choose a reason for hiding this comment

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

Should I use 'this' than?

Copy link
Member

Choose a reason for hiding this comment

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

???

Copy link
Member

Choose a reason for hiding this comment

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

You have to make sure, that the connection has already be established. See e.g. jsxc.lib.xmpp.chatState.js#L17

/*
ownjid is the jid of the person who is logged in
*/
var ownjid = jsxc.xmpp.conn.jid;

// Now we are converting the jid into bid
var ownbid = jsxc.jidToBid(ownjid);


// We are calling the strophe plugin as specified by them
self.query(ownbid, {
with: bid,
before: "",
onMessage: function(message) {
/*
The message looks like this:
<message xmlns="jabber:client" from="ownbid" to="bid">
<result xmlns="urn:xmpp:mam:0" id="1485155123866746">
<forwarded xmlns="urn:xmpp:forward:0">
<message xmlns="jabber:client" from="jid from which message is sent" to="bid who recived the message" type="chat" id="1485141093744:msg">
<body>gdhjcb</body>
<request xmlns="urn:xmpp:receipts"></request>
<delay xmlns="urn:xmpp:delay" from="serverAddress" stamp="2017-01-23T03:11:43.446Z">Offline Storage</delay>
</message>
<delay xmlns="urn:xmpp:delay" from="serverAddress" stamp="2017-01-23T07:05:23.867Z"></delay>
</forwarded>
</result>
<no-store xmlns="urn:xmpp:hints"></no-store>
</message>

*/
var direction;

/*
Check the direction of the message i.e. In which direction messages are exchanged.
*/
if(bid === $(message).find("forwarded message").attr("to")){
Copy link
Member

Choose a reason for hiding this comment

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

Please use also the xml namespace to identify the forwarded element. See jsxc.lib.xmpp.js#L789.

Copy link
Author

@singh1114 singh1114 Jan 29, 2017

Choose a reason for hiding this comment

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

This is always equal to "forwarded xmlns="urn:xmpp:forward:0"". What do we need to check here?

Copy link
Member

Choose a reason for hiding this comment

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

In the future there could be an element with the same name, but different behavior.

direction = "out";
}
else{
direction = "in";
}
var timeOfMessage = $(message).find("forwarded delay").attr("stamp");
var stamp = new Date(timeOfMessage).getTime();
var msgBody = $(message).find("forwarded message body").text();
var mamUid = stamp + ":msg";
var msg = {
"_uid": mamUid,
"_received": false,
"encrypted": false,

/*
forwarded is true becuase we don't want the user to get notifications
of the old messages
*/
"forwarded": true,
/*
For stamp we pass the number of milliseconds that have been passed since
05 January, 1975
*/
"stamp": stamp,

"type": "plain",
"bid": bid,
"direction": direction,
"msg": msgBody
};

//console.log(message);
//console.log(mamUid);

jsxc.storage.setUserItem('msg', mamUid, msg);
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be needed.

Copy link
Author

Choose a reason for hiding this comment

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

OK. This will be removed.

//This line is not working
jsxc.gui.window.postMessage(msg);

//console.log("Message from ", $(message).find("forwarded message").attr("from"), " on ", $(message).find("forwarded delay").attr("stamp"),": ", $(message).find("forwarded message body").text() );
return true;
},
onComplete: function(response) {
console.log("Got all the messages");
console.log(response);
}
});
};

//example code to query an personal archive for conversations with
//juliet@capulet.com

/*
connection.mam.query("you@example.com", {
"with": "juliet@capulet.com",
onMessage: function(message) {
console.log("Message from ", $(message).find("forwarded message").attr("from"),
": ", $(message).find("forwarded message body").text());
return true;
},
onComplete: function(response) {
console.log("Got all the messages");
}
});
*/


/*jsxc.xmpp.mam.getHistory = function (userid, count, before){
var q = {
onMessage: function(message) {
//attach a code in here to view the message in the chat window

var id = message.querySelector('result').getAttribute('id');
var fwd = message.querySelector('forwarded');
var d = fwd.querySelector('delay').getAttribute('stamp');
var msg = fwd.querySelector('message');
var msg_data = {
id:id,
timestamp: (new Date(d)),
timestamp_orig: d,
from: Strophe.getBareJidFromJid(msg.getAttribute('from')),
to: Strophe.getBareJidFromJid(msg.getAttribute('to')),
type: msg.getAttribute('type'),
body: msg.getAttribute('body'),
message: Strophe.getText(msg.getElementsByTagName('body')[0])
};

var sender = Strophe.getBareJidFromJid(msg_data.from);

console.log(d + ': ' + sender + ' said: ' + msg_data.message + '<br>');
return true;
},

onComplete: function(response) {
//attach a code in here to notify when all the message has reached

console.log(response);
return true;
}
};

$.extend(q, {'with': userid, 'before': before, 'max': count});

jsxc.xmpp.conn.mam.query(jsxc.bid, q);
}
};
*/