Skip to content

Commit

Permalink
feat: render userclear messages on ban (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmo314 authored Dec 19, 2022
1 parent b7b1cb8 commit 693d15a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
25 changes: 16 additions & 9 deletions agent/internal/handler/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (h *TwitchHandler) bindOutboundClient(ctx context.Context) error {
for _, change := range snapshot.Changes {
if change.Kind == firestore.DocumentAdded {
var action struct {
ChannelId string
ChannelId string
Message string
TargetChannel string
ReplyMessageId string
Expand Down Expand Up @@ -328,17 +328,24 @@ func (h *TwitchHandler) bindEvents(client *twitch.Client) {

// configure handlers
client.OnClearChatMessage(func(message twitch.ClearChatMessage) {
if message.TargetUserID != "" || message.TargetUsername != "" {
return
}
// handle clear chat message
channelID := fmt.Sprintf("twitch:%s", message.RoomID)
data := map[string]interface{}{
"channelId": channelID,
"timestamp": message.Time,
"type": "clear",
if message.TargetUserID != "" || message.TargetUsername != "" {
data := map[string]interface{}{
"channelId": channelID,
"targetUserId": message.TargetUserID,
"timestamp": message.Time,
"type": "userclear",
}
go h.write(channelID, fmt.Sprintf("twitch:userclear-%s", message.Time.Format(time.RFC3339)), data)
} else {
data := map[string]interface{}{
"channelId": channelID,
"timestamp": message.Time,
"type": "clear",
}
go h.write(channelID, fmt.Sprintf("twitch:clear-%s", message.Time.Format(time.RFC3339)), data)
}
go h.write(channelID, fmt.Sprintf("twitch:clear-%s", message.Time.Format(time.RFC3339)), data)
})

client.OnClearMessage(func(message twitch.ClearMessage) {
Expand Down
26 changes: 15 additions & 11 deletions lib/models/adapters/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class UpdateDeltaEvent extends DeltaEvent {
: super(timestamp);
}

class UpdateAllDeltaEvent extends DeltaEvent {
final MessageModel Function(MessageModel) update;

const UpdateAllDeltaEvent(DateTime timestamp, this.update) : super(timestamp);
}

class ClearDeltaEvent extends DeltaEvent {
final String messageId;

Expand Down Expand Up @@ -105,17 +111,7 @@ DeltaEvent? _toDeltaEvent(
if (message is! TwitchMessageModel) {
return message;
}
return TwitchMessageModel(
messageId: message.messageId,
author: message.author,
message: message.message,
reply: message.reply,
tags: message.tags,
annotations: message.annotations,
thirdPartyEmotes: [],
timestamp: message.timestamp,
deleted: true,
channelId: data['channelId']);
return message.withDeleted(true);
});
case "channel.raid":
final model = TwitchRaidEventModel(
Expand All @@ -132,6 +128,14 @@ DeltaEvent? _toDeltaEvent(
messageId: doc.id,
timestamp: data['timestamp'].toDate(),
);
case "userclear":
return UpdateAllDeltaEvent(data['timestamp'].toDate(), (message) {
if (message is! TwitchMessageModel ||
message.author.userId != data['targetUserId']) {
return message;
}
return message.withDeleted(true);
});
case "host":
if (data['hosterChannelId'] == null) {
// Since we might have some events saved without this field.
Expand Down
14 changes: 14 additions & 0 deletions lib/models/messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,17 @@ class MessagesModel extends ChangeNotifier {
if (message is TwitchMessageModel && message.deleted) {
_tts?.unsay(message.messageId);
}
break;
}
}
} else if (event is UpdateAllDeltaEvent) {
for (var i = 0; i < _messages.length; i++) {
final message = event.update(_messages[i]);
if (message is TwitchMessageModel && message.deleted) {
_tts?.unsay(message.messageId);
}
_messages[i] = message;
}
} else if (event is ClearDeltaEvent) {
_messages = [
ChatClearedEventModel(
Expand Down Expand Up @@ -155,8 +164,13 @@ class MessagesModel extends ChangeNotifier {
final message = messages[i];
if (message.messageId == event.messageId) {
messages[i] = event.update(message);
break;
}
}
} else if (event is UpdateAllDeltaEvent) {
for (var i = 0; i < messages.length; i++) {
messages[i] = event.update(messages[i]);
}
} else if (event is ClearDeltaEvent) {
messages = [
ChatClearedEventModel(
Expand Down
14 changes: 14 additions & 0 deletions lib/models/messages/twitch/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,18 @@ class TwitchMessageModel extends MessageModel {
}
return tokens.toList();
}

TwitchMessageModel withDeleted(bool deleted) {
return TwitchMessageModel(
messageId: messageId,
author: author,
message: message,
reply: reply,
tags: tags,
annotations: annotations,
thirdPartyEmotes: thirdPartyEmotes,
timestamp: timestamp,
deleted: deleted,
channelId: channelId);
}
}

0 comments on commit 693d15a

Please sign in to comment.