示例#1
0
        public static void SendJsonCapsula(Context context, JsonCapsula toSend, long recepientId, long myUserId)
        {
            if (toSend == null)
            {
                return;
            }

            var recepient = context.Contacts
                            .Where(u => u.PublicId == recepientId)
                            .SingleOrDefault();

            if (recepient == null)
            {
                throw new Exception($"User is not downloaded in local database.");
            }
            else if (recepient.Trusted != 1)
            {
                throw new Exception($"User {recepient.PublicId} ({recepient.UserName}) is not trusted.");
            }

            long?blobId = null;

            if (myUserId == recepientId)
            {
                var blobMessage = new BlobMessages()
                {
                    SenderId = myUserId,
                    PublicId = null,
                    DoDelete = 0,
                    Failed   = 0
                };
                context.BlobMessages.Add(blobMessage);
                context.SaveChanges();

                blobId = blobMessage.Id;
                PullMessageParser.ParseIJTypeMessage(context, toSend, myUserId, blobMessage.Id, myUserId);
                context.SaveChanges();
            }

            context.ToSendMessages.Add(new ToSendMessages()
            {
                RecepientId    = recepientId,
                BlobMessagesId = blobId,
                Blob           = JsonEncoder.GetJsonEncoded(context, toSend, recepientId),
                Priority       = toSend.Message.GetPriority()
            });

            context.SaveChanges();

            PushAction.Changed = true;
        }