public static SignalMessage IncreaseReceiptCountLocked(SignalServiceEnvelope envelope) { SignalMessage m; bool set_mark = false; lock (DBLock) { using (var ctx = new SignalDBContext()) { m = ctx.Messages.SingleOrDefault(t => t.ComposedTimestamp == envelope.GetTimestamp()); if (m != null) { m.Receipts++; if (m.Status == SignalMessageStatus.Confirmed) { m.Status = SignalMessageStatus.Received; set_mark = true; } } else { ctx.EarlyReceipts.Add(new SignalEarlyReceipt() { DeviceId = (uint)envelope.GetSourceDevice(), Timestamp = envelope.GetTimestamp(), Username = envelope.GetSourceE164() }); } ctx.SaveChanges(); } } return(set_mark ? m : null); }