matrix_sdk_base/response_processors/profiles.rs
1// Copyright 2025 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use ruma::{
16 events::{
17 room::member::{MembershipState, RoomMemberEventContent},
18 SyncStateEvent,
19 },
20 RoomId,
21};
22
23use super::Context;
24
25/// Decide whether the profile must be created, updated or deleted based on the
26/// [`RoomMemberEventContent`].
27pub fn upsert_or_delete(
28 context: &mut Context,
29 room_id: &RoomId,
30 event: &SyncStateEvent<RoomMemberEventContent>,
31) {
32 // Senders can fake the profile easily so we keep track of profiles that the
33 // member set themselves to avoid having confusing profile changes when a
34 // member gets kicked/banned.
35 if event.state_key() == event.sender() {
36 context
37 .state_changes
38 .profiles
39 .entry(room_id.to_owned())
40 .or_default()
41 .insert(event.sender().to_owned(), event.into());
42 }
43
44 if *event.membership() == MembershipState::Invite {
45 // Remove any profile previously stored for the invited user.
46 //
47 // A room member could have joined the room and left it later; in that case, the
48 // server may return a dummy, empty profile along the `leave` event. We
49 // don't want to reuse that empty profile when the member has been
50 // re-invited, so we remove it from the database.
51 context
52 .state_changes
53 .profiles_to_delete
54 .entry(room_id.to_owned())
55 .or_default()
56 .push(event.state_key().clone());
57 }
58}