Skip to content

Commit 7a4084d

Browse files
cache account teams fixes
1 parent 238c2df commit 7a4084d

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

pbusinessaccount.c

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ void get_ba_member_email(uint64_t userid, char** email /*OUT*/, size_t *length /
324324
{
325325
binresult *bres;
326326
const binresult *users;
327+
char* fname, *lname;
327328

328329
binparam params[] = { P_STR("auth", psync_my_auth), P_STR("timeformat", "timestamp"), P_NUM("userids", userid) };
329330
bres = psync_api_run_command("account_users", params);
@@ -342,17 +343,21 @@ void get_ba_member_email(uint64_t userid, char** email /*OUT*/, size_t *length /
342343
debug(D_WARNING, "Account_users returned empty result!\n");
343344
return;
344345
} else {
345-
const char *emailret = psync_find_result(users->array[0], "email", PARAM_STR)->str;
346-
*length = strlen(emailret);
347-
*email = psync_strndup(emailret, *length);
346+
const char *resret = psync_find_result(users->array[0], "email", PARAM_STR)->str;
347+
*length = strlen(resret);
348+
*email = psync_strndup(resret, *length);
349+
fname = psync_find_result(users->array[0], "firstname", PARAM_STR)->str;
350+
lname = psync_find_result(users->array[0], "lastname", PARAM_STR)->str;
348351
}
349352
psync_free(bres);
350353

351354
if (*length) {
352355
psync_sql_res *q;
353-
q=psync_sql_prep_statement("INSERT INTO baccountemail (id, mail) VALUES (?, ?)");
356+
q=psync_sql_prep_statement("REPLACE INTO baccountemail (id, mail, firstname, lastname) VALUES (?, ?, ?, ?)");
354357
psync_sql_bind_uint(q, 1, userid);
355358
psync_sql_bind_lstring(q, 2, *email, *length);
359+
psync_sql_bind_lstring(q, 3, fname, strlen(fname));
360+
psync_sql_bind_lstring(q, 4, lname, strlen(lname));
356361
psync_sql_run_free(q);
357362
}
358363

@@ -437,7 +442,7 @@ void get_ba_team_name(uint64_t teamid, char** name /*OUT*/, size_t *length /*OUT
437442

438443
binresult *bres;
439444
int i;
440-
const binresult *users;
445+
const binresult *teams;
441446

442447
res=psync_sql_query("SELECT name FROM baccountteam WHERE id=?");
443448
psync_sql_bind_uint(res, 1, teamid);
@@ -462,23 +467,19 @@ void get_ba_team_name(uint64_t teamid, char** name /*OUT*/, size_t *length /*OUT
462467
if (api_error_result(bres))
463468
return;
464469

465-
users = psync_find_result(bres, "teams", PARAM_ARRAY);
470+
teams = psync_find_result(bres, "teams", PARAM_ARRAY);
466471

467472
//debug(D_NOTICE, "Result contains %d teams\n", users->length);
468473

469-
if (!users->length){
474+
if (!teams->length){
470475
psync_free(bres);
471476
debug(D_WARNING, "Account_teams returned empty result!\n");
472477
return;
473-
} else {
474-
475-
for (i = 0; i < users->length; ++i){
476-
//vis(i, users->array[i], param);
477-
const char *emailret = "";
478-
emailret = psync_find_result(users->array[i], "name", PARAM_STR)->str;
479-
*length = strlen(emailret);
480-
*name = psync_strndup(emailret, *length);
481-
}
478+
} else {
479+
const char *teamret = "";
480+
teamret = psync_find_result(teams->array[0], "name", PARAM_STR)->str;
481+
*length = strlen(teamret);
482+
*name = psync_strndup(teamret, *length);
482483
}
483484
psync_free(bres);
484485

@@ -488,8 +489,7 @@ void get_ba_team_name(uint64_t teamid, char** name /*OUT*/, size_t *length /*OUT
488489
psync_sql_bind_lstring(q, 2, *name, *length);
489490
psync_sql_run_free(q);
490491

491-
return;
492-
492+
return;
493493
}
494494

495495
// static void insert_cache_email(int i, const binresult *user, void *_this) {
@@ -517,9 +517,6 @@ void get_ba_team_name(uint64_t teamid, char** name /*OUT*/, size_t *length /*OUT
517517
// }
518518

519519
void cache_account_emails() {
520-
psync_sql_res *q;
521-
522-
//do_psync_account_users(userids, 0, &insert_cache_email, params);
523520
binresult *bres;
524521
int i;
525522
const binresult *users;
@@ -546,7 +543,7 @@ void cache_account_emails() {
546543
debug(D_WARNING, "Account_users returned empty result!\n");
547544
goto end_close;
548545
} else {
549-
546+
psync_sql_res *q;
550547
psync_sql_start_transaction();
551548
q=psync_sql_prep_statement("DELETE FROM baccountemail");
552549
if (unlikely(psync_sql_run_free(q))) {
@@ -556,21 +553,31 @@ void cache_account_emails() {
556553

557554
for (i = 0; i < users->length; ++i) {
558555
const char *nameret = 0;
559-
const binresult *team = users->array[i];
560-
uint64_t teamid = 0;
556+
const binresult *user = users->array[i];
557+
uint64_t userid = 0;
561558
psync_sql_res *res;
562-
563-
nameret = psync_find_result(team, "name", PARAM_STR)->str;
564-
teamid = psync_find_result(team, "id", PARAM_NUM)->num;
565-
//debug(D_NOTICE, "Team name %s team id %lld\n", nameret,(long long)teamid);
566-
567-
res=psync_sql_prep_statement("INSERT INTO baccountteam (id, name) VALUES (?, ?)");
568-
psync_sql_bind_uint(res, 1, teamid);
569-
psync_sql_bind_lstring(res, 2, nameret, strlen(nameret));
570-
if (unlikely(psync_sql_run_free(res))) {
571-
psync_sql_rollback_transaction();
572-
goto end_close;
573-
}
559+
char* fname, *lname;
560+
int active = 0;
561+
int frozen = 0;
562+
563+
active = psync_find_result(user, "active", PARAM_BOOL)->num;
564+
frozen = psync_find_result(user, "frozen", PARAM_BOOL)->num;
565+
nameret = psync_find_result(user, "email", PARAM_STR)->str;
566+
userid = psync_find_result(user, "id", PARAM_NUM)->num;
567+
fname = psync_find_result(user, "firstname", PARAM_STR)->str;
568+
lname = psync_find_result(user, "lastname", PARAM_STR)->str;
569+
570+
if (userid && (active || frozen)) {
571+
res = psync_sql_prep_statement("INSERT INTO baccountemail (id, mail, firstname, lastname) VALUES (?, ?, ?, ?)");
572+
psync_sql_bind_uint(res, 1, userid);
573+
psync_sql_bind_lstring(res, 2, nameret, strlen(nameret));
574+
psync_sql_bind_lstring(res, 3, fname, strlen(fname));
575+
psync_sql_bind_lstring(res, 4, lname, strlen(lname));
576+
if (unlikely(psync_sql_run_free(res))) {
577+
psync_sql_rollback_transaction();
578+
goto end_close;
579+
}
580+
}
574581
}
575582
psync_sql_commit_transaction();
576583
}
@@ -646,8 +653,10 @@ void cache_account_teams() {
646653
res=psync_sql_prep_statement("INSERT INTO baccountteam (id, name) VALUES (?, ?)");
647654
psync_sql_bind_uint(res, 1, teamid);
648655
psync_sql_bind_lstring(res, 2, nameret, strlen(nameret));
649-
psync_sql_run_free(res);
656+
if (unlikely(psync_sql_run_free(res)))
657+
psync_sql_rollback_transaction();
650658
}
659+
psync_sql_commit_transaction();
651660
//vis(i, users->array[i], param);
652661
}
653662
psync_free(bres);

0 commit comments

Comments
 (0)