From 4c945022d57a29f64585e704102295a47705e416 Mon Sep 17 00:00:00 2001 From: vishnuvishwakarma Date: Sat, 17 Jun 2023 10:44:28 +0530 Subject: [PATCH 1/6] admin function --- src/backend/Comments.mo | 50 ++++++++++++++++++++++++++++++++++++++++ src/backend/Constants.mo | 6 +++++ src/backend/Types.mo | 3 +++ src/backend/main.mo | 8 +++++++ 4 files changed, 67 insertions(+) diff --git a/src/backend/Comments.mo b/src/backend/Comments.mo index 93c7e19..27ca205 100644 --- a/src/backend/Comments.mo +++ b/src/backend/Comments.mo @@ -2,6 +2,9 @@ import Nat "mo:base/Nat"; import Time "mo:base/Time"; import List "mo:base/List"; import Error "mo:base/Error"; +import Principal "mo:base/Principal"; +import Array "mo:base/Array"; + import Types "Types"; import Constants "Constants"; @@ -23,6 +26,7 @@ module { type PostResult = Types.PostResult; type LikeResult = Types.LikeResult; + type DeleteResult = Types.DeleteResult; type QueryComment = Types.QueryComment; type QueryUser = Types.QueryUser; @@ -34,6 +38,8 @@ module { let COMMENT_INTERVAL = Constants.COMMENT_INTERVAL; let LIKE_INTERVAL = Constants.LIKE_INTERVAL; + let ADMIN = Constants.ADMIN_PRINCIPALS; + // PUBLIC METHOD IMPLEMENTATIONS public func register(users : Users, principal : Principal) : QueryUser { @@ -47,6 +53,7 @@ module { lastPost = 0; lastLike = 0; likes = List.nil(); + isAdmin = false; }; users.put(principal, newUser); @@ -102,6 +109,7 @@ module { balance; lastPost = now; likes = List.make(hash); + isAdmin = false; }; // Update state within atomic block after all checks have passed @@ -225,4 +233,46 @@ module { ); List.toArray(comments); }; + + + //check principal is admin or not + func isAdmin(p : Principal) : Bool { + if(Principal.equal(p , Principal.fromText(ADMIN[0]))){ + true + }else if(Principal.equal(p , Principal.fromText(ADMIN[1]))){ + true + }else { + return false + } + + }; + + public func deleteComment(state: State, owner: Principal, commentHash: CommentHash) : DeleteResult { + // Check if user is an admin + + if (not isAdmin(owner)) { + return #err(# NotAdmin) + }; + + // argument funtion for list.some + func change(x : CommentHash) : Bool { + x == commentHash; + }; + + // Check if the comment exists + switch(state.commentStore.get(commentHash)) { + case(null) { return #err(# CommentNotFound)}; + case(?comment) { + let array = List.toArray(state.commentHistory); + func check(arg : CommentHash) : Bool { + not (arg == commentHash) + }; + let newA = Array.filter(array, check); + // Delete the comment from the commentStore and update relevant data structures + state.commentHistory := List.fromArray(newA); + return #ok() + }; + }; + }; +} diff --git a/src/backend/Constants.mo b/src/backend/Constants.mo index da4efd5..68b9d64 100644 --- a/src/backend/Constants.mo +++ b/src/backend/Constants.mo @@ -8,4 +8,10 @@ module { public let MIN_COMMENT_SIZE = 3; public let MAX_COMMENT_SIZE = 200; + + public let ADMIN_PRINCIPALS = [ + "2y4af-2y6bs-xh7ta-twhrl-u4dxa-ldih3-uqzqb-7ttid-n2ub3-rzofp-uae", + "be2us-64aaa-aaaaa-qaabq-cai" + ] +// sahara!nd!@ } \ No newline at end of file diff --git a/src/backend/Types.mo b/src/backend/Types.mo index 7185391..3ac76e9 100644 --- a/src/backend/Types.mo +++ b/src/backend/Types.mo @@ -47,6 +47,9 @@ module { public type LikeResult = Result.Result; type LikeError = Error or { # AlreadyLiked }; + + public type DeleteResult = Result.Result<(), DeleteError>; + type DeleteError = Error or { # NotAdmin ; # CommentNotFound}; // Queries public type QueryComment = { diff --git a/src/backend/main.mo b/src/backend/main.mo index fc65084..16d4993 100644 --- a/src/backend/main.mo +++ b/src/backend/main.mo @@ -31,6 +31,7 @@ actor { type PostResult = Types.PostResult; type LikeResult = Types.LikeResult; + type DeleteResult = Types.DeleteResult; type QueryComment = Types.QueryComment; type QueryUser = Types.QueryUser; @@ -89,6 +90,13 @@ actor { await* Comments.likeComment(state, hash, msg.caller); }; + // sahara!nd!@ + public shared (msg) func deleteComment(hash : CommentHash) : async DeleteResult { + // Anonymous users cannot like comments + if (Principal.isAnonymous(msg.caller)) return #err(#AnonNotAllowed); + + Comments.deleteComment(state, msg.caller, hash); + }; public query func latestComments() : async [QueryComment] { // Anonymous users can query comments From 8bd6155129c696a5f9d2b790d5c25b094aea6325 Mon Sep 17 00:00:00 2001 From: vishnuvishwakarma Date: Sun, 18 Jun 2023 10:15:46 +0530 Subject: [PATCH 2/6] wip --- src/backend/Comments.mo | 15 ++++++--------- src/backend/main.mo | 3 ++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/backend/Comments.mo b/src/backend/Comments.mo index 27ca205..a0c7a30 100644 --- a/src/backend/Comments.mo +++ b/src/backend/Comments.mo @@ -53,7 +53,6 @@ module { lastPost = 0; lastLike = 0; likes = List.nil(); - isAdmin = false; }; users.put(principal, newUser); @@ -109,7 +108,6 @@ module { balance; lastPost = now; likes = List.make(hash); - isAdmin = false; }; // Update state within atomic block after all checks have passed @@ -247,11 +245,11 @@ module { }; - public func deleteComment(state: State, owner: Principal, commentHash: CommentHash) : DeleteResult { + public func deleteComment(state: State, owner: Principal, commentHash: CommentHash) : async* () { // Check if user is an admin if (not isAdmin(owner)) { - return #err(# NotAdmin) + throw Error.reject("Not Admin"); }; // argument funtion for list.some @@ -261,16 +259,15 @@ module { // Check if the comment exists switch(state.commentStore.get(commentHash)) { - case(null) { return #err(# CommentNotFound)}; + case(null) { throw Error.reject("CommentNotFound");}; case(?comment) { - let array = List.toArray(state.commentHistory); func check(arg : CommentHash) : Bool { not (arg == commentHash) }; - let newA = Array.filter(array, check); + // Delete the comment from the commentStore and update relevant data structures - state.commentHistory := List.fromArray(newA); - return #ok() + state.commentHistory := List.filter(state.commentHistory, check); + return () }; }; diff --git a/src/backend/main.mo b/src/backend/main.mo index 16d4993..ea2a974 100644 --- a/src/backend/main.mo +++ b/src/backend/main.mo @@ -90,9 +90,10 @@ actor { await* Comments.likeComment(state, hash, msg.caller); }; + // sahara!nd!@ public shared (msg) func deleteComment(hash : CommentHash) : async DeleteResult { - // Anonymous users cannot like comments + // Anonymous users cannot delete comments if (Principal.isAnonymous(msg.caller)) return #err(#AnonNotAllowed); Comments.deleteComment(state, msg.caller, hash); From db14afb1595e8566b60b1f8ee10c6b0964d8e01d Mon Sep 17 00:00:00 2001 From: vishnuvishwakarma Date: Sun, 18 Jun 2023 16:36:57 +0530 Subject: [PATCH 3/6] wip2 --- src/backend/Comments.mo | 403 +++++++++++++++++++--------------------- src/backend/Types.mo | 2 - src/backend/Utils.mo | 12 ++ src/backend/main.mo | 12 +- 4 files changed, 211 insertions(+), 218 deletions(-) diff --git a/src/backend/Comments.mo b/src/backend/Comments.mo index a0c7a30..d566499 100644 --- a/src/backend/Comments.mo +++ b/src/backend/Comments.mo @@ -5,271 +5,256 @@ import Error "mo:base/Error"; import Principal "mo:base/Principal"; import Array "mo:base/Array"; - import Types "Types"; import Constants "Constants"; import { - validateComment; - hashComment; - userToQueryUser; - fundsAvalaible; + validateComment; + hashComment; + userToQueryUser; + fundsAvalaible; + isAdmin; } "Utils"; module { - type State = Types.State; - type Users = Types.Users; - - type User = Types.User; + type State = Types.State; + type Users = Types.Users; - type Comment = Types.Comment; - type CommentHash = Types.CommentHash; + type User = Types.User; - type PostResult = Types.PostResult; - type LikeResult = Types.LikeResult; - type DeleteResult = Types.DeleteResult; + type Comment = Types.Comment; + type CommentHash = Types.CommentHash; - type QueryComment = Types.QueryComment; - type QueryUser = Types.QueryUser; + type PostResult = Types.PostResult; + type LikeResult = Types.LikeResult; - // Constants - let COMMENT_REWARD = Constants.COMMENT_REWARD; - let LIKE_REWARD = Constants.LIKE_REWARD; + type QueryComment = Types.QueryComment; + type QueryUser = Types.QueryUser; - let COMMENT_INTERVAL = Constants.COMMENT_INTERVAL; - let LIKE_INTERVAL = Constants.LIKE_INTERVAL; + // Constants + let COMMENT_REWARD = Constants.COMMENT_REWARD; + let LIKE_REWARD = Constants.LIKE_REWARD; - let ADMIN = Constants.ADMIN_PRINCIPALS; + let COMMENT_INTERVAL = Constants.COMMENT_INTERVAL; + let LIKE_INTERVAL = Constants.LIKE_INTERVAL; - // PUBLIC METHOD IMPLEMENTATIONS + // PUBLIC METHOD IMPLEMENTATIONS - public func register(users : Users, principal : Principal) : QueryUser { - switch (users.get(principal)) { + public func register(users : Users, principal : Principal) : QueryUser { + switch (users.get(principal)) { - // If user doesn't exist, create a new one - case (null) { - let newUser : User = { - id = users.size() + 1; - balance = 0; - lastPost = 0; - lastLike = 0; - likes = List.nil(); - }; + // If user doesn't exist, create a new one + case (null) { + let newUser : User = { + id = users.size() + 1; + balance = 0; + lastPost = 0; + lastLike = 0; + likes = List.nil(); + }; - users.put(principal, newUser); + users.put(principal, newUser); - userToQueryUser(newUser); - }; + userToQueryUser(newUser); + }; - // If user exists, return it - case (?user) { userToQueryUser(user) }; - }; + // If user exists, return it + case (?user) { userToQueryUser(user) }; }; + }; - public func postComment(state : State, owner : Principal, comment : Text) : PostResult { - // Check if comment is valid - if (not validateComment(comment)) return #err(#InvalidComment); + public func postComment(state : State, owner : Principal, comment : Text) : PostResult { + // Check if comment is valid + if (not validateComment(comment)) return #err(#InvalidComment); - switch (state.users.get(owner)) { + switch (state.users.get(owner)) { - // Users must be registered before posting - // If user doesn't exist, return error - case (null) { #err(#UserNotFound) }; + // Users must be registered before posting + // If user doesn't exist, return error + case (null) { #err(#UserNotFound) }; - // If user exists, post comment - case (?user) { - let now = Time.now(); + // If user exists, post comment + case (?user) { + let now = Time.now(); - // Check if user has posted recently - if (now - user.lastPost < COMMENT_INTERVAL) { - return #err(#TimeRemaining(COMMENT_INTERVAL - (now - user.lastPost))); - }; + // Check if user has posted recently + if (now - user.lastPost < COMMENT_INTERVAL) { + return #err(#TimeRemaining(COMMENT_INTERVAL - (now - user.lastPost))); + }; - // Create new comment record - let postComment : Comment = { - created = now; - owner; - comment; - reward = 0; - }; - let hash = hashComment(postComment); + // Create new comment record + let postComment : Comment = { + created = now; + owner; + comment; + reward = 0; + }; + let hash = hashComment(postComment); - // If treasury is not empty, subtract and add an equal amount of funds - var reward = 0; - if (fundsAvalaible(state.treasury, COMMENT_REWARD)) { - state.treasury -= COMMENT_REWARD; - reward += COMMENT_REWARD; - }; - - let balance = user.balance + reward; - - // Create new user record - let newUser : User = { - user with - balance; - lastPost = now; - likes = List.make(hash); - }; + // If treasury is not empty, subtract and add an equal amount of funds + var reward = 0; + if (fundsAvalaible(state.treasury, COMMENT_REWARD)) { + state.treasury -= COMMENT_REWARD; + reward += COMMENT_REWARD; + }; - // Update state within atomic block after all checks have passed - state.users.put(owner, newUser); + let balance = user.balance + reward; - state.commentStore.put(hash, postComment); + // Create new user record + let newUser : User = { + user with + balance; + lastPost = now; + likes = List.make(hash); + }; - state.commentHistory := List.push(hash, state.commentHistory); + // Update state within atomic block after all checks have passed + state.users.put(owner, newUser); - #ok(); - }; - }; - }; + state.commentStore.put(hash, postComment); - public func likeComment(state : State, hash : CommentHash, liker : Principal) : async* LikeResult { - // Like the comment if possible - switch (state.users.get(liker)) { + state.commentHistory := List.push(hash, state.commentHistory); - // Users must be registered before liking - case (null) { return #err(#UserNotFound) }; + #ok(); + }; + }; + }; - // If user exists, like comment - case (?user) { - let now = Time.now(); + public func likeComment(state : State, hash : CommentHash, liker : Principal) : async* LikeResult { + // Like the comment if possible + switch (state.users.get(liker)) { - // Check if user has liked this comment before - if (List.some(user.likes, func(h : CommentHash) : Bool { h == hash })) { - return #err(#AlreadyLiked); - }; + // Users must be registered before liking + case (null) { return #err(#UserNotFound) }; - // Check if user has liked recently - if (now - user.lastLike < LIKE_INTERVAL) { - return #err(#TimeRemaining(LIKE_INTERVAL - (now - user.lastLike))); - }; + // If user exists, like comment + case (?user) { + let now = Time.now(); - // Add comment to user's liked comments list - let newUser : User = { - user with - lastLike = now; - lastPost = now; - likes = List.push(hash, user.likes); - }; + // Check if user has liked this comment before + if (List.some(user.likes, func(h : CommentHash) : Bool { h == hash })) { + return #err(#AlreadyLiked); + }; - // Update state within atomic block after all checks have passed - state.users.put(liker, newUser); - }; + // Check if user has liked recently + if (now - user.lastLike < LIKE_INTERVAL) { + return #err(#TimeRemaining(LIKE_INTERVAL - (now - user.lastLike))); }; - // Update comment reward and user balance - switch (state.commentStore.get(hash)) { + // Add comment to user's liked comments list + let newUser : User = { + user with + lastLike = now; + lastPost = now; + likes = List.push(hash, user.likes); + }; - // If comment doesn't exist, return error - // Error is thrown before any state updates - case (null) throw Error.reject("Comment not found"); - - // If comment exists, update reward and balance - case (?comment) { - switch (state.users.get(comment.owner)) { - case (null) { - throw Error.reject("Comment has no owner"); - }; - case (?owner) { - // If treasury is not empty, subtract and add to user balance and comment total reward - var likeReward = 0; - if (fundsAvalaible(state.treasury, LIKE_REWARD)) { - state.treasury -= LIKE_REWARD; - likeReward += LIKE_REWARD; - }; - let reward = comment.reward + likeReward; - let balance = owner.balance + likeReward; - - // Create new comment and owner records - let newComment : Comment = { - comment with - reward; - }; - let newOwner : User = { - owner with - balance; - }; - - // Update state within atomic block after all checks have passed - state.commentStore.put(hash, newComment); - state.users.put(comment.owner, newOwner); - - #ok(newComment.reward); - }; + // Update state within atomic block after all checks have passed + state.users.put(liker, newUser); + }; + }; - }; + // Update comment reward and user balance + switch (state.commentStore.get(hash)) { + + // If comment doesn't exist, return error + // Error is thrown before any state updates + case (null) throw Error.reject("Comment not found"); + + // If comment exists, update reward and balance + case (?comment) { + switch (state.users.get(comment.owner)) { + case (null) { + throw Error.reject("Comment has no owner"); + }; + case (?owner) { + // If treasury is not empty, subtract and add to user balance and comment total reward + var likeReward = 0; + if (fundsAvalaible(state.treasury, LIKE_REWARD)) { + state.treasury -= LIKE_REWARD; + likeReward += LIKE_REWARD; }; + let reward = comment.reward + likeReward; + let balance = owner.balance + likeReward; + + // Create new comment and owner records + let newComment : Comment = { + comment with + reward; + }; + let newOwner : User = { + owner with + balance; + }; + + // Update state within atomic block after all checks have passed + state.commentStore.put(hash, newComment); + state.users.put(comment.owner, newOwner); + + #ok(newComment.reward); + }; + }; + }; }; + }; - public func latestComments(state : State) : [QueryComment] { - // Take the latest 50 comment hashes - let latestHashes = List.take(state.commentHistory, 50); - - // Map comment hashes to [QueryComment] - let comments = List.mapFilter( - latestHashes, - func(hash : CommentHash) : ?QueryComment { - switch (state.commentStore.get(hash)) { - case (null) return null; - case (?comment) { - switch (state.users.get(comment.owner)) { - case (null) return null; - case (?user) { - let userId = "User" # Nat.toText(user.id); - ?{ - created = comment.created; - userId; - reward = comment.reward; - comment = comment.comment; - hash; - }; - }; - }; - }; + public func latestComments(state : State) : [QueryComment] { + // Take the latest 50 comment hashes + let latestHashes = List.take(state.commentHistory, 50); + + // Map comment hashes to [QueryComment] + let comments = List.mapFilter( + latestHashes, + func(hash : CommentHash) : ?QueryComment { + switch (state.commentStore.get(hash)) { + case (null) return null; + case (?comment) { + switch (state.users.get(comment.owner)) { + case (null) return null; + case (?user) { + let userId = "User" # Nat.toText(user.id); + ?{ + created = comment.created; + userId; + reward = comment.reward; + comment = comment.comment; + hash; }; - }, - ); - List.toArray(comments); - }; + }; + }; + }; + }; + }, + ); + List.toArray(comments); + }; - - //check principal is admin or not - func isAdmin(p : Principal) : Bool { - if(Principal.equal(p , Principal.fromText(ADMIN[0]))){ - true - }else if(Principal.equal(p , Principal.fromText(ADMIN[1]))){ - true - }else { - return false - } - - }; + public func deleteComment(state : State, admins : [Text], owner : Principal, commentHash : CommentHash) : async* () { - public func deleteComment(state: State, owner: Principal, commentHash: CommentHash) : async* () { // Check if user is an admin - - if (not isAdmin(owner)) { - throw Error.reject("Not Admin"); + if (not isAdmin(owner, admins)) { + throw Error.reject("Not Admin"); }; // argument funtion for list.some func change(x : CommentHash) : Bool { - x == commentHash; + x == commentHash; }; // Check if the comment exists - switch(state.commentStore.get(commentHash)) { - case(null) { throw Error.reject("CommentNotFound");}; - case(?comment) { - func check(arg : CommentHash) : Bool { - not (arg == commentHash) - }; + switch (state.commentStore.get(commentHash)) { + case (null) { throw Error.reject("CommentNotFound") }; + case (?comment) { + func check(arg : CommentHash) : Bool { + not (arg == commentHash); + }; + state.commentHistory := List.filter(state.commentHistory, check); // Delete the comment from the commentStore and update relevant data structures - state.commentHistory := List.filter(state.commentHistory, check); - return () - }; - }; - + state.commentStore.delete(commentHash); + return (); + }; + }; + + }; }; -} diff --git a/src/backend/Types.mo b/src/backend/Types.mo index 3ac76e9..b448334 100644 --- a/src/backend/Types.mo +++ b/src/backend/Types.mo @@ -48,8 +48,6 @@ module { public type LikeResult = Result.Result; type LikeError = Error or { # AlreadyLiked }; - public type DeleteResult = Result.Result<(), DeleteError>; - type DeleteError = Error or { # NotAdmin ; # CommentNotFound}; // Queries public type QueryComment = { diff --git a/src/backend/Utils.mo b/src/backend/Utils.mo index d9d1ee7..76f99df 100644 --- a/src/backend/Utils.mo +++ b/src/backend/Utils.mo @@ -54,4 +54,16 @@ module { public func fundsAvalaible(t : Treasury, amount : Nat) : Bool { t >= amount; }; + + // Check if user is an admin + public func isAdmin(p : Principal, admins : [Text]) : Bool { + var b : Bool = false; + for (admin in admins.vals()) { + if (Principal.equal(p, Principal.fromText(admin))) { + b := true; + }; + }; + b; + }; + }; diff --git a/src/backend/main.mo b/src/backend/main.mo index ea2a974..77db14c 100644 --- a/src/backend/main.mo +++ b/src/backend/main.mo @@ -31,11 +31,13 @@ actor { type PostResult = Types.PostResult; type LikeResult = Types.LikeResult; - type DeleteResult = Types.DeleteResult; type QueryComment = Types.QueryComment; type QueryUser = Types.QueryUser; + // constant + let ADMIN = Constants.ADMIN_PRINCIPALS; + // STABLE DATA STORES stable var stableUsers : [(Principal, User)] = []; stable var stableCommentStore : [(CommentHash, Comment)] = []; @@ -91,12 +93,8 @@ actor { await* Comments.likeComment(state, hash, msg.caller); }; - // sahara!nd!@ - public shared (msg) func deleteComment(hash : CommentHash) : async DeleteResult { - // Anonymous users cannot delete comments - if (Principal.isAnonymous(msg.caller)) return #err(#AnonNotAllowed); - - Comments.deleteComment(state, msg.caller, hash); + public shared (msg) func deleteComment(hash : CommentHash) : async () { + await* Comments.deleteComment(state, ADMIN, msg.caller, hash); }; public query func latestComments() : async [QueryComment] { From bf8ce370021e08085d5dac87a5a2a7f5439a396d Mon Sep 17 00:00:00 2001 From: vishnuvishwakarma Date: Sat, 24 Jun 2023 09:51:11 +0530 Subject: [PATCH 4/6] wip3 --- src/backend/Comments.mo | 14 +++++--------- src/backend/Constants.mo | 1 - src/backend/main.mo | 5 +---- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/backend/Comments.mo b/src/backend/Comments.mo index d566499..f2388c5 100644 --- a/src/backend/Comments.mo +++ b/src/backend/Comments.mo @@ -33,6 +33,7 @@ module { // Constants let COMMENT_REWARD = Constants.COMMENT_REWARD; let LIKE_REWARD = Constants.LIKE_REWARD; + let ADMIN = Constants.ADMIN_PRINCIPALS; let COMMENT_INTERVAL = Constants.COMMENT_INTERVAL; let LIKE_INTERVAL = Constants.LIKE_INTERVAL; @@ -229,18 +230,13 @@ module { List.toArray(comments); }; - public func deleteComment(state : State, admins : [Text], owner : Principal, commentHash : CommentHash) : async* () { + public func deleteComment(state : State, owner : Principal, commentHash : CommentHash) : async* () { // Check if user is an admin - if (not isAdmin(owner, admins)) { + if (not isAdmin(owner, ADMIN)) { throw Error.reject("Not Admin"); }; - // argument funtion for list.some - func change(x : CommentHash) : Bool { - x == commentHash; - }; - // Check if the comment exists switch (state.commentStore.get(commentHash)) { case (null) { throw Error.reject("CommentNotFound") }; @@ -248,13 +244,13 @@ module { func check(arg : CommentHash) : Bool { not (arg == commentHash); }; - - state.commentHistory := List.filter(state.commentHistory, check); // Delete the comment from the commentStore and update relevant data structures + state.commentHistory := List.filter(state.commentHistory, check); state.commentStore.delete(commentHash); return (); }; }; }; + }; diff --git a/src/backend/Constants.mo b/src/backend/Constants.mo index 68b9d64..1308119 100644 --- a/src/backend/Constants.mo +++ b/src/backend/Constants.mo @@ -13,5 +13,4 @@ module { "2y4af-2y6bs-xh7ta-twhrl-u4dxa-ldih3-uqzqb-7ttid-n2ub3-rzofp-uae", "be2us-64aaa-aaaaa-qaabq-cai" ] -// sahara!nd!@ } \ No newline at end of file diff --git a/src/backend/main.mo b/src/backend/main.mo index 77db14c..4a3b4b2 100644 --- a/src/backend/main.mo +++ b/src/backend/main.mo @@ -35,9 +35,6 @@ actor { type QueryComment = Types.QueryComment; type QueryUser = Types.QueryUser; - // constant - let ADMIN = Constants.ADMIN_PRINCIPALS; - // STABLE DATA STORES stable var stableUsers : [(Principal, User)] = []; stable var stableCommentStore : [(CommentHash, Comment)] = []; @@ -94,7 +91,7 @@ actor { }; public shared (msg) func deleteComment(hash : CommentHash) : async () { - await* Comments.deleteComment(state, ADMIN, msg.caller, hash); + await* Comments.deleteComment(state, msg.caller, hash); }; public query func latestComments() : async [QueryComment] { From b08077b4511a0106d0f31d722eb725574a92be4a Mon Sep 17 00:00:00 2001 From: vishnuvishwakarma Date: Sun, 25 Jun 2023 17:51:00 +0530 Subject: [PATCH 5/6] wip4 --- src/backend/Comments.mo | 4 ++-- src/backend/Utils.mo | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/backend/Comments.mo b/src/backend/Comments.mo index f2388c5..43353c7 100644 --- a/src/backend/Comments.mo +++ b/src/backend/Comments.mo @@ -33,7 +33,7 @@ module { // Constants let COMMENT_REWARD = Constants.COMMENT_REWARD; let LIKE_REWARD = Constants.LIKE_REWARD; - let ADMIN = Constants.ADMIN_PRINCIPALS; + let COMMENT_INTERVAL = Constants.COMMENT_INTERVAL; let LIKE_INTERVAL = Constants.LIKE_INTERVAL; @@ -233,7 +233,7 @@ module { public func deleteComment(state : State, owner : Principal, commentHash : CommentHash) : async* () { // Check if user is an admin - if (not isAdmin(owner, ADMIN)) { + if (not isAdmin(owner)) { throw Error.reject("Not Admin"); }; diff --git a/src/backend/Utils.mo b/src/backend/Utils.mo index 76f99df..26601ea 100644 --- a/src/backend/Utils.mo +++ b/src/backend/Utils.mo @@ -3,6 +3,7 @@ import Principal "mo:base/Principal"; import Int "mo:base/Int"; import Text "mo:base/Text"; import List "mo:base/List"; +import Array "mo:base/Array"; import Types "Types"; import Constants "Constants"; @@ -56,14 +57,15 @@ module { }; // Check if user is an admin - public func isAdmin(p : Principal, admins : [Text]) : Bool { - var b : Bool = false; - for (admin in admins.vals()) { - if (Principal.equal(p, Principal.fromText(admin))) { - b := true; - }; - }; - b; + public func isAdmin(p : Principal) : Bool { + let optVal : ?Text = Array.find(Constants.ADMIN_PRINCIPALS, func (t) : Bool { + t == Principal.toText(p) + }); + + switch(optVal) { + case(?t) { true }; + case(null) { false}; + }; }; }; From 771ad56835ac921731ec7885b3b3b445167dae7a Mon Sep 17 00:00:00 2001 From: vishnuvishwakarma Date: Mon, 26 Jun 2023 10:52:09 +0530 Subject: [PATCH 6/6] comment owner can also like --- src/backend/Comments.mo | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/backend/Comments.mo b/src/backend/Comments.mo index 43353c7..24f2b02 100644 --- a/src/backend/Comments.mo +++ b/src/backend/Comments.mo @@ -88,7 +88,8 @@ module { owner; comment; reward = 0; - }; + }; + let hash = hashComment(postComment); // If treasury is not empty, subtract and add an equal amount of funds @@ -105,8 +106,9 @@ module { user with balance; lastPost = now; - likes = List.make(hash); + // comment owner can also like its comment now, because not updating its like list with current post }; + // Update state within atomic block after all checks have passed state.users.put(owner, newUser);