Beispiel #1
0
 public static void RaiseBubbleFailed(VisualBubble bubble, BubbleGroup bubbleGroup)
 {
     if (OnBubbleFailed != null)
     {
         OnBubbleFailed(bubble, bubbleGroup);
     }
 }
        protected internal void OnBubbleUpdated(VisualBubble bubble, BubbleGroup group)
        {
            if (Bubbles.Count >= BubblesCapSize)
            {
                Bubbles.RemoveAt(0);
            }

            var addedToEnd = false;

            var unreadIndicatorGuid = BubbleGroupSettingsManager.GetUnreadIndicatorGuid(this);

            for (int i = Bubbles.Count - 1; i >= 0; i--)
            {
                var nBubble = Bubbles[i];

                var unreadIndicatorIndex = -1;
                if (unreadIndicatorGuid != null && unreadIndicatorGuid == nBubble.ID)
                {
                    unreadIndicatorIndex = i;
                }

                if (nBubble.Time <= bubble.Time)
                {
                    // adding it to the end, we can do a simple contract
                    if (i == Bubbles.Count - 1)
                    {
                        addedToEnd = true;
                        Bubbles.Add(bubble);
                        if (bubble.Direction == Bubble.BubbleDirection.Incoming)
                        {
                            BubbleGroupSettingsManager.SetUnread(this, true);
                        }
                    }
                    // inserting, do a full contract
                    else
                    {
                        Bubbles.Insert(i + 1, bubble);
                    }
                    if (i >= unreadIndicatorIndex && bubble.Direction == Bubble.BubbleDirection.Outgoing)
                    {
                        BubbleGroupSettingsManager.SetUnreadIndicatorGuid(this, bubble.ID, false);
                    }
                    break;
                }

                // could not find a valid place to insert, then skip insertion.
                if (i == 0)
                {
                    return;
                }
            }

            if (SendingGroup != group && addedToEnd)
            {
                SendingGroup = group;
                BubbleGroupEvents.RaiseSendingServiceChange(this);
            }

            RaiseBubbleInserted(bubble);
        }
Beispiel #3
0
        public static BubbleGroup FindInner(VisualBubble visualBubble, BubbleGroup group)
        {
            if (visualBubble.BubbleGroupReference != null)
            {
                return(visualBubble.BubbleGroupReference);
            }

            var unifiedGroup = @group as UnifiedBubbleGroup;

            if (unifiedGroup == null)
            {
                return(@group);
            }

            // try to find it.
            foreach (var innerGroup in unifiedGroup.Groups)
            {
                foreach (var bubble in innerGroup.Bubbles)
                {
                    if (bubble.ID != visualBubble.ID)
                    {
                        continue;
                    }

                    return(innerGroup);
                }
            }

            return(null);
        }
Beispiel #4
0
 private static void AddUrlMarkupIfNeeded(VisualBubble bubble)
 {
     try
     {
         var textBubble = bubble as TextBubble;
         if (textBubble != null && !textBubble.HasParsedMessageForUrls)
         {
             var markups = new List <BubbleMarkup>();
             if (_linkExtraction == null)
             {
                 _linkExtraction = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
             }
             var rawString = textBubble.Message;
             foreach (Match m in _linkExtraction.Matches(rawString))
             {
                 markups.Add(new BubbleMarkupUrl
                 {
                     Url = m.Value,
                 });
             }
             if (textBubble.BubbleMarkups == null)
             {
                 textBubble.BubbleMarkups = new List <BubbleMarkup>();
             }
             textBubble.BubbleMarkups.AddRange(markups);
             textBubble.HasParsedMessageForUrls = true;
         }
     }
     catch (Exception e)
     {
         Utils.DebugPrint("Failed to add UrlMarkup: " + e);
     }
 }
Beispiel #5
0
 public static Task JustQueue(VisualBubble visualBubble)
 {
     return(Task.Factory.StartNew(() =>
     {
         var entry = Add(visualBubble);
     }));
 }
