2017-09-03 04:50:31 +00:00
/ *
*
* Gosora Alerts System
* Copyright Azareal 2017 - 2018
*
* /
2017-06-10 07:58:15 +00:00
package main
2017-11-11 04:06:16 +00:00
import (
"errors"
"log"
"strconv"
"strings"
"./common"
)
2017-06-10 07:58:15 +00:00
2017-08-18 12:16:56 +00:00
// These notes are for me, don't worry about it too much ^_^
2017-06-10 07:58:15 +00:00
/ *
"You received a friend invite from {user}"
"{x}{mentioned you on}{user}{'s profile}"
"{x}{mentioned you in}{topic}"
"{x}{likes}{you}"
"{x}{liked}{your topic}{topic}"
"{x}{liked}{your post on}{user}{'s profile}" todo
"{x}{liked}{your post in}{topic}"
"{x}{replied to}{your post in}{topic}" todo
"{x}{replied to}{topic}"
"{x}{replied to}{your topic}{topic}"
"{x}{created a new topic}{topic}"
* /
2017-11-11 04:06:16 +00:00
func buildAlert ( asid int , event string , elementType string , actorID int , targetUserID int , elementID int , user common . User /* The current user */ ) ( string , error ) {
var targetUser * common . User
2017-06-10 07:58:15 +00:00
2017-11-11 04:06:16 +00:00
actor , err := common . Users . Get ( actorID )
2017-07-29 14:04:20 +00:00
if err != nil {
return "" , errors . New ( "Unable to find the actor" )
}
2017-06-10 07:58:15 +00:00
2017-07-29 14:04:20 +00:00
/ * if elementType != "forum" {
2017-09-15 22:20:01 +00:00
targetUser , err = users . Get ( targetUser_id )
2017-07-29 14:04:20 +00:00
if err != nil {
LocalErrorJS ( "Unable to find the target user" , w , r )
return
}
} * /
2017-06-10 07:58:15 +00:00
2017-07-29 14:04:20 +00:00
if event == "friend_invite" {
2017-09-03 04:50:31 +00:00
return ` { "msg":"You received a friend invite from { 0}","sub":[" ` + actor . Name + ` "],"path":" ` + actor . Link + ` ","avatar":" ` + strings . Replace ( actor . Avatar , "/" , "\\/" , - 1 ) + ` ","asid":" ` + strconv . Itoa ( asid ) + ` "} ` , nil
2017-07-29 14:04:20 +00:00
}
2017-06-10 07:58:15 +00:00
2017-09-03 04:50:31 +00:00
var act , postAct , url , area string
var startFrag , endFrag string
switch elementType {
case "forum" :
if event == "reply" {
act = "created a new topic"
2017-11-11 04:06:16 +00:00
topic , err := common . Topics . Get ( elementID )
2017-07-29 14:04:20 +00:00
if err != nil {
2018-01-16 08:01:02 +00:00
if common . Dev . DebugMode {
log . Print ( "Unable to find linked topic " + strconv . Itoa ( elementID ) )
}
2017-07-29 14:04:20 +00:00
return "" , errors . New ( "Unable to find the linked topic" )
}
url = topic . Link
area = topic . Title
2017-09-03 04:50:31 +00:00
// Store the forum ID in the targetUser column instead of making a new one? o.O
// Add an additional column for extra information later on when we add the ability to link directly to posts. We don't need the forum data for now...
} else {
act = "did something in a forum"
}
case "topic" :
2017-11-11 04:06:16 +00:00
topic , err := common . Topics . Get ( elementID )
2017-09-03 04:50:31 +00:00
if err != nil {
2018-01-16 08:01:02 +00:00
if common . Dev . DebugMode {
log . Print ( "Unable to find linked topic " + strconv . Itoa ( elementID ) )
}
2017-09-03 04:50:31 +00:00
return "" , errors . New ( "Unable to find the linked topic" )
}
url = topic . Link
area = topic . Title
2017-06-10 07:58:15 +00:00
2017-09-03 04:50:31 +00:00
if targetUserID == user . ID {
postAct = " your topic"
}
case "user" :
2017-11-11 04:06:16 +00:00
targetUser , err = common . Users . Get ( elementID )
2017-09-03 04:50:31 +00:00
if err != nil {
2018-01-16 08:01:02 +00:00
if common . Dev . DebugMode {
log . Print ( "Unable to find target user " + strconv . Itoa ( elementID ) )
}
2017-09-03 04:50:31 +00:00
return "" , errors . New ( "Unable to find the target user" )
}
area = targetUser . Name
endFrag = "'s profile"
url = targetUser . Link
case "post" :
2017-11-11 04:06:16 +00:00
reply := common . BlankReply ( )
reply . ID = elementID
topic , err := reply . Topic ( )
2017-09-03 04:50:31 +00:00
if err != nil {
return "" , errors . New ( "Unable to find the linked reply or parent topic" )
}
url = topic . Link
area = topic . Title
if targetUserID == user . ID {
postAct = " your post in"
}
default :
return "" , errors . New ( "Invalid elementType" )
2017-07-29 14:04:20 +00:00
}
2017-06-10 07:58:15 +00:00
2017-09-03 04:50:31 +00:00
switch event {
case "like" :
if elementType == "user" {
act = "likes"
endFrag = ""
if targetUser . ID == user . ID {
area = "you"
2017-07-29 14:04:20 +00:00
}
2017-09-03 04:50:31 +00:00
} else {
act = "liked"
}
case "mention" :
if elementType == "user" {
act = "mentioned you on"
} else {
act = "mentioned you in"
postAct = ""
}
case "reply" :
act = "replied to"
2017-07-29 14:04:20 +00:00
}
2017-06-10 07:58:15 +00:00
2017-09-03 04:50:31 +00:00
return ` { "msg":" { 0} ` + startFrag + act + postAct + ` { 1} ` + endFrag + ` ","sub":[" ` + actor . Name + ` "," ` + area + ` "],"path":" ` + url + ` ","avatar":" ` + actor . Avatar + ` ","asid":" ` + strconv . Itoa ( asid ) + ` "} ` , nil
2017-06-10 07:58:15 +00:00
}
2017-06-12 09:03:14 +00:00
2017-09-03 04:50:31 +00:00
func notifyWatchers ( asid int64 ) {
2017-11-05 09:55:34 +00:00
rows , err := stmts . getWatchers . Query ( asid )
2017-07-29 14:04:20 +00:00
if err != nil && err != ErrNoRows {
2017-06-12 09:03:14 +00:00
log . Fatal ( err . Error ( ) )
return
}
2017-09-10 16:57:22 +00:00
defer rows . Close ( )
2017-06-12 09:03:14 +00:00
2017-07-29 14:04:20 +00:00
var uid int
var uids [ ] int
for rows . Next ( ) {
err := rows . Scan ( & uid )
if err != nil {
log . Fatal ( err . Error ( ) )
return
}
2017-09-03 04:50:31 +00:00
uids = append ( uids , uid )
2017-07-29 14:04:20 +00:00
}
err = rows . Err ( )
if err != nil {
log . Fatal ( err . Error ( ) )
return
}
2017-06-12 09:03:14 +00:00
2017-09-03 04:50:31 +00:00
var actorID , targetUserID , elementID int
2017-07-29 14:04:20 +00:00
var event , elementType string
2017-11-05 09:55:34 +00:00
err = stmts . getActivityEntry . QueryRow ( asid ) . Scan ( & actorID , & targetUserID , & event , & elementType , & elementID )
2017-07-29 14:04:20 +00:00
if err != nil && err != ErrNoRows {
log . Fatal ( err . Error ( ) )
return
}
2017-06-12 09:03:14 +00:00
2017-09-03 04:50:31 +00:00
_ = wsHub . pushAlerts ( uids , int ( asid ) , event , elementType , actorID , targetUserID , elementID )
2017-06-12 09:03:14 +00:00
}