Unverified Commit 59351d54 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Initial broadcast support

Also implemented two more SET commands
parent a3c6b0fa
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1254,6 +1254,7 @@ no default expiration date will be present. The latter should never be
specified for a folder or else the messages will disappear.`,
				MinArgs: 1,
				MaxArgs: 1,
				Action:  ActionSetDefaultExpire,
			},
			"EXPIRE_LIMIT": {
				Description: `Specifies expiration limit that  is allowed for messages. Non-privileged
@@ -1268,6 +1269,7 @@ The command SHOW FOLDER/FULL will show the expiration limit, if one
exists.  (NOTE: SHOW FOLDER/FULL is a privileged command.)`,
				MinArgs: 1,
				MaxArgs: 1,
				Action:  ActionSetExpireLimit,
			},
			"FOLDER": {
				Description: `Select a folder of messages.  Identical to the SELECT command.  See help
+1 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ func Loop() error {
	// TODO: END

	for {
		this.ShowBroadcast()
		line, err := rl.Readline()
		if err != nil {
			if err.Error() == "Interrupt" {
+15 −5
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ package repl
import (
	"errors"
	"fmt"
	"strconv"
	"strings"

	"git.lyda.ie/kevin/bulletin/dclish"
@@ -67,15 +68,24 @@ func ActionSetNobrief(_ *dclish.Command) error {
}

// ActionSetDefaultExpire handles the `SET DEFAULT_EXPIRE` command.
func ActionSetDefaultExpire(_ *dclish.Command) error {
	fmt.Println("TODO: implement ActionSetDefaultExpire.")
	return nil
func ActionSetDefaultExpire(cmd *dclish.Command) error {
	value, err := strconv.ParseInt(cmd.Args[0], 10, 64)
	if err != nil {
		return err
	}
	ctx := storage.Context()
	return this.Q.UpdateDefaultExpire(ctx, value)
}

// ActionSetExpireLimit handles the `SET EXPIRE_LIMIT` command.
func ActionSetExpireLimit(_ *dclish.Command) error {
func ActionSetExpireLimit(cmd *dclish.Command) error {
	fmt.Println("TODO: implement ActionSetExpireLimit.")
	return nil
	value, err := strconv.ParseInt(cmd.Args[0], 10, 64)
	if err != nil {
		return err
	}
	ctx := storage.Context()
	return this.Q.UpdateExpireLimit(ctx, value)
}

// ActionSetFolder handles the `SET FOLDER` command.  This selects a folder.
+93 −0
Original line number Diff line number Diff line
// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: broadcast.sql

package storage

import (
	"context"
	"time"
)

const clearBroadcasts = `-- name: ClearBroadcasts :exec
DELETE FROM broadcast
`

func (q *Queries) ClearBroadcasts(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, clearBroadcasts)
	return err
}

const createBroadcast = `-- name: CreateBroadcast :exec
INSERT INTO broadcast
    (author, bell, message)
  VALUES
    (?, ?, ?)
`

type CreateBroadcastParams struct {
	Author  string
	Bell    int64
	Message string
}

func (q *Queries) CreateBroadcast(ctx context.Context, arg CreateBroadcastParams) error {
	_, err := q.db.ExecContext(ctx, createBroadcast, arg.Author, arg.Bell, arg.Message)
	return err
}

const getBroadcasts = `-- name: GetBroadcasts :many
SELECT author, bell, message, create_at FROM broadcast WHERE create_at > ? ORDER BY create_at
`

func (q *Queries) GetBroadcasts(ctx context.Context, createAt time.Time) ([]Broadcast, error) {
	rows, err := q.db.QueryContext(ctx, getBroadcasts, createAt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Broadcast
	for rows.Next() {
		var i Broadcast
		if err := rows.Scan(
			&i.Author,
			&i.Bell,
			&i.Message,
			&i.CreateAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const reapBroadcasts = `-- name: ReapBroadcasts :exec
DELETE FROM broadcast WHERE julianday(create_at) > 3
`

func (q *Queries) ReapBroadcasts(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, reapBroadcasts)
	return err
}

const updateLastBroadcast = `-- name: UpdateLastBroadcast :exec
UPDATE users SET last_broadcast = ? WHERE login = ?
`

type UpdateLastBroadcastParams struct {
	LastBroadcast time.Time
	Login         string
}

func (q *Queries) UpdateLastBroadcast(ctx context.Context, arg UpdateLastBroadcastParams) error {
	_, err := q.db.ExecContext(ctx, updateLastBroadcast, arg.LastBroadcast, arg.Login)
	return err
}
+13 −0
Original line number Diff line number Diff line
@@ -61,3 +61,16 @@ func (f Folder) String() string {
		f.Expire,
		f.Visibility)
}

// String displays a folder (mainly used for debugging).
func (b Broadcast) String() string {
	bell := ""
	if b.Bell == 1 {
		bell = "\a\a"
	}
	return fmt.Sprintf("%sFrom: %s   %s\n%s\n",
		bell,
		b.Author,
		b.CreateAt.Format("06-01-02 15:04:05"),
		b.Message)
}
Loading