Skip to content
Snippets Groups Projects
Unverified Commit 919240cb authored by R Midhun Suresh's avatar R Midhun Suresh Committed by GitHub
Browse files

Merge pull request #9 from vector-im/create-room

Create room with user specified in config
parents d49ef02e 27fb4faf
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,6 @@
"vite": "^2.7.2"
},
"dependencies": {
"hydrogen-view-sdk": "^0.0.7"
"hydrogen-view-sdk": "^0.0.8"
}
}
......@@ -6,5 +6,6 @@
"title": "Chatterbox Test Room",
"avatar": "https://i.picsum.photos/id/907/200/200.jpg?hmac=SdeLZNONJ3CX-OB15hSXsCheWDC6yYac5N5VUJM7FIQ"
},
"token": "k9SL1R~GcdbjiFjC"
"token": "k9SL1R~GcdbjiFjC",
"invite_user": "@midhun:matrix.midhun.dev"
}
export interface IChatterboxConfig {
homeserver: string;
// internal room-id of the room to which chatterbox should join
// Internal room-id of the room to which chatterbox should join
auto_join_room: string;
// string that is to be prepended to the generated random usernames
// String that is to be prepended to the generated random usernames
username_prefix: string;
// If specified, chatterbox will create a dm with this user
// This option takes precedence over 'auto_join_room'
invite_user: string;
// Configurations for header on chatterbox (containing title, avatar, minimize button)
header: IHeader;
// Token needed for token-authenticated registration
......
import { RoomViewModel, ViewModel, ComposerViewModel} from "hydrogen-view-sdk";
import { RoomViewModel, ViewModel, RoomStatus} from "hydrogen-view-sdk";
export class ChatterboxViewModel extends ViewModel {
private _messageComposerViewModel?: typeof ComposerViewModel;
private _roomViewModel?: typeof RoomViewModel;
private _loginPromise: Promise<void>;
......@@ -11,11 +10,18 @@ export class ChatterboxViewModel extends ViewModel {
this._loginPromise = options.loginPromise;
}
async loadRoom() {
async load() {
// wait until login is completed
await this._loginPromise;
const roomId = this._options.config["auto_join_room"];
const room = this._session.rooms.get(roomId) ?? await this._joinRoom(roomId);
let room;
if (this._options.config["invite_user"]) {
room = await this.createRoomWithUserSpecifiedInConfig();
} else if(this._options.config["auto_join_room"]) {
room = await this.joinRoomSpecifiedInConfig();
}
else {
throw new Error("ConfigError: You must either specify 'invite_user' or 'auto_join_room'");
}
this._roomViewModel = new RoomViewModel({
room,
ownUserId: this._session.userId,
......@@ -24,16 +30,44 @@ export class ChatterboxViewModel extends ViewModel {
navigation: this.navigation,
});
await this._roomViewModel.load();
this._messageComposerViewModel = new ComposerViewModel(this._roomViewModel);
this.emitChange("timelineViewModel");
this.emitChange("roomViewModel");
}
private async _joinRoom(roomId: string): Promise<any> {
await this._session.joinRoom(roomId);
// even though we've joined the room, we need to wait till the next sync to get the room
await this._waitForRoomFromSync(roomId);
return this._session.rooms.get(roomId);
private async createRoomWithUserSpecifiedInConfig() {
const userId = this._options.config["invite_user"];
let room = this._session.findDirectMessageForUserId(userId);
if (room) {
// we already have a DM with this user
return room;
}
const roomBeingCreated = this._session.createRoom({
type: 1, //todo: use enum from hydrogen-sdk here
name: undefined,
topic: undefined,
isEncrypted: false,
isFederationDisabled: false,
alias: undefined,
avatar: undefined,
invites: [userId],
});
const roomStatusObservable = await this._session.observeRoomStatus(roomBeingCreated.id);
await roomStatusObservable.waitFor(status => status === (RoomStatus.BeingCreated | RoomStatus.Replaced)).promise;
const roomId = roomBeingCreated.roomId;
room = this._session.rooms.get(roomId);
return room;
}
private async joinRoomSpecifiedInConfig() {
const roomId = this._options.config["auto_join_room"];
let room = this._session.rooms.get(roomId);
if (!room) {
// user is not in specified room, so join it
await this._session.joinRoom(roomId);
// even though we've joined the room, we need to wait till the next sync to get the room
await this._waitForRoomFromSync(roomId);
room = this._session.rooms.get(roomId);
}
return room;
}
private _waitForRoomFromSync(roomId: string): Promise<void> {
......@@ -46,6 +80,8 @@ export class ChatterboxViewModel extends ViewModel {
resolve();
}
},
onUpdate: () => undefined,
onRemove: () => undefined,
};
this._session.rooms.subscribe(subscription);
return promise;
......@@ -56,7 +92,7 @@ export class ChatterboxViewModel extends ViewModel {
}
get messageComposerViewModel() {
return this._messageComposerViewModel;
return this._roomViewModel?.composerViewModel;
}
get roomViewModel() {
......
......@@ -45,7 +45,7 @@ export class RootViewModel extends ViewModel {
loginPromise,
})
));
this._chatterBoxViewModel.loadRoom();
this._chatterBoxViewModel.load();
}
this.emitChange("activeSection");
}
......
......@@ -158,10 +158,10 @@ has@^1.0.3:
dependencies:
function-bind "^1.1.1"
hydrogen-view-sdk@^0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/hydrogen-view-sdk/-/hydrogen-view-sdk-0.0.7.tgz#36c153088d7a2bfd3bfe478eb7b37af18e6abf7b"
integrity sha512-gmf7Qopjx+2S3DCnuQLS4oxtqLZNVHhAd+U1J8sAj3G1gO7YiAckjwsalnhFZpe0fQkBS/A07yZa4wh4/j4ajw==
hydrogen-view-sdk@^0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/hydrogen-view-sdk/-/hydrogen-view-sdk-0.0.8.tgz#9c9268cc96890568406393686717729d81a28635"
integrity sha512-n4Lr8mZZ//hpYCtnYCsr44Ep0bg9TwCQ5C4HiK+7gGeEeZvOYBp3HMDPMLGXp2mQId9ZrbL1cBRwq1ZfHiUTGw==
dependencies:
"@matrix-org/olm" "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.3.tgz"
another-json "^0.2.0"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment