The alert list can be localised now.

Removed a bit more boilerplate in the unit tests.
Fixed a bug where the alert list wouldn't load.
This commit is contained in:
Azareal 2018-10-05 17:41:28 +10:00
parent be66ac4c8d
commit 0b44d69efc
3 changed files with 65 additions and 67 deletions

View File

@ -60,6 +60,7 @@ func init() {
// TODO: See if we can json.Marshal instead? // TODO: See if we can json.Marshal instead?
func escapeTextInJson(in string) string { func escapeTextInJson(in string) string {
in = strings.Replace(in, "\"", "\\\"", -1)
return strings.Replace(in, "/", "\\/", -1) return strings.Replace(in, "/", "\\/", -1)
} }
@ -72,7 +73,7 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU
} }
/*if elementType != "forum" { /*if elementType != "forum" {
targetUser, err = users.Get(targetUser_id) targetUser, err = users.Get(targetUserID)
if err != nil { if err != nil {
LocalErrorJS("Unable to find the target user",w,r) LocalErrorJS("Unable to find the target user",w,r)
return return
@ -80,38 +81,37 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU
}*/ }*/
if event == "friend_invite" { if event == "friend_invite" {
return buildAlertString("You received a friend invite from {0}", []string{actor.Name}, actor.Link, actor.Avatar, asid), nil return buildAlertString(GetTmplPhrase("alerts_new_friend_invite"), []string{actor.Name}, actor.Link, actor.Avatar, asid), nil
} }
var act, postAct, url, area string // Not that many events for us to handle in a forum
var startFrag, endFrag string if elementType == "forum" {
switch elementType {
case "forum":
if event == "reply" { if event == "reply" {
act = "created a new topic"
topic, err := Topics.Get(elementID) topic, err := Topics.Get(elementID)
if err != nil { if err != nil {
DebugLogf("Unable to find linked topic %d", elementID) DebugLogf("Unable to find linked topic %d", elementID)
return "", errors.New("Unable to find the linked topic") return "", errors.New(GetErrorPhrase("alerts_no_linked_topic"))
} }
url = topic.Link
area = topic.Title
// Store the forum ID in the targetUser column instead of making a new one? o.O // 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... // 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 { return buildAlertString(GetTmplPhrase("alerts_forum_new_topic"), []string{actor.Name, topic.Title}, topic.Link, actor.Avatar, asid), nil
act = "did something in a forum"
} }
return buildAlertString(GetTmplPhrase("alerts_forum_unknown_action"), []string{actor.Name}, "", actor.Avatar, asid), nil
}
var url, area string
var phraseName = "alerts_" + elementType
switch elementType {
case "topic": case "topic":
topic, err := Topics.Get(elementID) topic, err := Topics.Get(elementID)
if err != nil { if err != nil {
DebugLogf("Unable to find linked topic %d", elementID) DebugLogf("Unable to find linked topic %d", elementID)
return "", errors.New("Unable to find the linked topic") return "", errors.New(GetErrorPhrase("alerts_no_linked_topic"))
} }
url = topic.Link url = topic.Link
area = topic.Title area = topic.Title
if targetUserID == user.ID { if targetUserID == user.ID {
postAct = " your topic" phraseName += "_own"
} }
case "user": case "user":
targetUser, err = Users.Get(elementID) targetUser, err = Users.Get(elementID)
@ -120,8 +120,10 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU
return "", errors.New("Unable to find the target user") return "", errors.New("Unable to find the target user")
} }
area = targetUser.Name area = targetUser.Name
endFrag = "'s profile"
url = targetUser.Link url = targetUser.Link
if targetUserID == user.ID {
phraseName += "_own"
}
case "post": case "post":
topic, err := TopicByReplyID(elementID) topic, err := TopicByReplyID(elementID)
if err != nil { if err != nil {
@ -130,7 +132,7 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU
url = topic.Link url = topic.Link
area = topic.Title area = topic.Title
if targetUserID == user.ID { if targetUserID == user.ID {
postAct = " your post in" phraseName += "_own"
} }
default: default:
return "", errors.New("Invalid elementType") return "", errors.New("Invalid elementType")
@ -138,26 +140,14 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU
switch event { switch event {
case "like": case "like":
if elementType == "user" { phraseName += "_like"
act = "likes"
endFrag = ""
if targetUser.ID == user.ID {
area = "you"
}
} else {
act = "liked"
}
case "mention": case "mention":
if elementType == "user" { phraseName += "_mention"
act = "mentioned you on"
} else {
act = "mentioned you in"
postAct = ""
}
case "reply": case "reply":
act = "replied to" phraseName += "_reply"
} }
return buildAlertString("{0} "+startFrag+act+postAct+" {1}"+endFrag, []string{actor.Name, area}, url, actor.Avatar, asid), nil
return buildAlertString(GetTmplPhrase(phraseName), []string{actor.Name, area}, url, actor.Avatar, asid), nil
} }
func buildAlertString(msg string, sub []string, path string, avatar string, asid int) string { func buildAlertString(msg string, sub []string, path string, avatar string, asid int) string {
@ -169,7 +159,7 @@ func buildAlertString(msg string, sub []string, path string, avatar string, asid
substring = substring[:len(substring)-1] substring = substring[:len(substring)-1]
} }
return `{"msg":"` + escapeTextInJson(msg) + `","sub":["` + substring + `"],"path":"` + escapeTextInJson(path) + `","avatar":"` + escapeTextInJson(avatar) + `","asid":"` + strconv.Itoa(asid) + `"}` return `{"msg":"` + escapeTextInJson(msg) + `","sub":[` + substring + `],"path":"` + escapeTextInJson(path) + `","avatar":"` + escapeTextInJson(avatar) + `","asid":"` + strconv.Itoa(asid) + `"}`
} }
func AddActivityAndNotifyAll(actor int, targetUser int, event string, elementType string, elementID int) error { func AddActivityAndNotifyAll(actor int, targetUser int, event string, elementType string, elementID int) error {

View File

@ -98,6 +98,8 @@
"register_username_too_long_prefix":"The username is too long, max: ", "register_username_too_long_prefix":"The username is too long, max: ",
"register_email_fail":"We were unable to send the email for you to confirm that this email address belongs to you. You may not have access to some functionality until you do so. Please ask an administrator for assistance.", "register_email_fail":"We were unable to send the email for you to confirm that this email address belongs to you. You may not have access to some functionality until you do so. Please ask an administrator for assistance.",
"alerts_no_linked_topic":"Unable to find the linked topic",
"panel_groups_need_name":"The group name can't be left blank.", "panel_groups_need_name":"The group name can't be left blank.",
"panel_groups_cannot_edit_admin":"You need the EditGroupAdmin permission to edit an admin group.", "panel_groups_cannot_edit_admin":"You need the EditGroupAdmin permission to edit an admin group.",
"panel_groups_cannot_edit_supermod":"You need the EditGroupSuperMod permission to edit a super-mod group.", "panel_groups_cannot_edit_supermod":"You need the EditGroupSuperMod permission to edit a super-mod group.",
@ -313,6 +315,31 @@
"menu_login":"Login", "menu_login":"Login",
"menu_register":"Register", "menu_register":"Register",
"alerts_forum_new_topic":"{0} created the topic {1}",
"alerts_forum_unknown_action":"{0} did something in a forum",
"alerts_topic_own_reply":"{0} replied to your topic {1}",
"alerts_topic_reply":"{0} replied to {1}",
"alerts_topic_own_like":"{0} liked your topic {1}",
"alerts_topic_like":"{0} liked {1}",
"alerts_topic_own_mention":"{0} mentioned you in {1}",
"alerts_topic_mention":"{0} mentioned you in {1}",
"alerts_post_own_reply":"{0} replied to your post in {1}",
"alerts_post_reply":"{0} replied to {1}",
"alerts_post_own_like":"{0} liked your post in {1}",
"alerts_post_like":"{0} liked a post in {1}",
"alerts_post_own_mention":"{0} mentioned you in {1}",
"alerts_post_mention":"{0} mentioned you in {1}",
"alerts_user_own_reply":"{0} made a post on your profile",
"alerts_user_reply":"{0} posted on {1}'s profile",
"alerts_user_own_like":"{0} likes you",
"alerts_user_like":"{0} likes {1}",
"alerts_user_own_mention":"{0} mentioned you on your profile",
"alerts_user_mention":"{0} mentioned you on {1}'s profile",
"alerts_new_friend_invite":"You received a friend invite from {0}",
"topics_click_topics_to_select":"Click the topics to select them", "topics_click_topics_to_select":"Click the topics to select them",
"topics_new_topic":"New Topic", "topics_new_topic":"New Topic",
"forum_locked":"Locked", "forum_locked":"Locked",

View File

@ -186,11 +186,14 @@ func userStoreTest(t *testing.T, newUserID int) {
expectIntToBeX(t, user.Group, 5, "Sam should still be in group 5 in this copy") expectIntToBeX(t, user.Group, 5, "Sam should still be in group 5 in this copy")
// ? - What if we change the caching mechanism so it isn't hard purged and reloaded? We'll deal with that when we come to it, but for now, this is a sign of a cache bug // ? - What if we change the caching mechanism so it isn't hard purged and reloaded? We'll deal with that when we come to it, but for now, this is a sign of a cache bug
var afterUserFlush = func(uid int) {
if ucache != nil { if ucache != nil {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d") expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
_, err = ucache.Get(newUserID) _, err = ucache.Get(uid)
recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", newUserID) recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", uid)
} }
}
afterUserFlush(newUserID)
user, err = common.Users.Get(newUserID) user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID) recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
@ -203,12 +206,7 @@ func userStoreTest(t *testing.T, newUserID int) {
err = user.Ban(duration, 1) err = user.Ban(duration, 1)
expectNilErr(t, err) expectNilErr(t, err)
expect(t, user.Group == common.Config.DefaultGroup, fmt.Sprintf("Sam should be in group %d, not %d", common.Config.DefaultGroup, user.Group)) expect(t, user.Group == common.Config.DefaultGroup, fmt.Sprintf("Sam should be in group %d, not %d", common.Config.DefaultGroup, user.Group))
afterUserFlush(newUserID)
if ucache != nil {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
_, err = ucache.Get(2)
recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", newUserID)
}
user, err = common.Users.Get(newUserID) user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID) recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
@ -219,12 +217,7 @@ func userStoreTest(t *testing.T, newUserID int) {
err = user.Unban() err = user.Unban()
expectNilErr(t, err) expectNilErr(t, err)
expectIntToBeX(t, user.Group, common.BanGroup, "Sam should still be in the ban group in this copy") expectIntToBeX(t, user.Group, common.BanGroup, "Sam should still be in the ban group in this copy")
afterUserFlush(newUserID)
if ucache != nil {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
_, err = ucache.Get(newUserID)
recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", newUserID)
}
user, err = common.Users.Get(newUserID) user, err = common.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID) recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
@ -291,12 +284,7 @@ func userStoreTest(t *testing.T, newUserID int) {
err = user.Delete() err = user.Delete()
expectNilErr(t, err) expectNilErr(t, err)
expect(t, !common.Users.Exists(newUserID), fmt.Sprintf("UID #%d should no longer exist", newUserID)) expect(t, !common.Users.Exists(newUserID), fmt.Sprintf("UID #%d should no longer exist", newUserID))
afterUserFlush(newUserID)
if ucache != nil {
expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d")
_, err = ucache.Get(newUserID)
recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", newUserID)
}
_, err = common.Users.Get(newUserID) _, err = common.Users.Get(newUserID)
recordMustNotExist(t, err, "UID #%d shouldn't exist", newUserID) recordMustNotExist(t, err, "UID #%d shouldn't exist", newUserID)
@ -452,10 +440,7 @@ func topicStoreTest(t *testing.T) {
topic, err = common.Topics.Get(1) topic, err = common.Topics.Get(1)
recordMustExist(t, err, "Couldn't find TID #1") recordMustExist(t, err, "Couldn't find TID #1")
expect(t, topic.ID == 1, fmt.Sprintf("topic.ID does not match the requested TID. Got '%d' instead.", topic.ID))
if topic.ID != 1 {
t.Errorf("topic.ID does not match the requested TID. Got '%d' instead.", topic.ID)
}
// TODO: Add BulkGetMap() to the TopicStore // TODO: Add BulkGetMap() to the TopicStore
@ -491,10 +476,7 @@ func TestForumStore(t *testing.T) {
forum, err := common.Forums.Get(1) forum, err := common.Forums.Get(1)
recordMustExist(t, err, "Couldn't find FID #1") recordMustExist(t, err, "Couldn't find FID #1")
expect(t, forum.ID == 1, fmt.Sprintf("forum.ID doesn't not match the requested FID. Got '%d' instead.'", forum.ID))
if forum.ID != 1 {
t.Errorf("forum.ID doesn't not match the requested FID. Got '%d' instead.'", forum.ID)
}
// TODO: Check the preset and forum permissions // TODO: Check the preset and forum permissions
expect(t, forum.Name == "Reports", fmt.Sprintf("FID #0 is named '%s' and not 'Reports'", forum.Name)) expect(t, forum.Name == "Reports", fmt.Sprintf("FID #0 is named '%s' and not 'Reports'", forum.Name))
expect(t, !forum.Active, fmt.Sprintf("The reports forum shouldn't be active")) expect(t, !forum.Active, fmt.Sprintf("The reports forum shouldn't be active"))
@ -502,7 +484,7 @@ func TestForumStore(t *testing.T) {
expect(t, forum.Desc == expectDesc, fmt.Sprintf("The forum description should be '%s' not '%s'", expectDesc, forum.Desc)) expect(t, forum.Desc == expectDesc, fmt.Sprintf("The forum description should be '%s' not '%s'", expectDesc, forum.Desc))
forum, err = common.Forums.Get(2) forum, err = common.Forums.Get(2)
recordMustExist(t, err, "Couldn't find FID #1") recordMustExist(t, err, "Couldn't find FID #2")
expect(t, forum.ID == 2, fmt.Sprintf("The FID should be 2 not %d", forum.ID)) expect(t, forum.ID == 2, fmt.Sprintf("The FID should be 2 not %d", forum.ID))
expect(t, forum.Name == "General", fmt.Sprintf("The name of the forum should be 'General' not '%s'", forum.Name)) expect(t, forum.Name == "General", fmt.Sprintf("The name of the forum should be 'General' not '%s'", forum.Name))
@ -579,7 +561,6 @@ func TestGroupStore(t *testing.T) {
// 0 aka Unknown, for system posts and other oddities // 0 aka Unknown, for system posts and other oddities
ok = common.Groups.Exists(0) ok = common.Groups.Exists(0)
expect(t, ok, "GID #0 should exist") expect(t, ok, "GID #0 should exist")
ok = common.Groups.Exists(1) ok = common.Groups.Exists(1)
expect(t, ok, "GID #1 should exist") expect(t, ok, "GID #1 should exist")