Exemplo n.º 1
0
        public async Task <IActionResult> Send(string SN)
        {
            var x = _handler;
            var websocketMenager = _handler.WebSocketConnectionManager;
            var socketId         = websocketMenager.GetIdBySN(SN);
            await _handler.SendMessageAsync(socketId, "CzeϾ tu api kontroller");

            return(Ok());
        }
Exemplo n.º 2
0
        public async Task <MessageListModel> Send(SendMessageModel model, int userId, Language language, string deviceId)
        {
            string sentMessage;
            var    caller = await _repository.Filter <User>(u => u.Id == userId).FirstOrDefaultAsync();

            if (caller == null)
            {
                throw new Exception("caller not found");
            }
            var conversation = await _repository.Filter <Conversation>(x => x.Id == model.ConversationId)
                               .FirstOrDefaultAsync();

            if (conversation.AnnouncementCreatorId != caller.Id && conversation.QuestionerId != caller.Id)
            {
                throw new Exception(_optionsBinder.Error().NotParticipating);
            }
            if (conversation.AnnouncementCreatorConversationIsDeleted || conversation.QuestionerConversationIsDeleted)
            {
                conversation.QuestionerConversationIsDeleted          = false;
                conversation.AnnouncementCreatorConversationIsDeleted = false;
                _repository.Update(conversation);
            }

            var  body            = model.MessageText;
            var  messageBodyType = MessageBodyType.Text;
            long length          = 0;

            if (model.File != null)
            {
                messageBodyType = MessageBodyType.Image;
                body            = await _accessor.Upload(model.File, UploadType.MessageFiles, conversation.Id);

                var extension = Path.GetExtension(body);
                if (extension != ".jpg" && extension != ".jpeg" && extension != ".png")
                {
                    messageBodyType = MessageBodyType.File;
                }
                length = await _accessor.GetLength(body, UploadType.MessageFiles, conversation.Id);
            }
            int?replayValue = null;
            var receiverId  = conversation.AnnouncementCreatorId == caller.Id
              ? conversation.QuestionerId
              : conversation.AnnouncementCreatorId;
            var message = _repository.Create(new Message
            {
                ConversationId  = conversation.Id,
                MessageBodyType = messageBodyType,
                MessageText     = body,
                SenderId        = caller.Id,
                FileLength      = length,
                ReciverId       = receiverId,
                ReplayMessageId = model.ReplayMessageId.HasValue ? model.ReplayMessageId.Value : replayValue
            });
            await _repository.SaveChangesAsync();

            int unreadConversationCount = _repository.Filter <Message>(m => m.ReciverId == receiverId && !m.IsSeen).GroupBy(m => m.ConversationId).Count();
            var success = _repository.FilterAsNoTracking <PersonSetting>(x => x.UserId == receiverId).Where(x =>
                                                                                                            x.SubscriptionsType == SubscriptionsType.EnableMessageNotification).Count();
            var notify = (new MessageListModel
            {
                SenderId = caller.Id,
                FullName = message.User != null ? message.User.FullName : "User",
                Photo = message.User != null ? new ImageOptimizer
                {
                    Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                     UploadType.ProfilePhoto, message.User.ProfilePhoto, ConstValues.Width, ConstValues.Height, false, 0),
                    PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, message.User.ProfilePhoto, 100, 100, true, 0)
                } : new ImageOptimizer(),
                CreatedDate = new DateTime(message.CreatedDt.Ticks),
                IsSentFromMe = false,
                MessageId = message.Id,
                MessageBodyType = messageBodyType,
                MessageText = message.MessageBodyType == MessageBodyType.Image
                    ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                               UploadType.MessageFiles, message.MessageText, 200, 200, false, conversation.Id) : message.MessageText,
                FileSize = length,
                FileUrl = messageBodyType == MessageBodyType.File
                    ? Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaDownload,
                                               UploadType.MessageFiles, message.MessageText, false, conversation.Id) : null,
                ReplayMessage = message.ReplayMessageId != null ? await _repository.Filter <Message>(s => s.Id == message.ReplayMessageId)
                                .Select(s => new MessageListModel
                {
                    MessageBodyType = s.MessageBodyType,
                    Photo = s.User != null ? new ImageOptimizer
                    {
                        Photo = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                         UploadType.ProfilePhoto, s.User.ProfilePhoto, ConstValues.Width, ConstValues.Height, false, 0),
                        PhotoBlur = Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                             UploadType.ProfilePhoto, s.User.ProfilePhoto, 100, 100, true, 0)
                    } : new ImageOptimizer(),
                    SenderId = s.SenderId,
                    FullName = s.User != null ? s.User.FullName : "User",
                    MessageId = s.Id,
                    MessageText = s.MessageBodyType == MessageBodyType.Image ?
                                  Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaResize,
                                                           UploadType.MessageFiles, s.MessageText, 1000, 1000, false, conversation.Id) : s.MessageText,
                    CreatedDate = s.CreatedDt,
                    IsSentFromMe = s.SenderId == caller.Id,
                    FileUrl = s.MessageBodyType == MessageBodyType.File ?
                              Utilities.ReturnFilePath(ConstValues.MediaBaseUrl, ConstValues.MediaDownload,
                                                       UploadType.MessageFiles, s.MessageText, false, conversation.Id) : null,
                    FileSize = s.FileLength
                }).FirstOrDefaultAsync() : null
            });

            var ann = _repository.Filter <Announcement>(a => a.Id == model.AnnouncementId).FirstOrDefault();

            if (ann != null)
            {
                await _activityService.AddOrUpdate(ann, caller.Id, false, ActivityType.Contacted);
            }
            var notifyToString = Utilities.SerializeObject(notify);
            var isSent         = await ChatMessageHandler.SendMessageAsync(conversation.Id, caller.Id, notifyToString, deviceId);

            if (isSent)
            {
                message.IsSeen = true;
                await _repository.SaveChangesAsync();
            }
            if (!isSent)
            {
                var receiverLanguage = await _repository.Filter <PersonOtherSetting>(x => x.UserId == receiverId)
                                       .Select(x => x.Language).FirstOrDefaultAsync();

                if (receiverLanguage == Language.English)
                {
                    sentMessage = "sent you a message";
                }
                else
                {
                    sentMessage = "ارسلت لك رساله";
                }
                await BaseMessageHandler.SendMessageAsync(conversation.Id, receiverId);

                if (success > 0)
                {
                    await _firebaseRepository.SendIndividualNotification(new NewMessageNotificationModel
                    {
                        SenderId    = caller.Id,
                        Description = messageBodyType == MessageBodyType.Image ? "Image" :
                                      messageBodyType == MessageBodyType.File ? "Photo" : model.MessageText,
                        GenericId               = conversation.Id,
                        NotificationType        = NotificationType.NewMessage,
                        ReceiverId              = receiverId,
                        Title                   = $"{caller.FullName} {sentMessage}",
                        UnreadConversationCount = unreadConversationCount
                    }, false);
                }
                await _firebaseRepository.SendIndividualNotification(new NewMessageNotificationModel
                {
                    SenderId    = caller.Id,
                    Description = messageBodyType == MessageBodyType.Image ? "Image" :
                                  messageBodyType == MessageBodyType.File ? "Photo" : model.MessageText,
                    GenericId               = conversation.Id,
                    NotificationType        = NotificationType.NewMessageGeneralInformation,
                    ReceiverId              = receiverId,
                    Title                   = $"{caller.FullName} {sentMessage}",
                    UnreadConversationCount = unreadConversationCount
                }, false);
            }
            return(notify);
        }