1 /* local and remote users have profiles */
\r
3 create sequence profile_seq;
\r
4 create table profile (
\r
5 id bigint default nextval('profile_seq') primary key /* comment 'unique identifier' */,
\r
6 nickname varchar(64) not null /* comment 'nickname or username' */,
\r
7 fullname varchar(255) /* comment 'display name' */,
\r
8 profileurl varchar(255) /* comment 'URL, cached so we dont regenerate' */,
\r
9 homepage varchar(255) /* comment 'identifying URL' */,
\r
10 bio varchar(140) /* comment 'descriptive biography' */,
\r
11 location varchar(255) /* comment 'physical location' */,
\r
12 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
13 modified timestamp /* comment 'date this record was modified' */,
\r
17 create index profile_nickname_idx on profile using btree(nickname);
\r
19 create table avatar (
\r
20 profile_id integer not null /* comment 'foreign key to profile table' */ references profile (id) ,
\r
21 original integer default 0 /* comment 'uploaded by user or generated?' */,
\r
22 width integer not null /* comment 'image width' */,
\r
23 height integer not null /* comment 'image height' */,
\r
24 mediatype varchar(32) not null /* comment 'file type' */,
\r
25 filename varchar(255) null /* comment 'local filename, if local' */,
\r
26 url varchar(255) unique /* comment 'avatar location' */,
\r
27 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
28 modified timestamp /* comment 'date this record was modified' */,
\r
30 primary key(profile_id, width, height)
\r
32 create index avatar_profile_id_idx on avatar using btree(profile_id);
\r
34 create sequence sms_carrier_seq;
\r
35 create table sms_carrier (
\r
36 id bigint default nextval('sms_carrier_seq') primary key /* comment 'primary key for SMS carrier' */,
\r
37 name varchar(64) unique /* comment 'name of the carrier' */,
\r
38 email_pattern varchar(255) not null /* comment 'sprintf pattern for making an email address from a phone number' */,
\r
39 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
40 modified timestamp /* comment 'date this record was modified ' */
\r
45 create table "user" (
\r
46 id integer primary key /* comment 'foreign key to profile table' */ references profile (id) ,
\r
47 nickname varchar(64) unique /* comment 'nickname or username, duped in profile' */,
\r
48 password varchar(255) /* comment 'salted password, can be null for OpenID users' */,
\r
49 email varchar(255) unique /* comment 'email address for password recovery etc.' */,
\r
50 incomingemail varchar(255) unique /* comment 'email address for post-by-email' */,
\r
51 emailnotifysub integer default 1 /* comment 'Notify by email of subscriptions' */,
\r
52 emailnotifyfav integer default 1 /* comment 'Notify by email of favorites' */,
\r
53 emailnotifynudge integer default 1 /* comment 'Notify by email of nudges' */,
\r
54 emailnotifymsg integer default 1 /* comment 'Notify by email of direct messages' */,
\r
55 emailnotifyattn integer default 1 /* command 'Notify by email of @-replies' */,
\r
56 emailmicroid integer default 1 /* comment 'whether to publish email microid' */,
\r
57 language varchar(50) /* comment 'preferred language' */,
\r
58 timezone varchar(50) /* comment 'timezone' */,
\r
59 emailpost integer default 1 /* comment 'Post by email' */,
\r
60 jabber varchar(255) unique /* comment 'jabber ID for notices' */,
\r
61 jabbernotify integer default 0 /* comment 'whether to send notices to jabber' */,
\r
62 jabberreplies integer default 0 /* comment 'whether to send notices to jabber on replies' */,
\r
63 jabbermicroid integer default 1 /* comment 'whether to publish xmpp microid' */,
\r
64 updatefrompresence integer default 0 /* comment 'whether to record updates from Jabber presence notices' */,
\r
65 sms varchar(64) unique /* comment 'sms phone number' */,
\r
66 carrier integer /* comment 'foreign key to sms_carrier' */ references sms_carrier (id) ,
\r
67 smsnotify integer default 0 /* comment 'whether to send notices to SMS' */,
\r
68 smsreplies integer default 0 /* comment 'whether to send notices to SMS on replies' */,
\r
69 smsemail varchar(255) /* comment 'built from sms and carrier' */,
\r
70 uri varchar(255) unique /* comment 'universally unique identifier, usually a tag URI' */,
\r
71 autosubscribe integer default 0 /* comment 'automatically subscribe to users who subscribe to us' */,
\r
72 urlshorteningservice varchar(50) default 'ur1.ca' /* comment 'service to use for auto-shortening URLs' */,
\r
73 inboxed integer default 0 /* comment 'has an inbox been created for this user?' */,
\r
74 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
75 modified timestamp /* comment 'date this record was modified' */
\r
78 create index user_smsemail_idx on "user" using btree(smsemail);
\r
82 create table remote_profile (
\r
83 id integer primary key /* comment 'foreign key to profile table' */ references profile (id) ,
\r
84 uri varchar(255) unique /* comment 'universally unique identifier, usually a tag URI' */,
\r
85 postnoticeurl varchar(255) /* comment 'URL we use for posting notices' */,
\r
86 updateprofileurl varchar(255) /* comment 'URL we use for updates to this profile' */,
\r
87 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
88 modified timestamp /* comment 'date this record was modified' */
\r
91 create table subscription (
\r
92 subscriber integer not null /* comment 'profile listening' */,
\r
93 subscribed integer not null /* comment 'profile being listened to' */,
\r
94 jabber integer default 1 /* comment 'deliver jabber messages' */,
\r
95 sms integer default 1 /* comment 'deliver sms messages' */,
\r
96 token varchar(255) /* comment 'authorization token' */,
\r
97 secret varchar(255) /* comment 'token secret' */,
\r
98 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
99 modified timestamp /* comment 'date this record was modified' */,
\r
101 primary key (subscriber, subscribed)
\r
103 create index subscription_subscriber_idx on subscription using btree(subscriber);
\r
104 create index subscription_subscribed_idx on subscription using btree(subscribed);
\r
106 create sequence notice_seq;
\r
107 create table notice (
\r
109 id bigint default nextval('notice_seq') primary key /* comment 'unique identifier' */,
\r
110 profile_id integer not null /* comment 'who made the update' */ references profile (id) ,
\r
111 uri varchar(255) unique /* comment 'universally unique identifier, usually a tag URI' */,
\r
112 content varchar(140) /* comment 'update content' */,
\r
113 rendered text /* comment 'HTML version of the content' */,
\r
114 url varchar(255) /* comment 'URL of any attachment (image, video, bookmark, whatever)' */,
\r
115 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
116 modified timestamp /* comment 'date this record was modified' */,
\r
117 reply_to integer /* comment 'notice replied to (usually a guess)' */ references notice (id) ,
\r
118 is_local integer default 0 /* comment 'notice was generated by a user' */,
\r
119 source varchar(32) /* comment 'source of comment, like "web", "im", or "clientname"' */
\r
121 /* FULLTEXT(content) */
\r
123 create index notice_profile_id_idx on notice using btree(profile_id);
\r
124 create index notice_created_idx on notice using btree(created);
\r
126 create table notice_source (
\r
127 code varchar(32) primary key not null /* comment 'source code' */,
\r
128 name varchar(255) not null /* comment 'name of the source' */,
\r
129 url varchar(255) not null /* comment 'url to link to' */,
\r
130 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
131 modified timestamp /* comment 'date this record was modified' */
\r
134 create table reply (
\r
136 notice_id integer not null /* comment 'notice that is the reply' */ references notice (id) ,
\r
137 profile_id integer not null /* comment 'profile replied to' */ references profile (id) ,
\r
138 modified timestamp /* comment 'date this record was modified' */,
\r
139 replied_id integer /* comment 'notice replied to (not used, see notice.reply_to)' */,
\r
141 primary key (notice_id, profile_id)
\r
144 create index reply_notice_id_idx on reply using btree(notice_id);
\r
145 create index reply_profile_id_idx on reply using btree(profile_id);
\r
146 create index reply_replied_id_idx on reply using btree(replied_id);
\r
148 create table fave (
\r
150 notice_id integer not null /* comment 'notice that is the favorite' */ references notice (id),
\r
151 user_id integer not null /* comment 'user who likes this notice' */ references "user" (id) ,
\r
152 modified timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was modified' */,
\r
153 primary key (notice_id, user_id)
\r
156 create index fave_notice_id_idx on fave using btree(notice_id);
\r
157 create index fave_user_id_idx on fave using btree(user_id);
\r
158 create index fave_modified_idx on fave using btree(modified);
\r
160 /* tables for OAuth */
\r
162 create table consumer (
\r
163 consumer_key varchar(255) primary key /* comment 'unique identifier, root URL' */,
\r
164 seed char(32) not null /* comment 'seed for new tokens by this consumer' */,
\r
166 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
167 modified timestamp /* comment 'date this record was modified' */
\r
170 create table token (
\r
171 consumer_key varchar(255) not null /* comment 'unique identifier, root URL' */ references consumer (consumer_key),
\r
172 tok char(32) not null /* comment 'identifying value' */,
\r
173 secret char(32) not null /* comment 'secret value' */,
\r
174 type integer not null default 0 /* comment 'request or access' */,
\r
175 state integer default 0 /* comment 'for requests; 0 = initial, 1 = authorized, 2 = used' */,
\r
177 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
178 modified timestamp /* comment 'date this record was modified' */,
\r
180 primary key (consumer_key, tok)
\r
183 create table nonce (
\r
184 consumer_key varchar(255) not null /* comment 'unique identifier, root URL' */,
\r
185 tok char(32) not null /* comment 'identifying value' */,
\r
186 nonce char(32) null /* comment 'buggy old value, ignored */,
\r
187 ts integer not null /* comment 'timestamp sent' values are epoch, and only used internally */,
\r
189 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
190 modified timestamp /* comment 'date this record was modified' */,
\r
192 primary key (consumer_key, ts, nonce)
\r
195 /* One-to-many relationship of user to openid_url */
\r
197 create table user_openid (
\r
198 canonical varchar(255) primary key /* comment 'Canonical true URL' */,
\r
199 display varchar(255) not null unique /* comment 'URL for viewing, may be different from canonical' */,
\r
200 user_id integer not null /* comment 'user owning this URL' */ references "user" (id) ,
\r
201 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
202 modified timestamp /* comment 'date this record was modified' */
\r
205 create index user_openid_user_id_idx on user_openid using btree(user_id);
\r
207 /* These are used by JanRain OpenID library */
\r
209 create table oid_associations (
\r
210 server_url varchar(2047),
\r
211 handle varchar(255),
\r
215 assoc_type varchar(64),
\r
216 primary key (server_url, handle)
\r
219 create table oid_nonces (
\r
220 server_url varchar(2047),
\r
221 "timestamp" integer,
\r
222 salt character(40),
\r
223 unique (server_url, "timestamp", salt)
\r
226 create table confirm_address (
\r
227 code varchar(32) not null primary key /* comment 'good random code' */,
\r
228 user_id integer not null /* comment 'user who requested confirmation' */ references "user" (id),
\r
229 address varchar(255) not null /* comment 'address (email, Jabber, SMS, etc.)' */,
\r
230 address_extra varchar(255) not null default '' /* comment 'carrier ID, for SMS' */,
\r
231 address_type varchar(8) not null /* comment 'address type ("email", "jabber", "sms")' */,
\r
232 claimed timestamp /* comment 'date this was claimed for queueing' */,
\r
233 sent timestamp /* comment 'date this was sent for queueing' */,
\r
234 modified timestamp /* comment 'date this record was modified' */
\r
237 create table remember_me (
\r
238 code varchar(32) not null primary key /* comment 'good random code' */,
\r
239 user_id integer not null /* comment 'user who is logged in' */ references "user" (id),
\r
240 modified timestamp /* comment 'date this record was modified' */
\r
243 create table queue_item (
\r
245 notice_id integer not null /* comment 'notice queued' */ references notice (id) ,
\r
246 transport varchar(8) not null /* comment 'queue for what? "email", "jabber", "sms", "irc", ...' */,
\r
247 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
248 claimed timestamp /* comment 'date this item was claimed' */,
\r
250 primary key (notice_id, transport)
\r
253 create index queue_item_created_idx on queue_item using btree(created);
\r
256 create table notice_tag (
\r
257 tag varchar( 64 ) not null /* comment 'hash tag associated with this notice' */,
\r
258 notice_id integer not null /* comment 'notice tagged' */ references notice (id) ,
\r
259 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
261 primary key (tag, notice_id)
\r
263 create index notice_tag_created_idx on notice_tag using btree(created);
\r
265 /* Synching with foreign services */
\r
267 create table foreign_service (
\r
268 id int not null primary key /* comment 'numeric key for service' */,
\r
269 name varchar(32) not null unique /* comment 'name of the service' */,
\r
270 description varchar(255) /* comment 'description' */,
\r
271 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
272 modified timestamp /* comment 'date this record was modified' */
\r
275 create table foreign_user (
\r
276 id int not null unique /* comment 'unique numeric key on foreign service' */,
\r
277 service int not null /* comment 'foreign key to service' */ references foreign_service(id) ,
\r
278 uri varchar(255) not null unique /* comment 'identifying URI' */,
\r
279 nickname varchar(255) /* comment 'nickname on foreign service' */,
\r
280 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
281 modified timestamp /* comment 'date this record was modified' */,
\r
283 primary key (id, service)
\r
286 create table foreign_link (
\r
287 user_id int /* comment 'link to user on this system, if exists' */ references "user" (id),
\r
288 foreign_id int /* comment 'link' */ references foreign_user (id),
\r
289 service int not null /* comment 'foreign key to service' */ references foreign_service (id),
\r
290 credentials varchar(255) /* comment 'authc credentials, typically a password' */,
\r
291 noticesync int not null default 1 /* comment 'notice synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies' */,
\r
292 friendsync int not null default 2 /* comment 'friend synchronisation, bit 1 = sync outgoing, bit 2 = sync incoming */,
\r
293 profilesync int not null default 1 /* comment 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming' */,
\r
294 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
295 modified timestamp /* comment 'date this record was modified' */,
\r
297 primary key (user_id,foreign_id,service)
\r
299 create index foreign_user_user_id_idx on foreign_link using btree(user_id);
\r
301 create table foreign_subscription (
\r
302 service int not null /* comment 'service where relationship happens' */ references foreign_service(id) ,
\r
303 subscriber int not null /* comment 'subscriber on foreign service' */ ,
\r
304 subscribed int not null /* comment 'subscribed user' */ ,
\r
305 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
307 primary key (service, subscriber, subscribed)
\r
309 create index foreign_subscription_subscriber_idx on foreign_subscription using btree(subscriber);
\r
310 create index foreign_subscription_subscribed_idx on foreign_subscription using btree(subscribed);
\r
312 create table invitation (
\r
313 code varchar(32) not null primary key /* comment 'random code for an invitation' */,
\r
314 user_id int not null /* comment 'who sent the invitation' */ references "user" (id),
\r
315 address varchar(255) not null /* comment 'invitation sent to' */,
\r
316 address_type varchar(8) not null /* comment 'address type ("email", "jabber", "sms") '*/,
\r
317 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */
\r
320 create index invitation_address_idx on invitation using btree(address,address_type);
\r
321 create index invitation_user_id_idx on invitation using btree(user_id);
\r
323 create sequence message_seq;
\r
324 create table message (
\r
326 id bigint default nextval('message_seq') primary key /* comment 'unique identifier' */,
\r
327 uri varchar(255) unique /* comment 'universally unique identifier' */,
\r
328 from_profile integer not null /* comment 'who the message is from' */ references profile (id),
\r
329 to_profile integer not null /* comment 'who the message is to' */ references profile (id),
\r
330 content varchar(140) /* comment 'message content' */,
\r
331 rendered text /* comment 'HTML version of the content' */,
\r
332 url varchar(255) /* comment 'URL of any attachment (image, video, bookmark, whatever)' */,
\r
333 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
334 modified timestamp /* comment 'date this record was modified' */,
\r
335 source varchar(32) /* comment 'source of comment, like "web", "im", or "clientname"' */
\r
338 create index message_from_idx on message using btree(from_profile);
\r
339 create index message_to_idx on message using btree(to_profile);
\r
340 create index message_created_idx on message using btree(created);
\r
342 create table notice_inbox (
\r
344 user_id integer not null /* comment 'user receiving the message' */ references "user" (id),
\r
345 notice_id integer not null /* comment 'notice received' */ references notice (id),
\r
346 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date the notice was created' */,
\r
347 source integer default 1 /* comment 'reason it is in the inbox; 1=subscription' */,
\r
349 primary key (user_id, notice_id)
\r
351 create index notice_inbox_notice_id_idx on notice_inbox using btree(notice_id);
\r
353 create table profile_tag (
\r
354 tagger integer not null /* comment 'user making the tag' */ references "user" (id),
\r
355 tagged integer not null /* comment 'profile tagged' */ references profile (id),
\r
356 tag varchar(64) not null /* comment 'hash tag associated with this notice' */,
\r
357 modified timestamp /* comment 'date the tag was added' */,
\r
359 primary key (tagger, tagged, tag)
\r
361 create index profile_tag_modified_idx on profile_tag using btree(modified);
\r
362 create index profile_tag_tagger_tag_idx on profile_tag using btree(tagger,tag);
\r
364 create table profile_block (
\r
366 blocker integer not null /* comment 'user making the block' */ references "user" (id),
\r
367 blocked integer not null /* comment 'profile that is blocked' */ references profile (id),
\r
368 modified timestamp /* comment 'date of blocking' */,
\r
370 primary key (blocker, blocked)
\r
374 create sequence user_group_seq;
\r
375 create table user_group (
\r
377 id bigint default nextval('user_group_seq') primary key /* comment 'unique identifier' */,
\r
379 nickname varchar(64) unique /* comment 'nickname for addressing' */,
\r
380 fullname varchar(255) /* comment 'display name' */,
\r
381 homepage varchar(255) /* comment 'URL, cached so we dont regenerate' */,
\r
382 description varchar(140) /* comment 'descriptive biography' */,
\r
383 location varchar(255) /* comment 'related physical location, if any' */,
\r
385 original_logo varchar(255) /* comment 'original size logo' */,
\r
386 homepage_logo varchar(255) /* comment 'homepage (profile) size logo' */,
\r
387 stream_logo varchar(255) /* comment 'stream-sized logo' */,
\r
388 mini_logo varchar(255) /* comment 'mini logo' */,
\r
390 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
391 modified timestamp /* comment 'date this record was modified' */
\r
394 create index user_group_nickname_idx on user_group using btree(nickname);
\r
396 create table group_member (
\r
398 group_id integer not null /* comment 'foreign key to user_group' */ references user_group (id),
\r
399 profile_id integer not null /* comment 'foreign key to profile table' */ references profile (id),
\r
400 is_admin integer default 0 /* comment 'is this user an admin?' */,
\r
402 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
403 modified timestamp /* comment 'date this record was modified' */,
\r
405 primary key (group_id, profile_id)
\r
408 create table related_group (
\r
410 group_id integer not null /* comment 'foreign key to user_group' */ references user_group (id) ,
\r
411 related_group_id integer not null /* comment 'foreign key to user_group' */ references user_group (id),
\r
413 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date this record was created' */,
\r
415 primary key (group_id, related_group_id)
\r
419 create table group_inbox (
\r
420 group_id integer not null /* comment 'group receiving the message' references user_group (id) */,
\r
421 notice_id integer not null /* comment 'notice received' references notice (id) */,
\r
422 created timestamp not null default CURRENT_TIMESTAMP /* comment 'date the notice was created' */,
\r
424 primary key (group_id, notice_id)
\r
426 create index group_inbox_created_idx on group_inbox using btree(created);
\r
428 /* Textsearch stuff */
\r
430 create index textsearch_idx on profile using gist(textsearch);
\r
431 create index noticecontent_idx on notice using gist(to_tsvector('english',content));
\r
432 create trigger textsearchupdate before insert or update on profile for each row
\r
433 execute procedure tsvector_update_trigger(textsearch, 'pg_catalog.english', nickname, fullname, location, bio, homepage);
\r