Enable marking DMs as read
This commit is contained in:
parent
39c2250719
commit
f927507089
@ -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)
|
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) {
|
func (app *Application) message_send(w http.ResponseWriter, r *http.Request) {
|
||||||
room_id := get_room_id_from_context(r.Context())
|
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()
|
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
|
// First send a message, if applicable
|
||||||
if is_sending {
|
if is_sending {
|
||||||
app.message_send(w, r)
|
app.message_send(w, r)
|
||||||
|
@ -184,6 +184,11 @@ h3 {
|
|||||||
height: auto;
|
height: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.disappearing {
|
||||||
|
transition: opacity 2s ease;
|
||||||
|
opacity: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/***************************************************************************************************
|
/***************************************************************************************************
|
||||||
*
|
*
|
||||||
|
@ -131,6 +131,9 @@
|
|||||||
<a class="button" href="https://twitter.com/messages/{{ $room.ID }}" target="_blank">
|
<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" />
|
<img class="svg-icon" src="/static/icons/external-link.svg" width="24" height="24" />
|
||||||
</a>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
@ -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)
|
return fmt.Errorf("Error initializing HTTP POST request:\n %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if body[0] == '{' {
|
||||||
req.Header.Set("content-type", "application/json")
|
req.Header.Set("content-type", "application/json")
|
||||||
|
} else {
|
||||||
|
req.Header.Set("content-type", "application/x-www-form-urlencoded")
|
||||||
|
}
|
||||||
|
|
||||||
api.add_authentication_headers(req)
|
api.add_authentication_headers(req)
|
||||||
|
|
||||||
@ -191,6 +195,11 @@ func (api *API) do_http_POST(remote_url string, body string, result interface{})
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == 204 {
|
||||||
|
// No Content
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
responseHeaders := ""
|
responseHeaders := ""
|
||||||
for header := range resp.Header {
|
for header := range resp.Header {
|
||||||
|
@ -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)
|
err = api.do_http_POST(url.String(), post_data, &result)
|
||||||
return result, err
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -111,3 +111,9 @@ func SendDMMessage(room_id DMChatRoomID, text string, in_reply_to_id DMMessageID
|
|||||||
}
|
}
|
||||||
return dm_response.ToDMTrove()
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user