aboutsummaryrefslogtreecommitdiffstats
path: root/docs/docs/api_overview.md
blob: edbdc8646132c0b5d7b7670f3ea35590ad399027 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513

# How to BBJ?

## Input

BBJ is interacted with entirely through POST requests, whose bodies are
json objects.

The endpoints, all listed below, can be contacted at the path /api/ relative
to the root of where BBJ is hosted. If bbj is hosted on a server on port 80
at the root:

`http://server.com/api/endpoint_here`

The body of your request contains all of it's argument fields, instead of
using URL parameters. As a demonstration, to call `thread_create`,
it requires two arguments: `title`, and `body`. We put those argument
names at the root of the json object, and their values are the info
passed into the API for that spot. Your input will look like this:

```json
{
    "title": "Hello world!!",
    "body": "Hi! I am exploring this cool board thing!!"
}
```

And you will POST this body to `http://server.com:PORT/api/thread_create`.

A few endpoints do not require any arguments. These can still be POSTed to,
but the body may be completely empty or an empty json object. You can even
GET these if you so choose.

For all endpoints, argument keys that are not consumed by the endpoint are
ignored. Posting an object with a key/value pair of `"sandwich": True` will
not clog up any pipes :) In the same vein, endpoints who dont take arguments
don't care if you supply them anyway.

## Output

BBJ returns data in a consistently formatted json object. The base object
has three keys: `data`, `usermap`, and `error`. Visualizied:

```javascript
{
  "error":   false, // boolean false or error object
  "data":    null,  // null or the requested data from endpoint.
  "usermap": {}     // potentially empty object, maps user_ids to user objects
}

// If "error" is true, it looks like this:

{
  "error": {
      "code": // an integer from 0 to 5,
      "description": // a string describing the error in detail.
  }
  "data": null   // ALWAYS null if error is not false
  "usermap": {}  // ALWAYS empty if error is not false
}
```

### data

`data` is what the endpoint actually returns. The type of contents vary
by endpoint and are documented below. If an endpoint says it returns a
boolean, it will look like `"data": True`. If it says it returns an array,
it will look like `"data": ["stuff", "goes", "here"]`

### usermap

The usermap is a json object mapping user_ids within `data` to full user
objects. BBJ handles users entirely by an ID system, meaning any references
to them inside of response data will not include vital information like their
username, or their profile information. Instead, we fetch those values from
this usermap object. All of it's root keys are user_id's and their values
are user objects. It should be noted that the anonymous user has it's own
ID and profile object as well.

### error

`error` is typically `false`. If it is __not__ false, then the request failed
and the json object that `error` contains should be inspected. (see the above
visualation) Errors follow a strict code system, making it easy for your client
to map these responses to native exception types or signals in your language of
choice. See [the full error page](errors.md) for details.


<br><br>
# Authorization
------
See also [the Authorization page](authorization.md).
## check_auth

**Arguments:**

 * __target_user__: string: either a user_name or a user_id

 * __target_hash__: string: sha256 hash for the password to check



Returns boolean `true` or `false` of whether the hash given
is correct for the given user.


<br>
<br><br>
# Threads & Messages
------
## delete_post

**Arguments:**

 * __thread_id__: string: the id of the thread this message was posted in.

 * __post_id__: integer: the id of the target message.



Requires the arguments `thread_id` and `post_id`.

Delete a message from a thread. The same rules apply
here as `edit_post` and `edit_query`: the logged in user
must either be the one who posted the message within 24hrs,
or have admin rights. The same error descriptions and code
are returned on falilure. Boolean true is returned on
success.

If the post_id is 0, the whole thread is deleted.


<br>
## edit_post

**Arguments:**

 * __thread_id__: string: the thread the message was posted in.

 * __post_id__: integer: the target post_id to edit.

 * __body__: string: the new message body.

 * __OPTIONAL: send_raw__: boolean: set the formatting mode for the target message.



Replace a post with a new body. Requires the arguments
`thread_id`, `post_id`, and `body`. This method verifies
that the user can edit a post before commiting the change,
otherwise an error object is returned whose description
should be shown to the user.

To perform sanity checks and retrieve the unformatted body
of a post without actually attempting to replace it, use
`edit_query` first.

Optionally you may also include the argument `send_raw` to
set the message's formatting flag. However, if this is the
only change you would like to make, you should use the
endpoint `set_post_raw` instead.

Returns the new message object.


<br>
## edit_query

**Arguments:**

 * __thread_id__: string: the id of the thread the message was posted in.

 * __post_id__: integer: the id of the target message.



Queries the database to ensure the user can edit a given
message. Requires the arguments `thread_id` and `post_id`
(does not require a new body)

Returns the original message object without any formatting
on success. Returns a descriptive code 4 otherwise.


<br>
## message_feed

**Arguments:**

 * __time__: int/float: epoch/unix time of the earliest point of interest

 * __OPTIONAL: format__: string: the specifier for the desired formatting engine



Returns a special object representing all activity on the board since `time`.

```javascript
{
    "threads": {
        "thread_id": {
            // ...thread object
        },
        // ...more thread_id/object pairs
    },
    "messages": [
        ...standard message object array sorted by date
    ]
}
```

The message objects in `messages` are the same objects returned
in threads normally. They each have a thread_id parameter, and
you can access metadata for these threads by the `threads` object
which is also provided.

The `messages` array is already sorted by submission time, newest
first. The order in the threads object is undefined and you should
instead use their `last_mod` attribute if you intend to list them
out visually.


<br>
## set_post_raw

