1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
describe('ModelService', function() {
// setup the dependencies
beforeEach(function() {
// dependencies
module('matrixService');
// tested service
module('modelService');
});
it('should be able to get a member in a room', inject(
function(modelService) {
var roomId = "!wefiohwefuiow:matrix.org";
var userId = "@bob:matrix.org";
modelService.getRoom(roomId).current_room_state.storeStateEvent({
type: "m.room.member",
id: "fwefw:matrix.org",
user_id: userId,
state_key: userId,
content: {
membership: "join"
}
});
var user = modelService.getMember(roomId, userId);
expect(user.event.state_key).toEqual(userId);
}));
it('should be able to get a users power level', inject(
function(modelService) {
var roomId = "!foo:matrix.org";
var room = modelService.getRoom(roomId);
room.current_room_state.storeStateEvent({
content: { membership: "join" },
user_id: "@adam:matrix.org",
state_key: "@adam:matrix.org",
type: "m.room.member"
});
room.current_room_state.storeStateEvent({
content: { membership: "join" },
user_id: "@beth:matrix.org",
state_key: "@beth:matrix.org",
type: "m.room.member"
});
room.current_room_state.storeStateEvent({
content: {
"@adam:matrix.org": 90,
"default": 50
},
user_id: "@adam:matrix.org",
type: "m.room.power_levels"
});
var num = modelService.getUserPowerLevel(roomId, "@beth:matrix.org");
expect(num).toEqual(50);
num = modelService.getUserPowerLevel(roomId, "@adam:matrix.org");
expect(num).toEqual(90);
num = modelService.getUserPowerLevel(roomId, "@unknown:matrix.org");
expect(num).toEqual(50);
}));
it('should be able to get a user', inject(
function(modelService) {
var roomId = "!wefiohwefuiow:matrix.org";
var userId = "@bob:matrix.org";
var presenceEvent = {
content: {
user_id: userId,
displayname: "Bob",
last_active_ago: 1415981891580
},
type: "m.presence",
event_id: "weofhwe@matrix.org"
};
modelService.setUser(presenceEvent);
var user = modelService.getUser(userId);
expect(user.event).toEqual(presenceEvent);
}));
it('should be able to create and get alias mappings.', inject(
function(modelService) {
var roomId = "!wefiohwefuiow:matrix.org";
var alias = "#foobar:matrix.org";
modelService.createRoomIdToAliasMapping(roomId, alias);
expect(modelService.getRoomIdToAliasMapping(roomId)).toEqual(alias);
expect(modelService.getAliasToRoomIdMapping(alias)).toEqual(roomId);
}));
it('should clobber alias mappings.', inject(
function(modelService) {
var roomId = "!wefiohwefuiow:matrix.org";
var alias = "#foobar:matrix.org";
var newAlias = "#foobarNEW:matrix.org";
modelService.createRoomIdToAliasMapping(roomId, alias);
expect(modelService.getRoomIdToAliasMapping(roomId)).toEqual(alias);
expect(modelService.getAliasToRoomIdMapping(alias)).toEqual(roomId);
modelService.createRoomIdToAliasMapping(roomId, newAlias);
expect(modelService.getRoomIdToAliasMapping(roomId)).toEqual(newAlias);
expect(modelService.getAliasToRoomIdMapping(newAlias)).toEqual(roomId);
}));
it('should update RoomMember when User is updated to point to the latest info.', inject(
function(modelService) {
var roomId = "!wefiohwefuiow:matrix.org";
var userId = "@bob:matrix.org";
var presenceEvent = {
content: {
user_id: userId,
displayname: "Bob",
last_active_ago: 1415
},
type: "m.presence",
event_id: "weofhwe@matrix.org"
};
var newPresenceEvent = {
content: {
user_id: userId,
displayname: "The only and only Bob",
last_active_ago: 1900
},
type: "m.presence",
event_id: "weofhtweterte@matrix.org"
};
modelService.setUser(presenceEvent);
modelService.getRoom(roomId).current_room_state.storeStateEvent({
type: "m.room.member",
id: "fwefw:matrix.org",
user_id: userId,
state_key: userId,
content: {
membership: "join"
}
});
var roomMember = modelService.getMember(roomId, userId);
expect(roomMember.user.event).toEqual(presenceEvent);
expect(roomMember.user.event.content.displayname).toEqual("Bob");
modelService.setUser(newPresenceEvent);
expect(roomMember.user.event.content.displayname).toEqual("The only and only Bob");
}));
it('should normalise power levels between 0-100.', inject(
function(modelService) {
var roomId = "!foo:matrix.org";
var room = modelService.getRoom(roomId);
room.current_room_state.storeStateEvent({
content: { membership: "join" },
user_id: "@adam:matrix.org",
state_key: "@adam:matrix.org",
type: "m.room.member"
});
room.current_room_state.storeStateEvent({
content: { membership: "join" },
user_id: "@beth:matrix.org",
state_key: "@beth:matrix.org",
type: "m.room.member"
});
room.current_room_state.storeStateEvent({
content: {
"@adam:matrix.org": 1000,
"default": 500
},
user_id: "@adam:matrix.org",
type: "m.room.power_levels"
});
var roomMember = modelService.getMember(roomId, "@beth:matrix.org");
expect(roomMember.power_level).toEqual(500);
expect(roomMember.power_level_norm).toEqual(50);
}));
});
|