2022-05-07 16:53:42 +00:00
package api
import (
2022-11-01 02:33:11 +00:00
"context"
2022-05-07 16:53:42 +00:00
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/payload"
2022-05-23 18:17:25 +00:00
"go.uber.org/zap"
2022-05-07 16:53:42 +00:00
)
const (
commentReplyNotificationTitleFormat = "%s in %s"
postReplyNotificationTitleFormat = "%s to %s"
privateMessageNotificationTitleFormat = "Message from %s"
subredditNotificationBodyFormat = "r/%s: \u201c%s\u201d"
subredditNotificationTitleFormat = "📣 \u201c%s\u201d Watcher"
trendingNotificationTitleFormat = "🔥 r/%s Trending"
usernameMentionNotificationTitleFormat = "Mention in \u201c%s\u201d"
)
type notificationGenerator func ( * payload . Payload )
func generateNotificationTester ( a * api , fun notificationGenerator ) func ( w http . ResponseWriter , r * http . Request ) {
return func ( w http . ResponseWriter , r * http . Request ) {
2022-11-01 02:33:11 +00:00
ctx , cancel := context . WithCancel ( r . Context ( ) )
defer cancel ( )
2022-05-07 16:53:42 +00:00
2022-05-07 19:04:35 +00:00
vars := mux . Vars ( r )
2022-05-07 16:53:42 +00:00
tok := vars [ "apns" ]
d , err := a . deviceRepo . GetByAPNSToken ( ctx , tok )
if err != nil {
2022-05-23 19:15:46 +00:00
a . logger . Info ( "failed to fetch device from database" , zap . Error ( err ) )
2022-05-21 14:00:21 +00:00
a . errorResponse ( w , r , 500 , err )
2022-05-07 16:53:42 +00:00
return
}
p := payload . NewPayload ( )
2022-05-07 16:57:14 +00:00
p . MutableContent ( ) .
Sound ( "traloop.wav" )
2022-05-07 16:53:42 +00:00
fun ( p )
notification := & apns2 . Notification { }
notification . Topic = "com.christianselig.Apollo"
notification . DeviceToken = d . APNSToken
notification . Payload = p
client := apns2 . NewTokenClient ( a . apns )
if ! d . Sandbox {
client = client . Production ( )
}
if _ , err := client . Push ( notification ) ; err != nil {
2022-05-23 18:17:25 +00:00
a . logger . Info ( "failed to send test notification" , zap . Error ( err ) )
2022-05-21 14:00:21 +00:00
a . errorResponse ( w , r , 500 , err )
2022-05-07 16:53:42 +00:00
return
}
w . WriteHeader ( http . StatusOK )
}
}
2022-05-07 16:57:14 +00:00
func privateMessage ( p * payload . Payload ) {
2022-05-07 16:53:42 +00:00
title := fmt . Sprintf ( privateMessageNotificationTitleFormat , "welcomebot" )
2022-05-07 16:57:14 +00:00
p . AlertTitle ( title ) .
2022-05-07 16:53:42 +00:00
AlertBody ( "**Welcome to r/GriefSupport!**\n\nWe're glad you found us, but sad you needed to. We're here to support you during whatever difficulties you're going through." ) .
2022-05-07 16:57:14 +00:00
AlertSubtitle ( "Welcome to r/GriefSupport!" ) .
2022-05-07 16:53:42 +00:00
AlertSummaryArg ( "welcomebot" ) .
2022-05-07 16:57:14 +00:00
Category ( "inbox-private-message" ) .
2022-05-07 16:53:42 +00:00
Custom ( "account_id" , "1ia22" ) .
Custom ( "author" , "welcomebot" ) .
2022-05-07 16:57:14 +00:00
Custom ( "comment_id" , "1d2oouy" ) .
2022-05-07 16:53:42 +00:00
Custom ( "destination_author" , "changelog" ) .
Custom ( "parent_id" , "" ) .
Custom ( "post_title" , "" ) .
Custom ( "subreddit" , "" ) .
Custom ( "type" , "private-message" )
}
2022-05-07 16:57:14 +00:00
func commentReply ( p * payload . Payload ) {
2022-05-07 16:53:42 +00:00
title := fmt . Sprintf ( commentReplyNotificationTitleFormat , "Equinox_Shift" , "Protests set to disrupt Ottawa's downtown for 3rd straight weekend" )
2022-05-07 16:57:14 +00:00
p . AlertTitle ( title ) .
2022-05-07 16:53:42 +00:00
AlertBody ( "They don't even go here." ) .
2022-05-07 16:57:14 +00:00
Category ( "inbox-comment-reply" ) .
2022-05-07 16:53:42 +00:00
Custom ( "account_id" , "1ia22" ) .
Custom ( "author" , "Equinox_Shift" ) .
2022-05-07 16:57:14 +00:00
Custom ( "comment_id" , "hwp66zg" ) .
2022-05-07 16:53:42 +00:00
Custom ( "destination_author" , "changelog" ) .
Custom ( "parent_id" , "t1_hwonb97" ) .
Custom ( "post_id" , "sqqk29" ) .
2022-05-07 16:57:14 +00:00
Custom ( "post_title" , "Protests set to disrupt Ottawa's downtown for 3rd straight weekend" ) .
2022-05-07 16:53:42 +00:00
Custom ( "subject" , "comment" ) .
Custom ( "subreddit" , "ottawa" ) .
2022-05-07 16:57:14 +00:00
Custom ( "subreddit" , "ottawa" ) .
2022-05-07 16:53:42 +00:00
Custom ( "type" , "comment" ) .
ThreadID ( "comment" )
}
2022-05-07 16:57:14 +00:00
func postReply ( p * payload . Payload ) {
2022-05-07 16:53:42 +00:00
title := fmt . Sprintf ( postReplyNotificationTitleFormat , "Ryfter" , "Quest 2 use during chemo" )
2022-05-07 16:57:14 +00:00
p . AlertTitle ( title ) .
2022-05-07 16:53:42 +00:00
AlertBody ( "As others have said, [Real Fishing VR](https://www.oculus.com/experiences/quest/2582932495064035). Especially if he likes to fish. My dad and mom were blown away by it." ) .
2022-05-07 16:57:14 +00:00
Category ( "inbox-comment-reply" ) .
2022-05-07 16:53:42 +00:00
Custom ( "account_id" , "1ia22" ) .
Custom ( "author" , "Ryfter" ) .
2022-05-07 16:57:14 +00:00
Custom ( "comment_id" , "hyg01ip" ) .
2022-05-07 16:53:42 +00:00
Custom ( "destination_author" , "changelog" ) .
Custom ( "parent_id" , "t3_t0qn4z" ) .
Custom ( "post_id" , "t0qn4z" ) .
2022-05-07 16:57:14 +00:00
Custom ( "post_title" , "Quest 2 use during chemo" ) .
2022-05-07 16:53:42 +00:00
Custom ( "subject" , "comment" ) .
Custom ( "subreddit" , "OculusQuest2" ) .
2022-05-07 16:57:14 +00:00
Custom ( "subreddit" , "OculusQuest2" ) .
2022-05-07 16:53:42 +00:00
Custom ( "type" , "post" ) .
ThreadID ( "comment" )
}
2022-05-07 16:57:14 +00:00
func usernameMention ( p * payload . Payload ) {
2022-05-07 16:53:42 +00:00
title := fmt . Sprintf ( usernameMentionNotificationTitleFormat , "testimg" )
2022-05-07 16:57:14 +00:00
p . AlertTitle ( title ) .
2022-05-07 16:53:42 +00:00
AlertBody ( "yo u/changelog what's good" ) .
2022-05-07 16:57:14 +00:00
Category ( "inbox-username-mention-no-context" ) .
2022-05-07 16:53:42 +00:00
Custom ( "account_id" , "1ia22" ) .
Custom ( "author" , "iamthatis" ) .
2022-05-07 16:57:14 +00:00
Custom ( "comment_id" , "i6xobpa" ) .
2022-05-07 16:53:42 +00:00
Custom ( "destination_author" , "changelog" ) .
Custom ( "parent_id" , "t3_u02338" ) .
Custom ( "post_id" , "u02338" ) .
2022-05-07 16:57:14 +00:00
Custom ( "post_title" , "testimg" ) .
2022-05-07 16:53:42 +00:00
Custom ( "subject" , "comment" ) .
Custom ( "subreddit" , "calicosummer" ) .
2022-05-07 16:57:14 +00:00
Custom ( "subreddit" , "calicosummer" ) .
2022-05-07 16:53:42 +00:00
Custom ( "type" , "username" )
}
2022-05-07 16:57:14 +00:00
func subredditWatcher ( p * payload . Payload ) {
2022-05-07 16:53:42 +00:00
title := fmt . Sprintf ( subredditNotificationTitleFormat , "bug pics" )
body := fmt . Sprintf ( subredditNotificationBodyFormat , "pics" , "A Goliath Stick Insect. Aware of my presence she let me get close enough for a photo. (OC)" )
2022-05-07 16:57:14 +00:00
p . AlertTitle ( title ) .
2022-05-07 16:53:42 +00:00
AlertBody ( body ) .
AlertSummaryArg ( "pics" ) .
Category ( "subreddit-watcher" ) .
Custom ( "author" , "befarked247" ) .
Custom ( "post_age" , 1651409659.0 ) .
2022-05-07 16:57:14 +00:00
Custom ( "post_id" , "ufzaml" ) .
Custom ( "post_title" , "A Goliath Stick Insect. Aware of my presence she let me get close enough for a photo. (OC)" ) .
Custom ( "subreddit" , "pics" ) .
Custom ( "thumbnail" , "https://a.thumbs.redditmedia.com/Lr4b-YHLTNu1LFuyUY1Zic8kHy3ojX06gLcZOuqxrr0.jpg" ) .
ThreadID ( "subreddit-watcher" )
2022-05-07 16:53:42 +00:00
}
2022-05-07 16:57:14 +00:00
func trendingPost ( p * payload . Payload ) {
2022-05-07 16:53:42 +00:00
title := fmt . Sprintf ( trendingNotificationTitleFormat , "pics" )
2022-05-07 16:57:14 +00:00
p . AlertTitle ( title ) .
2022-05-07 16:53:42 +00:00
AlertBody ( "A Goliath Stick Insect. Aware of my presence she let me get close enough for a photo. (OC)" ) .
AlertSummaryArg ( "pics" ) .
Category ( "trending-post" ) .
Custom ( "author" , "befarked247" ) .
Custom ( "post_age" , 1651409659.0 ) .
2022-05-07 16:57:14 +00:00
Custom ( "post_id" , "ufzaml" ) .
Custom ( "post_title" , "A Goliath Stick Insect. Aware of my presence she let me get close enough for a photo. (OC)" ) .
Custom ( "subreddit" , "pics" ) .
Custom ( "thumbnail" , "https://a.thumbs.redditmedia.com/Lr4b-YHLTNu1LFuyUY1Zic8kHy3ojX06gLcZOuqxrr0.jpg" ) .
ThreadID ( "trending-post" )
2022-05-07 16:53:42 +00:00
}