Beispiel #6
0
 private static Entry Add(VisualBubble bubble)
 {
     lock (_dbLock)
     {
         using (var db = new SqlDatabase <Entry>(Location))
         {
             // don't add a duplicate bubble in the queue
             foreach (var possibleBubble in db.Store)
             {
                 if (possibleBubble.Guid == bubble.ID)
                 {
                     return(possibleBubble);
                 }
             }
             var entry = new Entry()
             {
                 ServiceName = bubble.Service.Information.ServiceName,
                 Guid        = bubble.ID,
                 Time        = bubble.Time
             };
             db.Add(entry);
             return(entry);
         }
     }
 }
Beispiel #7
0
        public static string GetMediaFilePathIfPossible(VisualBubble bubble)
        {
            var imageBubble = bubble as ImageBubble;

            if (imageBubble != null)
            {
                return(imageBubble.ImagePath);
            }

            var videoBubble = bubble as VideoBubble;

            if (videoBubble != null)
            {
                return(videoBubble.VideoPath);
            }

            var audioBubble = bubble as AudioBubble;

            if (audioBubble != null)
            {
                return(audioBubble.AudioPath);
            }

            var fileBubble = bubble as FileBubble;

            if (fileBubble != null)
            {
                return(fileBubble.Path);
            }

            return(null);
        }
Beispiel #8
0
 public virtual Task GetQuotedMessageTitle(VisualBubble bubble, Action <string> result)
 {
     return(Task.Factory.StartNew(() =>
     {
         result(null);
     }));
 }
Beispiel #9
0
        private static bool IsBubbleDownloading(VisualBubble bubble)
        {
            var imageBubble = bubble as ImageBubble;

            if (imageBubble != null)
            {
                return(imageBubble.Transfer != null);
            }
            var videoBubble = bubble as VideoBubble;

            if (videoBubble != null)
            {
                return(videoBubble.Transfer != null);
            }
            var audioBubble = bubble as AudioBubble;

            if (audioBubble != null)
            {
                return(audioBubble.Transfer != null);
            }
            var fileBubble = bubble as FileBubble;

            if (fileBubble != null)
            {
                return(fileBubble.Transfer != null);
            }

            return(false);
        }
Beispiel #10
0
        public static bool Update(BubbleGroup group, VisualBubble visualBubble, int bubbleDepth = 100)
        {
            if (visualBubble.BubbleGroupReference != null)
            {
                BubbleGroupDatabase.UpdateBubble(visualBubble.BubbleGroupReference, visualBubble, bubbleDepth);
                return(true);
            }

            var unifiedGroup = @group as UnifiedBubbleGroup;

            if (unifiedGroup == null)
            {
                BubbleGroupDatabase.UpdateBubble(@group, visualBubble, bubbleDepth);
                return(true);
            }

            foreach (var innerGroup in unifiedGroup.Groups)
            {
                foreach (var bubble in innerGroup.Bubbles)
                {
                    if (bubble.ID != visualBubble.ID)
                    {
                        continue;
                    }

                    BubbleGroupDatabase.UpdateBubble(innerGroup, visualBubble, bubbleDepth);
                    return(true);
                }
            }

            return(false);
        }
Beispiel #11
0
        public Task GetBotCallbackAnswer(BubbleGroup group, VisualBubble bubble, Bots.KeyboardButton button, Action <Bots.MessagesBotCallbackAnswer> answer)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var client = new FullClientDisposable(this))
                {
                    // Note: Telegram also has KeyboardButtonGame which functions as a callback also
                    if (button is Bots.KeyboardButtonCallback)
                    {
                        var keyboardButtonCallback = button as Bots.KeyboardButtonCallback;

                        var args = new MessagesGetBotCallbackAnswerArgs
                        {
                            MsgId = uint.Parse(bubble.IdService),
                            Data = keyboardButtonCallback.Data,
                            Peer = GetInputPeer(group.Address, group.IsParty, group.IsExtendedParty)
                        };

                        SharpTelegram.Schema.MessagesBotCallbackAnswer telegramBotCallbackAnswer =
                            (SharpTelegram.Schema.MessagesBotCallbackAnswer)
                            TelegramUtils.RunSynchronously(
                                client.Client.Methods.MessagesGetBotCallbackAnswerAsync(args));

                        var disaBotCallbackAnswer = new Bots.MessagesBotCallbackAnswer
                        {
                            Alert = telegramBotCallbackAnswer.Alert != null ? true : false,
                            Message = telegramBotCallbackAnswer.Message
                        };

                        answer(disaBotCallbackAnswer);
                    }
                }
            }));
        }
