Example #1
0
        private async Task CreateChannelCSEntryChanges(string groupid, SchemaType schemaType)
        {
            if (!this.context.Types.Types.Contains("channel"))
            {
                return;
            }

            var channels = await GraphHelperTeams.GetChannels(this.betaClient, groupid, this.token);

            foreach (var channel in channels)
            {
                var members = await GraphHelperTeams.GetChannelMembers(this.betaClient, groupid, channel.Id, this.token);

                CSEntryChange c = CSEntryChange.Create();
                c.ObjectType             = "channel";
                c.ObjectModificationType = ObjectModificationType.Add;
                c.AnchorAttributes.Add(AnchorAttribute.Create("id", channel.Id));
                c.DN = channel.Id;

                c.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("displayName", channel.DisplayName));

                if (!string.IsNullOrWhiteSpace(channel.Description))
                {
                    c.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("description", channel.Description));
                }

                if (members.Count > 0)
                {
                    c.AttributeChanges.Add(AttributeChange.CreateAttributeAdd("member", members.Select(t => t.Id).ToList <object>()));
                }

                this.context.ImportItems.Add(c, this.token);
            }
        }
        private async Task CreateChannelCSEntryChanges(string groupid)
        {
            if (!this.context.Types.Types.Contains("publicChannel") && !this.context.Types.Types.Contains("privateChannel"))
            {
                return;
            }

            var channels = await GraphHelperTeams.GetChannels(this.betaClient, groupid, this.token);

            Regex regex = null;

            if (!string.IsNullOrWhiteSpace(this.context.ConfigParameters[ConfigParameterNames.ChannelNameFilter].Value))
            {
                regex = new Regex(this.context.ConfigParameters[ConfigParameterNames.ChannelNameFilter].Value, RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
            }

            foreach (var channel in channels)
            {
                string objectType;

                if (channel.MembershipType.HasValue)
                {
                    if (channel.MembershipType == Beta.ChannelMembershipType.UnknownFutureValue)
                    {
                        logger.Warn($"Ignoring unknown channel {channel.Id} of unknown type");
                        continue;
                    }
                    else
                    {
                        objectType = channel.MembershipType == Beta.ChannelMembershipType.Standard ? "publicChannel" : "privateChannel";
                    }
                }
                else
                {
                    objectType = "publicChannel";
                }

                if (!this.context.Types.Types.Contains(objectType))
                {
                    continue;
                }

                if (regex != null)
                {
                    if (!regex.IsMatch(channel.DisplayName))
                    {
                        logger.Trace($"Dropping channel {channel.Id} with display name {channel.DisplayName} as it did not match the regular expression filter");
                        continue;
                    }
                }

                CSEntryChange c = CSEntryChange.Create();

                c.ObjectModificationType = ObjectModificationType.Add;
                c.AnchorAttributes.Add(AnchorAttribute.Create("id", channel.Id));
                c.AnchorAttributes.Add(AnchorAttribute.Create("teamid", groupid));
                c.DN         = channel.Id;
                c.ObjectType = objectType;
                c.CreateAttributeAdd("team", groupid);

                SchemaType schemaType = this.context.Types.Types[objectType];

                if (schemaType.HasAttribute("displayName"))
                {
                    c.CreateAttributeAdd("displayName", channel.DisplayName);
                }

                if (schemaType.HasAttribute("description") && !string.IsNullOrWhiteSpace(channel.Description))
                {
                    c.CreateAttributeAdd("description", channel.Description);
                }

                if (schemaType.HasAttribute("email") && !string.IsNullOrWhiteSpace(channel.Email))
                {
                    c.CreateAttributeAdd("email", channel.Email);
                }

                if (schemaType.HasAttribute("webUrl") && !string.IsNullOrWhiteSpace(channel.WebUrl))
                {
                    c.CreateAttributeAdd("webUrl", channel.WebUrl);
                }

                if (schemaType.HasAttribute("isFavoriteByDefault"))
                {
                    c.CreateAttributeAdd("isFavoriteByDefault", channel.IsFavoriteByDefault ?? false);
                }

                if (channel.MembershipType == Beta.ChannelMembershipType.Private && (schemaType.HasAttribute("member") || schemaType.HasAttribute("owner")))
                {
                    List <Beta.AadUserConversationMember> members = await GraphHelperTeams.GetChannelMembers(this.betaClient, groupid, channel.Id, this.token);

                    if (members.Count > 0)
                    {
                        List <object> memberList = new List <object>();
                        List <object> ownerList  = new List <object>();

                        foreach (Beta.ConversationMember member in members)
                        {
                            string memberValue;

                            if (member is Beta.AadUserConversationMember conMember)
                            {
                                memberValue = conMember.UserId;
                            }
                            else
                            {
                                if (member.AdditionalData == null)
                                {
                                    logger.Warn("Member has no additional data and therefore no userId\r\n" + JsonConvert.SerializeObject(member));
                                    continue;
                                }

                                if (!member.AdditionalData.ContainsKey("userId"))
                                {
                                    logger.Warn("Member does not have userId\r\n" + JsonConvert.SerializeObject(member));
                                    continue;
                                }

                                memberValue = member.AdditionalData["userId"] as string;
                            }

                            if (memberValue == null)
                            {
                                logger.Warn("Member userId was null\r\n" + JsonConvert.SerializeObject(member));
                                continue;
                            }

                            if (this.userFilter.ShouldExclude(memberValue, this.token))
                            {
                                continue;
                            }

                            if (member.Roles.Contains("owner", StringComparer.OrdinalIgnoreCase))
                            {
                                ownerList.Add(memberValue);
                            }
                            else
                            {
                                memberList.Add(memberValue);
                            }
                        }

                        if (memberList.Count > 0 && schemaType.HasAttribute("member"))
                        {
                            c.CreateAttributeAdd("member", memberList);
                        }

                        if (ownerList.Count > 0 && schemaType.HasAttribute("owner"))
                        {
                            c.CreateAttributeAdd("owner", ownerList);
                        }
                    }
                }

                this.context.ImportItems.Add(c, this.token);
            }
        }