Ejemplo n.º 1
0
        private static async Task RecordVotes(IBinder binder, DialogSubmissionPayload payload)
        {
            var votesTable = await binder.GetTable("votes");

            var plansTable = await binder.GetTable("plans");

            var vote = await votesTable.Retrieve <Vote>(Utils.GetPartitionKeyWithAddon(payload.PartitionKey, payload.State), payload.User.Id)
                       ?? new Vote(payload.Team.Id, payload.Channel.Id, payload.State, payload.User.Id);

            vote.Proposal1 = payload.Submission["proposal1"];
            vote.Proposal2 = payload.Submission["proposal2"];
            vote.Proposal3 = payload.Submission["proposal3"];

            var result = await votesTable.ExecuteAsync(TableOperation.InsertOrReplace(vote));

            if (result.IsError())
            {
                await MessageHelpers.PostErrorMessage(binder, payload);

                return;
            }

            // TODO: Afficher les résultats en temps réel?

            var plan = await plansTable.Retrieve <Plan>(payload.PartitionKey, payload.State);

            var message = new PostEphemeralRequest {
                User        = payload.User.Id,
                Channel     = payload.Channel.Id,
                Text        = $"Merci! Vos votes pour le vidéo du {plan.Date:d MMMM} ont bien été reçus.",
                Attachments = { MessageHelpers.GetRemoveMessageAttachment() }
            };

            await SlackHelper.PostEphemeral(binder, payload.Team.Id, message);
        }
Ejemplo n.º 2
0
        private static async Task <IActionResult> DeleteProposal(IBinder binder, InteractiveMessagePayload payload, Proposal proposal)
        {
            var proposals = await binder.GetTable("proposals");

            if (proposal != null)
            {
                // Bloquer la suppression d'une proposition plannifiée
                if (!string.IsNullOrWhiteSpace(proposal.PlannedIn))
                {
                    var plan = await binder.GetTableRow <Plan>("plans", payload.PartitionKey, proposal.PlannedIn);

                    if (plan != null)
                    {
                        return(Utils.Ok(new SlackMessage {
                            Text = $"Ce vidéo est déjà planifié pour le {plan.Date:dddd d MMMM}.",
                            Attachments = { MessageHelpers.GetRemoveMessageAttachment() }
                        }));
                    }
                }

                var result = await proposals.ExecuteAsync(TableOperation.Delete(proposal));

                if (result.IsError())
                {
                    await MessageHelpers.PostErrorMessage(binder, payload);

                    return(Utils.Ok());
                }

                // Notifier le owner si c'est pas lui qui supprime sa proposition
                if (proposal.ProposedBy != payload.User.Id)
                {
                    await SlackHelper.PostMessage(binder, payload.Team.Id, new PostMessageRequest {
                        Channel     = proposal.ProposedBy,
                        Text        = $"<@{payload.User.Id}> vient de supprimer votre proposition de vidéo dans <#{payload.Channel.Id}>:",
                        Attachments = { await MessageHelpers.GetProposalAttachment(binder, proposal, allowActions: false) }
                    });
                }
            }

            // NOTE: Présentement la seule place qu'on peut supprimer une proposition c'est à partir de la liste de toutes les propositions.
            // Si ça change, va falloir ajouter plus de logique ici pour recréer le bon type de message.
            var allProposals = await ProposalHelpers.GetActiveProposals(proposals, payload.PartitionKey);

            var message = await MessageHelpers.GetListMessage(binder, allProposals, payload.Channel.Id);

            message.ReplaceOriginal = true;
            return(Utils.Ok(message));
        }
