Profile Management

Estimated reading time: 13 minutes

In this part, we will learn about user and group profile management like managing name, picture, status, presence information, etc. for a user or a group

Prerequisites

You MUST go through following prerequisites before you read further.

  • Read one of the Android, iOS, JS section of this tutorial.

Profile Management

A profile allows you to associate human readable information such as name, picture, status, presence information, etc. with a user or a group.

In case of messaging apps, you may need to display the profiles of contacts as a list and display profile details when you a user clicks on them. You can fetch the list of contacts for a user by first uploading the contacts on the user’s device and then synchronizing it with mesibo.

Once you get a list of contacts you need to create, update and manage the profiles for these contacts. To do this mesibo provides a helpful set of Profile Managemnt APIs.

Before we go about how you can manage profiles for your contacts, Let us first look at the basic concepts in mesibo profile mangament and some of the profile mangament APIs along with code examples. You can refer to the source code of Mesibo Sample Apps for a more complete understanding of these APIs. I

An overview of how mesibo handles profiles

In mesibo, You can create a profile for every user or group. Mesibo maintains a local registry of profiles and sets the profile fields in MessageParams every time a message is received from a user and a group for which a profile is set. First you need to fetch your users or groups from your servers(contact synchronization) and then let Mesibo know about it using setProfile API. Mesibo manages each profile using UserProfile object. You can set various information like user or group name, picture, status, presence information, etc.

Let us first learn about the basic APIs in profile management and then look at how you can use these APIs in your app.

Creating a profile

Create a UserProfile object. Use createProfile to reate a user or a group profile.

For example in Android,

Mesibo.UserProfile userProfile = Mesibo.createProfile('test_user', 0, 'user_address');
Mesibo.UserProfile groupProfile = Mesibo.createProfile('test_group', 98765, NULL);

In iOS, using Objective-C

MesiboUserProfile u = [MesiboInstance createProfile:'test_user' groupid:0 name:nil];

Storing a profile

Set the UserProfile object in mesibo’s registry. You need to use setProfile to set the user’s profile is mesibo’s registry. This profile will be sent in message parameters everytime a one-to-one message or group message is sent by the user.

Example,

In Android,

Mesibo.setProfile(userProfile);
Mesibo.setProfile(groupProfile);

In iOS,

[MesiboInstance setProfile:u refresh:NO];

Accessing a profile

Once the UserProfile is set in mesibo’s registry, each user profile is accessible via getProfile

For example, in Android

if (mGroupId > 0) {
  mUserProfile = Mesibo.getUserProfile(mGroupId);
} else {
  mUserProfile = Mesibo.getUserProfile(mPeer);
}

In iOS,

MesiboUserProfile *u = [MesiboInstance getUserProfile:users[i]];

Setting your own profile

Your own profile is a special profile which is stored for the self user. To set your own profile use setSelfProfile

To access your profile set using setSelfProfile user getSelfProfile

Displaying a list of profiles

There are a set of APIs which allows you to access the profiles stored in the mesibo registry as a list of profiles. You can read this list at once and display them. You can use getProfiles, getSortedUserProfiles, getRecentProfiles

Updating a profile

When one user updates their profile, you can notify their contacts and updated the profile details for that user. Callback functionslike below will be called on updating a picture, online status, etc

@Override
public void Mesibo_onUpdateUserPicture(Mesibo.UserProfile userProfile, Bitmap bitmap, String s) {

}

@Override
public void Mesibo_onUpdateUserOnlineStatus(Mesibo.UserProfile userProfile, String s) {

}

Using the profile mangement APIs

For every available user who is using mesibo, you need to create a contact profile with details such as the name, picture, status,etc to display them.

Here, is an example in Android from the Mesibo Messenger App, which uses profile management APIs described above to create a profile for each contact and set it.

