You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-07 23:02:56 +03:00
Add Room, RoomState and RoomMember classes.
This commit is contained in:
4
index.js
4
index.js
@@ -1,7 +1,3 @@
|
||||
var matrixcs = require("./lib/matrix");
|
||||
matrixcs.request(require("request"));
|
||||
matrixcs.usePromises = function() {
|
||||
matrixcs = require("./lib/matrix-promise");
|
||||
};
|
||||
|
||||
module.exports = matrixcs;
|
||||
|
@@ -10,6 +10,12 @@ module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi;
|
||||
module.exports.MatrixError = require("./http-api").MatrixError;
|
||||
/** The {@link module:client.MatrixClient|MatrixClient} class. */
|
||||
module.exports.MatrixClient = require("./client").MatrixClient;
|
||||
/** The {@link module:models/room~Room|Room} class. */
|
||||
module.exports.Room = require("./models/room");
|
||||
/** The {@link module:models/room-member~RoomMember|RoomMember} class. */
|
||||
module.exports.RoomMember = require("./models/room-member");
|
||||
/** The {@link module:models/room-state~RoomState|RoomState} class. */
|
||||
module.exports.RoomState = require("./models/room-state");
|
||||
|
||||
// expose the underlying request object so different environments can use
|
||||
// different request libs (e.g. request or browser-request)
|
||||
|
@@ -5,6 +5,20 @@
|
||||
* @module models/event
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Enum for event statuses.
|
||||
* @readonly
|
||||
* @enum {string}
|
||||
*/
|
||||
module.exports.EventStatus = {
|
||||
UNKNOWN: "unknown",
|
||||
SENT: "sent",
|
||||
NOT_SENT: "not_sent",
|
||||
SENDING: "sending",
|
||||
INCOMING: "incoming"
|
||||
};
|
||||
|
||||
/**
|
||||
* Construct a Matrix Event object
|
||||
* @constructor
|
||||
@@ -13,9 +27,17 @@
|
||||
* directly unless you absolutely have to. Prefer the getter methods defined on
|
||||
* this class. Using the getter methods shields your app from
|
||||
* changes to event JSON between Matrix versions.
|
||||
* @prop {RoomMember} sender The room member who sent this event, or null e.g.
|
||||
* this is a presence event.
|
||||
* @prop {RoomMember} target The room member who is the target of this event, e.g.
|
||||
* the invitee, the person being banned, etc.
|
||||
* @prop {EventStatus} status The sending status of the event.
|
||||
*/
|
||||
module.exports.MatrixEvent = function MatrixEvent(event) {
|
||||
this.event = event || {};
|
||||
this.sender = null;
|
||||
this.target = null;
|
||||
this.status = null;
|
||||
};
|
||||
module.exports.MatrixEvent.prototype = {
|
||||
/**
|
||||
|
36
lib/models/room-member.js
Normal file
36
lib/models/room-member.js
Normal file
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
/**
|
||||
* @module models/room-member
|
||||
*/
|
||||
|
||||
/**
|
||||
* Construct a new room member.
|
||||
* @constructor
|
||||
* @param {MatrixEvent} event The <code>m.room.member</code> event.
|
||||
* @prop {string} roomId The room ID for this member.
|
||||
* @prop {string} userId The user ID of this member.
|
||||
* @prop {MatrixEvent} event The <code>m.room.member</code> event.
|
||||
* @prop {boolean} typing True if the room member is currently typing.
|
||||
* @prop {string} name The human-readable name for this room member.
|
||||
* @prop {Number} powerLevel The power level for this room member.
|
||||
* @prop {Number} powerLevelNorm The normalised power level (0-100) for this
|
||||
* room member.
|
||||
* @throws If the event provided is not <code>m.room.member</code>
|
||||
*/
|
||||
function RoomMember(event) {
|
||||
if (event.getType() !== "m.room.member") {
|
||||
throw new Error("Invalid event type: " + event.getType());
|
||||
}
|
||||
this.roomId = event.getRoomId();
|
||||
this.userId = event.getSender();
|
||||
this.event = event;
|
||||
this.typing = false;
|
||||
this.name = this.userId;
|
||||
this.powerLevel = 0;
|
||||
this.powerLevelNorm = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The RoomMember class.
|
||||
*/
|
||||
module.exports = RoomMember;
|
28
lib/models/room-state.js
Normal file
28
lib/models/room-state.js
Normal file
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
/**
|
||||
* @module models/room-state
|
||||
*/
|
||||
|
||||
/**
|
||||
* Construct room state.
|
||||
* @constructor
|
||||
* @prop {Object.<string, RoomMember>} members The room member dictionary, keyed
|
||||
* on the user's ID.
|
||||
* @prop {Object.<string, Object.<string, MatrixEvent>>} stateEvents The state
|
||||
* events dictionary, keyed on the event type and then the state_key value.
|
||||
* @prop {string} paginationToken The pagination token for this state.
|
||||
*/
|
||||
function RoomState() {
|
||||
this.members = {
|
||||
// userId: RoomMember
|
||||
};
|
||||
this.stateEvents = {
|
||||
// eventType: { stateKey: MatrixEvent }
|
||||
};
|
||||
this.paginationToken = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The RoomState class.
|
||||
*/
|
||||
module.exports = RoomState;
|
@@ -1,5 +1,21 @@
|
||||
"use strict";
|
||||
/**
|
||||
* @module models/room
|
||||
*/
|
||||
|
||||
/**
|
||||
* Construct a new Room.
|
||||
* @constructor
|
||||
* @param {string} roomId The ID of this room.
|
||||
* @prop {string} roomId The ID of this room.
|
||||
* @prop {string} name The human-readable display name for this room.
|
||||
* @prop {Array<MatrixEvent>} timeline The ordered list of message events for
|
||||
* this room.
|
||||
* @prop {RoomState} oldState The state of the room at the time of the oldest
|
||||
* event in the timeline.
|
||||
* @prop {RoomState} currentState The state of the room at the time of the
|
||||
* newest event in the timeline.
|
||||
*/
|
||||
function Room(roomId) {
|
||||
this.roomId = roomId;
|
||||
this.name = roomId;
|
||||
|
Reference in New Issue
Block a user