|
| 1 | +import Payload from './Payload' |
| 2 | +import { Thrift, TCompactProtocol, Int64 } from 'thrift' |
| 3 | +import Message from '../../types/message' |
| 4 | +import RandomIntGenerator from '../../RandomIntGenerator' |
| 5 | +import Long from 'long' |
| 6 | + |
| 7 | +export default class ReadReceiptPayload extends Payload { |
| 8 | + |
| 9 | + otherUserFbId: string // Used for individuals |
| 10 | + threadFbId: string // Used for groups, same as threadId ( usually ? ) |
| 11 | + watermarkTimestamp: number// Must match timestamp of a message |
| 12 | + |
| 13 | + public constructor (message: Message) { |
| 14 | + super() |
| 15 | + |
| 16 | + if (message.authorId.toString() === message.threadId.toString()) { |
| 17 | + // If author id equals thread id, then this is not a group convo |
| 18 | + this.otherUserFbId = message.authorId.toString() |
| 19 | + } else { |
| 20 | + // Otherwise it is |
| 21 | + this.threadFbId = message.threadId.toString() |
| 22 | + } |
| 23 | + |
| 24 | + this.watermarkTimestamp = message.timestamp |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + encode (proto: TCompactProtocol): Promise<void> { |
| 29 | + ///////////////////// |
| 30 | + // thrift header? |
| 31 | + ///////////////////// |
| 32 | + |
| 33 | + // Always completely empty. |
| 34 | + proto.writeByte(0) |
| 35 | + |
| 36 | + // Note lack of end-of-object null |
| 37 | + |
| 38 | + ///////////////////// |
| 39 | + // Typing presence |
| 40 | + ///////////////////// |
| 41 | + |
| 42 | + proto.writeFieldBegin('mark', Thrift.Type.STRING, 1) |
| 43 | + proto.writeString('read') |
| 44 | + |
| 45 | + proto.writeFieldBegin('state', Thrift.Type.BOOL, 2) |
| 46 | + proto.writeBool(true) |
| 47 | + |
| 48 | + if (this.threadFbId != null) { |
| 49 | + proto.writeFieldBegin('threadFbId', Thrift.Type.I64, 6) |
| 50 | + proto.writeI64(new Int64(Buffer.from(Long.fromString(this.threadFbId).toBytes()))) |
| 51 | + } else if (this.otherUserFbId != null) { |
| 52 | + proto.writeFieldBegin('otherUserFbId', Thrift.Type.I64, 7) |
| 53 | + proto.writeI64(new Int64(Buffer.from(Long.fromString(this.otherUserFbId).toBytes()))) |
| 54 | + } else { |
| 55 | + // Throw? |
| 56 | + } |
| 57 | + |
| 58 | + proto.writeFieldBegin('watermarkTimestamp', Thrift.Type.I64, 9) |
| 59 | + proto.writeI64(this.watermarkTimestamp) |
| 60 | + |
| 61 | + proto.writeFieldBegin('attemptId', Thrift.Type.I64, 13) |
| 62 | + proto.writeI64(new Int64(Buffer.from(RandomIntGenerator.getAttemptId().toBytes()))) |
| 63 | + |
| 64 | + // denotes end of object |
| 65 | + proto.writeByte(0) |
| 66 | + |
| 67 | + return null |
| 68 | + } |
| 69 | + |
| 70 | + getTopic (): string { |
| 71 | + return '/t_mt_req' |
| 72 | + } |
| 73 | +} |
0 commit comments