ENGINE-398: added social graph, but of course sqlite3 is complaining. ENGINE-398
authorKrista Bennett <krista@pep-project.org>
Wed, 13 Jun 2018 10:32:39 +0200
branchENGINE-398
changeset 2747f1158ffe70e2
parent 2746 2d8e203bf93c
child 2752 713b65791080
ENGINE-398: added social graph, but of course sqlite3 is complaining.
src/pEpEngine.c
src/pEp_internal.h
     1.1 --- a/src/pEpEngine.c	Wed Jun 13 08:56:26 2018 +0200
     1.2 +++ b/src/pEpEngine.c	Wed Jun 13 10:32:39 2018 +0200
     1.3 @@ -426,6 +426,10 @@
     1.4  static const char *sql_add_userid_alias =
     1.5      "insert or replace into alternate_user_id (alternate_id, default_id) "
     1.6      "values (?2, ?1) ;";
     1.7 +
     1.8 +static const char *sql_add_into_social_graph =
     1.9 +    "insert or replace into social_graph(own_userid, own_address, contact_userid text) "
    1.10 +    "values (?1, ?2, ?3) ;";
    1.11      
    1.12  static int user_version(void *_version, int count, char **text, char **name)
    1.13  {
    1.14 @@ -720,9 +724,15 @@
    1.15                  ");\n"
    1.16                  // social graph for key resets
    1.17                  "create table if not exists social_graph (\n"
    1.18 -                "    own_id text,"
    1.19 +                "    own_userid text,\n"
    1.20                  "    own_address text,\n"
    1.21 -                "    out_count integer default 0\n"
    1.22 +                "    contact_userid text\n"
    1.23 +                "       references person (id)\n"
    1.24 +                "       on delete cascade on update cascade,\n"
    1.25 +                "    CONSTRAINT fk_identity\n"
    1.26 +                "       FOREIGN KEY(own_address, own_userid)\n" 
    1.27 +                "       REFERENCES identity(address, user_id)\n"
    1.28 +                "       ON DELETE CASCADE ON UPDATE CASCADE\n"
    1.29                  ");\n"
    1.30                  ,
    1.31              NULL,
    1.32 @@ -1029,6 +1039,27 @@
    1.33                  );
    1.34                  assert(int_result == SQLITE_OK);    
    1.35              }
    1.36 +            if (version < 9) {
    1.37 +                int_result = sqlite3_exec(
    1.38 +                    _session->db,
    1.39 +                    "create table if not exists social_graph (\n"
    1.40 +                    "    own_userid text,\n"
    1.41 +                    "    own_address text,\n"
    1.42 +                    "    contact_userid text\n"
    1.43 +                    "       references person (id)\n"
    1.44 +                    "       on delete cascade on update cascade,\n"
    1.45 +                    "    CONSTRAINT fk_identity\n"
    1.46 +                    "       FOREIGN KEY(own_address, own_userid)\n" 
    1.47 +                    "       REFERENCES identity(address, user_id)\n"
    1.48 +                    "       ON DELETE CASCADE ON UPDATE CASCADE\n"
    1.49 +                    ");\n"
    1.50 +                    ,
    1.51 +                    NULL,
    1.52 +                    NULL,
    1.53 +                    NULL
    1.54 +                );
    1.55 +                assert(int_result == SQLITE_OK);    
    1.56 +            }
    1.57          }        
    1.58          else { 
    1.59              // Version from DB was 0, it means this is initial setup.
    1.60 @@ -1138,6 +1169,10 @@
    1.61              (int)strlen(sql_is_pep_user), &_session->is_pep_user, NULL);
    1.62      assert(int_result == SQLITE_OK);
    1.63  
    1.64 +    int_result = sqlite3_prepare_v2(_session->db, sql_add_into_social_graph,
    1.65 +            (int)strlen(sql_add_into_social_graph), &_session->add_into_social_graph, NULL);
    1.66 +    assert(int_result == SQLITE_OK);
    1.67 +
    1.68      int_result = sqlite3_prepare_v2(_session->db, sql_set_device_group,
    1.69              (int)strlen(sql_set_device_group), &_session->set_device_group, NULL);
    1.70      assert(int_result == SQLITE_OK);
    1.71 @@ -1403,7 +1438,9 @@
    1.72              if (session->is_pep_user)
    1.73                  sqlite3_finalize(session->is_pep_user);
    1.74              if (session->exists_person)
    1.75 -                sqlite3_finalize(session->exists_person);                        
    1.76 +                sqlite3_finalize(session->exists_person);
    1.77 +            if (session->add_into_social_graph)
    1.78 +                sqlite3_finalize(session->add_into_social_graph);                        
    1.79              if (session->set_device_group)
    1.80                  sqlite3_finalize(session->set_device_group);
    1.81              if (session->get_device_group)
    1.82 @@ -2600,6 +2637,30 @@
    1.83      return PEP_STATUS_OK;
    1.84  }
    1.85  
    1.86 +PEP_STATUS bind_own_ident_with_contact_ident(PEP_SESSION session,
    1.87 +                                             pEp_identity* own_ident, 
    1.88 +                                             pEp_identity* contact_ident) {
    1.89 +    if (!own_ident || !contact_ident || 
    1.90 +        !own_ident->address || !own_ident->user_id || !contact_ident->user_id)
    1.91 +        return PEP_ILLEGAL_VALUE;
    1.92 +        
    1.93 +    sqlite3_reset(session->add_into_social_graph);
    1.94 +    sqlite3_bind_text(session->add_into_social_graph, 1, own_ident->user_id, -1,
    1.95 +            SQLITE_STATIC);
    1.96 +    sqlite3_bind_text(session->add_into_social_graph, 2, own_ident->address, -1,
    1.97 +            SQLITE_STATIC);
    1.98 +    sqlite3_bind_text(session->add_into_social_graph, 3, contact_ident->user_id, -1,
    1.99 +            SQLITE_STATIC);
   1.100 +        
   1.101 +    int result = sqlite3_step(session->add_into_social_graph);
   1.102 +    sqlite3_reset(session->add_into_social_graph);
   1.103 +    
   1.104 +    if (result != SQLITE_DONE)
   1.105 +        return PEP_CANNOT_SET_PERSON;
   1.106 +
   1.107 +    return PEP_STATUS_OK;
   1.108 +}
   1.109 +
   1.110  
   1.111  PEP_STATUS remove_fpr_as_default(PEP_SESSION session, 
   1.112                                   const char* fpr) 
   1.113 @@ -3878,6 +3939,7 @@
   1.114                                                                      keylist);
   1.115  }
   1.116  
   1.117 +
   1.118  DYNAMIC_API const char* get_engine_version() {
   1.119      return PEP_ENGINE_VERSION;
   1.120  }
     2.1 --- a/src/pEp_internal.h	Wed Jun 13 08:56:26 2018 +0200
     2.2 +++ b/src/pEp_internal.h	Wed Jun 13 10:32:39 2018 +0200
     2.3 @@ -141,6 +141,7 @@
     2.4      sqlite3_stmt *exists_person;    
     2.5      sqlite3_stmt *set_as_pep_user;
     2.6      sqlite3_stmt *is_pep_user;
     2.7 +    sqlite3_stmt *add_into_social_graph;
     2.8      sqlite3_stmt *set_device_group;
     2.9      sqlite3_stmt *get_device_group;
    2.10      sqlite3_stmt *set_pgp_keypair;