1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Convert all helper functions which make events to use kwargs

This commit is contained in:
Kegan Dougal
2015-06-24 11:43:16 +01:00
parent 26a23b19b3
commit f763eb3f37
4 changed files with 141 additions and 105 deletions

View File

@@ -44,78 +44,87 @@ module.exports.mock = function(constr, name) {
};
/**
* Create a JSON object representing an Event.
* @param {string} type The event.type
* @param {string} room The event.room_id
* @param {string} userId The event.user_id
* @param {Object} content The event.content
* Create an Event.
* @param {Object} opts Values for the event.
* @param {string} opts.type The event.type
* @param {string} opts.room The event.room_id
* @param {string} opts.user The event.user_id
* @param {string} opts.skey Optional. The state key (auto inserts empty string)
* @param {Object} opts.content The event.content
* @param {boolean} opts.event True to make a MatrixEvent.
* @return {Object} a JSON object representing this event.
*/
module.exports.mkEvent = function(type, room, userId, content) {
module.exports.mkEvent = function(opts) {
if (!opts.type || !opts.content) {
throw new Error("Missing .type or .content =>"+JSON.stringify(opts));
}
var event = {
type: type,
room_id: room,
user_id: userId,
content: content,
type: opts.type,
room_id: opts.room,
user_id: opts.user,
content: opts.content,
event_id: "$" + Math.random() + "-" + Math.random()
};
if (["m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
if (opts.skey) {
event.state_key = opts.skey;
}
else if (["m.room.name", "m.room.topic", "m.room.create", "m.room.join_rules",
"m.room.power_levels", "m.room.topic",
"com.example.state"].indexOf(type) !== -1) {
"com.example.state"].indexOf(opts.type) !== -1) {
event.state_key = "";
}
return event;
return opts.event ? new MatrixEvent(event) : event;
};
/**
* Create an m.room.member POJO.
* @param {string} room The room ID for the event.
* @param {string} membership The content.membership for the event.
* @param {string} userId The user ID for the event.
* @param {string} otherUserId The other user ID for the event if applicable
* Create an m.room.member event.
* @param {Object} opts Values for the membership.
* @param {string} opts.room The room ID for the event.
* @param {string} opts.mship The content.membership for the event.
* @param {string} opts.user The user ID for the event.
* @param {string} opts.skey The other user ID for the event if applicable
* e.g. for invites/bans.
* @param {string} displayName The content.displayname for the event.
* @param {string} avatarUrl The content.avatar_url for the event.
* @return {Object} The event
* @param {string} opts.name The content.displayname for the event.
* @param {string} opts.url The content.avatar_url for the event.
* @param {boolean} opts.event True to make a MatrixEvent.
* @return {Object|MatrixEvent} The event
*/
module.exports.mkMembership = function(room, membership, userId, otherUserId,
displayName, avatarUrl) {
var event = module.exports.mkEvent("m.room.member", room, userId, {
membership: membership,
displayname: displayName,
avatar_url: avatarUrl
});
event.state_key = userId;
if (["invite", "ban"].indexOf(membership) !== -1) {
event.state_key = otherUserId;
module.exports.mkMembership = function(opts) {
opts.type = "m.room.member";
if (!opts.skey) {
opts.skey = opts.user;
}
return event;
if (!opts.mship) {
throw new Error("Missing .mship => "+JSON.stringify(opts));
}
opts.content = {
membership: opts.mship
};
if (opts.name) { opts.content.displayname = opts.name; }
if (opts.url) { opts.content.avatar_url = opts.url; }
return module.exports.mkEvent(opts);
};
/**
* Create an m.room.message POJO.
* Create an m.room.message event.
* @param {Object} opts Values for the message
* @param {string} opts.room The room ID for the event.
* @param {string} opts.user The user ID for the event.
* @param {string} opts.msg Optional. The content.body for the event.
* @param {boolean} opts.event True to make a MatrixEvent.
* @return {Object} The event
* @return {Object|MatrixEvent} The event
*/
module.exports.mkMessage = function(opts) {
opts.type = "m.room.message";
if (!opts.msg) {
opts.msg = "Random->" + Math.random();
}
if (!opts.room || !opts.user) {
throw new Error("Missing .room or .user from %s", opts);
}
opts.type = "m.room.message";
opts.content = {
msgtype: "m.text",
body: opts.msg
};
//var pojo = module.exports.mkEvent(opts);
var pojo = module.exports.mkEvent(
opts.type, opts.room, opts.user, opts.content
);
return opts.event ? new MatrixEvent(pojo) : pojo;
return module.exports.mkEvent(opts);
};