From 0b44d69efc643c9190ab96267d977acffac89f52 Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 5 Oct 2018 17:41:28 +1000 Subject: [PATCH] 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. --- common/alerts.go | 60 +++++++++++++++++++--------------------------- langs/english.json | 27 +++++++++++++++++++++ misc_test.go | 45 ++++++++++------------------------ 3 files changed, 65 insertions(+), 67 deletions(-) diff --git a/common/alerts.go b/common/alerts.go index bf2dce55..1c560b67 100644 --- a/common/alerts.go +++ b/common/alerts.go @@ -60,6 +60,7 @@ func init() { // TODO: See if we can json.Marshal instead? func escapeTextInJson(in string) string { + in = 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" { - targetUser, err = users.Get(targetUser_id) + targetUser, err = users.Get(targetUserID) if err != nil { LocalErrorJS("Unable to find the target user",w,r) return @@ -80,38 +81,37 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU }*/ 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 - var startFrag, endFrag string - switch elementType { - case "forum": + // Not that many events for us to handle in a forum + if elementType == "forum" { if event == "reply" { - act = "created a new topic" topic, err := Topics.Get(elementID) if err != nil { 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 // 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" + return buildAlertString(GetTmplPhrase("alerts_forum_new_topic"), []string{actor.Name, topic.Title}, topic.Link, actor.Avatar, asid), nil } + 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": topic, err := Topics.Get(elementID) if err != nil { 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 - if targetUserID == user.ID { - postAct = " your topic" + phraseName += "_own" } case "user": 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") } area = targetUser.Name - endFrag = "'s profile" url = targetUser.Link + if targetUserID == user.ID { + phraseName += "_own" + } case "post": topic, err := TopicByReplyID(elementID) if err != nil { @@ -130,7 +132,7 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU url = topic.Link area = topic.Title if targetUserID == user.ID { - postAct = " your post in" + phraseName += "_own" } default: return "", errors.New("Invalid elementType") @@ -138,26 +140,14 @@ func BuildAlert(asid int, event string, elementType string, actorID int, targetU switch event { case "like": - if elementType == "user" { - act = "likes" - endFrag = "" - if targetUser.ID == user.ID { - area = "you" - } - } else { - act = "liked" - } + phraseName += "_like" case "mention": - if elementType == "user" { - act = "mentioned you on" - } else { - act = "mentioned you in" - postAct = "" - } + phraseName += "_mention" 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 { @@ -169,7 +159,7 @@ func buildAlertString(msg string, sub []string, path string, avatar string, asid 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 { diff --git a/langs/english.json b/langs/english.json index 9306dd88..c6aa9458 100644 --- a/langs/english.json +++ b/langs/english.json @@ -98,6 +98,8 @@ "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.", + "alerts_no_linked_topic":"Unable to find the linked topic", + "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_supermod":"You need the EditGroupSuperMod permission to edit a super-mod group.", @@ -313,6 +315,31 @@ "menu_login":"Login", "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_new_topic":"New Topic", "forum_locked":"Locked", diff --git a/misc_test.go b/misc_test.go index 25d39a17..e6c9f3ae 100644 --- a/misc_test.go +++ b/misc_test.go @@ -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") // ? - 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 - 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) + var afterUserFlush = func(uid int) { + if ucache != nil { + expectIntToBeX(t, ucache.Length(), 0, "User cache length should be 0, not %d") + _, err = ucache.Get(uid) + recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", uid) + } } + afterUserFlush(newUserID) user, err = common.Users.Get(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) 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)) - - 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) - } + afterUserFlush(newUserID) user, err = common.Users.Get(newUserID) recordMustExist(t, err, "Couldn't find UID #%d", newUserID) @@ -219,12 +217,7 @@ func userStoreTest(t *testing.T, newUserID int) { err = user.Unban() expectNilErr(t, err) expectIntToBeX(t, user.Group, common.BanGroup, "Sam should still be in the ban group in this copy") - - 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) - } + afterUserFlush(newUserID) user, err = common.Users.Get(newUserID) recordMustExist(t, err, "Couldn't find UID #%d", newUserID) @@ -291,12 +284,7 @@ func userStoreTest(t *testing.T, newUserID int) { err = user.Delete() expectNilErr(t, err) expect(t, !common.Users.Exists(newUserID), fmt.Sprintf("UID #%d should no longer exist", 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) - } + afterUserFlush(newUserID) _, err = common.Users.Get(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) recordMustExist(t, err, "Couldn't find TID #1") - - if topic.ID != 1 { - t.Errorf("topic.ID does not match the requested TID. Got '%d' instead.", topic.ID) - } + expect(t, topic.ID == 1, fmt.Sprintf("topic.ID does not match the requested TID. Got '%d' instead.", topic.ID)) // TODO: Add BulkGetMap() to the TopicStore @@ -491,10 +476,7 @@ func TestForumStore(t *testing.T) { forum, err := common.Forums.Get(1) recordMustExist(t, err, "Couldn't find FID #1") - - if forum.ID != 1 { - t.Errorf("forum.ID doesn't not match the requested FID. Got '%d' instead.'", forum.ID) - } + expect(t, forum.ID == 1, fmt.Sprintf("forum.ID doesn't not match the requested FID. Got '%d' instead.'", forum.ID)) // 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.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)) 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.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 ok = common.Groups.Exists(0) expect(t, ok, "GID #0 should exist") - ok = common.Groups.Exists(1) expect(t, ok, "GID #1 should exist")