Skip to content

Commit

Permalink
Update: Use PrincipalIds between canisters (#22)
Browse files Browse the repository at this point in the history
* Update: Use PrincipalIds between canisters

* Fix: Add back in some whitespace

* Fix: Add back in some whitespace

* Fix: Remove extra imports
  • Loading branch information
Stanley Jones authored Apr 10, 2020
1 parent c0420d7 commit 59f754c
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 542 deletions.
12 changes: 6 additions & 6 deletions src/connectd/digraph.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ module {

public class Digraph() {

var vertexList : [Vertex] = [];
var edgeList : [(Vertex, Vertex)] = [];
var vertexList: [Vertex] = [];
var edgeList: [(Vertex, Vertex)] = [];

public func addVertex(vertex : Vertex) {
public func addVertex(vertex: Vertex) {
vertexList := Array.append<Vertex>(vertexList, [vertex]);
};

public func addEdge(fromVertex : Vertex, toVertex : Vertex) {
public func addEdge(fromVertex: Vertex, toVertex: Vertex) {
edgeList := Array.append<(Vertex, Vertex)>(edgeList, [(fromVertex, toVertex)]);
};

public func getAdjacent(vertex : Vertex) : [Vertex] {
var adjacencyList : [Vertex] = [];
public func getAdjacent(vertex: Vertex): [Vertex] {
var adjacencyList: [Vertex] = [];
for ((fromVertex, toVertex) in Iter.fromArray<(Vertex, Vertex)>(edgeList)) {
if (fromVertex == vertex) {
adjacencyList := Array.append<Vertex>(adjacencyList, [toVertex]);
Expand Down
8 changes: 4 additions & 4 deletions src/connectd/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import Types "./types";
type Vertex = Types.Vertex;

actor Connectd {
var graph : Digraph.Digraph = Digraph.Digraph();
var graph: Digraph.Digraph = Digraph.Digraph();

public func healthcheck() : async Bool { true };
public func healthcheck(): async Bool { true };

public func connect(userA : Vertex, userB : Vertex) : async () {
public func connect(userA: Vertex, userB: Vertex): async () {
graph.addEdge(userA, userB);
};

public func getConnections(user : Vertex) : async [Vertex] {
public func getConnections(user: Vertex): async [Vertex] {
graph.getAdjacent(user)
};

Expand Down
4 changes: 2 additions & 2 deletions src/connectd/types.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Hash "mo:stdlib/hash";
import Principal "mo:stdlib/principalId";

module {
public type Vertex = Nat32;
public type Vertex = Principal;
};
225 changes: 15 additions & 210 deletions src/linkedup/database.mo

Large diffs are not rendered by default.

54 changes: 22 additions & 32 deletions src/linkedup/main.mo
Original file line number Diff line number Diff line change
@@ -1,69 +1,59 @@
// Make the Connectd app's public methods available locally
import Connectd "canister:connectd";

import Database "./database";
import Types "./types";
import Utils "./utils";

type NewProfile = Types.NewProfile;
type Profile = Types.Profile;
type PrincipalId = Types.PrincipalId;
type UserId = Types.UserId;

actor LinkedUp {
var directory : Database.Directory = Database.Directory();
directory.seed();
var directory: Database.Directory = Database.Directory();

// Healthcheck

public func healthcheck () : async Bool { true };
public func healthcheck(): async Bool { true };

// Profiles

public shared(msg) func create (profile : NewProfile) : async PrincipalId {
let newUserId = Utils.getUserId(msg.caller);
directory.createOne(newUserId, profile);
newUserId
public shared(msg) func create(profile: NewProfile): async () {
directory.createOne(msg.caller, profile);
};

public shared(msg) func update (profile : Profile) : async () {
if (Utils.hasAccess(Utils.getUserId(msg.caller), profile)) {
public shared(msg) func update(profile: Profile): async () {
if(Utils.hasAccess(msg.caller, profile)) {
directory.updateOne(profile.id, profile);
};
};

public shared query(msg) func getOwn () : async Profile {
Utils.getProfile(directory, Utils.getUserId(msg.caller))
};

public query func get (userId : PrincipalId) : async Profile {
public query func get(userId: UserId): async Profile {
Utils.getProfile(directory, userId)
};

public query func search (term : Text) : async [Profile] {
public query func search(term: Text): async [Profile] {
directory.findBy(term)
};

// Connections

public shared(msg) func connect (userId : PrincipalId) : async () {
let callerId : PrincipalId = Utils.getUserId(msg.caller);
public shared(msg) func connect(userId: UserId): async () {
// Call Connectd's public methods without an API
await Connectd.connect(Utils.toEntryId(callerId), Utils.toEntryId(userId));
await Connectd.connect(msg.caller, userId);
};

public shared(msg) func getOwnConnections () : async [Profile] {
let callerId : PrincipalId = Utils.getUserId(msg.caller);
let entryIds = await Connectd.getConnections(Utils.toEntryId(callerId));
Utils.getConnectionProfiles(directory, entryIds)
public func getConnections(userId: UserId): async [Profile] {
let userIds = await Connectd.getConnections(userId);
directory.findMany(userIds)
};

public func getConnections (userId : PrincipalId) : async [Profile] {
let entryIds = await Connectd.getConnections(Utils.toEntryId(userId));
Utils.getConnectionProfiles(directory, entryIds)
public shared(msg) func isConnected(userId: UserId): async Bool {
let userIds = await Connectd.getConnections(msg.caller);
Utils.includes(userId, userIds)
};

public shared(msg) func isConnected (userId : PrincipalId) : async Bool {
let callerId : PrincipalId = Utils.getUserId(msg.caller);
let entryIds = await Connectd.getConnections(Utils.toEntryId(callerId));
Utils.includes(Utils.toEntryId(userId), entryIds)
};
// User Auth

public shared query(msg) func getOwnId(): async UserId { msg.caller }

};
Loading

0 comments on commit 59f754c

Please sign in to comment.