+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+/* Synching with foreign services */
+
+create table foreign_service (
+ id int not null primary key comment 'numeric key for service',
+ name varchar(32) not null unique key comment 'name of the service',
+ description varchar(255) comment 'description',
+ created datetime not null comment 'date this record was created',
+ modified timestamp comment 'date this record was modified'
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table foreign_user (
+ id int not null comment 'unique numeric key on foreign service',
+ service int not null comment 'foreign key to service' references foreign_service(id),
+ uri varchar(255) not null unique key comment 'identifying URI',
+ nickname varchar(255) comment 'nickname on foreign service',
+ created datetime not null comment 'date this record was created',
+ modified timestamp comment 'date this record was modified',
+
+ constraint primary key (id, service)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table foreign_link (
+ user_id int comment 'link to user on this system, if exists' references user (id),
+ foreign_id int comment 'link ' references foreign_user(id),
+ service int not null comment 'foreign key to service' references foreign_service(id),
+ credentials varchar(255) comment 'authc credentials, typically a password',
+ noticesync tinyint not null default 1 comment 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies',
+ friendsync tinyint not null default 2 comment 'friend synchronization, bit 1 = sync outgoing, bit 2 = sync incoming',
+ profilesync tinyint not null default 1 comment 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming',
+ created datetime not null comment 'date this record was created',
+ modified timestamp comment 'date this record was modified',
+
+ constraint primary key (user_id, foreign_id, service),
+ index foreign_user_user_id_idx (user_id)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table foreign_subscription (
+ service int not null comment 'service where relationship happens' references foreign_service(id),
+ subscriber int not null comment 'subscriber on foreign service' references foreign_user (id),
+ subscribed int not null comment 'subscribed user' references foreign_user (id),
+ created datetime not null comment 'date this record was created',
+
+ constraint primary key (service, subscriber, subscribed),
+ index foreign_subscription_subscriber_idx (subscriber),
+ index foreign_subscription_subscribed_idx (subscribed)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table invitation (
+ code varchar(32) not null primary key comment 'random code for an invitation',
+ user_id int not null comment 'who sent the invitation' references user (id),
+ address varchar(255) not null comment 'invitation sent to',
+ address_type varchar(8) not null comment 'address type ("email", "jabber", "sms")',
+ created datetime not null comment 'date this record was created',
+
+ index invitation_address_idx (address, address_type),
+ index invitation_user_id_idx (user_id)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table message (
+
+ id integer auto_increment primary key comment 'unique identifier',
+ uri varchar(255) unique key comment 'universally unique identifier',
+ from_profile integer not null comment 'who the message is from' references profile (id),
+ to_profile integer not null comment 'who the message is to' references profile (id),
+ content varchar(140) comment 'message content',
+ rendered text comment 'HTML version of the content',
+ url varchar(255) comment 'URL of any attachment (image, video, bookmark, whatever)',
+ created datetime not null comment 'date this record was created',
+ modified timestamp comment 'date this record was modified',
+ source varchar(32) comment 'source of comment, like "web", "im", or "clientname"',
+
+ index message_from_idx (from_profile),
+ index message_to_idx (to_profile),
+ index message_created_idx (created)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table notice_inbox (
+
+ user_id integer not null comment 'user receiving the message' references user (id),
+ notice_id integer not null comment 'notice received' references notice (id),
+ created datetime not null comment 'date the notice was created',
+ source tinyint default 1 comment 'reason it is in the inbox; 1=subscription',
+
+ constraint primary key (user_id, notice_id),
+ index notice_inbox_notice_id_idx (notice_id)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table profile_tag (
+ tagger integer not null comment 'user making the tag' references user (id),
+ tagged integer not null comment 'profile tagged' references profile (id),
+ tag varchar(64) not null comment 'hash tag associated with this notice',
+ modified timestamp comment 'date the tag was added',
+
+ constraint primary key (tagger, tagged, tag),
+ index profile_tag_modified_idx (modified),
+ index profile_tag_tagger_tag_idx (tagger, tag)
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table profile_block (
+
+ blocker integer not null comment 'user making the block' references user (id),
+ blocked integer not null comment 'profile that is blocked' references profile (id),
+ modified timestamp comment 'date of blocking',
+
+ constraint primary key (blocker, blocked)
+
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table user_group (
+
+ id integer auto_increment primary key comment 'unique identifier',
+
+ nickname varchar(64) unique key comment 'nickname for addressing',
+ fullname varchar(255) comment 'display name',
+ homepage varchar(255) comment 'URL, cached so we dont regenerate',
+ description varchar(140) comment 'descriptive biography',
+ location varchar(255) comment 'related physical location, if any',
+
+ original_logo varchar(255) comment 'original size logo',
+ homepage_logo varchar(255) comment 'homepage (profile) size logo',
+ stream_logo varchar(255) comment 'stream-sized logo',
+ mini_logo varchar(255) comment 'mini logo',
+
+ created datetime not null comment 'date this record was created',
+ modified timestamp comment 'date this record was modified',
+
+ index user_group_nickname_idx (nickname)
+
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table group_member (
+
+ group_id integer not null comment 'foreign key to user_group' references user_group (id),
+ profile_id integer not null comment 'foreign key to profile table' references profile (id),
+ is_admin boolean default false comment 'is this user an admin?',
+
+ created datetime not null comment 'date this record was created',
+ modified timestamp comment 'date this record was modified',
+
+ constraint primary key (group_id, profile_id)
+
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table related_group (
+
+ group_id integer not null comment 'foreign key to user_group' references user_group (id),
+ related_group_id integer not null comment 'foreign key to user_group' references user_group (id),
+
+ created datetime not null comment 'date this record was created',
+
+ constraint primary key (group_id, related_group_id)
+
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+
+create table group_inbox (
+ group_id integer not null comment 'group receiving the message' references user_group (id),
+ notice_id integer not null comment 'notice received' references notice (id),
+ created datetime not null comment 'date the notice was created',
+
+ constraint primary key (group_id, notice_id),
+ index group_inbox_created_idx (created)
+
+) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
+