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

Commit e044902

Browse files
committed
Support UI for MSC2876: Widgets reading events from rooms
MSC: matrix-org/matrix-spec-proposals#2876 Fixes element-hq/element-web#15747 Requires matrix-org/matrix-widget-api#34
1 parent 8dbcc85 commit e044902

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/i18n/strings/en_EN.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,20 +598,33 @@
598598
"Change which room, message, or user you're viewing": "Change which room, message, or user you're viewing",
599599
"Change the topic of this room": "Change the topic of this room",
600600
"See when the topic changes in this room": "See when the topic changes in this room",
601+
"See the current topic for this room": "See the current topic for this room",
601602
"Change the topic of your active room": "Change the topic of your active room",
602603
"See when the topic changes in your active room": "See when the topic changes in your active room",
604+
"See the current topic in your active room": "See the current topic in your active room",
603605
"Change the name of this room": "Change the name of this room",
604606
"See when the name changes in this room": "See when the name changes in this room",
607+
"See the current name for this room": "See the current name for this room",
605608
"Change the name of your active room": "Change the name of your active room",
606609
"See when the name changes in your active room": "See when the name changes in your active room",
610+
"See the current name of your active room": "See the current name of your active room",
607611
"Change the avatar of this room": "Change the avatar of this room",
608612
"See when the avatar changes in this room": "See when the avatar changes in this room",
613+
"See the current avatar for this room": "See the current avatar for this room",
609614
"Change the avatar of your active room": "Change the avatar of your active room",
610615
"See when the avatar changes in your active room": "See when the avatar changes in your active room",
616+
"See the current avatar for your active room": "See the current avatar for your active room",
617+
"Kick, ban, or invite people to this room, and make you leave": "Kick, ban, or invite people to this room, and make you leave",
618+
"See when people join, leave, or are invited to this room": "See when people join, leave, or are invited to this room",
619+
"See the membership status of anyone in this room": "See the membership status of anyone in this room",
620+
"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",
621+
"See when people join, leave, or are invited to your active room": "See when people join, leave, or are invited to your active room",
611622
"Send stickers to this room as you": "Send stickers to this room as you",
612623
"See when a sticker is posted in this room": "See when a sticker is posted in this room",
624+
"See recent stickers posted to this room": "See recent stickers posted to this room",
613625
"Send stickers to your active room as you": "Send stickers to your active room as you",
614626
"See when anyone posts a sticker to your active room": "See when anyone posts a sticker to your active room",
627+
"See recent stickers posted to your active room": "See recent stickers posted to your active room",
615628
"with an empty state key": "with an empty state key",
616629
"with state key %(stateKey)s": "with state key %(stateKey)s",
617630
"Send <b>%(eventType)s</b> events as you in this room": "Send <b>%(eventType)s</b> events as you in this room",

src/stores/widgets/StopGapWidgetDriver.ts

Lines changed: 45 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,50 @@ 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(eventType: string, stateKey: string | undefined, limit: number): Promise<MatrixEvent[]> {
171+
limit = limit > 0 ? Math.min(limit, 100) : 100; // arbitrary choice
172+
173+
const client = MatrixClientPeg.get();
174+
const roomId = ActiveRoomObserver.activeRoomId;
175+
const room = client.getRoom(roomId);
176+
if (!client || !roomId || !room) throw new Error("Not in a room or not attached to a client");
177+
178+
const results: MatrixEvent[] = [];
179+
const state = room.currentState.events.get(eventType);
180+
if (state) {
181+
if (stateKey === "" || !!stateKey) {
182+
const forKey = state.get(stateKey);
183+
if (forKey) results.push(forKey);
184+
} else {
185+
results.push(...Array.from(state.values()));
186+
}
187+
}
188+
189+
return results.slice(0, limit).map(e => e.event);
190+
}
191+
147192
public async askOpenID(observer: SimpleObservable<IOpenIDUpdate>) {
148193
const oidcState = WidgetPermissionStore.instance.getOIDCState(
149194
this.forWidget, this.forWidgetKind, this.inRoomId,

src/widgets/CapabilityText.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,48 @@ export class CapabilityText {
7070
[WidgetKind.Room]: {
7171
[EventDirection.Send]: _td("Change the topic of this room"),
7272
[EventDirection.Receive]: _td("See when the topic changes in this room"),
73+
[EventDirection.Read]: _td("See the current topic for this room"),
7374
},
7475
[GENERIC_WIDGET_KIND]: {
7576
[EventDirection.Send]: _td("Change the topic of your active room"),
7677
[EventDirection.Receive]: _td("See when the topic changes in your active room"),
78+
[EventDirection.Read]: _td("See the current topic in your active room"),
7779
},
7880
},
7981
[EventType.RoomName]: {
8082
[WidgetKind.Room]: {
8183
[EventDirection.Send]: _td("Change the name of this room"),
8284
[EventDirection.Receive]: _td("See when the name changes in this room"),
85+
[EventDirection.Read]: _td("See the current name for this room"),
8386
},
8487
[GENERIC_WIDGET_KIND]: {
8588
[EventDirection.Send]: _td("Change the name of your active room"),
8689
[EventDirection.Receive]: _td("See when the name changes in your active room"),
90+
[EventDirection.Read]: _td("See the current name of your active room"),
8791
},
8892
},
8993
[EventType.RoomAvatar]: {
9094
[WidgetKind.Room]: {
9195
[EventDirection.Send]: _td("Change the avatar of this room"),
9296
[EventDirection.Receive]: _td("See when the avatar changes in this room"),
97+
[EventDirection.Read]: _td("See the current avatar for this room"),
9398
},
9499
[GENERIC_WIDGET_KIND]: {
95100
[EventDirection.Send]: _td("Change the avatar of your active room"),
96101
[EventDirection.Receive]: _td("See when the avatar changes in your active room"),
102+
[EventDirection.Read]: _td("See the current avatar for your active room"),
103+
},
104+
},
105+
[EventType.RoomMember]: {
106+
[WidgetKind.Room]: {
107+
[EventDirection.Send]: _td("Kick, ban, or invite people to this room, and make you leave"),
108+
[EventDirection.Receive]: _td("See when people join, leave, or are invited to this room"),
109+
[EventDirection.Read]: _td("See the membership status of anyone in this room"),
110+
},
111+
[GENERIC_WIDGET_KIND]: {
112+
[EventDirection.Send]: _td("Kick, ban, or invite people to your active room, and make you leave"),
113+
[EventDirection.Receive]: _td("See when people join, leave, or are invited to your active room"),
114+
[EventDirection.Read]: _td("See the membership status of anyone in this room"),
97115
},
98116
},
99117
};
@@ -103,10 +121,12 @@ export class CapabilityText {
103121
[WidgetKind.Room]: {
104122
[EventDirection.Send]: _td("Send stickers to this room as you"),
105123
[EventDirection.Receive]: _td("See when a sticker is posted in this room"),
124+
[EventDirection.Read]: _td("See recent stickers posted to this room"),
106125
},
107126
[GENERIC_WIDGET_KIND]: {
108127
[EventDirection.Send]: _td("Send stickers to your active room as you"),
109128
[EventDirection.Receive]: _td("See when anyone posts a sticker to your active room"),
129+
[EventDirection.Read]: _td("See recent stickers posted to your active room"),
110130
},
111131
},
112132
};

0 commit comments

Comments
 (0)