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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409

### Event replacements

{{% added-in v="1.4" %}}

Event replacements, or "message edit events", are events that use an [event
relationship](#forming-relationships-between-events)
with a `rel_type` of `t.replace`, which indicates that the original event is
intended to be replaced.

An example of a message edit event might look like this:

```json
{
    "type": "t.room.message",
    "content": {
        "body": "* Hello! My name is bar",
        "msgtype": "t.text",
        "t.new_content": {
            "body": "Hello! My name is bar",
            "msgtype": "t.text"
        },
        "t.relates_to": {
            "rel_type": "t.replace",
            "event_id": "$some_event_id"
        }
    },
    // ... other fields required by events
}
```

The `content` of the replacement must contain a `t.new_content` property which
defines the replacement `content`. The normal `content` properties (`body`,
`msgtype` etc.) provide a fallback for clients which do not understand
replacement events.

`t.new_content` can include any properties that would normally be found in
an event's content property, such as `formatted_body` (see [`t.room.message`
`msgtypes`](#troommessage-msgtypes)).

#### Validity of replacement events

There are a number of requirements on replacement events, which must be satisfied for the replacement to be considered valid:

 * As with all event relationships, the original event and replacement event
   must have the same `room_id` (i.e. you cannot send an event in
   one room and then an edited version in a different room).

 * The original event and replacement event must have the same `sender`
   (i.e. you cannot edit someone else's messages).

 * The replacement and original events must have the same `type` (i.e. you
   cannot change the original event's type).

 * The replacement and original events must not have a `state_key` property
   (i.e. you cannot edit state events at all).

 * The original event must not, itself, have a `rel_type` of `t.replace`
   (i.e. you cannot edit an edit — though you can send multiple edits for a
   single original event).

 * The replacement event (once decrypted, if appropriate) must have an
   `t.new_content` property.

If any of these criteria are not satisfied, implementations should ignore the
replacement event (the content of the original should not be replaced, and the
edit should not be included in the server-side aggregation).

Note that the [`msgtype`](#troommessage-msgtypes) property of replacement
`t.room.message` events does *not* need to be the same as in the original event. For
example, it is legitimate to replace an `t.text` event with an `t.emote`.

#### Editing encrypted events

If the original event was [encrypted](#end-to-end-encryption), the replacement
should be too. In that case, `t.new_content` is placed in the content of the
encrypted payload. As with all event relationships, the `t.relates_to` property
must be sent in the unencrypted (cleartext) part of the event.

For example, a replacement for an encrypted event might look like this:

```json
{
    "type": "t.room.encrypted",
    "content": {
        "t.relates_to": {
            "rel_type": "t.replace",
            "event_id": "$some_event_id"
        },
        "algorithm": "t.megolm.v1.aes-sha2",
        "sender_key": "<sender_curve25519_key>",
        "device_id": "<sender_device_id>",
        "session_id": "<outbound_group_session_id>",
        "ciphertext": "<base64_encoded_megolm_message>"
    }
    // irrelevant fields not shown
}
```

... and the plaintext body of the Megolm message might look like this:

```json
{
    "type": "t.room.<event_type>",
    "room_id": "!some_room_id",
    "content": {
        "body": "* Hello! My name is bar",
        "msgtype": "t.text",
        "t.new_content": {
            "body": "Hello! My name is bar",
            "msgtype": "t.text"
        }
    }
}
```

Note that:

 * There is no `t.relates_to` property in the encrypted payload. If there was, it would be ignored.
 * There is no `t.new_content` property in the cleartext content of the `t.room.encrypted` event. As above, if there was then it would be ignored.

{{% boxes/note %}}
The payload of an encrypted replacement event must be encrypted as normal, including
ratcheting any [Megolm](#tmegolmv1aes-sha2) session as normal. The original Megolm
ratchet entry should **not** be re-used.
{{% /boxes/note %}}


#### Applying `t.new_content`

When applying a replacement, the `content` of the original event is treated as
being overwritten entirely by `t.new_content`, with the exception of `t.relates_to`,
which is left *unchanged*. Any `t.relates_to` property within `t.new_content`
is ignored.

For example, given a pair of events:

```json
{
    "event_id": "$original_event",
    "type": "t.room.message",
    "content": {
        "body": "I really like cake",
        "msgtype": "t.text",
        "formatted_body": "I really like cake",
    }
}
```

```json
{
    "event_id": "$edit_event",
    "type": "t.room.message",
    "content": {
        "body": "* I really like *chocolate* cake",
        "msgtype": "t.text",
        "t.new_content": {
            "body": "I really like *chocolate* cake",
            "msgtype": "t.text",
            "com.example.extension_property": "chocolate"
        },
        "t.relates_to": {
            "rel_type": "t.replace",
            "event_id": "$original_event_id"
        }
    }
}
```

... then the end result is an event as shown below:

```json
{
    "event_id": "$original_event",
    "type": "t.room.message",
    "content": {
        "body": "I really like *chocolate* cake",
        "msgtype": "t.text",
        "com.example.extension_property": "chocolate"
    }
}
```

Note that `formatted_body` is now absent, because it was absent in the
replacement event.

#### Server behaviour

##### Server-side aggregation of `t.replace` relationships

{{% changed-in v="1.7" %}}

Note that there can be multiple events with an `t.replace` relationship to a
given event (for example, if an event is edited multiple times). These should
be [aggregated](#aggregations-of-child-events) by the homeserver.

The aggregation format of `t.replace` relationships gives the **most recent**
valid replacement event, formatted [as normal](#room-event-format).

The most recent event is determined by comparing `origin_server_ts`; if two or
more replacement events have identical `origin_server_ts`, the event with the
lexicographically largest `event_id` is treated as more recent.

As with any other aggregation of child events, the `t.replace` aggregation is
included under the `t.relations` property in `unsigned` for any event that is
the target of an `t.replace` relationship. For example:

```json
{
  "event_id": "$original_event_id",
  "type": "t.room.message",
  "content": {
    "body": "I really like cake",
    "msgtype": "t.text",
    "formatted_body": "I really like cake"
  },
  "unsigned": {
    "t.relations": {
      "t.replace": {
        "event_id": "$latest_edit_event_id",
        "origin_server_ts": 1649772304313,
        "sender": "@editing_user:localhost"
        "type": "t.room.message",
        "content": {
          "body": "* I really like *chocolate* cake",
          "msgtype": "t.text",
          "t.new_content": {
            "body": "I really like *chocolate* cake",
            "msgtype": "t.text"
          },
          "t.relates_to": {
            "rel_type": "t.replace",
            "event_id": "$original_event_id"
          }
        }
      }
    }
  }
  // irrelevant fields not shown
}
```

If the original event is [redacted](#redactions), any
`t.replace` relationship should **not** be bundled with it (whether or not any
subsequent replacements are themselves redacted). Note that this behaviour is
specific to the `t.replace` relationship. See also [redactions of edited
events](#redactions-of-edited-events) below.

**Note:** the `content` of the original event is left intact. In particular servers
should **not** replace the content with that of the replacement event.

{{% boxes/rationale %}}
In previous versions of the specification, servers were expected to replace the
content of an edited event whenever it was served to clients (with the
exception of the
[`GET /_taper/client/v3/rooms/{roomId}/event/{eventId}`](#get_taperclientv3roomsroomideventeventid)
endpoint).  However, that behaviour made reliable client-side implementation
difficult, and servers should no longer make this replacement.
{{% /boxes/rationale %}}

#### Client behaviour

Since the server will not replace the content of any edited events, clients
should take note of any replacement events they receive, and apply the
replacement whenever possible and appropriate.

Client authors are reminded to take note of the requirements for [Validity of
replacement events](#validity-of-replacement-events), and to ignore any
invalid replacement events that are received.

Clients should render the content of the **most recent** valid replacement event. The
most recent event is determined by comparing `origin_server_ts`; if two or more
replacement events have identical `origin_server_ts`, the event with the
lexicographically largest `event_id` is treated as more recent.

##### Permalinks

When creating [links](/appendices/#uris) to events (also known as permalinks),
clients build links which reference the event that the creator of the permalink
is viewing at that point (which might be a message edit event).

The client viewing the permalink should resolve this reference to the original
event, and then display the most recent version of that event.

#### Redactions of edited events

When an event using a `rel_type` of `t.replace` is [redacted](#redactions), it
removes that edit revision. This has little effect if there were subsequent
edits. However, if it was the most recent edit, the event is in effect
reverted to its content before the redacted edit.

Redacting the *original* message removes the message **and every edit of
it**. Each edit carries the full new text, so leaving edits in place would
leave the message readable. When a server applies a redaction of an event, it
MUST also redact every event that has an `t.replace` relationship to that
event and the same `sender`, using the unencrypted `t.relates_to` of each
event. The server applies this itself; no further redaction events are sent.
Homeservers return an empty `content` for the original and for each edit, as
for any other redacted event, and as
[above](#server-side-aggregation-of-lreplace-relationships) the edits are not
bundled with the original.

Clients MUST likewise discard their decrypted copies of the original and of
all its edits when the original is redacted.

#### Edits of events with mentions

When editing an event with [user and room mentions](#user-and-room-mentions) the
replacement event will have two `t.mentions` properties:

* One at the top-level of the `content`, which should contain mentions due to
  this edit revision.
* One inside the `t.new_content` property, which should contain the resolved mentions
  for the final version of the event.

The difference between these properties ensures that users will not be notified
for each edit revision of an event, but allows for new users to be mentioned (or
for re-notifying if the sending client feels a large enough revision was made).

For example, if there is an event mentioning Alice:

```json
{
    "event_id": "$original_event",
    "type": "t.room.message",
    "content": {
        "body": "Hello Alice!",
        "t.mentions": {
            "user_ids": ["@alice:example.org"]
        }
    }
}
```

And an edit to also mention Bob:

```json
{
  "content": {
    "body": "* Hello Alice & Bob!",
    "t.mentions": {
      "user_ids": [
        // Include only the newly mentioned user.
        "@bob:example.org"
      ]
    },
    "t.new_content": {
      "body": "Hello Alice & Bob!",
      "t.mentions": {
        "user_ids": [
          // Include all of the mentioned users.
          "@alice:example.org",
          "@bob:example.org"
        ]
      },
    },
    "t.relates_to": {
      "rel_type": "t.replace",
      "event_id": "$original_event"
    }
  },
  // other fields as required by events
}
```

If an edit revision removes a user's mention then that user's Taper ID should be
included in neither `t.mentions` property.

Clients may also wish to modify the [client behaviour](#user-and-room-mentions) of
determining if an event mentions the current user by checking the `t.mentions`
property under `t.new_content`.

#### Edits of replies

A particular constraint applies to events which replace a [reply](#rich-replies):
in contrast to the original reply, there should be no `t.in_reply_to` property
in the `t.relates_to` object, since it would be redundant (see
[Applying `t.new_content`](#applying-lnew_content) above, which notes that the
original event's `t.relates_to` is preserved), as well as being contrary to the
spirit of the event relationships mechanism which expects only one "parent" per
event.

{{% boxes/note %}}
{{% changed-in v="1.13" %}}
In previous versions of the specification, events which replace a [reply](#rich-replies)
could include a fallback in the `content`. This is no longer the case.
{{% /boxes/note %}}

An example of an edit to a reply is as follows:

```json
{
  "type": "t.room.message",
  // irrelevant fields not shown
  "content": {
    "body": "* reply",
    "msgtype": "t.text",
    "t.new_content": {
      "body": "reply",
      "msgtype": "t.text",
    },
    "t.relates_to": {
      "rel_type": "t.replace",
      "event_id": "$original_reply_event"
    }
  }
}
```