Beispiel #12
0
        public static void ForwardCopyToMediaDirectoryIfNeeded(VisualBubble vb)
        {
            if (vb is TextBubble)
            {
                return;
            }
            if (vb is LocationBubble)
            {
                return;
            }
            if (vb is ContactBubble)
            {
                return;
            }

            var videoBubble = vb as VideoBubble;

            if (videoBubble != null)
            {
                if (IsInMediaLocation(videoBubble.VideoPath))
                {
                    return;
                }
                var path = CopyVideoToDisaVideoLocation(videoBubble.VideoPath);
                if (path == null)
                {
                    return;
                }
                videoBubble.VideoPath = path;
            }

            var audioBubble = vb as AudioBubble;

            if (audioBubble != null)
            {
                if (IsInMediaLocation(audioBubble.AudioPath))
                {
                    return;
                }
                var path = CopyAudioToDisaAudioLocation(audioBubble.AudioPath);
                audioBubble.AudioPath = path;
            }

            var imageBubble = vb as ImageBubble;

            if (imageBubble != null)
            {
                if (IsInMediaLocation(imageBubble.ImagePath))
                {
                    return;
                }
                var path = CopyPhotoToDisaPictureLocation(imageBubble.ImagePath);
                if (path == null)
                {
                    return;
                }
                imageBubble.ImagePath = path;
            }
        }
Beispiel #13
0
 public bool LoadBubblesComparer(VisualBubble localBubble, VisualBubble agentBubble)
 {
     if (localBubble.IdService == agentBubble.IdService)
     {
         return(true);
     }
     return(false);
 }
Beispiel #14
0
 public UnifiedBubbleGroup(List <BubbleGroup> groups, BubbleGroup primaryGroup,
                           VisualBubble initialBubble, string id = null)
     : base(initialBubble, id, false)
 {
     _unifiedService = initialBubble.Service;
     Groups          = groups;
     PrimaryGroup    = primaryGroup;
     _sendingGroup   = primaryGroup;
 }
Beispiel #15
0
        private static byte[] SerializeBubble(VisualBubble b)
        {
            using (var ms = new MemoryStream())
            {
                Serializer.Serialize(ms, b);

                return(ms.ToArray());
            }
        }
Beispiel #16
0
 public static void UpdateStatus(VisualBubble b, Bubble.BubbleStatus status, BubbleGroup group,
                                 bool updateBubbleGroupBubbles = true)
 {
     b.Status = status;
     BubbleGroupDatabase.UpdateBubble(@group, b);
     if (updateBubbleGroupBubbles)
     {
         BubbleGroupEvents.RaiseBubblesUpdated(@group);
     }
     BubbleGroupEvents.RaiseRefreshed(@group);
 }
Beispiel #17
0
        public static BubbleGroup AddNewIfNotExist(VisualBubble bubble, bool updateUi = false)
        {
            var group =
                BubbleGroupManager.FindWithAddress(bubble.Service, bubble.Address);

            if (@group != null)
            {
                return(null);
            }

            return(AddNewInternal(bubble, updateUi));
        }
