Add emojis page

This commit is contained in:
r 2019-12-22 18:10:42 +00:00
parent 967a9c954e
commit 08cc2a6ade
9 changed files with 109 additions and 0 deletions

View File

@ -63,3 +63,13 @@ func (c *Client) GetInstancePeers(ctx context.Context) ([]string, error) {
}
return peers, nil
}
// GetInstanceEmojis return instance emojis.
func (c *Client) GetInstanceEmojis(ctx context.Context) ([]*Emoji, error) {
var emojis []*Emoji
err := c.doAPI(ctx, http.MethodGet, "/api/v1/custom_emojis", nil, &emojis, nil)
if err != nil {
return nil, err
}
return emojis, nil
}

View File

@ -97,3 +97,15 @@ func NewAboutPageTemplateData(navbarData *NavbarTemplateData) *AboutPageTemplate
NavbarData: navbarData,
}
}
type EmojiPageTemplateData struct {
NavbarData *NavbarTemplateData
Emojis []*mastodon.Emoji
}
func NewEmojiPageTemplateData(navbarData *NavbarTemplateData, emojis []*mastodon.Emoji) *EmojiPageTemplateData {
return &EmojiPageTemplateData{
NavbarData: navbarData,
Emojis: emojis,
}
}

View File

@ -20,6 +20,7 @@ type Renderer interface {
RenderNotificationPage(ctx context.Context, writer io.Writer, data *NotificationPageTemplateData) (err error)
RenderUserPage(ctx context.Context, writer io.Writer, data *UserPageTemplateData) (err error)
RenderAboutPage(ctx context.Context, writer io.Writer, data *AboutPageTemplateData) (err error)
RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (err error)
}
type renderer struct {
@ -76,6 +77,10 @@ func (r *renderer) RenderAboutPage(ctx context.Context, writer io.Writer, data *
return r.template.ExecuteTemplate(writer, "about.tmpl", data)
}
func (r *renderer) RenderEmojiPage(ctx context.Context, writer io.Writer, data *EmojiPageTemplateData) (err error) {
return r.template.ExecuteTemplate(writer, "emoji.tmpl", data)
}
func EmojiFilter(content string, emojis []mastodon.Emoji) string {
var replacements []string
for _, e := range emojis {

View File

@ -125,6 +125,14 @@ func (s *authService) ServeAboutPage(ctx context.Context, client io.Writer, c *m
return s.Service.ServeAboutPage(ctx, client, c)
}
func (s *authService) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
c, err = s.getClient(ctx)
if err != nil {
return
}
return s.Service.ServeEmojiPage(ctx, client, c)
}
func (s *authService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
c, err = s.getClient(ctx)
if err != nil {

View File

@ -101,6 +101,14 @@ func (s *loggingService) ServeAboutPage(ctx context.Context, client io.Writer, c
return s.Service.ServeAboutPage(ctx, client, c)
}
func (s *loggingService) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, took=%v, err=%v\n",
"ServeEmojiPage", time.Since(begin), err)
}(time.Now())
return s.Service.ServeEmojiPage(ctx, client, c)
}
func (s *loggingService) Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error) {
defer func(begin time.Time) {
s.logger.Printf("method=%v, id=%v, took=%v, err=%v\n",

View File

@ -34,6 +34,7 @@ type Service interface {
ServeNotificationPage(ctx context.Context, client io.Writer, c *model.Client, maxID string, minID string) (err error)
ServeUserPage(ctx context.Context, client io.Writer, c *model.Client, id string, maxID string, minID string) (err error)
ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error)
ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error)
Like(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
UnLike(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
Retweet(ctx context.Context, client io.Writer, c *model.Client, id string) (err error)
@ -444,6 +445,26 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod
return
}
func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
if err != nil {
return
}
emojis, err := c.GetInstanceEmojis(ctx)
if err != nil {
return
}
data := renderer.NewEmojiPageTemplateData(navbarData, emojis)
err = svc.renderer.RenderEmojiPage(ctx, client, data)
if err != nil {
return
}
return
}
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarTemplateData, err error) {
notifications, err := c.GetNotifications(ctx, nil)
if err != nil {

View File

@ -241,6 +241,16 @@ func NewHandler(s Service, staticDir string) http.Handler {
}
}).Methods(http.MethodGet)
r.HandleFunc("/emojis", func(w http.ResponseWriter, req *http.Request) {
ctx := getContextWithSession(context.Background(), req)
err := s.ServeEmojiPage(ctx, w, nil)
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"))

View File

@ -316,3 +316,22 @@
.post-form-field>* {
vertical-align: middle;
}
.emoji {
min-width: 220px;
display: inline-block;
margin-bottom: 2px;
}
.emoji-img {
height: 24px;
width: 24px;
object-fit: contain;
vertical-align: middle;
}
.emoji-shortcode {
vertical-align: middle;
display: inline-block;
}

16
templates/emoji.tmpl Normal file
View File

@ -0,0 +1,16 @@
{{template "header.tmpl"}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Emojis </div>
<div class="emoji-list-container">
{{range .Emojis}}
<div class="emoji">
<img class="emoji-img" src="{{.URL}}" alt="{{.ShortCode}}" />
<div class="emoji-shortcode">:{{.ShortCode}}:</div>
</div>
{{end}}
</div>
{{template "footer.tmpl"}}