Ejemplo n.º 1
0
    async Task <InboxMessageBox> ReloadInternalAsync(bool all, IEnumerable <string>?targetChannelIDs, CancellationToken cancel)
    {
        List <InboxMessage> msgList = new List <InboxMessage>();

        // Team info
        this.TeamInfo = await Api !.GetTeamInfoAsync(cancel);

        currentAccountInfoStr = this.TeamInfo.name;

        if (all)
        {
            // Enum users
            this.UserList = await Api.GetUsersListAsync(cancel);

            // Clear cache
            this.MessageListPerConversation.Clear();

            // Muted channels list
            this.MutedChannelList = await Api.GetMutedChannels(cancel);
        }

        // Enum conversations
        this.ConversationList = await Api.GetConversationsListAsync(cancel);

        if (LastSnapshotDay != DateTime.Now.Day)
        {
            LastSnapshotDay = DateTime.Now.Day;

            SlackSnapshot snapshot = new SlackSnapshot
            {
                TeamInfo         = this.TeamInfo,
                UserList         = this.UserList,
                ConversationList = this.ConversationList,
                MutedChannelList = this.MutedChannelList,
            };

            snapshot._PostData("slack_snapshot");
        }

        // Enum messages
        foreach (var conv in ConversationList)
        {
            if (conv.id._IsFilled())
            {
                bool reload = false;

                if (conv.IsTarget())
                {
                    if (all)
                    {
                        reload = true;
                    }
                    else
                    {
                        if (targetChannelIDs !.Contains(conv.id, StrComparer.IgnoreCaseComparer))
                        {
                            reload = true;
                        }
                    }

                    if (MutedChannelList != null && MutedChannelList.Where(x => x._IsSamei(conv.id)).Any())
                    {
                        reload = false;
                    }
                }

                if (reload)
                {
                    // Get the conversation info
                    SlackApi.Channel convInfo = await Api.GetConversationInfoAsync(conv.id, cancel);

                    // Get unread messages
                    SlackApi.Message[] messages = await Api.GetConversationHistoryAsync(conv.id, convInfo.last_read, this.Inbox.Options.MaxMessagesPerAdapter, cancel : cancel);

                    MessageListPerConversation[conv.id] = messages;
                }

                if (conv.IsTarget())
                {
                    if (MessageListPerConversation.TryGetValue(conv.id, out SlackApi.Message[]? messages))
                    {
                        foreach (SlackApi.Message message in messages)
                        {
                            var user = GetUser(message.user);

                            string group_name = "";

                            if (conv.is_channel)
                            {
                                group_name = "#" + conv.name_normalized;
                            }
                            else if (conv.is_im)
                            {
                                group_name = "@" + GetUser(conv.user)?.profile?.real_name ?? "unknown";
                            }
                            else
                            {
                                group_name = "@" + conv.name_normalized;
                            }

                            InboxMessage m = new InboxMessage
                            {
                                Id = this.Guid + "_" + message.ts.ToString(),

                                Service   = TeamInfo.name._DecodeHtml(),
                                FromImage = TeamInfo.icon?.image_132 ?? "",

                                From         = (user?.profile?.real_name ?? "Unknown User")._DecodeHtml(),
                                ServiceImage = user?.profile?.image_512 ?? "",

                                Group = group_name._DecodeHtml(),

                                Body      = message.text._DecodeHtml(),
                                Timestamp = message.ts._ToDateTimeOfSlack(),
                            };

                            m.Subject = this.TeamInfo.name._DecodeHtml();

                            m.Body = m.Body._SlackExpandBodyUsername(this.UserList);

                            if (message.upload)
                            {
                                m.Body += $"Filename: '{message.files!.FirstOrDefault()?.name ?? "Unknown Filename"}'";
                            }

                            msgList.Add(m);
                        }
                    }
                }
            }
        }

        InboxMessageBox ret = new InboxMessageBox(false)
        {
            MessageList = msgList.OrderByDescending(x => x.Timestamp).Take(this.Inbox.Options.MaxMessagesPerAdapter).ToArray(),
        };

        int numUnread = ret.MessageList.Length;

        if (this.LastUnread != numUnread)
        {
            this.LastUnread = numUnread;

            SlackStatus st = new SlackStatus
            {
                NumUnreadMessages = numUnread,
                TeamInfo          = this.TeamInfo,
            };

            st._PostData("slack_status");
        }

        ClearLastError();

        if (numReload == 0)
        {
            ret.IsFirst = true;
        }

        numReload++;

        return(ret);
    }
 public SlackNotificationEvent(string username, string domain, string action, SlackStatus status)
     : base(username, domain, action)
 {
     SlackStatus = status.ToString().ToLower();
 }