Beispiel #18
0
        public static VisualBubble FetchNewestBubbleIfNotWaiting(Stream stream, Service service,
                                                                 int searchDepth = 10)
        {
            VisualBubble firstBubble = null;

            lock (OperationLock)
            {
                stream.Seek(stream.Length, SeekOrigin.Begin);

                for (var i = 0; i < searchDepth; i++)
                {
                    if (stream.Position == 0)
                    {
                        break;
                    }

                    byte[] headerBytes;
                    int    headerBytesLength;
                    int    endPosition;

                    BubbleGroupDatabasePrimitives.ReadBubbleHeaderType(stream, out headerBytes,
                                                                       out headerBytesLength, out endPosition);

                    var guid = BubbleGroupDatabasePrimitives.ReadBubbleHeaderStruct(headerBytes, headerBytesLength,
                                                                                    endPosition + 1, out endPosition);

                    var bubbleData = BubbleGroupDatabasePrimitives.ReadBubbleData(stream);

                    var visualBubble = Deserialize(bubbleData);
                    if (visualBubble == null)
                    {
                        continue;
                    }

                    // if any of the bubbles are waiting, then make sure to perform a full load
                    if (!(visualBubble is NewBubble) &&
                        (visualBubble.Status == Bubble.BubbleStatus.Waiting && visualBubble.Direction == Bubble.BubbleDirection.Outgoing))
                    {
                        return(null);
                    }

                    if (firstBubble == null)
                    {
                        visualBubble.Service = service;
                        visualBubble.ID      = guid;
                        firstBubble          = visualBubble;
                    }
                }
            }

            return(firstBubble);
        }
Beispiel #19
0
 private static bool IsBubbleSending(VisualBubble bubble)
 {
     if (bubble is NewBubble)
     {
         return(false);
     }
     if (bubble.Status == Bubble.BubbleStatus.Waiting &&
         bubble.Direction == Bubble.BubbleDirection.Outgoing)
     {
         return(true);
     }
     return(false);
 }
Beispiel #20
0
        private static void FailBubbleIfPathDoesntExist(VisualBubble bubble)
        {
            var path = GetMediaFilePathIfPossible(bubble);

            if (String.IsNullOrWhiteSpace(path))
            {
                return;
            }
            if (!File.Exists(path))
            {
                throw new ServiceBubbleSendFailedException("Visual bubble media file path doesn't exist.");
            }
        }
Beispiel #21
0
        internal static UnifiedBubbleGroup CreateUnifiedInternal(List <BubbleGroup> groups, BubbleGroup primaryGroup, string id = null)
        {
            var service = ServiceManager.GetUnified();

            if (service == null)
            {
                return(null);
            }

            var newBubble = new NewBubble(Time.GetNowUnixTimestamp(), Bubble.BubbleDirection.Incoming,
                                          Guid.NewGuid() + "@unified", null, false, service);
            var unified = new UnifiedBubbleGroup(groups, primaryGroup, newBubble, id);

            if (id == null)
            {
                var unread = groups.FirstOrDefault(BubbleGroupSettingsManager.GetUnread) != null;
                BubbleGroupSettingsManager.SetUnread(unified, unread);
            }

            foreach (var group in groups)
            {
                @group.RegisterUnified(unified);
            }

            var associatedPartiallyLoadedGroups = groups.Where(x => x.PartiallyLoaded).ToList();

            if (associatedPartiallyLoadedGroups.Any())
            {
                VisualBubble latest = null;
                foreach (var innerGroup in unified.Groups)
                {
                    var current = innerGroup.Bubbles.Last();
                    if (latest == null || current.Time > latest.Time)
                    {
                        latest = current;
                    }
                }
                if (latest != null)
                {
                    unified.Bubbles.Clear();
                    unified.Bubbles.Add(latest);
                }
                return(unified);
            }

            Populate(unified);

            return(unified);
        }
 private static void RemoveNotQueued(VisualBubble bubble)
 {
     lock (_dbLock)
     {
         var bubbleId = bubble.ID;
         using (var db = new SqlDatabase <NotQueuedEntry>(NotQueuedLocation))
         {
             // don't add a duplicate group into the queue
             foreach (var possibleGroup in db.Store.Where(x => x.Guid == bubbleId))
             {
                 db.Remove(possibleGroup);
             }
         }
     }
 }
        public static string PatchPath(VisualBubble bubble)
        {
            string path    = null;
            string newBase = null;

            var imageBubble = bubble as ImageBubble;
            var videoBubble = bubble as VideoBubble;
            var audioBubble = bubble as AudioBubble;
            var fileBubble  = bubble as FileBubble;

            if (imageBubble != null)
            {
                path    = imageBubble.ImagePathNative;
                newBase = Platform.GetPicturesPath();
            }
            else if (videoBubble != null)
            {
                path    = videoBubble.VideoPathNative;
                newBase = Platform.GetVideosPath();
            }
            else if (audioBubble != null)
            {
                path    = audioBubble.AudioPathNative;
                newBase = Platform.GetAudioPath();
            }
            else if (fileBubble != null)
            {
                path    = fileBubble.PathNative;
                newBase = Platform.GetFilesPath();
            }

            if (path == null || newBase == null)
            {
                throw new Exception("Uknown bubble");
            }

            var indexes    = FindIndexesFromRear(path, Path.DirectorySeparatorChar);
            var seperators = indexes.Take(2).ToList();

            if (seperators.Count < 2)
            {
                return(path);
            }

            var end = path.Substring(seperators[1] + 1);

            return(Path.Combine(newBase, end));
        }
