Skip to content

Commit

Permalink
Merge pull request #188 from kounoike:improvement/manual-record-sequence
Browse files Browse the repository at this point in the history
メッセージからの手動録画のシーケンス書き直し
  • Loading branch information
kounoike authored May 2, 2023
2 parents 0306228 + a9f73df commit ef626d4
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 105 deletions.
11 changes: 11 additions & 0 deletions .devcontainer/devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& /bin/bash /tmp/common-debian.sh "${INSTALL_ZSH}" "${USERNAME}" "${USER_UID}" "${USER_GID}" "${UPGRADE_PACKAGES}" \
&& apt-get install -y --no-install-recommends \
ca-certificates \
intel-media-va-driver-non-free \
libva-dev \
libmfx-dev \
mariadb-client \
nodejs \
python-is-python3 \
python3-pip \
vainfo \
wget \
xz-utils \
&& apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/common-debian.sh

RUN wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n6.0-latest-linux64-gpl-6.0.tar.xz && \
tar Jxf ffmpeg-n6.0-latest-linux64-gpl-6.0.tar.xz -C /usr/local --strip-component 1 && \
rm ffmpeg-n6.0-latest-linux64-gpl-6.0.tar.xz


RUN npm install -g yarn

RUN wget https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n6.0-latest-linux64-gpl-6.0.tar.xz && \
Expand Down
1 change: 1 addition & 0 deletions discord/emoji.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const (
RecordedReactionEmoji = "📼"
EncodedReactionEmoji = "🗜️"
OkReactionEmoji = "🆗"
NgReactionEmoji = "🆖"
NotifyReactionEmoji = "👀"
AutoSearchReactionEmoji = "🔍"
TranscriptionReactionEmoji = "📝"
Expand Down
123 changes: 123 additions & 0 deletions dtv/check_recording_schedule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package dtv

import (
"context"
"database/sql"
"strings"

"github.com/kounoike/dtv-discord-go/db"
"github.com/kounoike/dtv-discord-go/discord"
"github.com/pkg/errors"
"go.uber.org/zap"
)

func (dtv *DTVUsecase) scheduleRecord(ctx context.Context, programMessage db.ProgramMessage) error {
// 録画しよう!
program, err := dtv.queries.GetProgram(ctx, programMessage.ProgramID)
if err != nil {
return err
}
service, err := dtv.queries.GetServiceByProgramID(ctx, programMessage.ProgramID)
if err != nil {
return err
}

contentPath, err := dtv.getContentPath(ctx, program, service)
if err != nil {
return err
}

if err := dtv.queries.InsertProgramRecording(ctx, db.InsertProgramRecordingParams{ProgramID: programMessage.ProgramID, ContentPath: contentPath}); err != nil {
return err
}

if err = dtv.mirakc.AddRecordingSchedule(programMessage.ProgramID, contentPath); err != nil {
return err
}

dtv.logger.Debug("録画予約 OK", zap.Int64("ProgramID", programMessage.ProgramID), zap.String("contentPath", contentPath))
if err := dtv.discord.MessageReactionAdd(programMessage.ChannelID, programMessage.MessageID, discord.OkReactionEmoji); err != nil {
return err
}

return nil
}

func (dtv *DTVUsecase) cancelRecordSchedule(ctx context.Context, programMessage db.ProgramMessage) error {
if err := dtv.queries.DeleteProgramRecordingByProgramId(ctx, programMessage.ProgramID); err != nil {
return err
}

if err := dtv.mirakc.DeleteRecordingSchedule(programMessage.ProgramID); err != nil {
return err
}

dtv.logger.Debug("録画予約キャンセル OK", zap.Int64("ProgramID", programMessage.ProgramID))
if err := dtv.discord.MessageReactionRemove(programMessage.ChannelID, programMessage.MessageID, discord.OkReactionEmoji); err != nil {
return err
}

return nil
}

func (dtv *DTVUsecase) checkRecordScheduleForMessage(ctx context.Context, channelID string, messageID string) error {
programMessage, err := dtv.queries.GetProgramMessageByMessageID(ctx, messageID)
if errors.Cause(err) == sql.ErrNoRows {
// NOTE: 番組情報以外の発言の場合は無視する
return nil
}
if err != nil {
return err
}

ngEmojis, err := dtv.discord.GetMessageReactions(channelID, messageID, discord.NgReactionEmoji)
if err != nil {
return err
}
if len(ngEmojis) > 0 {
return dtv.cancelRecordSchedule(ctx, programMessage)
}

recordingEmojis, err := dtv.discord.GetMessageReactions(channelID, messageID, discord.RecordingReactionEmoji)
if err != nil {
return err
}
if len(recordingEmojis) > 0 {
for _, recordingEmoji := range recordingEmojis {
if recordingEmoji.ID == dtv.discord.Session().State.User.ID {
// NOTE: 自分のリアクションなので無視
continue
}
return dtv.scheduleRecord(ctx, programMessage)
}
}

asEmojis, err := dtv.discord.GetMessageReactions(channelID, messageID, discord.AutoSearchReactionEmoji)
if err != nil {
return err
}
if len(asEmojis) > 0 {
for _, asEmoji := range asEmojis {
if asEmoji.ID == dtv.discord.Session().State.User.ID {
return dtv.scheduleRecord(ctx, programMessage)
}
}
}

return dtv.cancelRecordSchedule(ctx, programMessage)
}

func toSafePath(s string) string {
safePathReplacer := strings.NewReplacer(
"/", "/",
":", ":",
"*", "*",
"\\", "¥",
"?", "?",
"\"", "‟",
"<", "<",
">", ">",
"|", "|",
)
return safePathReplacer.Replace(s)
}
2 changes: 1 addition & 1 deletion dtv/on_program_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (dtv *DTVUsecase) sendAutoSearchMatchMessage(ctx context.Context, msg *disc
return nil
}
}
err = dtv.scheduleRecordForMessage(ctx, msg.ChannelID, msg.ID)
err = dtv.checkRecordScheduleForMessage(ctx, msg.ChannelID, msg.ID)
if err != nil {
return err
}
Expand Down
76 changes: 1 addition & 75 deletions dtv/on_recording_emoji_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,14 @@ package dtv

