Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 3308368

Browse files
authored
Merge pull request #5960 from matrix-org/travis/msc2876
Support UI for MSC2762: Widgets reading events from rooms
2 parents 9182bff + 5093657 commit 3308368

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/i18n/strings/en_EN.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,10 @@
600600
"See when the avatar changes in this room": "See when the avatar changes in this room",
601601
"Change the avatar of your active room": "Change the avatar of your active room",
602602
"See when the avatar changes in your active room": "See when the avatar changes in your active room",
603+
"Kick, ban, or invite people to this room, and make you leave": "Kick, ban, or invite people to this room, and make you leave",
604+
"See when people join, leave, or are invited to this room": "See when people join, leave, or are invited to this room",
605+
"Kick, ban, or invite people to your active room, and make you leave": "Kick, ban, or invite people to your active room, and make you leave",
606+
"See when people join, leave, or are invited to your active room": "See when people join, leave, or are invited to your active room",
603607
"Send stickers to this room as you": "Send stickers to this room as you",
604608
"See when a sticker is posted in this room": "See when a sticker is posted in this room",
605609
"Send stickers to your active room as you": "Send stickers to your active room as you",

src/stores/widgets/StopGapWidgetDriver.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { CHAT_EFFECTS } from "../../effects";
4444
import { containsEmoji } from "../../effects/utils";
4545
import dis from "../../dispatcher/dispatcher";
4646
import {tryTransformPermalinkToLocalHref} from "../../utils/permalinks/Permalinks";
47+
import {MatrixEvent} from "matrix-js-sdk/src/models/event";
4748

4849
// TODO: Purge this from the universe
4950

@@ -144,6 +145,52 @@ export class StopGapWidgetDriver extends WidgetDriver {
144145
return {roomId, eventId: r.event_id};
145146
}
146147

148+
public async readRoomEvents(eventType: string, msgtype: string | undefined, limit: number): Promise<MatrixEvent[]> {
149+
limit = limit > 0 ? Math.min(limit, 25) : 25; // arbitrary choice
150+
151+
const client = MatrixClientPeg.get();
152+
const roomId = ActiveRoomObserver.activeRoomId;
153+
const room = client.getRoom(roomId);
154+
if (!client || !roomId || !room) throw new Error("Not in a room or not attached to a client");
155+
156+
const results: MatrixEvent[] = [];
157+
const events = room.getLiveTimeline().getEvents(); // timelines are most recent last
158+
for (let i = events.length - 1; i > 0; i--) {
159+
if (results.length >= limit) break;
160+
161+
const ev = events[i];
162+
if (ev.getType() !== eventType) continue;
163+
if (eventType === EventType.RoomMessage && msgtype && msgtype !== ev.getContent()['msgtype']) continue;
164+
results.push(ev);
165+
}
166+
167+
return results.map(e => e.event);
168+
}
169+
170+
public async readStateEvents(
171+
eventType: string, stateKey: string | undefined, limit: number,
172+
): Promise<MatrixEvent[]> {
173+
limit = limit > 0 ? Math.min(limit, 100) : 100; // arbitrary choice
174+
175+
const client = MatrixClientPeg.get();
176+
const roomId = ActiveRoomObserver.activeRoomId;
177+
const room = client.getRoom(roomId);
178+
if (!client || !roomId || !room) throw new Error("Not in a room or not attached to a client");
179+
180+
const results: MatrixEvent[] = [];
181+
const state = room.currentState.events.get(eventType);
182+
if (state) {
183+
if (stateKey === "" || !!stateKey) {
184+
const forKey = state.get(stateKey);
185+
if (forKey) results.push(forKey);
186+
} else {
187+
results.push(...Array.from(state.values()));
188+
}
189+
}
190+
191+
return results.slice(0, limit).map(e => e.event);
192+
}
193+
147194
public async askOpenID(observer: SimpleObservable<IOpenIDUpdate>) {
148195
const oidcState = WidgetPermissionStore.instance.getOIDCState(
149196
this.forWidget, this.forWidgetKind, this.inRoomId,

src/widgets/CapabilityText.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ export class CapabilityText {
9696
[EventDirection.Receive]: _td("See when the avatar changes in your active room"),
9797
},
9898
},
99+
[EventType.RoomMember]: {
100+
[WidgetKind.Room]: {
101+
[EventDirection.Send]: _td("Kick, ban, or invite people to this room, and make you leave"),
102+
[EventDirection.Receive]: _td("See when people join, leave, or are invited to this room"),
103+
},
104+
[GENERIC_WIDGET_KIND]: {
105+
[EventDirection.Send]: _td("Kick, ban, or invite people to your active room, and make you leave"),
106+
[EventDirection.Receive]: _td("See when people join, leave, or are invited to your active room"),
107+
},
108+
},
99109
};
100110

101111
private static nonStateSendRecvCaps: ISendRecvStaticCapText = {

0 commit comments

Comments
 (0)