Beispiel #24
0
        public static bool UpdateQuotedMessageTitle(VisualBubble bubble, Action <string> onTitleUpdated)
        {
            var service = bubble.Service;

            if (!ServiceManager.IsRunning(service))
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(bubble.QuotedAddress))
            {
                return(false);
            }

            try
            {
                service.GetQuotedMessageTitle(bubble, result =>
                {
                    var bubbleGroup = BubbleGroupManager.FindWithAddress(bubble.Service, bubble.Address);
                    if (bubbleGroup != null)
                    {
                        var quotedTitles = bubbleGroup.QuotedTitles == null ?
                                           new List <DisaQuotedTitle>() : bubbleGroup.QuotedTitles.ToList();
                        var quotedTitle = quotedTitles.FirstOrDefault(x =>
                                                                      bubbleGroup.Service.BubbleGroupComparer(x.Address, bubble.QuotedAddress));
                        if (quotedTitle == null)
                        {
                            quotedTitles.Add(new DisaQuotedTitle
                            {
                                Address = bubble.QuotedAddress,
                                Title   = result,
                            });
                        }
                        else
                        {
                            quotedTitle.Title = result;
                        }
                        bubbleGroup.QuotedTitles = quotedTitles.ToArray();
                    }
                    onTitleUpdated(result);
                });
                return(true);
            }
            catch (Exception e)
            {
                Utils.DebugPrint("Error getting quoted message title " + e);
                return(false);
            }
        }
Beispiel #25
0
 public InsertBubble(VisualBubble visualBubble, bool shouldInsert)
 {
     _shouldInsert = shouldInsert;
     _visualBubble = visualBubble;
     if (_shouldInsert)
     {
         _entry = Add(_visualBubble);
         lock (Sending)
         {
             Sending.Add(_visualBubble);
         }
     }
     else
     {
         _entry = null;
     }
 }
 public InsertBubble(BubbleGroup group, VisualBubble visualBubble, bool shouldInsert)
 {
     _shouldInsert = shouldInsert;
     _visualBubble = visualBubble;
     if (_shouldInsert)
     {
         _entry = Add(group, _visualBubble);
         lock (Sending)
         {
             Sending.Add(_visualBubble);
         }
     }
     else
     {
         AddNotQueued(visualBubble, group);
         _entry = null;
     }
 }
