Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
feat(backend): add root document field for the account profile
Browse files Browse the repository at this point in the history
  • Loading branch information
burdiyan committed Mar 21, 2024
1 parent 41d236c commit 309a95b
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 51 deletions.
19 changes: 19 additions & 0 deletions backend/daemon/api/accounts/v1alpha/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"mintter/backend/hyper"
"mintter/backend/hyper/hypersql"
"mintter/backend/pkg/future"
"regexp"
"strings"

"crawshaw.io/sqlite"
Expand Down Expand Up @@ -121,6 +122,11 @@ func (srv *Server) GetAccount(ctx context.Context, in *accounts.GetAccountReques
acc.Profile.Avatar = v.(cid.Cid).String()
}

v, ok = entity.Get("rootDocument")
if ok {
acc.Profile.RootDocument = v.(string)
}

return acc, nil
}

Expand Down Expand Up @@ -170,6 +176,8 @@ func (srv *Server) UpdateProfile(ctx context.Context, in *accounts.Profile) (*ac
return srv.GetAccount(ctx, &accounts.GetAccountRequest{})
}

var rootDocMatch = regexp.MustCompile(`^hm:\/\/d\/[a-zA-Z0-9]+$`)

// UpdateProfile is public so it can be called from sites.
func UpdateProfile(ctx context.Context, me core.Identity, blobs *hyper.Storage, in *accounts.Profile) error {
eid := hyper.EntityID("hm://a/" + me.Account().Principal().String())
Expand Down Expand Up @@ -209,6 +217,17 @@ func UpdateProfile(ctx context.Context, me core.Identity, blobs *hyper.Storage,
}
}

if in.RootDocument != "" {
if !rootDocMatch.MatchString(in.RootDocument) {
return status.Errorf(codes.InvalidArgument, "root document must be ID of a document entity in form of 'hm://d/<id>' got: %s", in.RootDocument)
}

v, ok := e.Get("rootDocument")
if (ok && v.(string) != in.RootDocument) || (!ok && in.RootDocument != "") {
patch["rootDocument"] = in.RootDocument
}
}

if len(patch) == 0 {
return nil
}
Expand Down
39 changes: 39 additions & 0 deletions backend/daemon/daemon_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
accounts "mintter/backend/genproto/accounts/v1alpha"
daemon "mintter/backend/genproto/daemon/v1alpha"
documents "mintter/backend/genproto/documents/v1alpha"
entities "mintter/backend/genproto/entities/v1alpha"
networking "mintter/backend/genproto/networking/v1alpha"
"mintter/backend/ipfs"
"mintter/backend/mttnet"
Expand Down Expand Up @@ -382,6 +383,44 @@ func TestNetworkingListPeers(t *testing.T) {
require.Len(t, pList.Peers, 1)
}

func TestAccountRootDocument(t *testing.T) {
t.Parallel()

alice := makeTestApp(t, "alice", makeTestConfig(t), true)
ctx := context.Background()

var rootDocID string
{
draft, err := alice.RPC.Documents.CreateDraft(ctx, &documents.CreateDraftRequest{})
require.NoError(t, err)

resp, err := alice.RPC.Documents.UpdateDraft(ctx, &documents.UpdateDraftRequest{
DocumentId: draft.Id,
Changes: []*documents.DocumentChange{
{Op: &documents.DocumentChange_SetTitle{SetTitle: "My Root Document"}},
},
})
draft = resp.UpdatedDocument

pub, err := alice.RPC.Documents.PublishDraft(ctx, &documents.PublishDraftRequest{DocumentId: draft.Id})
require.NoError(t, err)

rootDocID = pub.Document.Id
}

acc, err := alice.RPC.Accounts.UpdateProfile(ctx, &accounts.Profile{
RootDocument: rootDocID,
})
require.NoError(t, err)

require.Equal(t, rootDocID, acc.Profile.RootDocument)

mentions, err := alice.RPC.Entities.ListEntityMentions(ctx, &entities.ListEntityMentionsRequest{Id: rootDocID})
require.NoError(t, err)

require.Len(t, mentions.Mentions, 1, "root document must have a mentions from the account")
}

