Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion opal.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mistwarp-api",
"version": "1.1.0",
"version": "1.1.1",
"main": "main.osl",
"scripts": {
"dev": ["osl", "run", "main.osl"],
Expand Down
12 changes: 7 additions & 5 deletions src/community/notifications.osl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand Down
129 changes: 129 additions & 0 deletions src/community/notifications.test.osl
Original file line number Diff line number Diff line change
@@ -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")
15 changes: 3 additions & 12 deletions src/rotur/rotur-signals.osl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export {
commentRefusal,
isMessageNotification,
roturMessageRefusal,
roturMessageAllowed,
roturPurchaseRefusal
}

Expand Down Expand Up @@ -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 (
Expand Down
7 changes: 3 additions & 4 deletions src/rotur/rotur-signals.test.osl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))
Loading