Beispiel #27
0
        public static BubbleGroup Find(VisualBubble bubble)
        {
            lock (BubbleGroupsLock)
            {
                var unifiedGroups = BubbleGroups.OfType <UnifiedBubbleGroup>();
                foreach (var unifiedGroup in unifiedGroups)
                {
                    if (unifiedGroup.Bubbles.Any(unifiedGroupBubble => unifiedGroupBubble == bubble))
                    {
                        return(unifiedGroup);
                    }
                }

                return
                    (BubbleGroups.FirstOrDefault(
                         group => @group.Bubbles.FirstOrDefault(secondBubble => secondBubble == bubble) != null));
            }
        }
Beispiel #28
0
        private uint GetMessagePtsForTime(BubbleGroup group, long fromTime, out bool queryChat, out bool lastMessageIsExtendedParty)
        {
            VisualBubble bubbleToSyncFrom = null;
            bool         localQueryChat   = false;
            bool         localLastMessageIsExtendedParty = false;

            foreach (var bubble in BubbleGroupSync.ReadBubblesFromDatabase(group))
            {
                if (bubble is PartyInformationBubble)
                {
                    if ((bubble as PartyInformationBubble).Type == PartyInformationBubble.InformationType.UpgradedToExtendedParty)
                    {
                        localQueryChat = true;
                        if (bubble.Time <= fromTime)
                        {
                            localLastMessageIsExtendedParty = true;
                            bubbleToSyncFrom = bubble;
                            break;
                        }
                    }
                }
                if (bubble.Time <= fromTime)
                {
                    bubbleToSyncFrom = bubble;
                    break;
                }
            }
            if (bubbleToSyncFrom != null)
            {
                if (bubbleToSyncFrom.IdService == null)
                {
                    queryChat = localQueryChat;
                    lastMessageIsExtendedParty = localLastMessageIsExtendedParty;
                    return(1);
                }
                queryChat = localQueryChat;
                lastMessageIsExtendedParty = localLastMessageIsExtendedParty;
                return(uint.Parse(bubbleToSyncFrom.IdService));
            }
            queryChat = localQueryChat;
            lastMessageIsExtendedParty = localLastMessageIsExtendedParty;
            return(1);
        }
Beispiel #29
0
        public static void AddBubble(BubbleGroup group, VisualBubble b)
        {
            lock (OperationLock)
            {
                using (var ms = new MemoryStream())
                {
                    Serializer.Serialize(ms, b);

                    var bubbleData   = ms.ToArray();
                    var bubbleHeader = b.GetType().Name + ":" + b.ID + ":" + b.Time;

                    var file = GetLocation(@group);

                    using (var stream = File.Open(file, FileMode.Append, FileAccess.Write))
                    {
                        BubbleGroupDatabasePrimitives.WriteBubbleData(stream, bubbleData);
                        BubbleGroupDatabasePrimitives.WriteBubbleHeader(stream, bubbleHeader);
                    }
                }
            }
        }
Beispiel #30
0
        private static BubbleGroup AddNewInternal(VisualBubble newBubble, bool raiseBubbleInserted)
        {
            var group = new BubbleGroup(newBubble, null, false);

            BubbleGroupSettingsManager.SetUnreadIndicatorGuid(group, group.LastBubbleSafe().ID, true);

            if (ServiceManager.IsRunning(@group.Service))
            {
                newBubble.Service.NewBubbleGroupCreated(@group).ContinueWith(x =>
                {
                    // force the UI to refetch the photo
                    @group.IsPhotoSetFromService = false;
                    BubbleManager.SendSubscribe(@group, true);
                    BubbleGroupUpdater.Update(@group);
                });
            }

            BubbleGroupManager.BubbleGroupsAdd(@group);

            BubbleGroupDatabase.AddBubble(@group, newBubble);

            if (raiseBubbleInserted)
            {
                try
                {
                    BubbleGroupEvents.RaiseBubbleInserted(newBubble, @group);
                }
                catch (Exception ex)
                {
                    Utils.DebugPrint(
                        "Error in notifying the interface that the bubble group has been updated (" +
                        newBubble.Service.Information.ServiceName + "): " + ex.Message);
                }
            }

            BubbleGroupUpdater.Update(@group);

            return(@group);
        }