func getAddrs(t *testing.T, a *App) []string {
return mttnet.AddrInfoToStrings(a.Net.MustGet().AddrInfo())
}
Expand Down
100 changes: 58 additions & 42 deletions backend/genproto/accounts/v1alpha/accounts.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions backend/hyper/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ func (bs *indexer) indexChange(idx *indexingCtx, id int64, c cid.Cid, v Change)
if v, ok := v.Patch["avatar"].(cid.Cid); ok {
sb.AddBlobLink("account/avatar", v)
}
alias, ok := v.Patch["alias"].(string)
if ok {

if alias, ok := v.Patch["alias"].(string); ok {
sb.Meta = alias
}

if doc, ok := v.Patch["rootDocument"].(string); ok {
sb.AddResourceLink("account/root-document", IRI(doc), false, nil)
}
case v.Entity.HasPrefix("hm://d/"):
title, ok := v.Patch["title"].(string)
if ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const Accounts = {
},
/**
* Set or unset the trustness of an account. An account is untrusted by default except for our own.
* Returns the modified account.
* Returns the modified account.
*
* @generated from rpc com.mintter.accounts.v1alpha.Accounts.SetAccountTrust
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,40 @@ export class Account extends Message<Account> {
}

/**
* Profile information of the user Account.
*
* @generated from message com.mintter.accounts.v1alpha.Profile
*/
export class Profile extends Message<Profile> {
/**
* Optional. Alias is a user-defined handle for their profile.
* There's no enforcement on this being unique.
*
* @generated from field: string alias = 1;
*/
alias = "";

/**
* Optional. Description of the user's profile.
*
* @generated from field: string bio = 2;
*/
bio = "";

/**
* Optional. CID to the avatar image hosted on IPFS.
*
* @generated from field: string avatar = 3;
*/
avatar = "";

/**
* Optional. Hypermedia ID of the Account's root/entrypoint document.
*
* @generated from field: string root_document = 4;
*/
rootDocument = "";

constructor(data?: PartialMessage<Profile>) {
super();
proto3.util.initPartial(data, this);
Expand All @@ -224,6 +240,7 @@ export class Profile extends Message<Profile> {
{ no: 1, name: "alias", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 2, name: "bio", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 3, name: "avatar", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 4, name: "root_document", kind: "scalar", T: 9 /* ScalarType.STRING */ },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Profile {
Expand Down
11 changes: 9 additions & 2 deletions proto/accounts/v1alpha/accounts.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ service Accounts {
rpc ListAccounts(ListAccountsRequest) returns (ListAccountsResponse);

// Set or unset the trustness of an account. An account is untrusted by default except for our own.
// Returns the modified account.
// Returns the modified account.
rpc SetAccountTrust(SetAccountTrustRequest) returns (Account);

}

message GetAccountRequest {
Expand Down Expand Up @@ -55,12 +54,20 @@ message Account {
bool is_trusted = 4;
}

// Profile information of the user Account.
message Profile {
// Optional. Alias is a user-defined handle for their profile.
// There's no enforcement on this being unique.
string alias = 1;

// Optional. Description of the user's profile.
string bio = 2;

// Optional. CID to the avatar image hosted on IPFS.
string avatar = 3;

// Optional. Hypermedia ID of the Account's root/entrypoint document.
string root_document = 4;
}

message Device {
Expand Down
4 changes: 2 additions & 2 deletions proto/accounts/v1alpha/go.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: c4600f222089723131e27d5ec269510c
outs: 6b8605d5d48ac7813f418028e07b4e76
srcs: 257538a4fdc6194f93c68ec9239649fd
outs: cbb85d01a8c375c8dcf2b7af5a9490ed
4 changes: 2 additions & 2 deletions proto/accounts/v1alpha/js.gensum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
srcs: c4600f222089723131e27d5ec269510c
outs: d6f65d7d66d5dfdb49e0d5a284809309
srcs: 257538a4fdc6194f93c68ec9239649fd
outs: 1fd37955cd492767debe73b4311043f4

0 comments on commit 309a95b

Please sign in to comment.