-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upload group conversation image #1067
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import { config } from '../../config'; | |
import { get } from '../api/rest'; | ||
import { MemberNetworks } from '../../store/users/types'; | ||
import { getFilteredMembersForAutoComplete, setAsDM } from './matrix/utils'; | ||
import { uploadImage } from '../../store/channels-list/api'; | ||
|
||
enum ConnectionStatus { | ||
Connected = 'connected', | ||
|
@@ -151,11 +152,17 @@ export class MatrixClient implements IChatClient { | |
return { messages: mappedMessages as any, hasMore: false }; | ||
} | ||
|
||
async createConversation(users: User[], name: string = null, _image: File = null, _optimisticId: string) { | ||
async createConversation(users: User[], name: string = null, image: File = null, _optimisticId: string) { | ||
await this.waitForConnection(); | ||
const initial_state = [ | ||
const coverUrl = await this.uploadCoverImage(image); | ||
|
||
const initial_state: any[] = [ | ||
{ type: 'm.room.guest_access', state_key: '', content: { guest_access: GuestAccess.Forbidden } }, | ||
]; | ||
if (coverUrl) { | ||
initial_state.push({ type: EventType.RoomAvatar, state_key: '', content: { url: coverUrl } }); | ||
} | ||
|
||
const options: ICreateRoomOpts = { | ||
name, | ||
preset: Preset.TrustedPrivateChat, | ||
|
@@ -256,6 +263,9 @@ export class MatrixClient implements IChatClient { | |
if (event.type === 'm.room.create') { | ||
this.roomCreated(event); | ||
} | ||
if (event.type === EventType.RoomAvatar) { | ||
this.publishRoomAvatarChange(event); | ||
} | ||
}); | ||
this.matrix.on(RoomMemberEvent.Membership, async (_event, member) => { | ||
if (member.membership === 'invite' && member.userId === this.userId) { | ||
|
@@ -354,6 +364,10 @@ export class MatrixClient implements IChatClient { | |
} | ||
}; | ||
|
||
private publishRoomAvatarChange = (event) => { | ||
this.events.onRoomAvatarChanged(event.room_id, event.content?.url); | ||
}; | ||
|
||
private publishMembershipChange = (event: MatrixEvent) => { | ||
if (event.getType() === EventType.RoomMember) { | ||
const user = this.mapUser(event.getStateKey()); | ||
|
@@ -370,13 +384,14 @@ export class MatrixClient implements IChatClient { | |
private mapToGeneralChannel(room: Room) { | ||
const otherMembers = this.getOtherMembersFromRoom(room).map((userId) => this.mapUser(userId)); | ||
const name = this.getRoomName(room); | ||
const avatarUrl = this.getRoomAvatar(room); | ||
|
||
const messages = this.getAllMessagesFromRoom(room); | ||
|
||
return { | ||
id: room.roomId, | ||
name, | ||
icon: null, | ||
icon: avatarUrl, | ||
isChannel: true, | ||
// Even if a member leaves they stay in the member list so this will still be correct | ||
// as zOS considers any conversation to have ever had more than 2 people to not be 1 on 1 | ||
|
@@ -457,6 +472,15 @@ export class MatrixClient implements IChatClient { | |
return ''; | ||
} | ||
|
||
private getRoomAvatar(room: Room): string { | ||
const roomAvatarEvent = room | ||
.getLiveTimeline() | ||
.getState(EventTimeline.FORWARDS) | ||
.getStateEvents(EventType.RoomAvatar, ''); | ||
|
||
return roomAvatarEvent?.getContent()?.url; | ||
} | ||
|
||
private getAllMessagesFromRoom(room: Room) { | ||
const timeline = room.getLiveTimeline().getEvents(); | ||
const messages = timeline | ||
|
@@ -488,4 +512,17 @@ export class MatrixClient implements IChatClient { | |
|
||
return Object.values(content ?? {}).flat(); | ||
} | ||
|
||
private async uploadCoverImage(image) { | ||
if (image) { | ||
try { | ||
return (await uploadImage(image)).url; | ||
} catch (error) { | ||
// For now, we will just ignore the error and continue to create the room | ||
// No reason for the image upload to block the room creation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
console.error(error); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add a helper function for all of these 'get' methods at some point 🙂 will take a look if I happen to work around these