diff --git a/opal.json b/opal.json index bbbb63e..c179af4 100644 --- a/opal.json +++ b/opal.json @@ -1,6 +1,6 @@ { "name": "mistwarp-api", - "version": "1.1.0", + "version": "1.1.1", "main": "main.osl", "scripts": { "dev": ["osl", "run", "main.osl"], diff --git a/src/community/notifications.osl b/src/community/notifications.osl index f6f2b49..0840619 100644 --- a/src/community/notifications.osl +++ b/src/community/notifications.osl @@ -347,9 +347,6 @@ def addNotification(string target, string actor, string notifType, object extra) if actorKey != "" and userShadowBanned(actor) ( return ) - if isMessageNotification(notifType) and roturMessageRefusal(actor, target) != "" ( - return - ) object payload = sanitizeNotificationPayload(extra, notifType, actor) object item = { @@ -364,8 +361,13 @@ def addNotification(string target, string actor, string notifType, object extra) } boolean keepLocal = isStudentUserId(targetId) or isStudentUserId(actorId) or isStudentUsername(actor) or notifType.startsWith("classroom_") - if !keepLocal and postRoturNotification(targetKey, targetId, actor, actorId, notifType, payload) ( - return + if !keepLocal ( + if postRoturNotification(targetKey, targetId, actor, actorId, notifType, payload) ( + return + ) + if !roturMessageAllowed(actor, target) ( + return + ) ) sync.lock("notifications:" ++ targetKey) diff --git a/src/community/notifications.test.osl b/src/community/notifications.test.osl new file mode 100644 index 0000000..59c2a55 --- /dev/null +++ b/src/community/notifications.test.osl @@ -0,0 +1,129 @@ +import "std:testing" +import "std:sync" +import "std:json" +import "std:requests" +import "std:serve" +import "std:thread" + +string notificationsDir = "notifications/" +string editorOrigin = "https://mistwarp.org" +string mistwarpRoturToken = "token" +string roturBase = "http://127.0.0.1:18343" +object files = {} +array askedRotur = [] +number roturStatus = 200 +array messageRefused = [] + +def normalizeUsername(string name) string ( + return name.toLower().strip() +) + +def isSafePathPart(string value) boolean ( + return value != "" +) + +def readFile(string path) object ( + if !files.contains(path) ( + return {ok: false, data: null} + ) + return {ok: true, data: files[path]} +) + +def writeFile(string path, object data) boolean ( + files[path] = data + return true +) + +def userIdForUsername(string name) string ( + return "u-" ++ name.toLower() +) + +def loadSafety(string username) object ( + if username == "blocker" ( + return {blocked: ["stranger"], muted: []} + ) + return {blocked: [], muted: []} +) + +def userHasBlocked(string actor, string target) boolean ( + return false +) + +def userShadowBanned(string actor) boolean ( + return false +) + +def isStudentUserId(string id) boolean ( + return id == "u-student" +) + +def isStudentUsername(string name) boolean ( + return name == "student" +) + +def roturMessageAllowed(string sender, string recipient) boolean ( + return !messageRefused.contains(sender ++ ">" ++ recipient) +) + +def randomString(number length) string ( + return "r" * length +) + +def contextString(*serve.Context c, string key) string ( + return "" +) + +def syncFollowerMilestones(string username) ( + void username +) + +def usernameForId(string id) string ( + return id +) + +def rewriteNotificationIdentityFields(object data, string oldKey, string newName) boolean ( + return false +) + +import "./notifications.osl" + +def handleFakeNotification(*serve.Context c) ( + askedRotur.append(c.body()) + c.json(roturStatus, {ok: roturStatus == 200}) +) +*serve.Router fakeRotur = serve.new() +fakeRotur.POST("/v2/notifications", handleFakeNotification) +fakeRotur.GET("/ready", handleFakeNotification) + +def runFakeRotur() ( + fakeRotur.run(":18343") +) +thread.new(runFakeRotur) +for i 50 ( + if requests.get(roturBase ++ "/ready").isOk() ( + break + ) + wait 0.1 +) +askedRotur = [] + +messageRefused = ["stranger>owner"] +addNotification("owner", "stranger", "comment", {projectId: "p1", projectTitle: "Game", commentId: "c1", preview: "hi"}) +testing.assert(askedRotur.len == 1, "a comment from someone the owner doesn't accept messages from still goes to Rotur") +testing.assert(loadNotifications("owner").len == 0, "and Rotur keeps it, not MistWarp") + +addNotification("blocker", "stranger", "comment", {projectId: "p1"}) +testing.assert(askedRotur.len == 1, "a MistWarp block still stops it") + +roturStatus = 500 +addNotification("owner", "stranger", "comment", {projectId: "p2"}) +testing.assert(loadNotifications("owner").len == 0, "when Rotur can't take it, a refused message signal stops the local copy") +messageRefused = [] +addNotification("owner", "friend", "comment", {projectId: "p3"}) +testing.assert(loadNotifications("owner").len == 1, "and an allowed one is kept locally") + +askedRotur = [] +messageRefused = ["student>owner"] +addNotification("owner", "student", "comment", {projectId: "p4"}) +testing.assert(askedRotur.len == 0, "a student's notification stays in MistWarp") +testing.assert(loadNotifications("owner").len == 2, "and classroom rules, not Rotur's message signal, decide it") diff --git a/src/rotur/rotur-signals.osl b/src/rotur/rotur-signals.osl index 7a44bad..6213197 100644 --- a/src/rotur/rotur-signals.osl +++ b/src/rotur/rotur-signals.osl @@ -1,7 +1,6 @@ export { commentRefusal, - isMessageNotification, - roturMessageRefusal, + roturMessageAllowed, roturPurchaseRefusal } @@ -32,16 +31,8 @@ def roturMessageSignal(string sender, string recipient) object ( return signal ) -def roturMessageRefusal(string sender, string recipient) string ( - object signal = roturMessageSignal(sender, recipient) - if signal.allowed == true ( - return "" - ) - return signal.reason.toStr() -) - -def isMessageNotification(string notifType) boolean ( - return ["comment", "reply", "mention", "profile_comment", "space_comment", "roadmap_comment", "donation"].contains(notifType) +def roturMessageAllowed(string sender, string recipient) boolean ( + return roturMessageSignal(sender, recipient).allowed == true ) def roturPurchaseRefusal(string buyer, number amount, string note) string ( diff --git a/src/rotur/rotur-signals.test.osl b/src/rotur/rotur-signals.test.osl index 0836718..1c96210 100644 --- a/src/rotur/rotur-signals.test.osl +++ b/src/rotur/rotur-signals.test.osl @@ -36,8 +36,7 @@ import "./rotur-signals.osl" refusals = {"owner>commenter": "settings"} testing.assert(commentRefusal("profile-owner", "owner", "c1") == "", "a message setting doesn't stop a public reply") -testing.assert(roturMessageRefusal("owner", "commenter") == "refused: settings", "it does stop the reply notifying them") -testing.assert(isMessageNotification("reply") and isMessageNotification("mention") and !isMessageNotification("follow"), "replies and mentions carry words, follows don't") +testing.assert(!roturMessageAllowed("owner", "commenter") and roturMessageAllowed("commenter", "owner"), "roturMessageAllowed reads the same signal") refusals = {"stranger>owner": "unavailable"} testing.assert(commentRefusal("project-p1", "stranger", "") == "", "someone who can't use MistWarp right now still has comments on their project") @@ -52,6 +51,6 @@ testing.assert(commentRefusal("profile-owner", "owner", "") == "", "commenting o asked = [] refusals = {} -roturMessageRefusal("a", "b") -roturMessageRefusal("a", "b") +commentRefusal("profile-b", "a", "") +commentRefusal("profile-b", "a", "") testing.assert(asked.len == 1, "an answer is kept for a minute: " ++ json.stringify(asked))