BUGFIX: fix reaccs panicking if the reacc'd message isn't the latest one in the chat

This commit is contained in:
Alessio 2024-11-09 18:13:05 -08:00
parent 724533ecec
commit 97888e2b23
3 changed files with 30 additions and 13 deletions

View File

@ -98,7 +98,20 @@ func (app *Application) message_detail(w http.ResponseWriter, r *http.Request) {
panic_if(json.Unmarshal(data_, &data)) panic_if(json.Unmarshal(data_, &data))
panic_if(app.API.SendDMReaction(room_id, data.MessageID, data.Reacc)) panic_if(app.API.SendDMReaction(room_id, data.MessageID, data.Reacc))
dm_message := global_data.Messages[data.MessageID] dm_message, is_ok := global_data.Messages[data.MessageID]
if !is_ok {
// TODO: this seems kind of silly to use global data in the first place; it's unlikely
// to have the relevant tweet in it, since it just gets the one latest tweet from the
// convo. This handler should be pulled out and just fetch the tweet directly--
// performance probably doesn't matter, but it's spaghetti code otherwise
trove_with_dm_message, err := app.Profile.GetChatMessage(data.MessageID)
panic_if(err)
global_data.MergeWith(trove_with_dm_message)
dm_message, is_ok = global_data.Messages[data.MessageID]
if !is_ok {
panic(global_data)
}
}
dm_message.Reactions[app.ActiveUser.ID] = scraper.DMReaction{ dm_message.Reactions[app.ActiveUser.ID] = scraper.DMReaction{
ID: 0, // Hopefully will be OK temporarily ID: 0, // Hopefully will be OK temporarily
DMMessageID: dm_message.ID, DMMessageID: dm_message.ID,

View File

@ -169,27 +169,27 @@ func (p Profile) SaveChatMessage(m DMMessage) error {
return nil return nil
} }
// Get a single chat message, filling its attachment contents. // Get a single chat message, filling its attachment contents. Returns a TweetTrove because a
// // message can have a tweet attachment, etc.
// This function is only used in tests. func (p Profile) GetChatMessage(id DMMessageID) (TweetTrove, error) {
func (p Profile) GetChatMessage(id DMMessageID) (ret DMMessage, err error) { trove := NewTweetTrove()
err = p.DB.Get(&ret, ` var msg DMMessage
err := p.DB.Get(&msg, `
select `+CHAT_MESSAGES_ALL_SQL_FIELDS+` select `+CHAT_MESSAGES_ALL_SQL_FIELDS+`
from chat_messages from chat_messages
where id = ? where id = ?
`, id, `, id,
) )
if err != nil { if err != nil {
return ret, fmt.Errorf("Error getting chat message %d:\n %w", id, err) return trove, fmt.Errorf("Error getting chat message %d:\n %w", id, err)
} }
ret.Reactions = make(map[UserID]DMReaction) msg.Reactions = make(map[UserID]DMReaction)
// This is a bit circuitous, but it doesn't matter because this function is only used in tests // This is a bit circuitous, but it doesn't matter because this function is only used in tests
trove := NewTweetTrove() trove.Messages[msg.ID] = msg
trove.Messages[ret.ID] = ret
p.fill_dm_contents(&trove) p.fill_dm_contents(&trove)
return trove.Messages[ret.ID], nil return trove, nil
} }
type DMChatView struct { type DMChatView struct {

View File

@ -110,8 +110,10 @@ func TestSaveAndLoadChatMessage(t *testing.T) {
require.NoError(err) require.NoError(err)
// Reload it // Reload it
new_message, err := profile.GetChatMessage(message.ID) trove_with_new_message, err := profile.GetChatMessage(message.ID)
require.NoError(err) require.NoError(err)
new_message, is_ok := trove_with_new_message.Messages[message.ID]
require.True(is_ok)
if diff := deep.Equal(message, new_message); diff != nil { if diff := deep.Equal(message, new_message); diff != nil {
t.Error(diff) t.Error(diff)
@ -145,8 +147,10 @@ func TestAddReactionToChatMessage(t *testing.T) {
require.NoError(profile.SaveChatMessage(message)) require.NoError(profile.SaveChatMessage(message))
// Reload it // Reload it
new_message, err := profile.GetChatMessage(message.ID) trove_with_new_message, err := profile.GetChatMessage(message.ID)
require.NoError(err) require.NoError(err)
new_message, is_ok := trove_with_new_message.Messages[message.ID]
require.True(is_ok)
if diff := deep.Equal(message, new_message); diff != nil { if diff := deep.Equal(message, new_message); diff != nil {
t.Error(diff) t.Error(diff)