**Arguments:**

 * __thread_id__: string: the id of the thread the message was posted in.

 * __post_id__: integer: the id of the target message.

 * __value__: boolean: the new `send_raw` value to apply to the message.



Requires the boolean argument of `value`, string argument
`thread_id`, and integer argument `post_id`. `value`, when false,
means that the message will be passed through message formatters
before being sent to clients. When `value` is true, this means
it will never go through formatters, all of its whitespace is
sent to clients verbatim and expressions are not processed.

The same rules for editing messages (see `edit_query`) apply here
and the same error objects are returned for violations.

You may optionally set this value as well when using `edit_post`,
but if this is the only change you want to make to the message,
using this endpoint instead is preferable.


<br>
## set_thread_pin

**Arguments:**

 * __thread_id__: string: the id of the thread to modify.

 * __value__: boolean: `true` to pin thread, `false` otherwise.



Requires the arguments `thread_id` and `value`. `value`
must be a boolean of what the pinned status should be.
This method requires that the caller is logged in and
has admin status on their account.

Returns the same boolean you supply as `value`


<br>
## thread_create

**Arguments:**

 * __body__: string: The body of the first message

 * __title__: string: The title name for this thread

 * __OPTIONAL: send_raw__: boolean: formatting mode for the first message.



Creates a new thread and returns it. Requires the non-empty
string arguments `body` and `title`.

If the argument `send_raw` is specified and has a non-nil
value, the OP message will never recieve special formatting.


<br>
## thread_index

**Arguments:**

 * __OPTIONAL: include_op__: boolean: Include a `messages` object containing the original post



Return an array with all the server's threads. They are already sorted for
you; most recently modified threads are at the beginning of the array.
Unless you supply `include_op`, these threads have no `messages` parameter.
If you do, the `messages` parameter is an array with a single message object
for the original post.


<br>
## thread_load

**Arguments:**

 * __thread_id__: string: the thread to load.

 * __OPTIONAL: op_only__: boolean: include only the original message in `messages`

 * __OPTIONAL: format__: string: the formatting type of the returned messages.



Returns the thread object with all of its messages loaded.
Requires the argument `thread_id`. `format` may also be
specified as a formatter to run the messages through.
Currently only "sequential" is supported.

You may also supply the parameter `op_only`. When it's value
is non-nil, the messages array will only include post_id 0 (the first)


<br>
## thread_reply

**Arguments:**

 * __thread_id__: string: the id for the thread this message should post to.

 * __body__: string: the message's body of text.

 * __OPTIONAL: send_raw__: boolean: formatting mode for the posted message.



Creates a new reply for the given thread and returns it.
Requires the string arguments `thread_id` and `body`

If the argument `send_raw` is specified and has a non-nil
value, the message will never recieve special formatting.


<br>
<br><br>
# Tools
------
## db_validate

**Arguments:**

 * __key__: string: the identifier for the ruleset to check.

 * __value__: VARIES: the object for which `key` will check for.

 * __OPTIONAL: error__: boolean: when `true`, will return an API error response instead of a special object.



See also [the Input Validation page](validation.md).

Requires the arguments `key` and `value`. Returns an object
with information about the database sanity criteria for
key. This can be used to validate user input in the client
before trying to send it to the server.

If the argument `error` is supplied with a non-nil value,
the server will return a standard error object on failure
instead of the special object described below.

The returned object has two keys:

{
  "bool": true/false,
  "description": null/"why this value is bad"
}

If bool == false, description is a string describing the
problem. If bool == true, description is null and the
provided value is safe to use.


<br>
## format_message

**Arguments:**

 * __body__: string: the message body to apply formatting to.

 * __format__: string: the specifier for the desired formatting engine



Requires the arguments `body` and `format`. Applies
`format` to `body` and returns the new object. See
`thread_load` for supported specifications for `format`.


<br>
## user_map

_requires no arguments_

Returns an array with all registered user_ids, with the usermap
object populated by their full objects. This method is _NEVER_
neccesary when using other endpoints, as the usermap returned
on those requests already contains all the information you will
need. This endpoint is useful for statistic purposes only.


<br>
<br><br>
# Users
------
## get_me

_requires no arguments_

Requires no arguments. Returns your internal user object,
including your `auth_hash`.


<br>
## is_admin

**Arguments:**

 * __target_user__: string: user_id or user_name to check against.



Requires the argument `target_user`. Returns a boolean
of whether that user is an admin.


<br>
## user_get

**Arguments:**

 * __target_user__: string: either a user_name or a user_id



Returns a user object for the given target.


<br>
## user_is_registered

**Arguments:**

 * __target_user__: string: either a user_name or a user_id



Returns boolean `true` or `false` of whether the given target is
registered on the server.


<br>
## user_register

**Arguments:**

 * __user_name__: string: the desired display name

 * __auth_hash__: string: a sha256 hash of a password



Register a new user into the system and return the new user object
on success. The returned object includes the same `user_name` and
`auth_hash` that you supply, in addition to all the default user
parameters. Returns code 4 errors for any failures.


<br>
## user_update

**Arguments:**

 * __Any of the following may be submitted__: 

 * __user_name__: string: a desired display name

 * __auth_hash__: string: sha256 hash for a new password

 * __quip__: string: a short string that can be used as a signature

 * __bio__: string: a user biography for their profile

 * __color__: integer: 0-6, a display color for the user



Receives new parameters and assigns them to the user object.
This method requires that you send a valid User/Auth header
pair with your request, and the changes are made to that
account.

Take care to keep your client's User/Auth header pair up to date
after using this method.

The newly updated user object is returned on success,
including the `auth_hash`.


<br>
Un proyecto texto-plano.xyz