commit 78ad5eac116f54ce45a16493451e4c172842f356
parent 9a816921494fa8528a369e983fc23778e8bbf910
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 5 Feb 2023 04:06:22 -0800
bring back optional sound notifications directly in the chat
Diffstat:
7 files changed, 92 insertions(+), 28 deletions(-)
diff --git a/pkg/web/handlers/api/v1/data.go b/pkg/web/handlers/api/v1/data.go
@@ -35,6 +35,9 @@ type chatMessagesData struct {
OfficialRooms []database.ChatRoomAug
SubscribedRooms []database.ChatRoomAug
ForceManualRefresh bool
+ NewMessageSound bool
+ TaggedSound bool
+ PmSound bool
Error string
ErrorTs int64
}
diff --git a/pkg/web/handlers/api/v1/handlers.go b/pkg/web/handlers/api/v1/handlers.go
@@ -183,6 +183,49 @@ func ChatMessagesHandler(c echo.Context) error {
}
data.NbButtons = utils.CountBools(bools...)
+ if authUser.NotifyNewMessage || authUser.NotifyPmmed || authUser.NotifyTagged {
+ lastKnownDate := ""
+ if lastKnownDateCookie, err := hutils.GetLastMsgCookie(c, roomName); err == nil {
+ lastKnownDate = lastKnownDateCookie.Value
+ }
+ newMessageSound := false
+ pmSound := false
+ taggedSound := false
+ if len(msgs) > 0 {
+ if lastKnownMsgDate, err := time.Parse(time.RFC3339Nano, lastKnownDate); err == nil {
+ for _, msg := range msgs {
+ lastKnownDateTrunc := lastKnownMsgDate.Truncate(time.Second)
+ createdAtTrunc := msg.CreatedAt.Truncate(time.Second)
+ if createdAtTrunc.After(lastKnownDateTrunc) {
+ if msg.User.ID != authUser.ID {
+ newMessageSound = true
+ if strings.Contains(msg.Message, "@"+authUser.Username) {
+ taggedSound = true
+ }
+ if msg.ToUserID != nil && *msg.ToUserID == authUser.ID {
+ pmSound = true
+ }
+ break
+ }
+ } else if createdAtTrunc.Before(lastKnownMsgDate) {
+ break
+ }
+ }
+ }
+ lastMsg := msgs[0]
+ hutils.CreateLastMsgCookie(c, roomName, lastMsg.CreatedAt.Format(time.RFC3339))
+ }
+ if authUser.NotifyNewMessage {
+ data.NewMessageSound = newMessageSound
+ }
+ if authUser.NotifyPmmed {
+ data.PmSound = pmSound
+ }
+ if authUser.NotifyTagged {
+ data.TaggedSound = taggedSound
+ }
+ }
+
if c.QueryParams().Has("json") {
return c.JSON(http.StatusOK, data)
}
diff --git a/pkg/web/handlers/utils/utils.go b/pkg/web/handlers/utils/utils.go
@@ -85,6 +85,10 @@ func getGistCookieName(gistUUID string) string {
return fmt.Sprintf("gist_%s_auth", gistUUID)
}
+func getLastMsgCookieName(roomName string) string {
+ return fmt.Sprintf("last_known_msg_%s", roomName)
+}
+
func getRoomCookieName(roomID int64) string {
return fmt.Sprintf("room_%d_auth", roomID)
}
@@ -123,6 +127,14 @@ func CreateGistCookie(c echo.Context, gistUUID, v string) {
c.SetCookie(CreateCookie(getGistCookieName(gistUUID), v, utils.OneDaySecs))
}
+func CreateLastMsgCookie(c echo.Context, roomName, v string) {
+ c.SetCookie(CreateCookie(getLastMsgCookieName(roomName), v, utils.OneDaySecs))
+}
+
+func GetLastMsgCookie(c echo.Context, roomName string) (*http.Cookie, error) {
+ return c.Cookie(getLastMsgCookieName(roomName))
+}
+
func GetAprilFoolCookie(c echo.Context) int {
v, err := c.Cookie(AprilFoolCookieName)
if err != nil {
diff --git a/pkg/web/public/mp3/sound5.mp3 b/pkg/web/public/mp3/sound5.mp3
Binary files differ.
diff --git a/pkg/web/public/mp3/sound6.mp3 b/pkg/web/public/mp3/sound6.mp3
Binary files differ.
diff --git a/pkg/web/public/views/pages/chat-messages.gohtml b/pkg/web/public/views/pages/chat-messages.gohtml
@@ -190,6 +190,12 @@
<a href="/api/v1/chat/messages/{{ .Data.RoomName }}" id="msg-err">× {{ .Data.Error }}</a>
{{- end -}}
+ {{ if or .Data.PmSound .Data.TaggedSound }}
+ <audio src="/public/mp3/sound5.mp3" autoplay></audio>
+ {{ else if .Data.NewMessageSound }}
+ <audio src="/public/mp3/sound6.mp3" autoplay></audio>
+ {{ end }}
+
<div id="msgs">
{{ $baseTopBarURL := (print "/api/v1/chat/top-bar/" $.Data.RoomName) }}
{{ $readMarkerRendered := false }}
diff --git a/pkg/web/public/views/pages/settings/chat.gohtml b/pkg/web/public/views/pages/settings/chat.gohtml
@@ -208,15 +208,15 @@
<option value="2"{{ if eq .Data.Theme 2 }} selected{{ end }}>Plain</option>
</select>
</div>
-<!-- <div class="form-group">-->
-<!-- <table>-->
-<!-- <tr>-->
-<!-- <td>-->
-<!-- <div class="form-check">-->
-<!-- <input type="checkbox" class="form-check-input" name="notify_new_message" id="notify_new_message" value="1"{{ if .Data.NotifyNewMessage }} checked{{ end }} />-->
-<!-- <label class="form-check-label" for="notify_new_message">{{ t "Notify on new message" . }}</label>-->
-<!-- </div>-->
-<!-- </td>-->
+ <div class="form-group">
+ <table>
+ <tr>
+ <td>
+ <div class="form-check">
+ <input type="checkbox" class="form-check-input" name="notify_new_message" id="notify_new_message" value="1"{{ if .Data.NotifyNewMessage }} checked{{ end }} />
+ <label class="form-check-label" for="notify_new_message">{{ t "Notify on new message" . }}</label>
+ </div>
+ </td>
<!-- <td>-->
<!-- <select name="notify_new_message_sound">-->
<!-- <option value="1"{{ if eq .Data.NotifyNewMessageSound 1 }} selected{{ end }}>Sound 1</option>-->
@@ -225,14 +225,14 @@
<!-- <option value="4"{{ if eq .Data.NotifyNewMessageSound 4 }} selected{{ end }}>Sound 4</option>-->
<!-- </select>-->
<!-- </td>-->
-<!-- </tr>-->
-<!-- <tr>-->
-<!-- <td>-->
-<!-- <div class="form-check">-->
-<!-- <input type="checkbox" class="form-check-input" name="notify_tagged" id="notify_tagged" value="1"{{ if .Data.NotifyTagged }} checked{{ end }} />-->
-<!-- <label class="form-check-label" for="notify_tagged">{{ t "Notify when tagged" . }}</label>-->
-<!-- </div>-->
-<!-- </td>-->
+ </tr>
+ <tr>
+ <td>
+ <div class="form-check">
+ <input type="checkbox" class="form-check-input" name="notify_tagged" id="notify_tagged" value="1"{{ if .Data.NotifyTagged }} checked{{ end }} />
+ <label class="form-check-label" for="notify_tagged">{{ t "Notify when tagged" . }}</label>
+ </div>
+ </td>
<!-- <td>-->
<!-- <select name="notify_tagged_sound">-->
<!-- <option value="1"{{ if eq .Data.NotifyTaggedSound 1 }} selected{{ end }}>Sound 1</option>-->
@@ -241,14 +241,14 @@
<!-- <option value="4"{{ if eq .Data.NotifyTaggedSound 4 }} selected{{ end }}>Sound 4</option>-->
<!-- </select>-->
<!-- </td>-->
-<!-- </tr>-->
-<!-- <tr>-->
-<!-- <td>-->
-<!-- <div class="form-check">-->
-<!-- <input type="checkbox" class="form-check-input" name="notify_pmmed" id="notify_pmmed" value="1"{{ if .Data.NotifyPmmed }} checked{{ end }} />-->
-<!-- <label class="form-check-label" for="notify_pmmed">{{ t "Notify when receiving private message" . }}</label>-->
-<!-- </div>-->
-<!-- </td>-->
+ </tr>
+ <tr>
+ <td>
+ <div class="form-check">
+ <input type="checkbox" class="form-check-input" name="notify_pmmed" id="notify_pmmed" value="1"{{ if .Data.NotifyPmmed }} checked{{ end }} />
+ <label class="form-check-label" for="notify_pmmed">{{ t "Notify when receiving private message" . }}</label>
+ </div>
+ </td>
<!-- <td>-->
<!-- <select name="notify_pmmed_sound">-->
<!-- <option value="1"{{ if eq .Data.NotifyPmmedSound 1 }} selected{{ end }}>Sound 1</option>-->
@@ -257,9 +257,9 @@
<!-- <option value="4"{{ if eq .Data.NotifyPmmedSound 4 }} selected{{ end }}>Sound 4</option>-->
<!-- </select>-->
<!-- </td>-->
-<!-- </tr>-->
-<!-- </table>-->
-<!-- </div>-->
+ </tr>
+ </table>
+ </div>
<div class="form-group">
<input type="submit" value="{{ t "Save" . }}" class="btn btn-primary" />
</div>