コード例 #1
0
    public async Task <PKGroup> UpdateGroup(GroupId id, GroupPatch patch, IPKConnection?conn = null)
    {
        _logger.Information("Updated {GroupId}: {@GroupPatch}", id, patch);
        var query = patch.Apply(new Query("groups").Where("id", id));
        var group = await _db.QueryFirst <PKGroup>(conn, query, "returning *");

        if (conn == null)
        {
            _ = _dispatch.Dispatch(id,
                                   new UpdateDispatchData {
                Event = DispatchEvent.UPDATE_GROUP, EventData = patch.ToJson()
            });
        }
        return(group);
    }
コード例 #2
0
    public async Task CreateGroup(Context ctx)
    {
        ctx.CheckSystem();

        // Check group name length
        var groupName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a group name.");

        if (groupName.Length > Limits.MaxGroupNameLength)
        {
            throw new PKError($"Group name too long ({groupName.Length}/{Limits.MaxGroupNameLength} characters).");
        }

        // Check group cap
        var existingGroupCount = await ctx.Repository.GetSystemGroupCount(ctx.System.Id);

        var groupLimit = ctx.Config.GroupLimitOverride ?? Limits.MaxGroupCount;

        if (existingGroupCount >= groupLimit)
        {
            throw new PKError(
                      $"System has reached the maximum number of groups ({groupLimit}). Please delete unused groups first in order to create new ones.");
        }

        // Warn if there's already a group by this name
        var existingGroup = await ctx.Repository.GetGroupByName(ctx.System.Id, groupName);

        if (existingGroup != null)
        {
            var msg =
                $"{Emojis.Warn} You already have a group in your system with the name \"{existingGroup.Name}\" (with ID `{existingGroup.Hid}`). Do you want to create another group with the same name?";
            if (!await ctx.PromptYesNo(msg, "Create"))
            {
                throw new PKError("Group creation cancelled.");
            }
        }

        // todo: this is supposed to be a transaction, but it's not used in any useful way
        // consider removing it?
        using var conn = await ctx.Database.Obtain();

        var newGroup = await ctx.Repository.CreateGroup(ctx.System.Id, groupName);

        var dispatchData = new JObject();

        dispatchData.Add("name", groupName);

        if (ctx.Config.GroupDefaultPrivate)
        {
            var patch = new GroupPatch().WithAllPrivacy(PrivacyLevel.Private);
            await ctx.Repository.UpdateGroup(newGroup.Id, patch, conn);

            dispatchData.Merge(patch.ToJson());
        }

        _ = _dispatch.Dispatch(newGroup.Id, new UpdateDispatchData
        {
            Event     = DispatchEvent.CREATE_GROUP,
            EventData = dispatchData
        });

        var reference = newGroup.Reference(ctx);

        var eb = new EmbedBuilder()
                 .Description(
            $"Your new group, **{groupName}**, has been created, with the group ID **`{newGroup.Hid}`**.\nBelow are a couple of useful commands:")
                 .Field(new Embed.Field("View the group card", $"> pk;group **{reference}**"))
                 .Field(new Embed.Field("Add members to the group",
                                        $"> pk;group **{reference}** add **MemberName**\n> pk;group **{reference}** add **Member1** **Member2** **Member3** (and so on...)"))
                 .Field(new Embed.Field("Set the description",
                                        $"> pk;group **{reference}** description **This is my new group, and here is the description!**"))
                 .Field(new Embed.Field("Set the group icon",
                                        $"> pk;group **{reference}** icon\n*(with an image attached)*"));
        await ctx.Reply($"{Emojis.Success} Group created!", eb.Build());

        if (existingGroupCount >= Limits.WarnThreshold(groupLimit))
        {
            await ctx.Reply(
                $"{Emojis.Warn} You are approaching the per-system group limit ({existingGroupCount} / {groupLimit} groups). Please review your group list for unused or duplicate groups.");
        }
    }