import (
"context"
"database/sql"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/kounoike/dtv-discord-go/db"
"github.com/kounoike/dtv-discord-go/discord"
"github.com/pkg/errors"
"go.uber.org/zap"
)

func (dtv *DTVUsecase) scheduleRecordForMessage(ctx context.Context, channelID string, messageID string) error {
programMessage, err := dtv.queries.GetProgramMessageByMessageID(ctx, messageID)
if errors.Cause(err) == sql.ErrNoRows {
// NOTE: 番組情報以外の発言の場合は無視する
return nil
}
if err != nil {
return err
}

_, err = dtv.queries.GetProgramRecordingByProgramId(ctx, programMessage.ProgramID)
if err != nil && errors.Cause(err) != sql.ErrNoRows {
return err
}
if err == nil {
// すでに録画がスケジュールされているので何もしない
return nil
}

// 録画しよう!
program, err := dtv.queries.GetProgram(ctx, programMessage.ProgramID)
if err != nil {
return err
}
service, err := dtv.queries.GetServiceByProgramID(ctx, programMessage.ProgramID)
if err != nil {
return err
}

contentPath, err := dtv.getContentPath(ctx, program, service)
if err != nil {
return err
}

err = dtv.queries.InsertProgramRecording(ctx, db.InsertProgramRecordingParams{ProgramID: programMessage.ProgramID, ContentPath: contentPath})
if err != nil {
return err
}

err = dtv.mirakc.AddRecordingSchedule(programMessage.ProgramID, contentPath)
if err != nil {
return err
}

dtv.logger.Debug("録画予約 OK", zap.Int64("ProgramID", programMessage.ProgramID), zap.String("contentPath", contentPath))
err = dtv.discord.MessageReactionAdd(channelID, messageID, discord.OkReactionEmoji)
if err != nil {
return err
}

return nil
}

func (dtv *DTVUsecase) OnRecordingEmojiAdd(ctx context.Context, reaction *discordgo.MessageReactionAdd) error {
if reaction.UserID == dtv.discord.Session().State.User.ID {
// NOTE: 自分のリアクションなので無視
return nil
}
return dtv.scheduleRecordForMessage(ctx, reaction.ChannelID, reaction.MessageID)
}

func toSafePath(s string) string {
safePathReplacer := strings.NewReplacer(
"/", "/",
":", ":",
"*", "*",
"\\", "¥",
"?", "?",
"\"", "‟",
"<", "<",
">", ">",
"|", "|",
)
return safePathReplacer.Replace(s)
return dtv.checkRecordScheduleForMessage(ctx, reaction.ChannelID, reaction.MessageID)
}
30 changes: 1 addition & 29 deletions dtv/on_recording_emoji_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,8 @@ import (
"context"

"github.com/bwmarrin/discordgo"
"github.com/kounoike/dtv-discord-go/discord"
"go.uber.org/zap"
)

func (dtv *DTVUsecase) OnRecordingEmojiRemove(ctx context.Context, reaction *discordgo.MessageReactionRemove) error {
users, err := dtv.discord.GetMessageReactions(reaction.ChannelID, reaction.MessageID, discord.RecordingReactionEmoji)
if err != nil {
return err
}
if len(users) == 1 && users[0].ID == dtv.discord.Session().State.User.ID {
programMessage, err := dtv.queries.GetProgramMessageByMessageID(ctx, reaction.MessageID)
if err != nil {
return err
}
err = dtv.mirakc.DeleteRecordingSchedule(programMessage.ProgramID)
if err != nil {
return err
}
err = dtv.discord.MessageReactionRemove(reaction.ChannelID, reaction.MessageID, discord.OkReactionEmoji)
if err != nil {
return err
}
err = dtv.queries.DeleteProgramRecordingByProgramId(ctx, programMessage.ProgramID)
if err != nil {
return err
}
dtv.logger.Debug("録画取り消し完了", zap.String("MessageID", reaction.MessageID), zap.Int64("ProgramID", programMessage.ProgramID))
} else {
dtv.logger.Debug("他のユーザの予約が残っています", zap.String("MessageID", reaction.MessageID))
}

return nil
return dtv.checkRecordScheduleForMessage(ctx, reaction.ChannelID, reaction.MessageID)
}

0 comments on commit ef626d4

Please sign in to comment.