keanu-weblite/src/components/roomInfoMixin.js

233 lines
6.7 KiB
JavaScript
Raw Normal View History

2023-06-28 12:14:44 +00:00
import utils from "../plugins/utils";
2023-08-07 14:13:35 +00:00
import roomTypeMixin from "./roomTypeMixin";
2023-06-28 12:14:44 +00:00
2021-03-11 13:55:10 +01:00
export default {
2023-08-07 14:13:35 +00:00
mixins: [roomTypeMixin],
data() {
return {
roomJoinRule: null,
2021-05-06 13:23:17 +02:00
userCanChangeJoinRule: false,
userCanPurgeRoom: false,
editedRoomName: "",
2022-11-26 12:48:41 +02:00
isRoomNameEditMode: false,
roomNameErrorMessage: null,
editedRoomTopic: "",
isRoomTopicEditMode: false,
roomTopicErrorMessage: null,
2024-02-06 10:22:35 +00:00
};
},
mounted() {
this.$matrix.on("Room.timeline", this.roomInfoMixinOnEvent);
this.updatePermissions();
},
destroyed() {
this.$matrix.off("Room.timeline", this.roomInfoMixinOnEvent);
},
2021-03-11 13:55:10 +01:00
computed: {
room() {
return this.$matrix.currentRoom;
},
roomName() {
if (this.room) {
return this.room.name;
}
return "";
},
roomTopic() {
if (this.room) {
return this.room.topic;
}
return "";
},
roomAvatar() {
if (this.room) {
return this.room.avatar;
}
return "";
},
roomIsEncrypted() {
if (this.room) {
return this.$matrix.matrixClient.isRoomEncrypted(this.room.roomId);
}
return false;
},
publicRoomLink() {
if (this.room && this.roomJoinRule == "public") {
return this.$router.getRoomLink(
2024-02-06 10:22:35 +00:00
this.room.getCanonicalAlias(),
this.room.roomId,
this.room.name,
utils.roomDisplayTypeToQueryParam(this.room, this.roomDisplayType)
);
}
return null;
},
roomHistory() {
if (this.room) {
2024-02-06 10:22:35 +00:00
return this.room.shouldEncryptForInvitedMembers();
}
return false;
2022-05-23 15:19:55 +00:00
},
userCanExportChat() {
// We say that if you can redact events, you are allowed to export chats.
const me = this.room && this.room.getMember(this.$matrix.currentUserId);
let isAdmin =
me && this.room.currentState && this.room.currentState.hasSufficientPowerLevelFor("redact", me.powerLevel);
return isAdmin;
},
isPrivate() {
return this.$matrix.isDirectRoom(this.room);
},
/**
* If this is a direct chat with someone, return that member here.
* @returns MXMember of the one we are chatting with, or 'undefined'.
*/
privateParty() {
if (this.isPrivate) {
2024-02-06 10:22:35 +00:00
const membersButMe = this.room.getMembers().filter((m) => m.userId != this.$matrix.currentUserId);
if (membersButMe.length == 1) {
return membersButMe[0];
}
}
return undefined;
},
2024-02-06 10:22:35 +00:00
canViewRetentionPolicy() {
return true;
},
/**
* Return true if we can set message retention policy in the room.
*/
canChangeRetentionPolicy() {
return this.userCanExportChat;
2024-02-06 10:22:35 +00:00
},
2021-03-11 13:55:10 +01:00
},
watch: {
room: {
handler(ignoredNewVal, ignoredOldVal) {
this.updatePermissions();
},
},
roomJoinRule: {
handler(newVal, oldVal) {
if (newVal && oldVal && newVal != oldVal) {
this.setRoomJoinRule(newVal);
}
},
},
},
methods: {
2024-02-06 10:22:35 +00:00
/**
* Get a string describing current room retention setting.
* Can be "None", "1 week", "1 hour" etc...
*/
roomMessageRetentionDisplay(maybeEvent) {
const retention = this.roomMessageRetention(maybeEvent);
if (retention == 0) {
return this.$t("room_info.message_retention_none");
} else if (retention == 60 * 60 * 1000) {
return this.$t("room_info.message_retention_1_hour");
} else if (retention == 24 * 60 * 60 * 1000) {
return this.$t("room_info.message_retention_1_day");
} else if (retention == 7 * 24 * 60 * 60 * 1000) {
return this.$t("room_info.message_retention_1_week");
} else if (retention == 30 * 24 * 60 * 60 * 1000) {
return this.$t("room_info.message_retention_30_days");
} else if (retention == 365 * 24 * 60 * 60 * 1000) {
return this.$t("room_info.message_retention_365_days");
}
return this.$t("room_info.message_retention_other_seconds", { count: retention / 1000 });
},
roomMessageRetention(maybeEvent) {
const retentionEvent = maybeEvent || (this.room && this.room.currentState.getStateEvents("m.room.retention", ""));
if (retentionEvent) {
console.log("Retention event found", JSON.stringify(retentionEvent));
const maxLifetime = parseInt(retentionEvent.getContent().max_lifetime);
if (maxLifetime) {
return maxLifetime;
}
}
return 0;
},
onRoomNameClicked() {
2024-02-06 10:22:35 +00:00
if (this.userCanPurgeRoom) {
2022-11-26 12:48:41 +02:00
this.isRoomNameEditMode = !this.isRoomNameEditMode;
this.editedRoomName = this.roomName;
}
},
updateRoomName() {
2024-02-06 10:22:35 +00:00
if (this.editedRoomName) {
this.$matrix.matrixClient.setRoomName(this.room.roomId, this.editedRoomName);
2022-11-26 12:48:41 +02:00
this.isRoomNameEditMode = !this.isRoomNameEditMode;
} else {
this.$refs.editedRoomName.focus();
}
},
2022-11-26 12:48:41 +02:00
onRoomTopicClicked() {
2024-02-06 10:22:35 +00:00
if (this.userCanPurgeRoom) {
2022-11-26 12:48:41 +02:00
this.isRoomTopicEditMode = !this.isRoomTopicEditMode;
this.editedRoomTopic = this.roomTopic;
}
},
updateRoomTopic() {
2024-02-06 10:22:35 +00:00
if (this.editedRoomTopic) {
2022-11-26 12:48:41 +02:00
this.$matrix.matrixClient.setRoomTopic(this.room.roomId, this.editedRoomTopic);
this.isRoomTopicEditMode = !this.isRoomTopicEditMode;
} else {
this.$refs.editedRoomTopic.focus();
}
},
setRoomJoinRule(ignoredJoinRule) {
// Do nothing in the general mixin, override in RoomInfo.vue
},
getRoomJoinRule() {
return this.$matrix.getRoomJoinRule(this.room);
},
updatePermissions() {
if (this.room) {
this.roomJoinRule = this.getRoomJoinRule();
const canChangeAccess =
2024-02-06 10:22:35 +00:00
this.room.currentState.mayClientSendStateEvent("m.room.join_rules", this.$matrix.matrixClient) &&
this.room.currentState.mayClientSendStateEvent("m.room.guest_access", this.$matrix.matrixClient);
this.userCanChangeJoinRule = canChangeAccess;
2021-05-06 13:23:17 +02:00
this.userCanPurgeRoom = canChangeAccess; //TODO - need different permissions here?
} else {
this.roomJoinRule = null;
this.userCanChangeJoinRule = false;
2021-05-06 13:23:17 +02:00
this.userCanPurgeRoom = false;
}
},
roomInfoMixinOnEvent(event) {
if (event.getRoomId() !== this.roomId) {
return; // Not for this room
}
2024-02-06 10:22:35 +00:00
if (event.getType() == "m.room.join_rules" || event.getType() == "m.room.guest_access") {
this.updatePermissions();
}
},
privatePartyAvatar(size) {
const other = this.privateParty;
if (other) {
2024-02-06 10:22:35 +00:00
return other.getAvatarUrl(this.$matrix.matrixClient.getHomeserverUrl(), size, size, "scale", true);
}
return undefined;
},
},
2024-02-06 10:22:35 +00:00
};