* Appends a new revision * @param {Object} aChangeset The changeset to append to the pad * @param {String} authorId The id of the author * @return {Promise }
(aChangeset:string, authorId = '')
| 278 | * @return {Promise<number|string>} |
| 279 | */ |
| 280 | async appendRevision(aChangeset:string, authorId = '') { |
| 281 | // Centralised "every insert op carries an author attribute" |
| 282 | // invariant. The socket handler enforces the same rule at the wire |
| 283 | // boundary; checking here covers the non-wire callers (HTTP API |
| 284 | // setHTML/setText/restoreRevision, plugin paths that call |
| 285 | // appendRevision directly). |
| 286 | Pad._assertInsertOpsCarryAuthor(aChangeset, this.pool); |
| 287 | |
| 288 | const newAText = applyToAText(aChangeset, this.atext, this.pool); |
| 289 | if (newAText.text === this.atext.text && newAText.attribs === this.atext.attribs && |
| 290 | this.head !== -1) { |
| 291 | return this.head; |
| 292 | } |
| 293 | copyAText(newAText, this.atext); |
| 294 | |
| 295 | const newRev = ++this.head; |
| 296 | |
| 297 | // ex. getNumForAuthor |
| 298 | if (authorId !== '') this.pool.putAttrib(['author', authorId]); |
| 299 | |
| 300 | const hook = this.head === 0 ? 'padCreate' : 'padUpdate'; |
| 301 | await Promise.all([ |
| 302 | // @ts-ignore |
| 303 | this.db.set(`pad:${this.id}:revs:${newRev}`, { |
| 304 | changeset: aChangeset, |
| 305 | meta: { |
| 306 | author: authorId, |
| 307 | timestamp: Date.now(), |
| 308 | ...newRev === this.getKeyRevisionNumber(newRev) ? { |
| 309 | pool: this.pool, |
| 310 | atext: this.atext, |
| 311 | } : {}, |
| 312 | }, |
| 313 | }), |
| 314 | this.saveToDatabase(), |
| 315 | authorId && authorManager.addPad(authorId, this.id), |
| 316 | hooks.aCallAll(hook, { |
| 317 | pad: this, |
| 318 | padId: this.id, |
| 319 | authorId, |
| 320 | get author() { |
| 321 | pad_utils.warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); |
| 322 | return this.authorId; |
| 323 | }, |
| 324 | set author(authorId) { |
| 325 | pad_utils.warnDeprecated(`${hook} hook author context is deprecated; use authorId instead`); |
| 326 | this.authorId = authorId; |
| 327 | }, |
| 328 | ...this.head === 0 ? {} : { |
| 329 | revs: newRev, |
| 330 | changeset: aChangeset, |
| 331 | }, |
| 332 | }), |
| 333 | ]); |
| 334 | return newRev; |
| 335 | } |
| 336 | |
| 337 | toJSON() { |
no test coverage detected