Commit f5e7955a authored by Adrien Dorsaz's avatar Adrien Dorsaz

Add basic presence and message support

parent 44cd9618
......@@ -24,6 +24,12 @@ class Constants {
static get NS_XMPP_BIND() {
return "urn:ietf:params:xml:ns:xmpp-bind";
}
static get STANZA_MESSAGE_VALID_TYPE() {
return ["error"];
}
static get STANZA_PRESENCE_VALID_TYPE() {
return ["error"];
}
static get STANZA_IQ_VALID_TYPE() {
return ["get", "set", "result", "error"];
}
......
......@@ -7,4 +7,44 @@ class Message extends Stanza {
constructor(_config) {
super(_config);
}
build(bodyElement) {
return new Promise((resolve, reject) => {
let message = this.dom.createElement('presence');
// By default, the "to" attribute should be defined
// but it could be not defined to send message. to bare JID of the client
if (this.to) {
message.setAttribute('to', this.to);
}
message.setAttribute('from', this.from);
if (this.xmllang) {
message.setAttribute('xml:lang', this.xmllang);
}
message.setAttribute('id', this.id);
for (let key of Object.keys(this.extendedAttributes)) {
message.setAttribute(key, this.extendedAttributes[key]);
}
if (!Constants.STANZA_MESSAGE_VALID_TYPE.find((type) => {
return type == this.type
})) {
reject('message error: type ' + this.type + ' is unknown for message stanzas.');
}
message.setAttribute('type', this.type);
if (typeof (bodyElement) == 'object') {
message.appendChild(bodyElement);
resolve(iq);
}
else {
reject('type of body is not DOM Element');
}
});
}
}
......@@ -7,4 +7,44 @@ class Presence extends Stanza {
constructor(_config) {
super(_config);
}
build(bodyElement) {
return new Promise((resolve, reject) => {
let presence = this.dom.createElement('presence');
// By default, the "to" attribute is not used to broadcast presence to every subscribed entities
// However client MAY set "to" to route the presence to a recipient
if (this.to) {
presence.setAttribute('to', this.to);
}
presence.setAttribute('from', this.from);
if (this.xmllang) {
presence.setAttribute('xml:lang', this.xmllang);
}
presence.setAttribute('id', this.id);
for (let key of Object.keys(this.extendedAttributes)) {
presence.setAttribute(key, this.extendedAttributes[key]);
}
if (!Constants.STANZA_PRESENCE_VALID_TYPE.find((type) => {
return type == this.type
})) {
reject('presence error: type ' + this.type + ' is unknown for presence stanzas.');
}
presence.setAttribute('type', this.type);
if (typeof (bodyElement) == 'object') {
presence.appendChild(bodyElement);
resolve(iq);
}
else {
reject('type of body is not DOM Element');
}
});
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment