ENGINE-463 - get_identities_by_userid ENGINE-463
authorKrista Bennett <krista@pep-project.org>
Tue, 02 Oct 2018 13:32:52 +0200
branchENGINE-463
changeset 296629f67097eaf0
parent 2965 a15e73c2a4e8
child 2967 bb34d5f60fd5
ENGINE-463 - get_identities_by_userid
src/pEpEngine.c
src/pEp_internal.h
     1.1 --- a/src/pEpEngine.c	Tue Oct 02 13:26:19 2018 +0200
     1.2 +++ b/src/pEpEngine.c	Tue Oct 02 13:32:52 2018 +0200
     1.3 @@ -113,6 +113,7 @@
     1.4  
     1.5  static const char *sql_get_identities_by_address =  
     1.6      "select user_id, identity.main_key_id, username, lang,"
     1.7 +
     1.8      "   identity.flags, is_own"
     1.9      "   from identity"
    1.10      "   join person on id = identity.user_id"
    1.11 @@ -123,6 +124,15 @@
    1.12      "          end) = 1 "
    1.13      "   order by is_own desc, "
    1.14      "   timestamp desc; ";
    1.15 +    
    1.16 +static const char* sql_get_identities_by_userid =
    1.17 +    "select address, identity.main_key_id, username, lang"
    1.18 +    "   identity.flags, is_own"
    1.19 +    "   from identity"
    1.20 +    "   join person on id = identity.user_id"
    1.21 +    "   where identity.user_id = ?1 "
    1.22 +    "   order by is_own desc, "
    1.23 +    "   timestamp desc; ";
    1.24  
    1.25  static const char *sql_replace_identities_fpr =  
    1.26      "update identity"
    1.27 @@ -1073,6 +1083,11 @@
    1.28              (int)strlen(sql_get_identities_by_address), 
    1.29              &_session->get_identities_by_address, NULL);
    1.30      assert(int_result == SQLITE_OK);
    1.31 +    
    1.32 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_userid,
    1.33 +            (int)strlen(sql_get_identities_by_userid), 
    1.34 +            &_session->get_identities_by_userid, NULL);
    1.35 +    assert(int_result == SQLITE_OK);
    1.36  
    1.37      int_result = sqlite3_prepare_v2(_session->db, sql_get_user_default_key,
    1.38              (int)strlen(sql_get_user_default_key), &_session->get_user_default_key, NULL);
    1.39 @@ -1386,6 +1401,8 @@
    1.40                  sqlite3_finalize(session->get_identity_without_trust_check);
    1.41              if (session->get_identities_by_address)
    1.42                  sqlite3_finalize(session->get_identities_by_address);            
    1.43 +            if (session->get_identities_by_userid)
    1.44 +                sqlite3_finalize(session->get_identities_by_userid);                            
    1.45              if (session->get_user_default_key)
    1.46                  sqlite3_finalize(session->get_user_default_key);    
    1.47              if (session->get_default_own_userid)
    1.48 @@ -2127,6 +2144,76 @@
    1.49      return PEP_STATUS_OK;
    1.50  }
    1.51  
    1.52 +PEP_STATUS get_identities_by_userid(
    1.53 +        PEP_SESSION session,
    1.54 +        const char *user_id,
    1.55 +        identity_list** id_list
    1.56 +    )
    1.57 +{
    1.58 +    pEp_identity* ident;
    1.59 +
    1.60 +    assert(session);
    1.61 +    assert(user_id);
    1.62 +    assert(user_id[0]);
    1.63 +    assert(id_list);
    1.64 +
    1.65 +    if (!(session && user_id && user_id[0] && id_list))
    1.66 +        return PEP_ILLEGAL_VALUE;
    1.67 +
    1.68 +    *id_list = NULL;
    1.69 +    identity_list* ident_list = NULL;
    1.70 +
    1.71 +    sqlite3_reset(session->get_identities_by_userid);
    1.72 +    sqlite3_bind_text(session->get_identities_by_userid, 1, user_id, -1, SQLITE_STATIC);
    1.73 +    int result;
    1.74 +
    1.75 +    while ((result = sqlite3_step(session->get_identities_by_userid)) == SQLITE_ROW) {
    1.76 +        ident = new_identity(
    1.77 +                (const char *) sqlite3_column_text(session->get_identities_by_userid, 0),
    1.78 +                (const char *) sqlite3_column_text(session->get_identities_by_userid, 1),
    1.79 +                user_id,
    1.80 +                (const char *) sqlite3_column_text(session->get_identities_by_userid, 2)
    1.81 +                );
    1.82 +        assert(ident);
    1.83 +        if (ident == NULL) {
    1.84 +            sqlite3_reset(session->get_identities_by_userid);
    1.85 +            return PEP_OUT_OF_MEMORY;
    1.86 +        }
    1.87 +
    1.88 +        ident->comm_type = PEP_ct_unknown;
    1.89 +        
    1.90 +        const char* const _lang = (const char *)
    1.91 +            sqlite3_column_text(session->get_identities_by_userid, 3);
    1.92 +        if (_lang && _lang[0]) {
    1.93 +            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
    1.94 +            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
    1.95 +            assert(_lang[2] == 0);
    1.96 +            ident->lang[0] = _lang[0];
    1.97 +            ident->lang[1] = _lang[1];
    1.98 +            ident->lang[2] = 0;
    1.99 +        }
   1.100 +        ident->flags = (unsigned int)
   1.101 +            sqlite3_column_int(session->get_identities_by_userid, 4);
   1.102 +        ident->me = (unsigned int)
   1.103 +            sqlite3_column_int(session->get_identities_by_userid, 5);
   1.104 +    
   1.105 +        if (ident_list)
   1.106 +            identity_list_add(ident_list, ident);
   1.107 +        else
   1.108 +            ident_list = new_identity_list(ident);
   1.109 +    }
   1.110 +
   1.111 +    sqlite3_reset(session->get_identities_by_userid);
   1.112 +    
   1.113 +    *id_list = ident_list;
   1.114 +    
   1.115 +    if (!ident_list)
   1.116 +        return PEP_CANNOT_FIND_IDENTITY;
   1.117 +    
   1.118 +    return PEP_STATUS_OK;
   1.119 +}
   1.120 +
   1.121 +
   1.122  PEP_STATUS exists_identity_entry(PEP_SESSION session, pEp_identity* identity,
   1.123                                   bool* exists) {
   1.124      assert(session);
     2.1 --- a/src/pEp_internal.h	Tue Oct 02 13:26:19 2018 +0200
     2.2 +++ b/src/pEp_internal.h	Tue Oct 02 13:32:52 2018 +0200
     2.3 @@ -131,6 +131,7 @@
     2.4      sqlite3_stmt *get_identity;
     2.5      sqlite3_stmt *get_identity_without_trust_check;
     2.6      sqlite3_stmt *get_identities_by_address;
     2.7 +    sqlite3_stmt *get_identities_by_userid;
     2.8      sqlite3_stmt *replace_identities_fpr;
     2.9      sqlite3_stmt *replace_main_user_fpr;
    2.10      sqlite3_stmt *get_main_user_fpr;