Ejemplo n.º 3
0
        private static async Task <IActionResult> ProcessPlanAction(IBinder binder, InteractiveMessagePayload payload)
        {
            var plans = await binder.GetTable("plans");

            var action = payload.Actions.First();
            var plan   = await plans.Retrieve <Plan>(payload.PartitionKey, action.Value);

            if (plan == null)
            {
                return(Utils.Ok(new SlackMessage {
                    Text = "Oups! Ce Lunch & Watch ne semble plus exister.",
                    Attachments = { MessageHelpers.GetRemoveMessageAttachment() }
                }));
            }

            if (action.Name == "volunteer")
            {
                if (!string.IsNullOrWhiteSpace(plan.Owner))
                {
                    var message = new PostEphemeralRequest {
                        User        = payload.User.Id,
                        Channel     = payload.Channel.Id,
                        Text        = $"<@{plan.Owner}> est déjà responsable de ce Lunch & Watch.",
                        Attachments = new List <MessageAttachment> {
                            MessageHelpers.GetRemoveMessageAttachment()
                        }
                    };
                    await SlackHelper.PostEphemeral(binder, payload.Team.Id, message);

                    return(await UpdatePlanMessage(binder, payload, plan, ""));
                }

                plan.Owner = payload.User.Id;

                var result = await plans.ExecuteAsync(TableOperation.Replace(plan));

                if (result.IsError())
                {
                    await MessageHelpers.PostErrorMessage(binder, payload);

                    return(await UpdatePlanMessage(binder, payload, plan, ""));
                }

                if (plan.Date.Date == DateTime.Today && DateTime.Now.TimeOfDay >= TimeSpan.Parse("11:30"))
                {
                    var message = await MessageHelpers.GetPrepareVideoReminder(binder, plan);

                    await SlackHelper.PostMessage(binder, plan.Team, message);
                }

                return(await UpdatePlanMessage(binder, payload, plan, "Merci!"));
            }

            if (action.Name == "vote")
            {
                if (!string.IsNullOrWhiteSpace(plan.Video))
                {
                    return(Utils.Ok(new SlackMessage {
                        Text = "Le vidéo a déjà été sélectionné pour ce Lunch & Watch.",
                        Attachments = { MessageHelpers.GetRemoveMessageAttachment() }
                    }));
                }

                try
                {
                    var dialogRequest = new OpenDialogRequest {
                        TriggerId = payload.TriggerId,
                        Dialog    = await DialogHelpers.GetVoteDialog(binder, payload.PartitionKey, action.Value, payload.User.Id)
                    };

                    await SlackHelper.OpenDialog(binder, payload.Team.Id, dialogRequest);
                }
                catch (NoAvailableVideosException)
                {
                    return(Utils.Ok(new SlackMessage {
                        Text = "Oups! Aucun vidéo n'a été proposé. Proposez un vidéo avec /edu:propose.",
                        Attachments = { MessageHelpers.GetRemoveMessageAttachment() }
                    }));
                }
            }

            return(Utils.Ok());
        }
Ejemplo n.º 4
0
        private static async Task RecordPlan(IBinder binder, DialogSubmissionPayload planPayload)
        {
            var plans = await binder.GetTable("plans");

            var proposals = await binder.GetTable("proposals");

            var videoKey = planPayload.GetValue("video");

            if (!string.IsNullOrWhiteSpace(videoKey))
            {
                var proposal = await proposals.Retrieve <Proposal>(planPayload.State, videoKey);

                if (proposal == null)
                {
                    await MessageHelpers.PostErrorMessage(binder, planPayload, "Vidéo non trouvé");

                    return;
                }

                if (!string.IsNullOrWhiteSpace(proposal.PlannedIn))
                {
                    var otherPlan = await plans.Retrieve <Plan>(planPayload.State, proposal.PlannedIn);

                    if (otherPlan != null)
                    {
                        await MessageHelpers.PostErrorMessage(binder, planPayload, $"Ce vidéo est déjà planifié pour le {otherPlan.Date:dddd d MMMM}.");

                        return;
                    }
                }

                proposal.PlannedIn = planPayload.ActionTimestamp;

                var proposalResult = await proposals.ExecuteAsync(TableOperation.Replace(proposal));

                if (proposalResult.IsError())
                {
                    await MessageHelpers.PostErrorMessage(binder, planPayload);

                    return;
                }
            }

            var plan = new Plan {
                PartitionKey = planPayload.State,
                RowKey       = planPayload.ActionTimestamp,
                CreatedBy    = planPayload.User.Id,
                Team         = planPayload.Team.Id,
                Channel      = Utils.GetChannelFromPartitionKey(planPayload.State),
                Date         = Utils.ParseDate(planPayload.GetValue("date")).AddHours(12),
                Owner        = planPayload.GetValue("owner") ?? "",
                Video        = videoKey ?? ""
            };

            var result = await plans.ExecuteAsync(TableOperation.Insert(plan));

            if (result.IsError())
            {
                await MessageHelpers.PostErrorMessage(binder, planPayload);

                return;
            }

            var message = new PostMessageRequest {
                Text        = $"<@{planPayload.User.Id}> vient de planifier un Lunch & Watch :",
                Channel     = Utils.GetChannelFromPartitionKey(planPayload.State),
                Attachments = { await MessageHelpers.GetPlanAttachment(binder, plan) }
            };

            await SlackHelper.PostMessage(binder, planPayload.Team.Id, message);

            await binder.RecordChannelActivity(planPayload.Team.Id, planPayload.Channel.Id);
        }