"use strict";

/**
 * Construct an AS bot user which has various helper methods.
 * @constructor
 * @param {MatrixClient} client The client instance configured for the AS bot.
 * @param {AppServiceRegistration} registration The registration that the bot
 * is following. Used to determine which user IDs it is controlling.
 */
function AppServiceBot(client, registration) {
    this.client = client;
    this.registration = registration;
}

AppServiceBot.prototype.getClient = function() {
    return this.client;
};

AppServiceBot.prototype.getUserId = function() {
    return this.client.credentials.userId;
};

/**
 * Get the membership lists of every room the AS bot is in. This does an
 * initial sync.
 * @return {Promise} Resolves to a dict of room_id: {@link AppServiceBot~RoomInfo}.
 */
AppServiceBot.prototype.getMemberLists = function() {

};

module.exports = AppServiceBot;

/**
 * @typedef AppServiceBot~RoomInfo
 * @type {Object}
 * @property {string} id The matrix room ID
 * @property {Object[]} state The raw state events for this room
 * @property {string[]} realJoinedUsers A list of user IDs of real matrix users
 * that have joined this room.
 * @property {string[]} remoteJoinedUsers A list of user IDs of remote users
 * (provisioned by the AS) that have joined this room.
 */

@@ -433,422 +433,2202 @@
 tion
-;%0A%7D%0A%0AAppServiceBot.prototype.getClient = function() %7B%0A    return this.client;%0A%7D;%0A%0AAppServiceBot.prototype.getUserId = function() %7B%0A    return this.client.credentials.userId;%0A%7D;%0A%0A/**%0A * Get the membership lists of every room the AS bot is in. This does an%0A * initial sync.%0A * @return %7BPromise%7D Resolves to a dict of room_id: %7B@link AppServiceBot~RoomInfo%7D.%0A */%0AAppServiceBot.prototype.getMemberLists = function() %7B%0A
+.getOutput();%0A    var self = this;%0A    // yank out the exclusive user ID regex strings%0A    this.exclusiveUserRegexes = %5B%5D;%0A    if (this.registration.namespaces && this.registration.namespaces.users) %7B%0A        this.registration.namespaces.users.forEach(function(userEntry) %7B%0A            if (!userEntry.exclusive) %7B%0A                return;%0A            %7D%0A            self.exclusiveUserRegexes.push(userEntry.regex);%0A        %7D);%0A    %7D%0A%7D%0A%0AAppServiceBot.prototype.getClient = function() %7B%0A    return this.client;%0A%7D;%0A%0AAppServiceBot.prototype.getUserId = function() %7B%0A    return this.client.credentials.userId;%0A%7D;%0A%0A/**%0A * Get the membership lists of every room the AS bot is in. This does an%0A * initial sync.%0A * @return %7BPromise%7D Resolves to a dict of room_id: %7B@link AppServiceBot~RoomInfo%7D.%0A */%0AAppServiceBot.prototype.getMemberLists = function() %7B%0A    var self = this;%0A    return this.client._http.authedRequest(%0A        undefined, %22GET%22, %22/initialSync%22, %7Blimit: 0%7D%0A    ).then(function(res) %7B%0A        var rooms = res.rooms %7C%7C %5B%5D;%0A        return rooms.map(function(room) %7B%0A            return self._getRoomInfo(room.room_id, room.state);%0A        %7D);%0A    %7D);%0A%7D;%0A%0AAppServiceBot.prototype._getRoomInfo = function(roomId, stateEvents) %7B%0A    var self = this;%0A    stateEvents = stateEvents %7C%7C %5B%5D;%0A    var roomInfo = %7B%0A        id: roomId,%0A        state: stateEvents,%0A        realJoinedUsers: %5B%5D,%0A        remoteJoinedUsers: %5B%5D%0A    %7D;%0A    stateEvents.forEach(function(event) %7B%0A        if (event.type !== %22m.room.member%22 %7C%7C event.content.membership !== %22join%22) %7B%0A            return;%0A        %7D%0A        var userId = event.state_key;%0A        if (userId === self.getUserId()) %7B%0A            return;%0A        %7D%0A        if (self._isRemoteUser(roomId, userId)) %7B%0A            roomInfo.remoteJoinedUsers.push(userId);%0A        %7D%0A        else %7B%0A            roomInfo.realJoinedUsers.push(userId);%0A        %7D%0A    %7D);%0A    return roomInfo;%0A%7D%0A%0AAppServiceBot.prototype._isRemoteUser = function(roomId, userId) %7B%0A    for (var i = 0; i %3C this.exclusiveUserRegexes.length; i++) %7B%0A        var regex = new RegExp(this.exclusiveUserRegexes%5Bi%5D);%0A        if (regex.test(userId)) %7B%0A            return true;%0A        %7D%0A    %7D%0A    return false;
 %0A%7D;%0A

Implement getMemberLists for the AS bot