Enable marking DMs as read

This commit is contained in:
Alessio 2024-05-10 22:09:48 -07:00
parent 39c2250719
commit f927507089
6 changed files with 66 additions and 1 deletions

View File

@ -25,6 +25,30 @@ func (app *Application) messages_index(w http.ResponseWriter, r *http.Request) {
app.buffered_render_page(w, "tpl/messages.tpl", global_data, chat_view_data)
}
func (app *Application) message_mark_as_read(w http.ResponseWriter, r *http.Request) {
room_id := get_room_id_from_context(r.Context())
c := persistence.NewConversationCursor(room_id)
c.PageSize = 1
chat_contents := app.Profile.GetChatRoomMessagesByCursor(c)
last_message_id := chat_contents.MessageIDs[len(chat_contents.MessageIDs)-1]
scraper.MarkDMChatRead(room_id, last_message_id)
room := chat_contents.Rooms[room_id]
participant, is_ok := room.Participants[app.ActiveUser.ID]
if !is_ok {
panic(room)
}
participant.LastReadEventID = last_message_id
room.Participants[app.ActiveUser.ID] = participant
panic_if(app.Profile.SaveChatRoom(room))
app.toast(w, r, Toast{
Title: "Success",
Message: `Conversation marked as "read"`,
Type: "success",
AutoCloseDelay: 2000,
})
}
func (app *Application) message_send(w http.ResponseWriter, r *http.Request) {
room_id := get_room_id_from_context(r.Context())
@ -48,6 +72,11 @@ func (app *Application) message_detail(w http.ResponseWriter, r *http.Request) {
chat_view_data, global_data := app.get_message_global_data()
if len(parts) == 1 && parts[0] == "mark-as-read" {
app.message_mark_as_read(w, r)
return
}
// First send a message, if applicable
if is_sending {
app.message_send(w, r)

View File

@ -184,6 +184,11 @@ h3 {
height: auto;
}
.disappearing {
transition: opacity 2s ease;
opacity: 0;
}
/***************************************************************************************************
*

View File

@ -131,6 +131,9 @@
<a class="button" href="https://twitter.com/messages/{{ $room.ID }}" target="_blank">
<img class="svg-icon" src="/static/icons/external-link.svg" width="24" height="24" />
</a>
<a class="button" hx-post="/messages/{{ $room.ID }}/mark-as-read">
<img class="svg-icon" src="/static/icons/external-link.svg" width="24" height="24" />
</a>
</div>
</div>
{{end}}

View File

@ -164,7 +164,11 @@ func (api *API) do_http_POST(remote_url string, body string, result interface{})
return fmt.Errorf("Error initializing HTTP POST request:\n %w", err)
}
if body[0] == '{' {
req.Header.Set("content-type", "application/json")
} else {
req.Header.Set("content-type", "application/x-www-form-urlencoded")
}
api.add_authentication_headers(req)
@ -191,6 +195,11 @@ func (api *API) do_http_POST(remote_url string, body string, result interface{})
panic(err)
}
if resp.StatusCode == 204 {
// No Content
return nil
}
if resp.StatusCode != 200 {
responseHeaders := ""
for header := range resp.Header {

View File

@ -521,3 +521,16 @@ func (api *API) SendDMMessage(room_id DMChatRoomID, text string, in_reply_to_id
err = api.do_http_POST(url.String(), post_data, &result)
return result, err
}
// Mark a chat as read.
func (api *API) MarkDMChatRead(room_id DMChatRoomID, read_message_id DMMessageID) {
url := fmt.Sprintf("https://twitter.com/i/api/1.1/dm/conversation/%s/mark_read.json", room_id)
// `do_http_POST` will set the "content-type" header based on whether the body starts with '{' or not.
data := fmt.Sprintf("conversationId=%s&last_read_event_id=%d", room_id, read_message_id)
err := api.do_http_POST(url, data, nil) // Expected: HTTP 204
if err != nil {
panic(err)
}
}

View File

@ -111,3 +111,9 @@ func SendDMMessage(room_id DMChatRoomID, text string, in_reply_to_id DMMessageID
}
return dm_response.ToDMTrove()
}
func MarkDMChatRead(room_id DMChatRoomID, read_message_id DMMessageID) {
if !the_api.IsAuthenticated {
log.Fatalf("Writing DMs can only be done when authenticated. Please provide `--session [user]`")
}
the_api.MarkDMChatRead(room_id, read_message_id)
}