Skip to content
This repository has been archived by the owner on Jun 3, 2019. It is now read-only.

Commit

Permalink
Added a notification on group join
Browse files Browse the repository at this point in the history
  • Loading branch information
akyoto committed Nov 21, 2018
1 parent 645e579 commit cf622f1
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 11 deletions.
53 changes: 42 additions & 11 deletions Group.go
Expand Up @@ -2,6 +2,7 @@ package arn

import (
"errors"
"fmt"
"sync"

"github.com/aerogo/nano"
Expand All @@ -11,7 +12,7 @@ import (
type Group struct {
Name string `json:"name" editable:"true"`
Tagline string `json:"tagline" editable:"true"`
Image string `json:"image" editable:"true"`
Image GroupImage `json:"image"`
Description string `json:"description" editable:"true" type:"textarea"`
Rules string `json:"rules" editable:"true" type:"textarea"`
Tags []string `json:"tags" editable:"true"`
Expand Down Expand Up @@ -48,16 +49,6 @@ func (group *Group) String() string {
return group.TitleByUser(nil)
}

// ImageURL ...
func (group *Group) ImageURL() string {
if group.Image != "" {
return group.Image
}

return "https://media.kitsu.io/groups/avatars/2138/medium.png"
// return "/images/brand/144.png"
}

// FindMember returns the group member by user ID, if available.
func (group *Group) FindMember(userID string) *GroupMember {
group.membersMutex.Lock()
Expand Down Expand Up @@ -121,6 +112,7 @@ func (group *Group) Join(user *User) error {
Joined: DateTimeUTC(),
})

group.OnJoin(user)
return nil
}

Expand All @@ -139,6 +131,45 @@ func (group *Group) Leave(user *User) error {
return nil
}

// OnJoin sends notifications to the creator.
func (group *Group) OnJoin(user *User) {
go func() {
group.Creator().SendNotification(&PushNotification{
Title: "Someone joined your group!",
Message: fmt.Sprintf(`%s has joined your group "%s"`, user.Nick, group.Name),
Icon: user.AvatarLink("medium"),
Link: "https://notify.moe" + group.Link() + "/members",
Type: NotificationTypeGroupJoin,
})
}()
}

// AverageColor returns the average color of the image.
func (group *Group) AverageColor() string {
color := group.Image.AverageColor

if color.Hue == 0 && color.Saturation == 0 && color.Lightness == 0 {
return ""
}

return color.String()
}

// ImageLink returns a link to the group image.
func (group *Group) ImageLink(size string) string {
if !group.HasImage() {
return fmt.Sprintf("//%s/images/elements/no-group-image.svg", MediaHost)
}

extension := ".jpg"

if size == "original" {
extension = group.Image.Extension
}

return fmt.Sprintf("//%s/images/groups/%s/%s%s?%v", MediaHost, size, group.ID, extension, group.Image.LastModified)
}

// GetGroup ...
func GetGroup(id string) (*Group, error) {
obj, err := DB.Get("Group", id)
Expand Down
135 changes: 135 additions & 0 deletions GroupImage.go
@@ -0,0 +1,135 @@
package arn

import (
"bytes"
"image"
"path"
"time"

"github.com/blitzprog/imageoutput"
)

const (
// GroupImageSmallWidth is the minimum width in pixels of a small group image.
GroupImageSmallWidth = 70

// GroupImageSmallHeight is the minimum height in pixels of a small group image.
GroupImageSmallHeight = 70

// GroupImageWebPQuality is the WebP quality of group images.
GroupImageWebPQuality = 70

// GroupImageJPEGQuality is the JPEG quality of group images.
GroupImageJPEGQuality = 70

// GroupImageQualityBonusLowDPI ...
GroupImageQualityBonusLowDPI = 12

// GroupImageQualityBonusLarge ...
GroupImageQualityBonusLarge = 10

// GroupImageQualityBonusMedium ...
GroupImageQualityBonusMedium = 15

// GroupImageQualityBonusSmall ...
GroupImageQualityBonusSmall = 15
)

// Define the group image outputs
var groupImageOutputs = []imageoutput.Output{
// Original at full size
&imageoutput.OriginalFile{
Directory: path.Join(Root, "images/groups/original/"),
Width: 0,
Height: 0,
},

// JPEG - Small
&imageoutput.JPEGFile{
Directory: path.Join(Root, "images/groups/small/"),
Width: GroupImageSmallWidth,
Height: GroupImageSmallHeight,
Quality: GroupImageJPEGQuality + GroupImageQualityBonusLowDPI + GroupImageQualityBonusSmall,
},

// WebP - Small
&imageoutput.WebPFile{
Directory: path.Join(Root, "images/groups/small/"),
Width: GroupImageSmallWidth,
Height: GroupImageSmallHeight,
Quality: GroupImageWebPQuality + GroupImageQualityBonusLowDPI + GroupImageQualityBonusSmall,
},
}

// Define the high DPI group image outputs
var groupImageOutputsHighDPI = []imageoutput.Output{
// JPEG - Small
&imageoutput.JPEGFile{
Directory: path.Join(Root, "images/groups/small/"),
Width: GroupImageSmallWidth * 2,
Height: GroupImageSmallHeight * 2,
Quality: GroupImageJPEGQuality + GroupImageQualityBonusSmall,
},

// WebP - Small
&imageoutput.WebPFile{
Directory: path.Join(Root, "images/groups/small/"),
Width: GroupImageSmallWidth * 2,
Height: GroupImageSmallHeight * 2,
Quality: GroupImageWebPQuality + GroupImageQualityBonusSmall,
},
}

// GroupImage ...
type GroupImage AnimeImage

// SetImageBytes accepts a byte buffer that represents an image file and updates the group image.
func (group *Group) SetImageBytes(data []byte) error {
// Decode
img, format, err := image.Decode(bytes.NewReader(data))

if err != nil {
return err
}

return group.SetImage(&imageoutput.MetaImage{
Image: img,
Format: format,
Data: data,
})
}

// SetImage sets the group image to the given MetaImage.
func (group *Group) SetImage(metaImage *imageoutput.MetaImage) error {
var lastError error

// Save the different image formats and sizes in low DPI
for _, output := range groupImageOutputs {
err := output.Save(metaImage, group.ID)

if err != nil {
lastError = err
}
}

// Save the different image formats and sizes in high DPI
for _, output := range groupImageOutputsHighDPI {
err := output.Save(metaImage, group.ID+"@2")

if err != nil {
lastError = err
}
}

group.Image.Extension = metaImage.Extension()
group.Image.Width = metaImage.Image.Bounds().Dx()
group.Image.Height = metaImage.Image.Bounds().Dy()
group.Image.AverageColor = GetAverageColor(metaImage.Image)
group.Image.LastModified = time.Now().Unix()
return lastError
}

// HasImage returns true if the group has an image.
func (group *Group) HasImage() bool {
return group.Image.Extension != "" && group.Image.Width > 0
}
1 change: 1 addition & 0 deletions NotificationType.go
Expand Up @@ -10,4 +10,5 @@ const (
NotificationTypeLike = "like"
NotificationTypePurchase = "purchase"
NotificationTypePackageTest = "package-test"
NotificationTypeGroupJoin = "group-join"
)

0 comments on commit cf622f1

Please sign in to comment.