mirror of
https://gitea.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-30 02:43:00 +00:00
Add notification support
This commit is contained in:
parent
51a4b16af5
commit
f68d72ae0e
@ -4,16 +4,22 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NotificationPleroma struct {
|
||||
IsSeen bool `json:"is_seen"`
|
||||
}
|
||||
|
||||
// Notification hold information for mastodon notification.
|
||||
type Notification struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Account Account `json:"account"`
|
||||
Status *Status `json:"status"`
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Account Account `json:"account"`
|
||||
Status *Status `json:"status"`
|
||||
Pleroma *NotificationPleroma `json:"pleroma"`
|
||||
}
|
||||
|
||||
// GetNotifications return notifications.
|
||||
@ -40,3 +46,11 @@ func (c *Client) GetNotification(ctx context.Context, id string) (*Notification,
|
||||
func (c *Client) ClearNotifications(ctx context.Context) error {
|
||||
return c.doAPI(ctx, http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
|
||||
}
|
||||
|
||||
// ReadNotifications marks notifications as read
|
||||
// Currenly only works for Pleroma
|
||||
func (c *Client) ReadNotifications(ctx context.Context, maxID string) error {
|
||||
params := url.Values{}
|
||||
params.Set("max_id", maxID)
|
||||
return c.doAPI(ctx, http.MethodPost, "/api/v1/pleroma/notifications/read", params, nil, nil)
|
||||
}
|
||||
|
@ -4,22 +4,34 @@ import (
|
||||
"mastodon"
|
||||
)
|
||||
|
||||
type NavbarTemplateData struct {
|
||||
NotificationCount int
|
||||
}
|
||||
|
||||
func NewNavbarTemplateData(notificationCount int) *NavbarTemplateData {
|
||||
return &NavbarTemplateData{
|
||||
NotificationCount: notificationCount,
|
||||
}
|
||||
}
|
||||
|
||||
type TimelinePageTemplateData struct {
|
||||
Statuses []*mastodon.Status
|
||||
HasNext bool
|
||||
NextLink string
|
||||
HasPrev bool
|
||||
PrevLink string
|
||||
Statuses []*mastodon.Status
|
||||
HasNext bool
|
||||
NextLink string
|
||||
HasPrev bool
|
||||
PrevLink string
|
||||
NavbarData *NavbarTemplateData
|
||||
}
|
||||
|
||||
func NewTimelinePageTemplateData(statuses []*mastodon.Status, hasNext bool, nextLink string, hasPrev bool,
|
||||
prevLink string) *TimelinePageTemplateData {
|
||||
prevLink string, navbarData *NavbarTemplateData) *TimelinePageTemplateData {
|
||||
return &TimelinePageTemplateData{
|
||||
Statuses: statuses,
|
||||
HasNext: hasNext,
|
||||
NextLink: nextLink,
|
||||
HasPrev: hasPrev,
|
||||
PrevLink: prevLink,
|
||||
Statuses: statuses,
|
||||
HasNext: hasNext,
|
||||
NextLink: nextLink,
|
||||
HasPrev: hasPrev,
|
||||
PrevLink: prevLink,
|
||||
NavbarData: navbarData,
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,14 +41,32 @@ type ThreadPageTemplateData struct {
|
||||
PostReply bool
|
||||
ReplyToID string
|
||||
ReplyContent string
|
||||
NavbarData *NavbarTemplateData
|
||||
}
|
||||
|
||||
func NewThreadPageTemplateData(status *mastodon.Status, context *mastodon.Context, postReply bool, replyToID string, replyContent string) *ThreadPageTemplateData {
|
||||
func NewThreadPageTemplateData(status *mastodon.Status, context *mastodon.Context, postReply bool, replyToID string, replyContent string, navbarData *NavbarTemplateData) *ThreadPageTemplateData {
|
||||
return &ThreadPageTemplateData{
|
||||
Status: status,
|
||||
Context: context,
|
||||
PostReply: postReply,
|
||||
ReplyToID: replyToID,
|
||||
ReplyContent: replyContent,
|
||||
NavbarData: navbarData,
|
||||
}
|
||||
}
|
||||
|
||||
type NotificationPageTemplateData struct {
|
||||
Notifications []*mastodon.Notification
|
||||
HasNext bool
|
||||
NextLink string
|
||||
NavbarData *NavbarTemplateData
|
||||
}
|
||||
|
||||
func NewNotificationPageTemplateData(notifications []*mastodon.Notification, hasNext bool, nextLink string, navbarData *NavbarTemplateData) *NotificationPageTemplateData {
|
||||
return &NotificationPageTemplateData{
|
||||
Notifications: notifications,
|
||||
HasNext: hasNext,
|
||||
NextLink: nextLink,
|
||||
NavbarData: navbarData,
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ type Renderer interface {
|
||||
RenderSigninPage(ctx context.Context, writer io.Writer) (err error)
|
||||
RenderTimelinePage(ctx context.Context, writer io.Writer, data *TimelinePageTemplateData) (err error)
|
||||
RenderThreadPage(ctx context.Context, writer io.Writer, data *ThreadPageTemplateData) (err error)
|
||||
RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error)
|
||||
}
|
||||
|
||||
type renderer struct {
|
||||
@ -60,6 +61,10 @@ func (r *renderer) RenderThreadPage(ctx context.Context, writer io.Writer, data
|
||||
return r.template.ExecuteTemplate(writer, "thread.tmpl", data)
|
||||
}
|
||||
|
||||
func (r *renderer) RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error) {
|
||||
return r.template.ExecuteTemplate(writer, "notification.tmpl", data)
|
||||
}
|
||||
|
||||
func WithEmojis(content string, emojis []mastodon.Emoji) string {
|
||||
var emojiNameContentPair []string
|
||||
for _, e := range emojis {
|
||||
|
@ -111,6 +111,14 @@ func (s *authService) ServeThreadPage(ctx context.Context, client io.Writer, c *
|
||||
return s.Service.ServeThreadPage(ctx, client, c, id, reply)
|
||||
}
|
||||
|
||||
func (s *authService) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) {
|
||||
c, err = s.getClient(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return s.Service.ServeNotificationPage(ctx, client, c, maxID, minID)
|
||||
}
|
||||
|
||||
func (s *authService) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
|
||||
c, err = s.getClient(ctx)
|
||||
if err != nil {
|
||||
|
@ -77,6 +77,14 @@ func (s *loggingService) ServeThreadPage(ctx context.Context, client io.Writer,
|
||||
return s.Service.ServeThreadPage(ctx, client, c, id, reply)
|
||||
}
|
||||
|
||||
func (s *loggingService) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Printf("method=%v, max_id=%v, min_id=%v, took=%v, err=%v\n",
|
||||
"ServeNotificationPage", maxID, minID, time.Since(begin), err)
|
||||
}(time.Now())
|
||||
return s.Service.ServeNotificationPage(ctx, client, c, maxID, minID)
|
||||
}
|
||||
|
||||
func (s *loggingService) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
|
||||
defer func(begin time.Time) {
|
||||
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
@ -33,6 +32,7 @@ type Service interface {
|
||||
ServeSigninPage(ctx context.Context, client io.Writer) (err error)
|
||||
ServeTimelinePage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, sinceID string, minID string) (err error)
|
||||
ServeThreadPage(ctx context.Context, client io.Writer, c *mastodon.Client, id string, reply bool) (err error)
|
||||
ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error)
|
||||
Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
|
||||
UnLike(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
|
||||
Retweet(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error)
|
||||
@ -219,7 +219,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
|
||||
|
||||
if len(maxID) > 0 && len(statuses) > 0 {
|
||||
hasPrev = true
|
||||
prevLink = fmt.Sprintf("/timeline?min_id=%s", statuses[0].ID)
|
||||
prevLink = "/timeline?min_id=" + statuses[0].ID
|
||||
}
|
||||
if len(minID) > 0 && len(pg.MinID) > 0 {
|
||||
newStatuses, err := c.GetTimelineHome(ctx, &mastodon.Pagination{MinID: pg.MinID, Limit: 20})
|
||||
@ -229,21 +229,26 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
|
||||
newStatusesLen := len(newStatuses)
|
||||
if newStatusesLen == 20 {
|
||||
hasPrev = true
|
||||
prevLink = fmt.Sprintf("/timeline?min_id=%s", pg.MinID)
|
||||
prevLink = "/timeline?min_id=" + pg.MinID
|
||||
} else {
|
||||
i := 20 - newStatusesLen - 1
|
||||
if len(statuses) > i {
|
||||
hasPrev = true
|
||||
prevLink = fmt.Sprintf("/timeline?min_id=%s", statuses[i].ID)
|
||||
prevLink = "/timeline?min_id=" + statuses[i].ID
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(pg.MaxID) > 0 {
|
||||
hasNext = true
|
||||
nextLink = fmt.Sprintf("/timeline?max_id=%s", pg.MaxID)
|
||||
nextLink = "/timeline?max_id=" + pg.MaxID
|
||||
}
|
||||
|
||||
data := renderer.NewTimelinePageTemplateData(statuses, hasNext, nextLink, hasPrev, prevLink)
|
||||
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := renderer.NewTimelinePageTemplateData(statuses, hasNext, nextLink, hasPrev, prevLink, navbarData)
|
||||
err = svc.renderer.RenderTimelinePage(ctx, client, data)
|
||||
if err != nil {
|
||||
return
|
||||
@ -280,7 +285,12 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *ma
|
||||
}
|
||||
}
|
||||
|
||||
data := renderer.NewThreadPageTemplateData(status, context, reply, id, content)
|
||||
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := renderer.NewThreadPageTemplateData(status, context, reply, id, content, navbarData)
|
||||
err = svc.renderer.RenderThreadPage(ctx, client, data)
|
||||
if err != nil {
|
||||
return
|
||||
@ -289,6 +299,78 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *ma
|
||||
return
|
||||
}
|
||||
|
||||
func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer, c *mastodon.Client, maxID string, minID string) (err error) {
|
||||
var hasNext bool
|
||||
var nextLink string
|
||||
|
||||
var pg = mastodon.Pagination{
|
||||
MaxID: maxID,
|
||||
MinID: minID,
|
||||
Limit: 20,
|
||||
}
|
||||
|
||||
notifications, err := c.GetNotifications(ctx, &pg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var unreadCount int
|
||||
for i := range notifications {
|
||||
switch notifications[i].Type {
|
||||
case "reblog", "favourite":
|
||||
if notifications[i].Status != nil {
|
||||
notifications[i].Status.Account.ID = ""
|
||||
}
|
||||
}
|
||||
if notifications[i].Pleroma != nil && notifications[i].Pleroma.IsSeen {
|
||||
unreadCount++
|
||||
}
|
||||
}
|
||||
|
||||
if unreadCount > 0 {
|
||||
err := c.ReadNotifications(ctx, notifications[0].ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(pg.MaxID) > 0 {
|
||||
hasNext = true
|
||||
nextLink = "/notifications?max_id=" + pg.MaxID
|
||||
}
|
||||
|
||||
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := renderer.NewNotificationPageTemplateData(notifications, hasNext, nextLink, navbarData)
|
||||
err = svc.renderer.RenderNotificationPage(ctx, client, data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *mastodon.Client) (data *renderer.NavbarTemplateData, err error) {
|
||||
notifications, err := c.GetNotifications(ctx, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var notificationCount int
|
||||
for i := range notifications {
|
||||
if notifications[i].Pleroma != nil && !notifications[i].Pleroma.IsSeen {
|
||||
notificationCount++
|
||||
}
|
||||
}
|
||||
|
||||
data = renderer.NewNavbarTemplateData(notificationCount)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (svc *service) Like(ctx context.Context, client io.Writer, c *mastodon.Client, id string) (err error) {
|
||||
_, err = c.Favourite(ctx, id)
|
||||
return
|
||||
|
@ -179,6 +179,19 @@ func NewHandler(s Service, staticDir string) http.Handler {
|
||||
w.WriteHeader(http.StatusSeeOther)
|
||||
}).Methods(http.MethodPost)
|
||||
|
||||
r.HandleFunc("/notifications", func(w http.ResponseWriter, req *http.Request) {
|
||||
ctx := getContextWithSession(context.Background(), req)
|
||||
|
||||
maxID := req.URL.Query().Get("max_id")
|
||||
minID := req.URL.Query().Get("min_id")
|
||||
|
||||
err := s.ServeNotificationPage(ctx, w, nil, maxID, minID)
|
||||
if err != nil {
|
||||
s.ServeErrorPage(ctx, w, err)
|
||||
return
|
||||
}
|
||||
}).Methods(http.MethodGet)
|
||||
|
||||
r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) {
|
||||
// TODO remove session from database
|
||||
w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0"))
|
||||
|
@ -17,11 +17,10 @@
|
||||
.status-profile-img {
|
||||
height: 48px;
|
||||
width: 48px;
|
||||
object-fit: contain;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.status {
|
||||
margin: 0 8px;
|
||||
}
|
||||
|
||||
.status a {
|
||||
@ -119,7 +118,6 @@
|
||||
height: 24px;
|
||||
width: 24px;
|
||||
margin-bottom: -8px;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.retweet-info .status-dname{
|
||||
@ -143,3 +141,23 @@
|
||||
.pagination a {
|
||||
margin: 0 8px;
|
||||
}
|
||||
|
||||
.notification-container {
|
||||
margin: 4px 0;
|
||||
padding: 4px 4px;
|
||||
border-left: 4px solid transparent;
|
||||
}
|
||||
|
||||
.notification-container.unread {
|
||||
border-color: #777777;
|
||||
}
|
||||
|
||||
.notification-follow-container,
|
||||
.notification-like-container,
|
||||
.notification-retweet-container {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.notification-follow-uname {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
<div class="navigation">
|
||||
<a href="/timeline">home</a>
|
||||
<a href="/notifications">notifications{{if gt .NotificationCount 0}} ({{.NotificationCount}}){{end}}</a>
|
||||
<a href="/signout">sign out</a>
|
||||
</div>
|
||||
|
59
templates/notification.tmpl
Normal file
59
templates/notification.tmpl
Normal file
@ -0,0 +1,59 @@
|
||||
{{template "header.tmpl"}}
|
||||
{{template "navigation.tmpl" .NavbarData}}
|
||||
<div class="page-title"> Notifications </div>
|
||||
|
||||
{{range .Notifications}}
|
||||
<div class="notification-container {{if .Pleroma}}{{if not .Pleroma.IsSeen}}unread{{end}}{{end}}">
|
||||
{{if eq .Type "follow"}}
|
||||
<div class="notification-follow-container">
|
||||
<img class="status-profile-img" src="{{.Account.AvatarStatic}}" alt="profile-avatar" />
|
||||
<div>
|
||||
<div>
|
||||
<span class="status-dname"> {{WithEmojis .Account.DisplayName .Account.Emojis}} </span>
|
||||
<span class="icon dripicons-user-group"></span>
|
||||
followed you
|
||||
</div>
|
||||
<div class="notification-follow-uname">
|
||||
@{{.Account.Acct}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{else if eq .Type "mention"}}
|
||||
{{template "status" .Status}}
|
||||
|
||||
{{else if eq .Type "reblog"}}
|
||||
<div class="notification-retweet-container">
|
||||
<img class="status-profile-img" src="{{.Account.AvatarStatic}}" alt="profile-avatar" />
|
||||
<div>
|
||||
<div>
|
||||
<span class="status-dname"> {{WithEmojis .Account.DisplayName .Account.Emojis}} </span>
|
||||
<span class="icon dripicons-retweet retweeted"></span>
|
||||
retweeted your post
|
||||
</div>
|
||||
{{template "status" .Status}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{else if eq .Type "favourite"}}
|
||||
<div class="notification-like-container">
|
||||
<img class="status-profile-img" src="{{.Account.AvatarStatic}}" alt="profile-avatar" />
|
||||
<div>
|
||||
<div>
|
||||
<span class="status-dname"> {{WithEmojis .Account.DisplayName .Account.Emojis}} </span>
|
||||
<span class="icon dripicons-star liked"></span>
|
||||
liked your post
|
||||
</div>
|
||||
{{template "status" .Status}}
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
<div class="pagination">
|
||||
{{if .HasNext}}
|
||||
<a href="{{.NextLink}}">next</a>
|
||||
{{end}}
|
||||
</div>
|
||||
{{template "footer.tmpl"}}
|
@ -11,13 +11,17 @@
|
||||
{{block "status" .}}
|
||||
<div class="status-container">
|
||||
<div>
|
||||
{{if ne .Account.ID ""}}
|
||||
<img class="status-profile-img" src="{{.Account.AvatarStatic}}" alt="profile-avatar" />
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="status">
|
||||
{{if ne .Account.ID ""}}
|
||||
<div class="status-name">
|
||||
<span class="status-dname"> {{WithEmojis .Account.DisplayName .Account.Emojis}} </span>
|
||||
<span class="status-uname"> {{.Account.Acct}} </span>
|
||||
</div>
|
||||
{{end}}
|
||||
<div class="status-content"> {{WithEmojis .Content .Emojis}} </div>
|
||||
<div class="status-media-container">
|
||||
{{range .MediaAttachments}}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{{template "header.tmpl"}}
|
||||
{{template "navigation.tmpl" .NavbarData}}
|
||||
<div class="page-title"> Thread </div>
|
||||
{{template "navigation.tmpl"}}
|
||||
|
||||
{{range .Context.Ancestors}}
|
||||
{{template "status.tmpl" .}}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{{template "header.tmpl"}}
|
||||
{{template "navigation.tmpl" .NavbarData}}
|
||||
<div class="page-title"> Timeline </div>
|
||||
{{template "navigation.tmpl"}}
|
||||
|
||||
<form class="timeline-post-form" action="/post" method="POST" enctype="multipart/form-data">
|
||||
<label for="post-content"> New Post </label>
|
||||
|
Loading…
Reference in New Issue
Block a user