public static void createContact(String name, String phone,  long groupid, String status, String members, String photo, String tnBasee64, long ts, long when, boolean selfProfile, boolean refresh, int visibility) {
  Mesibo.UserProfile u = new Mesibo.UserProfile();
  u.address = phone;
  u.groupid = groupid;

  if(!selfProfile && 0 == u.groupid)
    u.name = phoneBookLookup(phone);

  if(TextUtils.isEmpty(u.name))
    u.name = name;

  if(TextUtils.isEmpty(u.name)) {
    u.name = phone;
    if(TextUtils.isEmpty(u.name))
      u.name = "Group-" + groupid;
  }

  if(groupid == 0 && !TextUtils.isEmpty(phone) && phone.equalsIgnoreCase("0")) {
    u.name = "hello";
    return;
  }

  u.status = status; // Base64.decode(c[i].status, Base64.DEFAULT).toString();

  if(groupid > 0) {
    u.groupMembers = members;
    String p = getPhone();
    if(null == p) return;
    //if members empty or doesn't contain myself, it means I am not a member or group deleted
    if(!members.contains(getPhone())) {
      updateDeletedGroup(groupid);
      return;
    }
    u.status = groupStatusFromMembers(members);
  }

  if(null == u.status) {
    u.status = "";
  }

  u.picturePath = photo;
  u.timestamp = ts;
  if(ts > 0 && u.timestamp > mContactTs)
    mContactTs = u.timestamp;

  if(when >= 0) {
    u.lastActiveTime = Mesibo.getTimestamp() - (when*1000);
  }

  if(!TextUtils.isEmpty(tnBasee64)) {
    byte[] tn = null;
    try {
      tn = Base64.decode(tnBasee64, Base64.DEFAULT);

      if(Mesibo.createFile(Mesibo.getFilePath(Mesibo.FileInfo.TYPE_PROFILETHUMBNAIL), photo, tn, true)) {
        //u.tnPath = photo;
      }
    } catch (Exception e) {}
  }

  if(visibility == VISIBILITY_HIDE)
    u.flag |= Mesibo.UserProfile.FLAG_HIDDEN;
  else if(visibility == VISIBILITY_UNCHANGED) {
    Mesibo.UserProfile tp = Mesibo.getUserProfile(phone, groupid);
    if(null != tp && (tp.flag&Mesibo.UserProfile.FLAG_HIDDEN) >0)
      u.flag |= Mesibo.UserProfile.FLAG_HIDDEN;
  }

  if(selfProfile) {
    AppConfig.getConfig().phone = u.address;
    Mesibo.setSelfProfile(u);
  }
  else
    Mesibo.setUserProfile(u, refresh);
}

Here, is an example in iOS from the Mesibo Messenger App, which uses profile management APIs described above to create a profile for each contact and set it.

-(void) createContact:(NSString *)name phone:(NSString *)phone groupid:(uint32_t)groupid status:(NSString *)status members:(NSString *)members photo:(NSString *)photo tnbase64:(NSString *)tnbase64 ts:(uint64_t)ts when:(int64_t)when selfProfile:(BOOL)selfProfile refresh:(BOOL)refresh visibility:(int)visibility {

  MesiboUserProfile *u = [[MesiboUserProfile alloc] init];
  u.address = phone;
  u.groupid = groupid;
  if(selfProfile) {
    u.groupid = 0;
    groupid = 0;
  }

  if(!selfProfile && 0 == u.groupid) {
    u.name = [self phoneBookLookup:phone];
  }

  if([SampleAPI isEmpty:u.name]) {
    u.name = name;
  }

  if([SampleAPI isEmpty:u.name]) {
    u.name = phone;
    if([SampleAPI isEmpty:u.name]) {
      u.name = [NSString stringWithFormat:@"Group-%d", groupid];
    }
  }

  if(0 == u.groupid && ![SampleAPI isEmpty:phone] && [phone isEqualToString:@"0"]) {
    //debug
    u.name = @"Hello - debug";
    return;
  }

  u.status = status;
  if(u.groupid) {
    u.groupMembers = members;
    NSString *phone = [self getPhone];
    if(!phone) {
      return;
    }
    if(![members containsString:phone]) {
      [self updateDeletedGroup:groupid];
      return;
    }
    u.status = [self groupStatusFromMembers:members];
  }

  if(!u.status) {
    u.status = @"";
  }

  u.picturePath = photo;
  u.timestamp = ts;
  if(!selfProfile &&  ts > 0 && u.timestamp > mContactTimestamp)
    mContactTimestamp = u.timestamp;

  if(when >= 0) {
    u.lastActiveTime = [MesiboInstance getTimestamp] - (when*100);
  }

  if([tnbase64 length] > 3) {
    NSData *tnData = [[NSData alloc] initWithBase64EncodedString:tnbase64 options:0];
    if(tnData && [tnData length] > 100) {
      NSString *imagePath = [MesiboInstance getFilePath:MESIBO_FILETYPE_PROFILETHUMBNAIL];
      if([MesiboInstance createFile:imagePath fileName:u.picturePath data:tnData overwrite:YES]) {
      }
    }
  }

  if(VISIBILITY_HIDE == visibility) {
    u.flag |= MESIBO_USERFLAG_HIDDEN;
  } else if(VISIBILITY_UNCHANGED == visibility) {
    MesiboUserProfile *tp = [MesiboInstance getProfile:u.address groupid:u.groupid];
    if(tp && (tp.flag&MESIBO_USERFLAG_HIDDEN)) {
      u.flag |= MESIBO_USERFLAG_HIDDEN;
    }
  }

  if(selfProfile) {
    mPhone = u.address;
    [MesiboInstance setSelfProfile:u];
  }
  else
    [MesiboInstance setProfile:u refresh:refresh];

}

There are more profile management APIs which can help you simplify your profile management. A complete reference can be found here

Synchronization >>

mesibo, android, ios